]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: rename numbers to integers
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 Aug 2025 06:10:04 +0000 (08:10 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 Aug 2025 06:10:04 +0000 (08:10 +0200)
packages/router/src/experimental/route-resolver/matchers/matcher-pattern.test-d.ts
packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts
packages/router/src/experimental/route-resolver/matchers/param-parsers/integers.spec.ts [new file with mode: 0644]
packages/router/src/experimental/route-resolver/matchers/param-parsers/integers.ts [moved from packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts with 100% similarity]

index 70173f05e8c7b8f2295096eefcc8643d4f0af531..30dd53fff43f3cff1e7c55349844a6f0b55cf34c 100644 (file)
@@ -1,6 +1,6 @@
 import { describe, expectTypeOf, it } from 'vitest'
 import { MatcherPatternPathDynamic } from './matcher-pattern'
-import { PARAM_INTEGER_SINGLE } from './param-parsers/numbers'
+import { PARAM_INTEGER_SINGLE } from './param-parsers/integers'
 import { PATH_PARAM_PARSER_DEFAULTS } from './param-parsers'
 import { PATH_PARAM_SINGLE_DEFAULT } from './param-parsers'
 
index bf621b28a5160aab5f2bd06519a6372c3089c4ca..a4d01437f44f0eda605035a9d7c9add82447caf8 100644 (file)
@@ -41,5 +41,5 @@ export const PATH_PARAM_PARSER_DEFAULTS = {
 
 export type { ParamParser }
 
-export { PARAM_PARSER_INT } from './numbers'
+export { PARAM_PARSER_INT } from './integers'
 export { PARAM_PARSER_BOOL } from './booleans'
diff --git a/packages/router/src/experimental/route-resolver/matchers/param-parsers/integers.spec.ts b/packages/router/src/experimental/route-resolver/matchers/param-parsers/integers.spec.ts
new file mode 100644 (file)
index 0000000..ca0727b
--- /dev/null
@@ -0,0 +1,246 @@
+import { describe, expect, it } from 'vitest'
+import {
+  PARAM_INTEGER_SINGLE,
+  PARAM_INTEGER_OPTIONAL,
+  PARAM_INTEGER_REPEATABLE,
+  PARAM_INTEGER_REPEATABLE_OPTIONAL,
+  PARAM_PARSER_INT,
+} from './integers'
+
+describe('PARAM_INTEGER_SINGLE', () => {
+  describe('get()', () => {
+    it('parses valid integers', () => {
+      expect(PARAM_INTEGER_SINGLE.get('0')).toBe(0)
+      expect(PARAM_INTEGER_SINGLE.get('1')).toBe(1)
+      expect(PARAM_INTEGER_SINGLE.get('42')).toBe(42)
+      expect(PARAM_INTEGER_SINGLE.get('-1')).toBe(-1)
+      expect(PARAM_INTEGER_SINGLE.get('-999')).toBe(-999)
+      expect(PARAM_INTEGER_SINGLE.get('2147483647')).toBe(2147483647)
+    })
+
+    it('throws for decimal numbers', () => {
+      expect(() => PARAM_INTEGER_SINGLE.get('1.5')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('3.14159')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('0.1')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('-2.5')).toThrow()
+    })
+
+    it('throws for non-numeric strings', () => {
+      expect(() => PARAM_INTEGER_SINGLE.get('abc')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('12abc')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('abc12')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('true')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('false')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('NaN')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('Infinity')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('-Infinity')).toThrow()
+    })
+
+    it('throws for empty strings', () => {
+      expect(() => PARAM_INTEGER_SINGLE.get('')).toThrow()
+    })
+
+    it('parses whitespace strings as zero', () => {
+      expect(PARAM_INTEGER_SINGLE.get(' ')).toBe(0)
+      expect(PARAM_INTEGER_SINGLE.get('  ')).toBe(0)
+      expect(PARAM_INTEGER_SINGLE.get('\n')).toBe(0)
+      expect(PARAM_INTEGER_SINGLE.get('\t')).toBe(0)
+    })
+
+    it('throws for null', () => {
+      expect(() => PARAM_INTEGER_SINGLE.get(null)).toThrow()
+    })
+
+    it('parses numbers with leading/trailing whitespace', () => {
+      expect(PARAM_INTEGER_SINGLE.get(' 42')).toBe(42)
+      expect(PARAM_INTEGER_SINGLE.get('42 ')).toBe(42)
+      expect(PARAM_INTEGER_SINGLE.get(' 42 ')).toBe(42)
+    })
+
+    it('parses valid scientific notation as integers', () => {
+      expect(PARAM_INTEGER_SINGLE.get('1e5')).toBe(100000)
+      expect(PARAM_INTEGER_SINGLE.get('1e2')).toBe(100)
+    })
+
+    it('parses scientific notation that results in large integers', () => {
+      expect(PARAM_INTEGER_SINGLE.get('2.5e10')).toBe(25000000000)
+      expect(PARAM_INTEGER_SINGLE.get('1.5e2')).toBe(150)
+    })
+
+    it('throws for scientific notation that results in decimals', () => {
+      expect(() => PARAM_INTEGER_SINGLE.get('1e-1')).toThrow()
+      expect(() => PARAM_INTEGER_SINGLE.get('1e-2')).toThrow()
+    })
+  })
+
+  describe('set()', () => {
+    it('converts integers to strings', () => {
+      expect(PARAM_INTEGER_SINGLE.set(0)).toBe('0')
+      expect(PARAM_INTEGER_SINGLE.set(1)).toBe('1')
+      expect(PARAM_INTEGER_SINGLE.set(42)).toBe('42')
+      expect(PARAM_INTEGER_SINGLE.set(-1)).toBe('-1')
+      expect(PARAM_INTEGER_SINGLE.set(-999)).toBe('-999')
+      expect(PARAM_INTEGER_SINGLE.set(2147483647)).toBe('2147483647')
+    })
+  })
+})
+
+describe('PARAM_INTEGER_OPTIONAL', () => {
+  describe('get()', () => {
+    it('returns null for null input', () => {
+      expect(PARAM_INTEGER_OPTIONAL.get(null)).toBe(null)
+    })
+
+    it('parses valid integers', () => {
+      expect(PARAM_INTEGER_OPTIONAL.get('0')).toBe(0)
+      expect(PARAM_INTEGER_OPTIONAL.get('42')).toBe(42)
+      expect(PARAM_INTEGER_OPTIONAL.get('-1')).toBe(-1)
+    })
+
+    it('throws for invalid values', () => {
+      expect(() => PARAM_INTEGER_OPTIONAL.get('invalid')).toThrow()
+      expect(() => PARAM_INTEGER_OPTIONAL.get('1.5')).toThrow()
+      expect(() => PARAM_INTEGER_OPTIONAL.get('')).toThrow()
+    })
+  })
+
+  describe('set()', () => {
+    it('returns null for null input', () => {
+      expect(PARAM_INTEGER_OPTIONAL.set(null)).toBe(null)
+    })
+
+    it('converts integers to strings', () => {
+      expect(PARAM_INTEGER_OPTIONAL.set(0)).toBe('0')
+      expect(PARAM_INTEGER_OPTIONAL.set(42)).toBe('42')
+      expect(PARAM_INTEGER_OPTIONAL.set(-1)).toBe('-1')
+    })
+  })
+})
+
+describe('PARAM_INTEGER_REPEATABLE', () => {
+  describe('get()', () => {
+    it('parses array of integer values', () => {
+      expect(
+        PARAM_INTEGER_REPEATABLE.get(['0', '1', '42', '-1', '-999'])
+      ).toEqual([0, 1, 42, -1, -999])
+    })
+
+    it('handles empty array', () => {
+      expect(PARAM_INTEGER_REPEATABLE.get([])).toEqual([])
+    })
+
+    it('throws for invalid values in array', () => {
+      expect(() => PARAM_INTEGER_REPEATABLE.get(['42', 'invalid'])).toThrow()
+      expect(() => PARAM_INTEGER_REPEATABLE.get(['1', '2.5'])).toThrow()
+      expect(() => PARAM_INTEGER_REPEATABLE.get(['1', ''])).toThrow()
+    })
+
+    it('throws if any element is null', () => {
+      expect(() => PARAM_INTEGER_REPEATABLE.get(['1', null, '3'])).toThrow()
+    })
+  })
+
+  describe('set()', () => {
+    it('converts array of integers to strings', () => {
+      expect(PARAM_INTEGER_REPEATABLE.set([0, 1, 42, -1, -999])).toEqual([
+        '0',
+        '1',
+        '42',
+        '-1',
+        '-999',
+      ])
+    })
+
+    it('handles empty array', () => {
+      expect(PARAM_INTEGER_REPEATABLE.set([])).toEqual([])
+    })
+  })
+})
+
+describe('PARAM_INTEGER_REPEATABLE_OPTIONAL', () => {
+  describe('get()', () => {
+    it('returns null for null input', () => {
+      expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.get(null)).toBe(null)
+    })
+
+    it('parses array of integer values', () => {
+      expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.get(['0', '42', '-1'])).toEqual([
+        0, 42, -1,
+      ])
+    })
+
+    it('handles empty array', () => {
+      expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.get([])).toEqual([])
+    })
+
+    it('throws for invalid values in array', () => {
+      expect(() =>
+        PARAM_INTEGER_REPEATABLE_OPTIONAL.get(['42', 'invalid'])
+      ).toThrow()
+    })
+  })
+
+  describe('set()', () => {
+    it('returns null for null input', () => {
+      expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.set(null)).toBe(null)
+    })
+
+    it('converts array of integers to strings', () => {
+      expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.set([0, 42, -1])).toEqual([
+        '0',
+        '42',
+        '-1',
+      ])
+    })
+
+    it('handles empty array', () => {
+      expect(PARAM_INTEGER_REPEATABLE_OPTIONAL.set([])).toEqual([])
+    })
+  })
+})
+
+describe('PARAM_PARSER_INT', () => {
+  describe('get()', () => {
+    it('handles single integer values', () => {
+      expect(PARAM_PARSER_INT.get('0')).toBe(0)
+      expect(PARAM_PARSER_INT.get('42')).toBe(42)
+      expect(PARAM_PARSER_INT.get('-1')).toBe(-1)
+    })
+
+    it('handles null values', () => {
+      expect(PARAM_PARSER_INT.get(null)).toBe(null)
+    })
+
+    it('handles array values', () => {
+      expect(PARAM_PARSER_INT.get(['0', '42', '-1'])).toEqual([0, 42, -1])
+      expect(PARAM_PARSER_INT.get([])).toEqual([])
+    })
+
+    it('throws for invalid single values', () => {
+      expect(() => PARAM_PARSER_INT.get('invalid')).toThrow()
+      expect(() => PARAM_PARSER_INT.get('1.5')).toThrow()
+    })
+
+    it('throws for invalid array values', () => {
+      expect(() => PARAM_PARSER_INT.get(['1', 'invalid'])).toThrow()
+      expect(() => PARAM_PARSER_INT.get(['1', '2.5'])).toThrow()
+    })
+  })
+
+  describe('set()', () => {
+    it('handles single integer values', () => {
+      expect(PARAM_PARSER_INT.set(0)).toBe('0')
+      expect(PARAM_PARSER_INT.set(42)).toBe('42')
+      expect(PARAM_PARSER_INT.set(-1)).toBe('-1')
+    })
+
+    it('handles null values', () => {
+      expect(PARAM_PARSER_INT.set(null)).toBe(null)
+    })
+
+    it('handles array values', () => {
+      expect(PARAM_PARSER_INT.set([0, 42, -1])).toEqual(['0', '42', '-1'])
+      expect(PARAM_PARSER_INT.set([])).toEqual([])
+    })
+  })
+})