Skip to content

Commit 6f0bec5

Browse files
committed
Pass though non-numbers
1 parent a07ff78 commit 6f0bec5

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/lib/parse.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,3 @@ test('parseUrlSearchParams: with URLSearchParams input', (t) => {
2323
'with URLSearchParams input',
2424
)
2525
})
26-
27-
test.todo('pass though number values that parse as NaN')
28-
test.todo(
29-
'pass though boolean values that do not parse as truthy or falsy values',
30-
)
31-
32-
// e.g., foo.bar= would conflict with foo.bar.a= or foo.bar.b=2
33-
// since this would be a null object containing values (null is still a value).
34-
test.todo('cannot parse conflicting object keys')

src/lib/parse.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ const parse = (k: string, values: string[], type: ValueType): unknown => {
6565

6666
const parseNumber = (v: string): number | null | string => {
6767
if (v.length === 0) return null
68+
if (v === 'Infinity' || v === '-Infinity') return v
6869
const n = Number(v)
6970
if (isNaN(n)) return v
71+
if (n === Infinity || n === -Infinity) return v
7072
return n
7173
}
7274

test/edge-cases.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import test from 'ava'
2+
import { z } from 'zod'
3+
4+
import { parseUrlSearchParams } from '@seamapi/url-search-params-parser'
5+
6+
test('pass though number values that do not parse as number', (t) => {
7+
t.deepEqual(parseUrlSearchParams('foo=a', z.object({ foo: z.number() })), {
8+
foo: 'a',
9+
})
10+
t.deepEqual(parseUrlSearchParams('foo=NaN', z.object({ foo: z.number() })), {
11+
foo: 'NaN',
12+
})
13+
t.deepEqual(
14+
parseUrlSearchParams('foo=Infinity', z.object({ foo: z.number() })),
15+
{
16+
foo: 'Infinity',
17+
},
18+
)
19+
t.deepEqual(
20+
parseUrlSearchParams('foo=-Infinity', z.object({ foo: z.number() })),
21+
{
22+
foo: '-Infinity',
23+
},
24+
)
25+
})
26+
test.todo(
27+
'pass though boolean values that do not parse as truthy or falsy values',
28+
)
29+
30+
// e.g., foo.bar= would conflict with foo.bar.a= or foo.bar.b=2
31+
// since this would be a null object containing values (null is still a value).
32+
test.todo('cannot parse conflicting object keys')

0 commit comments

Comments
 (0)