simple-query-string
Fast and simple way to parse and stringify URL query strings.
Utility javascript methods to encode and decode query string parameters with extreme performance and low memory usage.
Installation
NPM
$ npm install simple-query-string --save
Bower
$ bower install simple-query-string
Download
Browser - CDN
Features
Query String Decoding
-
#### fast
Several benchmarks are run at each release to ensure maximum performance.
-
#### query string parsing
simpleQueryString -
#### url with query string detection
There is no need to use
url.split('?')[1]
or any other code, just put the entire string!simpleQueryString -
#### location.hash support
simpleQueryString -
#### location.search support
simpleQueryString -
#### array detection
simpleQueryString // myarr: [1,2,3,4] -
#### anchor detection
simpleQueryString // #anchor will be ignored -
#### node.js module
var qs = ;var parsed = qs;console;console; -
#### browser
-
#### AMD module
; -
####
for..in
safeSafe to be used in a for in loop. The object is created with
Object.create(null)
.var dic = simpleQueryString;for var k in dicconsole; -
#### safely deals with invalid/empty input
simpleQueryString // equals to {} -
#### Custom delimiter
In some cases, you may want to use another separator instead of ampersand. Example using semicolon (';') as separator:
simpleQueryString // equals to '{ p1:'a', p2: 1}'
Query String Encoding
-
#### fast
Several benchmarks are run at each release to ensure maximum performance.
-
#### properties detection
simpleQueryString//=> 'key=val¶m=1' -
#### type detection
simpleQueryString // equals to 'p=1&p2=true&p3=false' -
#### array encoding
simpleQueryString // equals to 'myarr=1&myarr=2&myarr=3&myarr=4' -
#### node.js module
var qs = ;var str = qs;console; // equals to 'param=1&p2=true&p3=false' -
#### browser
-
#### AMD module
; -
#### safely ignore functions and prototype properties
simpleQueryString // equals to 'p2=1' -
#### safely deals with invalid/empty input
simpleQueryString // equals to '' -
#### Custom delimiter
In some cases, you may want to use another separator instead of ampersand. Example using semicolon (';') as separator:
simpleQueryString // equals to 'p1=a;p2=1'
Getting Started
Decode example:
var obj = simpleQueryString; // obj["key"] === "val" // obj["param"] === "1"
Encode example:
var p = key1: true key2: 0 1 2 key3: "string" key4: 4321; var qStr = simpleQueryString;
How to Test
Node.js
Install dependencies
$ npm install mocha -g
Run tests in node.js
Use npm to run the test script 'spec/simplequerystring-test.js'
$ npm test
Run tests in any browser
Run the tests by opening ./spec/testpage.html
.
Query string references
Some documentation for future reference.
Wikipedia on Query string structure
- The query string is composed of a series of field-value pairs.
- Within each pair, the field name and value are separated by an equals sign, '='.
- The series of pairs is separated by the ampersand, '&' (or semicolon, ';' for URLs embedded in HTML and not generated by a
<form>...</form>;
see below).
W3C semicolon recommendation
W3C - Ampersands in URI attribute values
We recommend that HTTP server implementors, and in particular, CGI implementors support the use of ";" in place of "&" to save authors the trouble of escaping "&" characters in this manner.
RFC 3986 on Query component
Some relevant parts of the documentation for future reference.
https://tools.ietf.org/html/rfc3986#section-3.4
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
https://tools.ietf.org/html/rfc3986#section-4.2
relative-ref = relative-part [ "?" query ] [ "#" fragment ]
License
MIT © 2016 Khalid Salomão