-
-
Save jikanter/6049914a86ce9d7b77705f8be4b54adb to your computer and use it in GitHub Desktop.
QueryString Parsing Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let parseQs = function(str){ | |
| if ('string' != typeof str) return {}; | |
| str = String.prototype.trim(str); | |
| if ('' == str) return {}; | |
| var obj = {}; | |
| var pairs = str.split('&'); | |
| for (var i = 0; i < pairs.length; i++) { | |
| var parts = pairs[i].split('='); | |
| obj[parts[0]] = null == parts[1] | |
| ? '' | |
| : decodeURIComponent(parts[1]); | |
| } | |
| return obj; | |
| }; | |
| let stringify = function(obj){ | |
| if (!obj) return ''; | |
| var pairs = []; | |
| for (var key in obj) { | |
| pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])); | |
| } | |
| return pairs.join('&'); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var trim = require('trim'); | |
| exports.parse = function(str){ | |
| if ('string' != typeof str) return {}; | |
| str = trim(str); | |
| if ('' == str) return {}; | |
| var obj = {}; | |
| var pairs = str.split('&'); | |
| for (var i = 0; i < pairs.length; i++) { | |
| var parts = pairs[i].split('='); | |
| obj[parts[0]] = null == parts[1] | |
| ? '' | |
| : decodeURIComponent(parts[1]); | |
| } | |
| return obj; | |
| }; | |
| exports.stringify = function(obj){ | |
| if (!obj) return ''; | |
| var pairs = []; | |
| for (var key in obj) { | |
| pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])); | |
| } | |
| return pairs.join('&'); | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var querystring = require('querystring'); | |
| querystring.parse(window.location.search.slice(1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment