-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Replace node querystring with URLSearchParams (#1828)
Fix #1570 Replace node querystring with URLSearchParams it mimics the way that querystring works ## Checklist - [x] I have ensured my pull request is not behind the main or master branch of the original repository. - [x] I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first. - [x] I have written a commit message that passes commitlint linting. - [x] I have ensured that my code changes pass linting tests. - [x] I have ensured that my code changes pass unit tests. - [x] I have described my pull request and the reasons for code changes along with context if necessary.
- Loading branch information
Showing
3 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const sp = require('../../lib/search-params') | ||
const assert = require('assert') | ||
|
||
describe('search-params', () => { | ||
describe('stringify', () => { | ||
it('Should stringify a simple object', () => { | ||
assert.deepStrictEqual(sp.stringify({ a: 1, b: 'b' }), 'a=1&b=b') | ||
}) | ||
|
||
it('Should stringify an object with an array', () => { | ||
assert.deepStrictEqual(sp.stringify({ a: [1, 2] }), 'a=1&a=2') | ||
}) | ||
|
||
it('Should stringify an object with an array with a single value', () => { | ||
assert.deepStrictEqual(sp.stringify({ a: [1] }), 'a=1') | ||
}) | ||
|
||
it('Stringify an object with an array with a single empty value', () => { | ||
assert.deepStrictEqual(sp.stringify({ a: [''] }), 'a=') | ||
}) | ||
|
||
it('Should not stringify an object with a nested object', () => { | ||
assert.deepStrictEqual(sp.stringify({ a: { b: 1 } }), 'a=') | ||
}) | ||
}) | ||
|
||
describe('parse', () => { | ||
it('Should parse a simple query string', () => { | ||
assert.deepStrictEqual(sp.parse('a=1&b=2'), { a: '1', b: '2' }) | ||
}) | ||
|
||
it('Should parse a query string with same key and multiple values', () => { | ||
assert.deepEqual(sp.parse('a=1&a=2'), { a: ['1', '2'] }) | ||
}) | ||
|
||
it('Should parse a query string with an array with a single empty value', () => { | ||
assert.deepStrictEqual(sp.parse('a='), { a: '' }) | ||
}) | ||
}) | ||
}) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const URLSearchParams = require('url').URLSearchParams | ||
|
||
module.exports = { | ||
stringify: (obj) => { | ||
const searchParams = new URLSearchParams() | ||
const addKey = (k, v, params) => { | ||
const val = typeof v === 'string' || typeof v === 'number' ? v : '' | ||
params.append(k, val) | ||
} | ||
|
||
for (const [key, value] of Object.entries(obj)) { | ||
if (Array.isArray(value)) { | ||
const lgth = value.length | ||
for (let i = 0; i < lgth; i++) { | ||
addKey(key, value[i], searchParams) | ||
} | ||
} else { | ||
addKey(key, value, searchParams) | ||
} | ||
} | ||
return searchParams.toString() | ||
}, | ||
|
||
parse: (str) => { | ||
const searchParams = new URLSearchParams(str) | ||
const obj = {} | ||
for (const key of searchParams.keys()) { | ||
const values = searchParams.getAll(key) | ||
obj[key] = values.length <= 1 ? values[0] : values | ||
} | ||
return obj | ||
} | ||
} |