diff --git a/.changeset/nine-masks-run.md b/.changeset/nine-masks-run.md new file mode 100644 index 0000000..d222e87 --- /dev/null +++ b/.changeset/nine-masks-run.md @@ -0,0 +1,5 @@ +--- +'@joyid/common': patch +--- + +fix: search params not handle single item array correctly diff --git a/packages/common/package.json b/packages/common/package.json index f4c31aa..8efa0f3 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -31,6 +31,7 @@ "scripts": { "build": "tsup", "lint": "eslint .", + "test": "vitest", "format": "eslint --fix ." }, "files": [ diff --git a/packages/common/src/utils/search-params.spec.ts b/packages/common/src/utils/search-params.spec.ts new file mode 100644 index 0000000..513b599 --- /dev/null +++ b/packages/common/src/utils/search-params.spec.ts @@ -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)) + }) + }) +}) diff --git a/packages/common/src/utils/search-params.ts b/packages/common/src/utils/search-params.ts index 3752e32..986a091 100644 --- a/packages/common/src/utils/search-params.ts +++ b/packages/common/src/utils/search-params.ts @@ -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 */ @@ -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) }