query-string
Advanced tools
+48
| 'use strict'; | ||
| exports.parse = function (str) { | ||
| if (typeof str !== 'string') { | ||
| return {}; | ||
| } | ||
| str = str.trim().replace(/^(\?|#)/, ''); | ||
| if (!str) { | ||
| return {}; | ||
| } | ||
| return str.trim().split('&').reduce(function (ret, param) { | ||
| var parts = param.replace(/\+/g, ' ').split('='); | ||
| var key = parts[0]; | ||
| var val = parts[1]; | ||
| key = decodeURIComponent(key); | ||
| // missing `=` should be `null`: | ||
| // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters | ||
| val = val === undefined ? null : decodeURIComponent(val); | ||
| if (!ret.hasOwnProperty(key)) { | ||
| ret[key] = val; | ||
| } else if (Array.isArray(ret[key])) { | ||
| ret[key].push(val); | ||
| } else { | ||
| ret[key] = [ret[key], val]; | ||
| } | ||
| return ret; | ||
| }, {}); | ||
| }; | ||
| exports.stringify = function (obj) { | ||
| return obj ? Object.keys(obj).map(function (key) { | ||
| var val = obj[key]; | ||
| if (Array.isArray(val)) { | ||
| return val.map(function (val2) { | ||
| return encodeURIComponent(key) + '=' + encodeURIComponent(val2); | ||
| }).join('&'); | ||
| } | ||
| return encodeURIComponent(key) + '=' + encodeURIComponent(val); | ||
| }).join('&') : ''; | ||
| }; |
+3
-4
| { | ||
| "name": "query-string", | ||
| "version": "1.0.1", | ||
| "version": "2.0.0", | ||
| "description": "Parse and stringify URL query strings", | ||
@@ -24,8 +24,7 @@ "keywords": [ | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "http://sindresorhus.com" | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "files": [ | ||
| "query-string.js" | ||
| "index.js" | ||
| ], | ||
| "main": "query-string.js", | ||
| "repository": "sindresorhus/query-string", | ||
@@ -32,0 +31,0 @@ "scripts": { |
+9
-15
@@ -8,31 +8,25 @@ # query-string [](https://travis-ci.org/sindresorhus/query-string) | ||
| ```sh | ||
| ``` | ||
| $ npm install --save query-string | ||
| ``` | ||
| ```sh | ||
| $ bower install --save query-string | ||
| ``` | ||
| ```sh | ||
| $ component install sindresorhus/query-string | ||
| ``` | ||
| ## Usage | ||
| ```js | ||
| var queryString = require('query-string'); | ||
| console.log(location.search); | ||
| // ?foo=bar | ||
| //=> ?foo=bar | ||
| var parsed = queryString.parse(location.search); | ||
| console.log(parsed); | ||
| // {foo: 'bar'} | ||
| //=> {foo: 'bar'} | ||
| console.log(location.hash); | ||
| // #token=bada55cafe | ||
| //=> #token=bada55cafe | ||
| var parsedHash = queryString.parse(location.hash); | ||
| console.log(parsedHash); | ||
| // {token: 'bada55cafe'} | ||
| //=> {token: 'bada55cafe'} | ||
@@ -45,3 +39,3 @@ parsed.foo = 'unicorn'; | ||
| console.log(location.search); | ||
| // ?foo=unicorn&ilike=pizza | ||
| //=> ?foo=unicorn&ilike=pizza | ||
| ``` | ||
@@ -74,3 +68,3 @@ | ||
| }); | ||
| // foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D | ||
| //=> foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D | ||
| ``` | ||
@@ -77,0 +71,0 @@ |
| /*! | ||
| query-string | ||
| Parse and stringify URL query strings | ||
| https://github.com/sindresorhus/query-string | ||
| by Sindre Sorhus | ||
| MIT License | ||
| */ | ||
| (function () { | ||
| 'use strict'; | ||
| var queryString = {}; | ||
| queryString.parse = function (str) { | ||
| if (typeof str !== 'string') { | ||
| return {}; | ||
| } | ||
| str = str.trim().replace(/^(\?|#)/, ''); | ||
| if (!str) { | ||
| return {}; | ||
| } | ||
| return str.trim().split('&').reduce(function (ret, param) { | ||
| var parts = param.replace(/\+/g, ' ').split('='); | ||
| var key = parts[0]; | ||
| var val = parts[1]; | ||
| key = decodeURIComponent(key); | ||
| // missing `=` should be `null`: | ||
| // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters | ||
| val = val === undefined ? null : decodeURIComponent(val); | ||
| if (!ret.hasOwnProperty(key)) { | ||
| ret[key] = val; | ||
| } else if (Array.isArray(ret[key])) { | ||
| ret[key].push(val); | ||
| } else { | ||
| ret[key] = [ret[key], val]; | ||
| } | ||
| return ret; | ||
| }, {}); | ||
| }; | ||
| queryString.stringify = function (obj) { | ||
| return obj ? Object.keys(obj).map(function (key) { | ||
| var val = obj[key]; | ||
| if (Array.isArray(val)) { | ||
| return val.map(function (val2) { | ||
| return encodeURIComponent(key) + '=' + encodeURIComponent(val2); | ||
| }).join('&'); | ||
| } | ||
| return encodeURIComponent(key) + '=' + encodeURIComponent(val); | ||
| }).join('&') : ''; | ||
| }; | ||
| if (typeof define === 'function' && define.amd) { | ||
| define(function() { return queryString; }); | ||
| } else if (typeof module !== 'undefined' && module.exports) { | ||
| module.exports = queryString; | ||
| } else { | ||
| self.queryString = queryString; | ||
| } | ||
| })(); |
4403
-11.28%38
-30.91%73
-7.59%