├── .npmignore ├── .gitignore ├── test ├── tap-index.js ├── common-index.js └── index.js ├── index.js ├── index.d.ts ├── encode.d.ts ├── .github └── workflows │ ├── ci.yml │ ├── release.yml │ └── ci-legacy.yml ├── README.md ├── decode.d.ts ├── CHANGELOG.md ├── package.json ├── LICENSE ├── encode.js └── decode.js /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | .github 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | -------------------------------------------------------------------------------- /test/tap-index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | require("retape")(require("./index")) -------------------------------------------------------------------------------- /test/common-index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | require("test").run(require("./index")) -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.decode = exports.parse = require('./decode'); 4 | exports.encode = exports.stringify = require('./encode'); 5 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import decodeFuncType from "./decode"; 2 | import encodeFuncType from "./encode"; 3 | 4 | export const decode: decodeFuncType; 5 | export const parse: decodeFuncType; 6 | 7 | export const encode: encodeFuncType; 8 | export const stringify: encodeFuncType; 9 | -------------------------------------------------------------------------------- /encode.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * It serializes passed object into string 3 | * The numeric values must be finite. 4 | * Any other input values will be coerced to empty strings. 5 | * 6 | * @param obj The object to serialize into a URL query string 7 | * @param sep The substring used to delimit key and value pairs in the query string 8 | * @param eq The substring used to delimit keys and values in the query string 9 | * @param name 10 | */ 11 | export type encodeFuncType = ( 12 | obj?: Record, 13 | sep?: string, 14 | eq?: string, 15 | name?: any 16 | ) => string; 17 | 18 | export default encodeFuncType; 19 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: push 4 | 5 | jobs: 6 | test: 7 | name: Test on Node ${{ matrix.node }} and ${{ matrix.os }} 8 | 9 | runs-on: ${{ matrix.os }} 10 | strategy: 11 | matrix: 12 | node: ["10.x", "12.x", "14.x"] 13 | os: [ubuntu-latest, windows-latest, macOS-latest] 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | 18 | - name: Setup node 19 | uses: actions/setup-node@v2 20 | with: 21 | node-version: ${{ matrix.node }} 22 | 23 | - name: Installing Dependencies 24 | run: npm install 25 | 26 | - name: Running Tests 27 | run: npm test 28 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: RELEASE 2 | 3 | on: 4 | push: 5 | tags: 6 | - v[0-9]+.[0-9]+.[0-9]+ 7 | 8 | jobs: 9 | test: 10 | name: Release 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Setup node 17 | uses: actions/setup-node@v2 18 | with: 19 | node-version: 14.x 20 | registry-url: https://registry.npmjs.org 21 | 22 | - name: Installing Dependencies 23 | run: npm install 24 | 25 | - name: Running Tests 26 | run: npm test 27 | 28 | - name: Publishing to NPM 29 | env: 30 | NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} 31 | run: npm publish 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LEGACY: querystring 2 | 3 | > The querystring API is considered Legacy. New code should use the [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API instead. 4 | 5 | [![NPM](https://img.shields.io/npm/v/querystring.svg)](https://npm.im/querystring) 6 | [![gzip](https://badgen.net/bundlephobia/minzip/querystring@latest)](https://bundlephobia.com/result?p=querystring@latest) 7 | 8 | Node's querystring module for all engines. 9 | 10 | ## 🔧 Install 11 | 12 | ```sh 13 | npm i querystring 14 | ``` 15 | 16 | ## 📖 Documentation 17 | 18 | Refer to [Node's documentation for `querystring`](https://nodejs.org/api/querystring.html). 19 | 20 | ## 📜 License 21 | 22 | MIT © [Gozala](https://github.com/Gozala) 23 | -------------------------------------------------------------------------------- /.github/workflows/ci-legacy.yml: -------------------------------------------------------------------------------- 1 | name: LEGACY 2 | 3 | on: push 4 | 5 | jobs: 6 | test: 7 | name: Test on Node ${{ matrix.node }} and ${{ matrix.os }} 8 | 9 | runs-on: ${{ matrix.os }} 10 | strategy: 11 | matrix: 12 | node: ["0.8", "0.10", "0.12", "4", "6", "8"] 13 | os: [ubuntu-16.04] 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | 18 | - name: Setup node ${{ matrix.node }} 19 | uses: dcodeIO/setup-node-nvm@master 20 | with: 21 | node-version: ${{ matrix.node }} 22 | 23 | - name: Disabling Strict SSL 24 | run: npm conf set strict-ssl false 25 | 26 | - name: Installing Dependencies 27 | run: npm install 28 | 29 | - name: Running Tests 30 | run: npm test 31 | -------------------------------------------------------------------------------- /decode.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * parses a URL query string into a collection of key and value pairs 3 | * 4 | * @param qs The URL query string to parse 5 | * @param sep The substring used to delimit key and value pairs in the query string 6 | * @param eq The substring used to delimit keys and values in the query string 7 | * @param options.decodeURIComponent The function to use when decoding percent-encoded characters in the query string 8 | * @param options.maxKeys Specifies the maximum number of keys to parse. Specify 0 to remove key counting limitations default 1000 9 | */ 10 | export type decodeFuncType = ( 11 | qs?: string, 12 | sep?: string, 13 | eq?: string, 14 | options?: { 15 | decodeURIComponent?: Function; 16 | maxKeys?: number; 17 | } 18 | ) => Record; 19 | 20 | export default decodeFuncType; 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ### [0.2.1](https://github.com/Gozala/querystring/compare/v0.2.0...v0.2.1) (2021-02-15) 6 | 7 | _Maintanance update_ 8 | 9 | ## [0.2.0] - 2013-02-21 10 | 11 | ### Changed 12 | 13 | - Refactor into function per-module idiomatic style. 14 | - Improved test coverage. 15 | 16 | ## [0.1.0] - 2011-12-13 17 | 18 | ### Changed 19 | 20 | - Minor project reorganization 21 | 22 | ## [0.0.3] - 2011-04-16 23 | 24 | ### Added 25 | 26 | - Support for AMD module loaders 27 | 28 | ## [0.0.2] - 2011-04-16 29 | 30 | ### Changed 31 | 32 | - Ported unit tests 33 | 34 | ### Removed 35 | 36 | - Removed functionality that depended on Buffers 37 | 38 | ## [0.0.1] - 2011-04-15 39 | 40 | ### Added 41 | 42 | - Initial release 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "querystring", 3 | "id": "querystring", 4 | "version": "0.2.1", 5 | "description": "Node's querystring module for all engines.", 6 | "keywords": [ 7 | "commonjs", 8 | "query", 9 | "querystring" 10 | ], 11 | "author": "Irakli Gozalishvili ", 12 | "main": "index.js", 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/Gozala/querystring.git", 16 | "web": "https://github.com/Gozala/querystring" 17 | }, 18 | "bugs": { 19 | "url": "http://github.com/Gozala/querystring/issues/" 20 | }, 21 | "devDependencies": { 22 | "test": "~0.x.0", 23 | "retape": "~0.x.0", 24 | "tape": "~0.1.5" 25 | }, 26 | "engines": { 27 | "node": ">=0.4.x" 28 | }, 29 | "scripts": { 30 | "test": "npm run test-node && npm run test-tap", 31 | "test-node": "node ./test/common-index.js", 32 | "test-tap": "node ./test/tap-index.js" 33 | }, 34 | "license": "MIT" 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Irakli Gozalishvili 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /encode.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 'use strict'; 23 | 24 | var stringifyPrimitive = function(v) { 25 | switch (typeof v) { 26 | case 'string': 27 | return v; 28 | 29 | case 'boolean': 30 | return v ? 'true' : 'false'; 31 | 32 | case 'number': 33 | return isFinite(v) ? v : ''; 34 | 35 | default: 36 | return ''; 37 | } 38 | }; 39 | 40 | module.exports = function(obj, sep, eq, name) { 41 | sep = sep || '&'; 42 | eq = eq || '='; 43 | if (obj === null) { 44 | obj = undefined; 45 | } 46 | 47 | if (typeof obj === 'object') { 48 | return Object.keys(obj).map(function(k) { 49 | var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; 50 | if (Array.isArray(obj[k])) { 51 | return obj[k].map(function(v) { 52 | return ks + encodeURIComponent(stringifyPrimitive(v)); 53 | }).join(sep); 54 | } else { 55 | return ks + encodeURIComponent(stringifyPrimitive(obj[k])); 56 | } 57 | }).filter(Boolean).join(sep); 58 | 59 | } 60 | 61 | if (!name) return ''; 62 | return encodeURIComponent(stringifyPrimitive(name)) + eq + 63 | encodeURIComponent(stringifyPrimitive(obj)); 64 | }; 65 | -------------------------------------------------------------------------------- /decode.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 'use strict'; 23 | 24 | // If obj.hasOwnProperty has been overridden, then calling 25 | // obj.hasOwnProperty(prop) will break. 26 | // See: https://github.com/joyent/node/issues/1707 27 | function hasOwnProperty(obj, prop) { 28 | return Object.prototype.hasOwnProperty.call(obj, prop); 29 | } 30 | 31 | module.exports = function(qs, sep, eq, options) { 32 | sep = sep || '&'; 33 | eq = eq || '='; 34 | var obj = {}; 35 | 36 | if (typeof qs !== 'string' || qs.length === 0) { 37 | return obj; 38 | } 39 | 40 | var regexp = /\+/g; 41 | qs = qs.split(sep); 42 | 43 | var maxKeys = 1000; 44 | if (options && typeof options.maxKeys === 'number') { 45 | maxKeys = options.maxKeys; 46 | } 47 | 48 | var len = qs.length; 49 | // maxKeys <= 0 means that we should not limit keys count 50 | if (maxKeys > 0 && len > maxKeys) { 51 | len = maxKeys; 52 | } 53 | 54 | for (var i = 0; i < len; ++i) { 55 | var x = qs[i].replace(regexp, '%20'), 56 | idx = x.indexOf(eq), 57 | kstr, vstr, k, v; 58 | 59 | if (idx >= 0) { 60 | kstr = x.substr(0, idx); 61 | vstr = x.substr(idx + 1); 62 | } else { 63 | kstr = x; 64 | vstr = ''; 65 | } 66 | 67 | k = decodeURIComponent(kstr); 68 | v = decodeURIComponent(vstr); 69 | 70 | if (!hasOwnProperty(obj, k)) { 71 | obj[k] = v; 72 | } else if (Array.isArray(obj[k])) { 73 | obj[k].push(v); 74 | } else { 75 | obj[k] = [obj[k], v]; 76 | } 77 | } 78 | 79 | return obj; 80 | }; 81 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | "use strict"; 23 | 24 | // test using assert 25 | var qs = require('../'); 26 | 27 | // folding block, commented to pass gjslint 28 | // {{{ 29 | // [ wonkyQS, canonicalQS, obj ] 30 | var qsTestCases = [ 31 | ['foo=918854443121279438895193', 32 | 'foo=918854443121279438895193', 33 | {'foo': '918854443121279438895193'}], 34 | ['foo=bar', 'foo=bar', {'foo': 'bar'}], 35 | ['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}], 36 | ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}], 37 | ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F', 38 | 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', 39 | {'my weird field': 'q1!2"\'w$5&7/z8)?' }], 40 | ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}], 41 | ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}], 42 | ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=', 43 | 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=', 44 | { 'str': 'foo', 45 | 'arr': ['1', '2', '3'], 46 | 'somenull': '', 47 | 'undef': ''}], 48 | [' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}], 49 | // disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}], 50 | ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }], 51 | // See: https://github.com/joyent/node/issues/1707 52 | ['hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz', 53 | 'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz', 54 | { hasOwnProperty: 'x', 55 | toString: 'foo', 56 | valueOf: 'bar', 57 | __defineGetter__: 'baz' }], 58 | // See: https://github.com/joyent/node/issues/3058 59 | ['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }] 60 | ]; 61 | 62 | // [ wonkyQS, canonicalQS, obj ] 63 | var qsColonTestCases = [ 64 | ['foo:bar', 'foo:bar', {'foo': 'bar'}], 65 | ['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}], 66 | ['foo:1&bar:2;baz:quux', 67 | 'foo:1%26bar%3A2;baz:quux', 68 | {'foo': '1&bar:2', 'baz': 'quux'}], 69 | ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}], 70 | ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}] 71 | ]; 72 | 73 | // [wonkyObj, qs, canonicalObj] 74 | var extendedFunction = function() {}; 75 | extendedFunction.prototype = {a: 'b'}; 76 | var qsWeirdObjects = [ 77 | [{regexp: /./g}, 'regexp=', {'regexp': ''}], 78 | [{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}], 79 | [{fn: function() {}}, 'fn=', {'fn': ''}], 80 | [{fn: new Function('')}, 'fn=', {'fn': ''}], 81 | [{math: Math}, 'math=', {'math': ''}], 82 | [{e: extendedFunction}, 'e=', {'e': ''}], 83 | [{d: new Date()}, 'd=', {'d': ''}], 84 | [{d: Date}, 'd=', {'d': ''}], 85 | [{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}], 86 | [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}], 87 | [{n: null}, 'n=', {'n': ''}], 88 | [{nan: NaN}, 'nan=', {'nan': ''}], 89 | [{inf: Infinity}, 'inf=', {'inf': ''}] 90 | ]; 91 | // }}} 92 | 93 | var qsNoMungeTestCases = [ 94 | ['', {}], 95 | ['foo=bar&foo=baz', {'foo': ['bar', 'baz']}], 96 | ['blah=burp', {'blah': 'burp'}], 97 | ['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}], 98 | ['frappucino=muffin&goat%5B%5D=scone&pond=moose', 99 | {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}], 100 | ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}] 101 | ]; 102 | 103 | var stringifyTestCases = [ 104 | ['', {'foo': []}], 105 | ['bar=baz', {'foo': [], bar: 'baz'}], 106 | ] 107 | 108 | exports['test basic'] = function(assert) { 109 | assert.strictEqual('918854443121279438895193', 110 | qs.parse('id=918854443121279438895193').id, 111 | 'prase id=918854443121279438895193'); 112 | }; 113 | 114 | exports['test that the canonical qs is parsed properly'] = function(assert) { 115 | qsTestCases.forEach(function(testCase) { 116 | assert.deepEqual(testCase[2], qs.parse(testCase[0]), 117 | 'parse ' + testCase[0]); 118 | }); 119 | }; 120 | 121 | 122 | exports['test that the colon test cases can do the same'] = function(assert) { 123 | qsColonTestCases.forEach(function(testCase) { 124 | assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'), 125 | 'parse ' + testCase[0] + ' -> ; :'); 126 | }); 127 | }; 128 | 129 | exports['test the weird objects, that they get parsed properly'] = function(assert) { 130 | qsWeirdObjects.forEach(function(testCase) { 131 | assert.deepEqual(testCase[2], qs.parse(testCase[1]), 132 | 'parse ' + testCase[1]); 133 | }); 134 | }; 135 | 136 | exports['test non munge test cases'] = function(assert) { 137 | qsNoMungeTestCases.forEach(function(testCase) { 138 | assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false), 139 | 'stringify ' + JSON.stringify(testCase[1]) + ' -> & ='); 140 | }); 141 | }; 142 | 143 | exports['test the nested qs-in-qs case'] = function(assert) { 144 | var f = qs.parse('a=b&q=x%3Dy%26y%3Dz'); 145 | f.q = qs.parse(f.q); 146 | assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }, 147 | 'parse a=b&q=x%3Dy%26y%3Dz'); 148 | }; 149 | 150 | exports['test nested in colon'] = function(assert) { 151 | var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':'); 152 | f.q = qs.parse(f.q, ';', ':'); 153 | assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }, 154 | 'parse a:b;q:x%3Ay%3By%3Az -> ; :'); 155 | }; 156 | 157 | exports['test stringifying'] = function(assert) { 158 | qsTestCases.forEach(function(testCase) { 159 | assert.equal(testCase[1], qs.stringify(testCase[2]), 160 | 'stringify ' + JSON.stringify(testCase[2])); 161 | }); 162 | 163 | stringifyTestCases.forEach(function(testCase) { 164 | assert.equal(testCase[0], qs.stringify(testCase[1]), 165 | 'stringify ' + JSON.stringify(testCase[1])); 166 | }); 167 | 168 | qsColonTestCases.forEach(function(testCase) { 169 | assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'), 170 | 'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :'); 171 | }); 172 | 173 | qsWeirdObjects.forEach(function(testCase) { 174 | assert.equal(testCase[1], qs.stringify(testCase[0]), 175 | 'stringify ' + JSON.stringify(testCase[0])); 176 | }); 177 | }; 178 | 179 | exports['test stringifying nested'] = function(assert) { 180 | var f = qs.stringify({ 181 | a: 'b', 182 | q: qs.stringify({ 183 | x: 'y', 184 | y: 'z' 185 | }) 186 | }); 187 | assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz', 188 | JSON.stringify({ 189 | a: 'b', 190 | 'qs.stringify -> q': { 191 | x: 'y', 192 | y: 'z' 193 | } 194 | })); 195 | 196 | var threw = false; 197 | try { qs.parse(undefined); } catch(error) { threw = true; } 198 | assert.ok(!threw, "does not throws on undefined"); 199 | }; 200 | 201 | exports['test nested in colon'] = function(assert) { 202 | var f = qs.stringify({ 203 | a: 'b', 204 | q: qs.stringify({ 205 | x: 'y', 206 | y: 'z' 207 | }, ';', ':') 208 | }, ';', ':'); 209 | assert.equal(f, 'a:b;q:x%3Ay%3By%3Az', 210 | 'stringify ' + JSON.stringify({ 211 | a: 'b', 212 | 'qs.stringify -> q': { 213 | x: 'y', 214 | y: 'z' 215 | } 216 | }) + ' -> ; : '); 217 | 218 | 219 | assert.deepEqual({}, qs.parse(), 'parse undefined'); 220 | }; 221 | --------------------------------------------------------------------------------