
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
simple-query-string
Advanced tools
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.
$ npm install simple-query-string --save
$ bower install simple-query-string
<script src="https://cdn.rawgit.com/khalidsalomao/simple-query-string/22cc5bbe/src/simplequerystring.min.js"></script>
Several benchmarks are run at each release to ensure maximum performance.
simpleQueryString.parse("key=val¶m=1")
There is no need to use url.split('?')[1]
or any other code, just put the entire string!
simpleQueryString.parse("http://example.org/test/?key=val¶m=1")
simpleQueryString.parse(location.hash)
simpleQueryString.parse(location.search)
simpleQueryString.parse("myarr=1&myarr=2&myarr=3&myarr=4") // myarr: [1,2,3,4]
simpleQueryString.parse("http://example.org/test/?key=val¶m=1#anchor") // #anchor will be ignored
var qs = require('simple-query-string');
var parsed = qs.parse("key=val¶m=1");
console.log(parsed["key"]);
console.log(parsed["param"]);
<script src="https://cdn.rawgit.com/khalidsalomao/simple-query-string/22cc5bbe/src/simplequerystring.min.js"></script>
<script>
var parsed = simpleQueryString.parse('key=val¶m=1');
console.log(parsed["key"]);
</script>
require(['simple-query-string'], function(qs){
var p = qs.parse('key=val¶m=1');
console.log(p);
});
for..in
safeSafe to be used in a for in loop. The object is created with Object.create(null)
.
var dic = simpleQueryString.parse("http://example.org/?p1=val&p2=true&p3=3&p4=str");
for (var k in dic) {
console.log(dic[k]);
}
simpleQueryString.parse(null) // equals to {}
In some cases, you may want to use another separator instead of ampersand. Example using semicolon (';') as separator:
simpleQueryString.parse('p1=a;p2=1', ';') // equals to '{ p1:'a', p2: 1}'
Several benchmarks are run at each release to ensure maximum performance.
simpleQueryString.stringify({ key: "val", param: 1 })
//=> 'key=val¶m=1'
simpleQueryString.stringify({ p: 1, p2: true, p3: false }) // equals to 'p=1&p2=true&p3=false'
simpleQueryString.stringify({ myarr: [1,2,3,4] }) // equals to 'myarr=1&myarr=2&myarr=3&myarr=4'
var qs = require('simple-query-string');
var str = qs.stringify({ param: 1, p2: true, p3: false });
console.log(str); // equals to 'param=1&p2=true&p3=false'
<script src="https://cdn.rawgit.com/khalidsalomao/simple-query-string/22cc5bbe/src/simplequerystring.min.js"></script>
<script>
var str = simpleQueryString.stringify({ param: 1, p2: true, p3: false });
console.log(str);
</script>
require(['simple-query-string'], function(qs){
var str = qs.stringify({ param: 1, p2: true, p3: false });
console.log(str);
});
simpleQueryString.stringify({ p1: function(){ return 0; }, p2: 1 }) // equals to 'p2=1'
simpleQueryString.stringify(null) // equals to ''
In some cases, you may want to use another separator instead of ampersand. Example using semicolon (';') as separator:
simpleQueryString.stringify({ p1: 'a', p2: 1 }, ';') // equals to 'p1=a;p2=1'
Decode example:
var obj = simpleQueryString.parse("http://example.org/test/?key=val¶m=1");
// obj["key"] === "val"
// obj["param"] === "1"
Encode example:
var p = {
key1: true,
key2: [0, 1, 2],
key3: "string",
key4: 4321
};
var qStr = simpleQueryString.stringify(p);
$ npm install mocha -g
Use npm to run the test script 'spec/simplequerystring-test.js'
$ npm test
Run the tests by opening ./spec/testpage.html
.
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 - 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.
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 ]
MIT © 2016 Khalid Salomão
[1.3.2]
FAQs
Fast and simple way to parse and stringify URL query strings
The npm package simple-query-string receives a total of 3,068 weekly downloads. As such, simple-query-string popularity was classified as popular.
We found that simple-query-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.