The URI specification RFC3986 defines very few rules on how URI queries are to be parsed. It merely states that URI queries are "non-hierarchical data" (in contrast to URI paths) and that "key=value" pairs are "often used". This module supports using pre-defined or defining your own URI query parsing strategies that support your framework of choice, personal preferences, legacy requirements, etc.
npm add uri-query
This module makes use of ES2015 features, but does not polyfill them. You will need to implement your own if the environment you are deploying to does not support:
Sets | Node 0.12+ | IE11 | Edge |
Object.assign() | Node 6.4+ | Edge | |
String.prototype.includes() | Node 4+ | Edge | |
Array.prototype.includes() | Node 6.13+ | Edge14+ |
The default strategy supports:
Objects | ?filters[price]=50-100 |
Arrays | ?platforms[0]=chrome&platforms[1]=opera |
Sets | ?platforms[]=chrome&platforms[]=opera |
Multidimensional Structures | ?q=jordans&filters[status]=in-stock&filters[price][range][0]=100&filters[price][range][1]=200&filters[price][currency]=USD |
True | ?view-all |
Null | ?q= |
From RFC3986: 3.4. Query
The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority (if any). The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI.
query = *( pchar / "/" / "?" )
The characters slash ("/") and question mark ("?") may represent data within the query component. Beware that some older, erroneous implementations may not handle such data correctly when it is used as the base URI for relative references (Section 5.1), apparently because they fail to distinguish query data from path data when looking for hierarchical separators. However, as query components are often used to carry identifying information in the form of "key=value" pairs and one frequently used value is a reference to another URI, it is sometimes better for usability to avoid percent- encoding those characters.
... pchar = unreserved / pct-encoded / sub-delims / ":" / "@" ... pct-encoded = "%" HEXDIG HEXDIG ... unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" ... sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" ...
... ALPHA = %x41-5A / %x61-7A ; A-Z / a-z ... DIGIT = %x30-39 ; 0-9 ... HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F" ...