You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parseURLParams.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* @flow */
  2. import Bourne from '@hapi/bourne';
  3. import { reportError } from './helpers';
  4. /**
  5. * A list if keys to ignore when parsing.
  6. *
  7. * @type {string[]}
  8. */
  9. const blacklist = [ '__proto__', 'constructor', 'prototype' ];
  10. /**
  11. * Parses the query/search or fragment/hash parameters out of a specific URL and
  12. * returns them as a JS object.
  13. *
  14. * @param {URL} url - The URL to parse.
  15. * @param {boolean} dontParse - If falsy, some transformations (for parsing the
  16. * value as JSON) will be executed.
  17. * @param {string} source - If {@code 'search'}, the parameters will parsed out
  18. * of {@code url.search}; otherwise, out of {@code url.hash}.
  19. * @returns {Object}
  20. */
  21. export function parseURLParams(
  22. url: URL,
  23. dontParse: boolean = false,
  24. source: string = 'hash'): Object {
  25. const paramStr = source === 'search' ? url.search : url.hash;
  26. const params = {};
  27. const paramParts = (paramStr && paramStr.substr(1).split('&')) || [];
  28. // Detect and ignore hash params for hash routers.
  29. if (source === 'hash' && paramParts.length === 1) {
  30. const firstParam = paramParts[0];
  31. if (firstParam.startsWith('/') && firstParam.split('&').length === 1) {
  32. return params;
  33. }
  34. }
  35. paramParts.forEach(part => {
  36. const param = part.split('=');
  37. const key = param[0];
  38. if (!key || key.split('.').some(k => blacklist.includes(k))) {
  39. return;
  40. }
  41. let value;
  42. try {
  43. value = param[1];
  44. if (!dontParse) {
  45. const decoded = decodeURIComponent(value).replace(/\\&/, '&');
  46. value = decoded === 'undefined' ? undefined : Bourne.parse(decoded);
  47. }
  48. } catch (e) {
  49. reportError(
  50. e, `Failed to parse URL parameter value: ${String(value)}`);
  51. return;
  52. }
  53. params[key] = value;
  54. });
  55. return params;
  56. }