Skip to content

Commit 4a3fde4

Browse files
committed
refactor: rename numbers to integers
1 parent d165003 commit 4a3fde4

File tree

4 files changed

+248
-2
lines changed

4 files changed

+248
-2
lines changed

packages/router/src/experimental/route-resolver/matchers/matcher-pattern.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expectTypeOf, it } from 'vitest'
22
import { MatcherPatternPathDynamic } from './matcher-pattern'
3-
import { PARAM_INTEGER_SINGLE } from './param-parsers/numbers'
3+
import { PARAM_INTEGER_SINGLE } from './param-parsers/integers'
44
import { PATH_PARAM_PARSER_DEFAULTS } from './param-parsers'
55
import { PATH_PARAM_SINGLE_DEFAULT } from './param-parsers'
66

packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export const PATH_PARAM_PARSER_DEFAULTS = {
4141

4242
export type { ParamParser }
4343

44-
export { PARAM_PARSER_INT } from './numbers'
44+
export { PARAM_PARSER_INT } from './integers'
4545
export { PARAM_PARSER_BOOL } from './booleans'
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
import { describe, expect, it } from 'vitest'
2+
import {
3+
PARAM_INTEGER_SINGLE,
4+
PARAM_INTEGER_OPTIONAL,
5+
PARAM_INTEGER_REPEATABLE,
6+
PARAM_INTEGER_REPEATABLE_OPTIONAL,
7+
PARAM_PARSER_INT,
8+
} from './integers'
9+
10+
describe('PARAM_INTEGER_SINGLE', () => {
11+
describe('get()', () => {
12+
it('parses valid integers', () => {
13+
expect(PARAM_INTEGER_SINGLE.get('0')).toBe(0)
14+
expect(PARAM_INTEGER_SINGLE.get('1')).toBe(1)
15+
expect(PARAM_INTEGER_SINGLE.get('42')).toBe(42)
16+
expect(PARAM_INTEGER_SINGLE.get('-1')).toBe(-1)
17+
expect(PARAM_INTEGER_SINGLE.get('-999')).toBe(-999)
18+
expect(PARAM_INTEGER_SINGLE.get('2147483647')).toBe(2147483647)
19+
})
20+
21+
it('throws for decimal numbers', () => {
22+
expect(() => PARAM_INTEGER_SINGLE.get('1.5')).toThrow()
23+
expect(() => PARAM_INTEGER_SINGLE.get('3.14159')).toThrow()
24+
expect(() => PARAM_INTEGER_SINGLE.get('0.1')).toThrow()
25+
expect(() => PARAM_INTEGER_SINGLE.get('-2.5')).toThrow()
26+
})
27+
28+
it('throws for non-numeric strings', () => {
29+
expect(() => PARAM_INTEGER_SINGLE.get('abc')).toThrow()
30+
expect(() => PARAM_INTEGER_SINGLE.get('12abc')).toThrow()
31+
expect(() => PARAM_INTEGER_SINGLE.get('abc12')).toThrow()
32+
expect(() => PARAM_INTEGER_SINGLE.get('true')).toThrow()
33+
expect(() => PARAM_INTEGER_SINGLE.get('false')).toThrow()
34+
expect(() => PARAM_INTEGER_SINGLE.get('NaN')).toThrow()
35+
expect(() => PARAM_INTEGER_SINGLE.get('Infinity')).toThrow()
36+
expect(() => PARAM_INTEGER_SINGLE.get('-Infinity')).toThrow()
37+
})
38+
39+
it('throws for empty strings', () => {
40+
expect(() => PARAM_INTEGER_SINGLE.get('')).toThrow()
41+
})
42+
43+
it('parses whitespace strings as zero', () => {
44+
expect(PARAM_INTEGER_SINGLE.get(' ')).toBe(0)
45+
expect(PARAM_INTEGER_SINGLE.get(' ')).toBe(0)
46+
expect(PARAM_INTEGER_SINGLE.get('\n')).toBe(0)
47+
expect(PARAM_INTEGER_SINGLE.get('\t')).toBe(0)
48+
})
49+
50+
it('throws for null', () => {
51+
expect(() => PARAM_INTEGER_SINGLE.get(null)).toThrow()
52+
})
53+
54+
it('parses numbers with leading/trailing whitespace', () => {
55+
expect(PARAM_INTEGER_SINGLE.get(' 42')).toBe(42)
56+
expect(PARAM_INTEGER_SINGLE.get('42 ')).toBe(42)
57+
expect(PARAM_INTEGER_SINGLE.get(' 42 ')).toBe(42)
58+
})
59+
60+
it('parses valid scientific notation as integers', () => {
61+
expect(PARAM_INTEGER_SINGLE.get('1e5')).toBe(100000)
62+
expect(PARAM_INTEGER_SINGLE.get('1e2')).toBe(100)
63+
})
64+
65+
it('parses scientific notation that results in large integers', () => {
66+
expect(PARAM_INTEGER_SINGLE.get('2.5e10')).toBe(25000000000)
67+
expect(PARAM_INTEGER_SINGLE.get('1.5e2')).toBe(150)
68+
})
69+
70+
it('throws for scientific notation that results in decimals', () => {
71+
expect(() => PARAM_INTEGER_SINGLE.get('1e-1')).toThrow()
72+
expect(() => PARAM_INTEGER_SINGLE.get('1e-2')).toThrow()
73+
})
74+
})
75+
76+
describe('set()', () => {
77+
it('converts integers to strings', () => {
78+
expect(PARAM_INTEGER_SINGLE.set(0)).toBe('0')
79+
expect(PARAM_INTEGER_SINGLE.set(1)).toBe('1')
80+
expect(PARAM_INTEGER_SINGLE.set(42)).toBe('42')
81+
expect(PARAM_INTEGER_SINGLE.set(-1)).toBe('-1')
82+
expect(PARAM_INTEGER_SINGLE.set(-999)).toBe('-999')
83+
expect(PARAM_INTEGER_SINGLE.set(2147483647)).toBe('2147483647')
84+
})
85+
})
86+
})
87+
88+
describe('PARAM_INTEGER_OPTIONAL', () => {
89+
describe('get()', () => {
90+
it('returns null for null input', () => {
91+
expect(PARAM_INTEGER_OPTIONAL.get(null)).toBe(null)
92+
})
93+
94+
it('parses valid integers', () => {
95+
expect(PARAM_INTEGER_OPTIONAL.get('0')).toBe(0)
96+
expect(PARAM_INTEGER_OPTIONAL.get('42')).toBe(42)
97+
expect(PARAM_INTEGER_OPTIONAL.get('-1')).toBe(-1)
98+
})
99+
100+
it('throws for invalid values', () => {
101+
expect(() => PARAM_INTEGER_OPTIONAL.get('invalid')).toThrow()
102+
expect(() => PARAM_INTEGER_OPTIONAL.get('1.5')).toThrow()
103+
expect(() => PARAM_INTEGER_OPTIONAL.get('')).toThrow()
104+
})
105+
})
106+
107+
describe('set()', () => {
108+
it('returns null for null input', () => {
109+
expect(PARAM_INTEGER_OPTIONAL.set(null)).toBe(null)
110+
})
111+
112+
it('converts integers to strings', () => {
113+
expect(PARAM_INTEGER_OPTIONAL.set(0)).toBe('0')
114+
expect(PARAM_INTEGER_OPTIONAL.set(42)).toBe('42')
115+
expect(PARAM_INTEGER_OPTIONAL.set(-1)).toBe('-1')
116+
})
117+
})
118+
})
119+
120+
describe('PARAM_INTEGER_REPEATABLE', () => {
121+
describe('get()', () => {
122+
it('parses array of integer values', () => {
123+
expect(
124+
PARAM_INTEGER_REPEATABLE.get(['0', '1', '42', '-1', '-999'])
125+
).toEqual([0, 1, 42, -1, -999])
126+
})
127+
128+
it('handles empty array', () => {
129+
expect(PARAM_INTEGER_REPEATABLE.get([])).toEqual([])
130+
})
131+
132+
it('throws for invalid values in array', () => {
133+
expect(() => PARAM_INTEGER_REPEATABLE.get(['42', 'invalid'])).toThrow()
134+
expect(() => PARAM_INTEGER_REPEATABLE.get(['1', '2.5'])).toThrow()
135+
expect(() => PARAM_INTEGER_REPEATABLE.get(['1', ''])).toThrow()
136+
})
137+
138+
it('throws if any element is null', () => {
139+
expect(() => PARAM_INTEGER_REPEATABLE.get(['1', null, '3'])).toThrow()
140+
})
141+
})
142+
143+
describe('set()', () => {
144+
it('converts array of integers to strings', () => {
145+
expect(PARAM_INTEGER_REPEATABLE.set([0, 1, 42, -1, -999])).toEqual([
146+
'0',
147+
'1',
148+
'42',
149+
'-1',
150+
'-999',
151+
])
152+
})
153+
154+
it('handles empty array', () => {
155+
expect(PARAM_INTEGER_REPEATABLE.set([])).toEqual([])
156+
})
157+
})
158+
})
159+
160+
describe('PARAM_INTEGER_REPEATABLE_OPTIONAL', () => {
161+
describe('get()', () => {
162+
it('returns null for null input', () => {
163+
expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.get(null)).toBe(null)
164+
})
165+
166+
it('parses array of integer values', () => {
167+
expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.get(['0', '42', '-1'])).toEqual([
168+
0, 42, -1,
169+
])
170+
})
171+
172+
it('handles empty array', () => {
173+
expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.get([])).toEqual([])
174+
})
175+
176+
it('throws for invalid values in array', () => {
177+
expect(() =>
178+
PARAM_INTEGER_REPEATABLE_OPTIONAL.get(['42', 'invalid'])
179+
).toThrow()
180+
})
181+
})
182+
183+
describe('set()', () => {
184+
it('returns null for null input', () => {
185+
expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.set(null)).toBe(null)
186+
})
187+
188+
it('converts array of integers to strings', () => {
189+
expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.set([0, 42, -1])).toEqual([
190+
'0',
191+
'42',
192+
'-1',
193+
])
194+
})
195+
196+
it('handles empty array', () => {
197+
expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.set([])).toEqual([])
198+
})
199+
})
200+
})
201+
202+
describe('PARAM_PARSER_INT', () => {
203+
describe('get()', () => {
204+
it('handles single integer values', () => {
205+
expect(PARAM_PARSER_INT.get('0')).toBe(0)
206+
expect(PARAM_PARSER_INT.get('42')).toBe(42)
207+
expect(PARAM_PARSER_INT.get('-1')).toBe(-1)
208+
})
209+
210+
it('handles null values', () => {
211+
expect(PARAM_PARSER_INT.get(null)).toBe(null)
212+
})
213+
214+
it('handles array values', () => {
215+
expect(PARAM_PARSER_INT.get(['0', '42', '-1'])).toEqual([0, 42, -1])
216+
expect(PARAM_PARSER_INT.get([])).toEqual([])
217+
})
218+
219+
it('throws for invalid single values', () => {
220+
expect(() => PARAM_PARSER_INT.get('invalid')).toThrow()
221+
expect(() => PARAM_PARSER_INT.get('1.5')).toThrow()
222+
})
223+
224+
it('throws for invalid array values', () => {
225+
expect(() => PARAM_PARSER_INT.get(['1', 'invalid'])).toThrow()
226+
expect(() => PARAM_PARSER_INT.get(['1', '2.5'])).toThrow()
227+
})
228+
})
229+
230+
describe('set()', () => {
231+
it('handles single integer values', () => {
232+
expect(PARAM_PARSER_INT.set(0)).toBe('0')
233+
expect(PARAM_PARSER_INT.set(42)).toBe('42')
234+
expect(PARAM_PARSER_INT.set(-1)).toBe('-1')
235+
})
236+
237+
it('handles null values', () => {
238+
expect(PARAM_PARSER_INT.set(null)).toBe(null)
239+
})
240+
241+
it('handles array values', () => {
242+
expect(PARAM_PARSER_INT.set([0, 42, -1])).toEqual(['0', '42', '-1'])
243+
expect(PARAM_PARSER_INT.set([])).toEqual([])
244+
})
245+
})
246+
})

0 commit comments

Comments
 (0)