Skip to content

Commit

Permalink
fix: tweak path language parser interface (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Sep 27, 2023
1 parent a15885a commit b13785b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
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

0 comments on commit b13785b

Please sign in to comment.