Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tweak path language parser interface #14

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ describe('getPathLanguage', () => {
})

test('parser option', () => {
const nullLangParser = {
parse: () => 'null',
}
const nullLangParser = () => 'null'
expect(getPathLanguage('/en/foo', nullLangParser)).toBe('null')
})
})
Expand All @@ -35,9 +33,7 @@ describe('getPathLocale', () => {
})

test('RangeError', () => {
const nullLangParser = {
parse: () => 'null',
}
const nullLangParser = () => 'null'
expect(() => getPathLocale('/en/foo', nullLangParser)).toThrowError(
RangeError,
)
Expand Down
3 changes: 1 addition & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
})
})
20 changes: 9 additions & 11 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] || '' : ''
}
}

Expand Down