From b13785b1c52719bb5f55d1a7ac9cdcdcff522ff2 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Wed, 27 Sep 2023 19:29:19 +0900 Subject: [PATCH] fix: tweak path language parser interface (#14) --- src/http.test.ts | 8 ++------ src/http.ts | 3 +-- src/shared.test.ts | 6 +++--- src/shared.ts | 20 +++++++++----------- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/http.test.ts b/src/http.test.ts index aa7b0e1..75807f9 100644 --- a/src/http.test.ts +++ b/src/http.test.ts @@ -17,9 +17,7 @@ describe('getPathLanguage', () => { }) test('parser option', () => { - const nullLangParser = { - parse: () => 'null', - } + const nullLangParser = () => 'null' expect(getPathLanguage('/en/foo', nullLangParser)).toBe('null') }) }) @@ -35,9 +33,7 @@ describe('getPathLocale', () => { }) test('RangeError', () => { - const nullLangParser = { - parse: () => 'null', - } + const nullLangParser = () => 'null' expect(() => getPathLocale('/en/foo', nullLangParser)).toThrowError( RangeError, ) diff --git a/src/http.ts b/src/http.ts index ee70154..088c10e 100644 --- a/src/http.ts +++ b/src/http.ts @@ -166,8 +166,7 @@ export function getPathLanguage( path: string | URL, parser?: PathLanguageParser, ): string { - const _parser = parser || pathLanguageParser - return _parser.parse(path) + return (parser || pathLanguageParser)(path) } /** diff --git a/src/shared.test.ts b/src/shared.test.ts index 00cb349..647061d 100644 --- a/src/shared.test.ts +++ b/src/shared.test.ts @@ -79,15 +79,15 @@ describe('normalizeLanguageName', () => { describe('PathIndexLanguageParser', () => { test('default index: 0', () => { - expect(pathLanguageParser.parse('/en/hello')).toEqual('en') + expect(pathLanguageParser('/en/hello')).toEqual('en') }) test('index 1', () => { const parser = createPathIndexLanguageParser(1) - expect(parser.parse('/hello/ja/bar')).toEqual('ja') + expect(parser('/hello/ja/bar')).toEqual('ja') }) test('empty', () => { - expect(pathLanguageParser.parse('/')).toEqual('') + expect(pathLanguageParser('/')).toEqual('') }) }) diff --git a/src/shared.ts b/src/shared.ts index 334a1a1..ad31219 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -83,22 +83,20 @@ export interface PathLanguageParser { * * @returns {string} the language, if it cannot parse the path is not found, you need to return empty string (`''`) */ - parse(path: string | URL): string + (path: string | URL): string } export function createPathIndexLanguageParser( index = 0, ): PathLanguageParser { - return { - parse(path: string | URL): string { - const rawPath = typeof path === 'string' ? path : path.pathname - const normalizedPath = rawPath.split('?')[0] - const parts = normalizedPath.split('/') - if (parts[0] === '') { - parts.shift() - } - return parts.length > index ? parts[index] || '' : '' - }, + return (path: string | URL): string => { + const rawPath = typeof path === 'string' ? path : path.pathname + const normalizedPath = rawPath.split('?')[0] + const parts = normalizedPath.split('/') + if (parts[0] === '') { + parts.shift() + } + return parts.length > index ? parts[index] || '' : '' } }