Skip to content

Commit

Permalink
Merge pull request #1802 from SUI-Components/feature/parse-query-stri…
Browse files Browse the repository at this point in the history
…ng-allow-sparse-config

feat(packages/sui-js): parse query string allow sparse config
  • Loading branch information
AgonisticKatai authored Aug 2, 2024
2 parents 09703ea + 79cb856 commit 10b2873
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/sui-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ console.log(hasAccents('Árbol')) // true
import {parseQueryString} from '@s-ui/js/lib/string'

console.log(parseQueryString('?targetPage=pta')) // {targetPage: "pta"}
console.log(parseQueryString('?makeIds[0]=123&makeIds[2]=456')) // {makeIds: ["123", "456"]}

// example with allowSparse option
const query = '?makeIds[0]=123&makeIds[2]=456'
const options = {allowSparse: true}
const parsedQueryString = parseQueryString(query, options)
console.log(parsedQueryString) // {makeIds: ["123", undefined, "456"]}
```

```js
Expand Down Expand Up @@ -162,6 +169,18 @@ const queryParams = {a: 1, b: 'lorem/ipsum', m: [1, 2]}
const options = {encode: false}
const queryString = toQueryString(queryParams, options)
console.log(queryString) // 'a=1&b=lorem/ipsum&m=1,2'

// example with addQueryPrefix option
const queryParams = {a: 1, b: 2}
const options = {addQueryPrefix: true}
const queryString = toQueryString(queryParams, options)
console.log(queryString) // '?a=1&b=2'

// example with skipNulls option
const queryParams = {a: 1, b: null}
const options = {skipNulls: true}
const queryString = toQueryString(queryParams, options)
console.log(queryString) // 'a=1'
```

```js
Expand Down
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

0 comments on commit 10b2873

Please sign in to comment.