Skip to content

Commit

Permalink
fix: search params not handle single item array correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Jul 3, 2024
1 parent fb85d7a commit 287507e
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-masks-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@joyid/common': patch
---

fix: search params not handle single item array correctly
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"scripts": {
"build": "tsup",
"lint": "eslint .",
"test": "vitest",
"format": "eslint --fix ."
},
"files": [
Expand Down
179 changes: 179 additions & 0 deletions packages/common/src/utils/search-params.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { describe, expect, it } from 'vitest'
import { encodeSearch, decodeSearch } from './search-params'

describe('search-params', () => {
describe('array', () => {
it('array', () => {
const data = {
a: [1, 2, 3],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - single number', () => {
const data = {
a: [1],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - single string', () => {
const data = {
a: ['1'],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - single object', () => {
const data = {
a: [{ b: 1 }],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - single object with array', () => {
const data = {
a: [{ b: [1, 2] }],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - single object with object', () => {
const data = {
a: [{ b: { c: 1 } }],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - single object with array of objects', () => {
const data = {
a: [{ b: [{ c: 1 }, { c: 2 }] }],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - single object with object of arrays', () => {
const data = {
a: [{ b: { c: [1, 2] } }],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - multiple types', () => {
const data = {
a: [1, '2', { c: 3 }],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('array - nested', () => {
const data = {
a: [1, [2, [3]]],
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})
})

describe('object', () => {
it('object - single number', () => {
const data = {
a: { b: 1 },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - single string', () => {
const data = {
a: { b: '1' },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - single object', () => {
const data = {
a: { b: { c: 1 } },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - single object with array', () => {
const data = {
a: { b: [1, 2] },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - single object with object', () => {
const data = {
a: { b: { c: 1 } },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - single object with array of objects', () => {
const data = {
a: { b: [{ c: 1 }, { c: 2 }] },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - single object with object of arrays', () => {
const data = {
a: { b: { c: [1, 2] } },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - multiple types', () => {
const data = {
a: { b: 1, c: '2', d: { e: 3 } },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})

it('object - nested', () => {
const data = {
a: { b: 1, c: { d: 2, e: { f: 3 } } },
}

const encoded = encodeSearch(data)
expect(data).toStrictEqual(decodeSearch(encoded))
})
})
})
3 changes: 0 additions & 3 deletions packages/common/src/utils/search-params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable unicorn/no-array-callback-reference */
/* eslint-disable unicorn/no-typeof-undefined */
/* eslint-disable unicorn/no-array-for-each */
/* eslint-disable unicorn/prefer-string-slice */
Expand Down Expand Up @@ -67,8 +66,6 @@ export function stringifySearchWith(
const val = search[key]
if (typeof val === 'undefined' || val === undefined) {
delete search[key]
} else if (Array.isArray(val)) {
search[key] = val.map(stringifyValue)
} else {
search[key] = stringifyValue(val)
}
Expand Down

0 comments on commit 287507e

Please sign in to comment.