]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: add string parser
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 29 Aug 2025 13:06:32 +0000 (15:06 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 29 Aug 2025 13:06:32 +0000 (15:06 +0200)
packages/router/src/experimental/route-resolver/matchers/param-parsers/index.ts
packages/router/src/experimental/route-resolver/matchers/param-parsers/strings.spec.ts [new file with mode: 0644]
packages/router/src/experimental/route-resolver/matchers/param-parsers/strings.ts [new file with mode: 0644]

index 0e457ceaac22c530e877ad3ec3eca6d7ab9924e9..c5888f68fadafc652cf6d335fb4cd33443df0569 100644 (file)
@@ -3,6 +3,7 @@ import type { ParamParser } from './types'
 
 export { PARAM_PARSER_BOOL } from './booleans'
 export { PARAM_PARSER_INT } from './integers'
+export { PARAM_PARSER_STRING } from './strings'
 
 export const PATH_PARAM_SINGLE_DEFAULT: ParamParser<string, string> = {}
 
diff --git a/packages/router/src/experimental/route-resolver/matchers/param-parsers/strings.spec.ts b/packages/router/src/experimental/route-resolver/matchers/param-parsers/strings.spec.ts
new file mode 100644 (file)
index 0000000..0ea83ef
--- /dev/null
@@ -0,0 +1,143 @@
+import { describe, expect, it } from 'vitest'
+import { PARAM_PARSER_STRING } from './strings'
+import { MatchMiss } from '../errors'
+
+describe('PARAM_PARSER_STRING', () => {
+  describe('get() - Single Values', () => {
+    it('returns string values as-is', () => {
+      expect(PARAM_PARSER_STRING.get('hello')).toBe('hello')
+      expect(PARAM_PARSER_STRING.get('world')).toBe('world')
+      expect(PARAM_PARSER_STRING.get('123')).toBe('123')
+      expect(PARAM_PARSER_STRING.get('true')).toBe('true')
+      expect(PARAM_PARSER_STRING.get('')).toBe('')
+    })
+
+    it('returns empty string for null values', () => {
+      expect(PARAM_PARSER_STRING.get(null)).toBe('')
+    })
+
+    it('returns empty string for undefined values', () => {
+      expect(PARAM_PARSER_STRING.get(undefined)).toBe('')
+    })
+  })
+
+  describe('get() - Array Values', () => {
+    it('returns arrays of strings as-is', () => {
+      expect(PARAM_PARSER_STRING.get(['hello', 'world'])).toEqual([
+        'hello',
+        'world',
+      ])
+      expect(PARAM_PARSER_STRING.get(['one', 'two', 'three'])).toEqual([
+        'one',
+        'two',
+        'three',
+      ])
+      expect(PARAM_PARSER_STRING.get(['123', 'true', 'false'])).toEqual([
+        '123',
+        'true',
+        'false',
+      ])
+    })
+
+    it('handles empty arrays', () => {
+      expect(PARAM_PARSER_STRING.get([])).toEqual([])
+    })
+
+    it('handles arrays with special characters', () => {
+      expect(PARAM_PARSER_STRING.get(['hello world', 'foo-bar'])).toEqual([
+        'hello world',
+        'foo-bar',
+      ])
+      expect(
+        PARAM_PARSER_STRING.get(['hello@world.com', '!@#$%^&*()'])
+      ).toEqual(['hello@world.com', '!@#$%^&*()'])
+    })
+
+    it('handles arrays with empty strings', () => {
+      expect(PARAM_PARSER_STRING.get(['', 'hello', ''])).toEqual([
+        '',
+        'hello',
+        '',
+      ])
+      expect(PARAM_PARSER_STRING.get([''])).toEqual([''])
+    })
+
+    it('filters out null values from arrays', () => {
+      expect(PARAM_PARSER_STRING.get(['hello', null, 'world'])).toEqual([
+        'hello',
+        'world',
+      ])
+      expect(PARAM_PARSER_STRING.get([null])).toEqual([])
+      expect(PARAM_PARSER_STRING.get(['foo', null, null, 'bar'])).toEqual([
+        'foo',
+        'bar',
+      ])
+      expect(PARAM_PARSER_STRING.get([null, null])).toEqual([])
+    })
+
+    it('handles mixed arrays with null values', () => {
+      expect(
+        PARAM_PARSER_STRING.get([null, 'hello', null, 'world', null])
+      ).toEqual(['hello', 'world'])
+      expect(PARAM_PARSER_STRING.get(['first', null, '', 'last'])).toEqual([
+        'first',
+        '',
+        'last',
+      ])
+    })
+  })
+
+  describe('set() - Single Values', () => {
+    it('converts values to strings', () => {
+      expect(PARAM_PARSER_STRING.set('hello')).toBe('hello')
+      expect(PARAM_PARSER_STRING.set('world')).toBe('world')
+      expect(PARAM_PARSER_STRING.set('')).toBe('')
+    })
+
+    it('returns an empty string for null values', () => {
+      expect(PARAM_PARSER_STRING.set(null)).toBe('')
+    })
+
+    // NOTE: undefined is not allowed as input to set() per the type definition
+    // it('returns an empty string for undefined values', () => {
+    //   expect(PARAM_PARSER_STRING.set(undefined)).toBe('')
+    // })
+  })
+
+  describe('set() - Array Values', () => {
+    it('converts arrays of strings to arrays of strings', () => {
+      expect(PARAM_PARSER_STRING.set(['hello', 'world'])).toEqual([
+        'hello',
+        'world',
+      ])
+      expect(PARAM_PARSER_STRING.set(['one', 'two', 'three'])).toEqual([
+        'one',
+        'two',
+        'three',
+      ])
+    })
+
+    it('handles empty arrays', () => {
+      expect(PARAM_PARSER_STRING.set([])).toEqual([])
+    })
+
+    it('handles arrays with empty strings', () => {
+      expect(PARAM_PARSER_STRING.set(['', 'hello', ''])).toEqual([
+        '',
+        'hello',
+        '',
+      ])
+      expect(PARAM_PARSER_STRING.set([''])).toEqual([''])
+    })
+
+    it('handles arrays with special characters', () => {
+      expect(PARAM_PARSER_STRING.set(['hello world', 'foo-bar'])).toEqual([
+        'hello world',
+        'foo-bar',
+      ])
+      expect(
+        PARAM_PARSER_STRING.set(['hello@world.com', '!@#$%^&*()'])
+      ).toEqual(['hello@world.com', '!@#$%^&*()'])
+    })
+  })
+})
diff --git a/packages/router/src/experimental/route-resolver/matchers/param-parsers/strings.ts b/packages/router/src/experimental/route-resolver/matchers/param-parsers/strings.ts
new file mode 100644 (file)
index 0000000..22fa9e0
--- /dev/null
@@ -0,0 +1,28 @@
+import { ParamParser } from './types'
+
+const PARAM_STRING_SINGLE = {
+  get: (value: string | null | undefined): string => value ?? '',
+  set: (value: string) => String(value),
+} satisfies ParamParser<string, string | null | undefined>
+
+const PARAM_STRING_REPEATABLE = {
+  get: (value: (string | null)[]) =>
+    value.filter((v): v is string => v != null),
+  set: (value: string[]) => value.map(String),
+} satisfies ParamParser<string[], (string | null)[]>
+
+/**
+ * Native Param parser for strings.
+ *
+ * @internal
+ */
+export const PARAM_PARSER_STRING = {
+  get: value =>
+    Array.isArray(value)
+      ? PARAM_STRING_REPEATABLE.get(value)
+      : PARAM_STRING_SINGLE.get(value),
+  set: value =>
+    Array.isArray(value)
+      ? PARAM_STRING_REPEATABLE.set(value)
+      : PARAM_STRING_SINGLE.set(value ?? ''),
+} satisfies ParamParser<string | string[] | null>