-
-
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.
Merge branch 'master' into statuses@2
- Loading branch information
Showing
5 changed files
with
100 additions
and
5 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
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
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 | ||
} | ||
} |