getParam.js 516 B

123456789101112131415
  1. var typecast = require('../string/typecast');
  2. var getQuery = require('./getQuery');
  3. /**
  4. * Get query parameter value.
  5. */
  6. function getParam(url, param, shouldTypecast){
  7. var regexp = new RegExp('(\\?|&)'+ param + '=([^&]*)'), //matches `?param=value` or `&param=value`, value = $2
  8. result = regexp.exec( getQuery(url) ),
  9. val = (result && result[2])? result[2] : null;
  10. return shouldTypecast === false? val : typecast(val);
  11. }
  12. module.exports = getParam;