From: Eduardo San Martin Morote Date: Sun, 24 Aug 2025 06:10:04 +0000 (+0200) Subject: refactor: rename numbers to integers X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4a3fde4bba42097bfce2ded3ad52c7f75e1d9128;p=thirdparty%2Fvuejs%2Frouter.git refactor: rename numbers to integers --- diff --git a/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.test-d.ts b/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.test-d.ts index 70173f05..30dd53ff 100644 --- a/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.test-d.ts +++ b/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.test-d.ts @@ -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' diff --git a/packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts b/packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts index bf621b28..a4d01437 100644 --- a/packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts +++ b/packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts @@ -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 index 00000000..ca0727b3 --- /dev/null +++ b/packages/router/src/experimental/route-resolver/matchers/param-parsers/integers.spec.ts @@ -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([]) + }) + }) +}) diff --git a/packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts b/packages/router/src/experimental/route-resolver/matchers/param-parsers/integers.ts similarity index 100% rename from packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts rename to packages/router/src/experimental/route-resolver/matchers/param-parsers/integers.ts