-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(urls): Add
queryStringify
utility
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 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 @@ | ||
export * from './query-stringify/query-stringify'; |
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,32 @@ | ||
import { queryStringify } from './query-stringify'; | ||
|
||
describe('query-stringify', () => { | ||
it('Returns empty string for empty/invalid input', () => { | ||
/* @ts-expect-error -- Edge case */ | ||
expect(queryStringify('')).toBe(''); | ||
|
||
expect(queryStringify([])).toBe(''); | ||
expect(queryStringify(undefined)).toBe(''); | ||
expect(queryStringify({})).toBe(''); | ||
}); | ||
|
||
it('Stringifies a simple object', () => { | ||
expect(queryStringify({ a: 1, b: '2' })).toBe('a=1&b=2'); | ||
}); | ||
|
||
it('Encodes keys and values', () => { | ||
expect(queryStringify({ 'foo+bar': 'foo+bar' })).toBe('foo%2Bbar=foo%2Bbar'); | ||
}); | ||
|
||
it('Stringifies an object containing nested keys', () => { | ||
expect(queryStringify({ a: 1, b: '2', c: { a: { d: 1 } } })).toBe('a=1&b=2&c[a][d]=1'); | ||
}); | ||
|
||
it('Stringifies an arrays', () => { | ||
expect(queryStringify({ a: 1, b: ['2', 3] })).toBe('a=1&b[0]=2&b[1]=3'); | ||
}); | ||
|
||
it('Respects `prefix` argument', () => { | ||
expect(queryStringify({ a: 1, b: 2 }, 'data')).toBe('data[a]=1&data[b]=2'); | ||
}); | ||
}); |
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,26 @@ | ||
import { isPlainObject, isArray, isEmpty } from '../../guards'; | ||
import { tsObject } from '../../objects'; | ||
import type { PlainObject } from '../../types'; | ||
|
||
/** | ||
* Stringify an object for use in a query string. | ||
* | ||
* @param object An object to stringify. | ||
* @param prefix A prefix for root element. | ||
* | ||
* @returns Query string built from the object. | ||
*/ | ||
export const queryStringify = (object: PlainObject | undefined, prefix?: string) => { | ||
if (isEmpty(object)) return ''; | ||
|
||
return tsObject.entries(object).reduce<string[]>((acc, [key, value]) => { | ||
const encodedKey = encodeURIComponent(key); | ||
const pairKey = prefix ? `${prefix}[${encodedKey}]` : encodedKey; | ||
const pair = isPlainObject(value) || isArray(value) | ||
? queryStringify(value, pairKey) | ||
: `${pairKey}=${encodeURIComponent(value)}`; | ||
|
||
acc.push(pair); | ||
return acc; | ||
}, []).filter(Boolean).join('&'); | ||
}; |