Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cloud-walker committed Nov 22, 2023
1 parent b10ffbd commit f74820d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
8 changes: 1 addition & 7 deletions packages/zod-sp/src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,8 @@ describe('.with(ZodSchema)', () => {
{ input: '', expected: { c: false, e: defaultTuple } },
{ input: 'c=true&a=123&e[]=foo,123', expected: { a: '123', c: true, e: ['foo', 123] } },
{ input: 'c=true&a=123&e[]=foo,123', expected: { a: '123', c: true, e: ['foo', 123] } },
{ input: zodSp.serialize({ d: [new Date(ISO_DATE_STUB), new Date(ISO_DATE_STUB)] }), expected: { a: '123', c: true, d: [new Date(ISO_DATE_STUB), new Date(ISO_DATE_STUB)], e: ['default', 0] } },
{ input: zodSp.serialize({ d: [new Date(ISO_DATE_STUB), new Date(ISO_DATE_STUB)] }), expected: { c: false, d: [new Date(ISO_DATE_STUB), new Date(ISO_DATE_STUB)], e: ['default', 0] } },
])('.parse($input) -> $expected', ({ input, expected }) => {
expect(dummySp.parse(new URLSearchParams(input))).toEqual(expected)
})

test.each([
{ input: 1, expected: 1 },
])('.serialize($input) -> $expected', ({ input, expected }) => {
expect(input).toBe(expected)
})
})
8 changes: 4 additions & 4 deletions packages/zod-sp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { ZodObject, ZodRawShape, TypeOf } from 'zod'
export const zodSp = {
parse(searchParams: URLSearchParams): Record<string, string | string[]> {
return [...searchParams.entries()].reduce<Record<string, string | string[]>>((acc, [key, value]) => {
if (value == null) {
return acc
}
// if (value == null) {
// return acc
// }

if (key.endsWith('[]')) {
acc[key.replace('[]', '')] = value.split(',')
Expand Down Expand Up @@ -45,7 +45,7 @@ export const zodSp = {
console.log(parsed)
return schema.parse(parsed)
}
const serialize = () => { }
const serialize = this.serialize
return { parse, serialize } as const
}
} as const

0 comments on commit f74820d

Please sign in to comment.