Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packages/sui-js): parse query string allow sparse config #1802

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/sui-js/src/string/parse-query-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import {parse} from 'qs'
*
* @param {string} query
* @param {object} [options={}]
* @param {boolean} [options.ignoreQueryPrefix=true] - avoid the leading question mark
* @param {boolean} [options.allowSparse] - parse sparse arrays
* @param {boolean} [options.comma] - comma to join array
* @param {boolean} [options.ignoreQueryPrefix=true] - avoid the leading question mark
* @param {string} [options.delimiter] - delimiter
*/
function parseQueryString(query, options = {}) {
const {ignoreQueryPrefix = true, comma, delimiter} = options
const {allowSparse = false, ignoreQueryPrefix = true, comma, delimiter} = options

const mergedOptions = {
allowSparse,
ignoreQueryPrefix,
...(typeof comma !== 'undefined' && {comma}),
...(typeof delimiter !== 'undefined' && {delimiter})
Expand Down
9 changes: 9 additions & 0 deletions packages/sui-js/test/stringSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ describe('@s-ui/js', () => {
const expected = {a: 'b', c: 'd'}
expect(parsedQueryParams).to.deep.equal(expected)
})

it('should convert query string to object params compacting a sparse array to only the existing values preserving their order', () => {
const query = '?a[0]=b&a[2]=c'
const options = {allowSparse: true}
const parsedQueryParams = parseQueryString(query, options)

const expected = {a: ['b', undefined, 'c']}
expect(parsedQueryParams).to.deep.equal(expected)
})
})
describe('string:fromArrayToCommaQueryString', () => {
it('should convert params array to comma separated object params', () => {
Expand Down