Add a JavaScript utility function for parsing query strings in URLs.

Review Request #9711 — Created March 1, 2018 and submitted

Christian Hammond

This introduces Djblets.parseQueryString(), which will take a query
string or a URL containing one and parse it. It understands key/value
pairs, standalone keys (which will be assigned the value null), and
optionally supports storing multiple values for a key as an array.

Unit tests were added that test all the standard query string cases.

Unit tests pass.

Used this in an upcoming change in Review Board.

  • 0
  • 0
  • 3
  • 0
  • 3
Description From Last Updated
David Trowbridge
  2. , optional?

  3. Given the default value for options, I think you don't need this check and can just use options.allowMultiValue. If someone passes in something that's falsy and not an object they're using the API wrong.

  4. Can we move the decodeURIComponent call into the if/else above?

    1. We need to call it in both cases. Might as well only do it once.

    2. It's not like it will ever be called more than once--this is a conditional.

      This is fewer LOC, avoids the multiple-assignment of "key", and is nicer to read IMO:

      if (j === -1) {
          key = decodeURIComponent(queryParam);
          value = null;
      } else {
          key = decodeURIComponent(queryParam.substr(0, j));
          value = decodeURIComponent(queryParam.substr(j + 1));
Christian Hammond
Christian Hammond
David Trowbridge
  1. Ship It!
Christian Hammond
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-1.0.x (438db54)