Skip to content

Commit

Permalink
add private use extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Oct 10, 2023
1 parent 1444659 commit b88f011
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/locale.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
CheckRange,
ParseKeyword,
ParseLangSubtag,
ParsePuExtension,
ParseRegionSubtag,
ParseScriptSubtag,
ParseTransformedExtension,
Expand Down Expand Up @@ -394,3 +395,46 @@ test('ParseTransformedExtension', () => {
[never, 11]
>()
})

test('ParsePuExtension', () => {
/**
* Success cases
*/
expectTypeOf<
ParsePuExtension<['1234', 'abcde']>
>()
.toMatchTypeOf<
[
{
type: 'x'
value: '1234-abcde'
},
never,
]
>()

/**
* Fail cases
*/
// empty
expectTypeOf<
ParsePuExtension<['']>
>()
.toMatchTypeOf<
[
never,
12,
]
>()

// not alphabet or digit
expectTypeOf<
ParsePuExtension<['1あ']>
>()
.toMatchTypeOf<
[
never,
12,
]
>()
})
34 changes: 32 additions & 2 deletions src/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const localeErrors = /* @__PURE__ */ {
9: 'malformed unicode extension',
10: 'missing tvalue for tkey',
11: 'malformed transformed extension',
12: 'malformed private use extension',
1024: 'Unexpected error',
} as const

Expand Down Expand Up @@ -549,8 +550,37 @@ type ParseTransformedExtensionFieldsValue<
: Value
: Value

// TODO:
type ParsePuExtension<T extends string> = true
/**
* parse private use extensions
* https://unicode.org/reports/tr35/#pu_extensions
* = sep [xX] (sep alphanum{1,8})+ ;
*/
// deno-fmt-ignore
export type ParsePuExtension<
Chunks extends unknown[],
Exts extends unknown[] = _ParsePuExtension<
Chunks
>,
Result extends [PuExtension, number] = Length<Exts> extends 0
? [never, 12]
: [{ type: 'x'; value: Join<Exts, '-'> }, never],
> = Result

export type _ParsePuExtension<
Chunks extends unknown[],
Exts extends unknown[] = [],
RemainChunks extends unknown[] = Shift<Chunks>,
Chunk extends string = Chunks[0] extends string ? Chunks[0] : never,
ChunkChars extends unknown[] = StringToArray<Chunk>,
> = Length<Chunks> extends 0 ? Exts
: CheckRange<ChunkChars, [1, 2, 3, 4, 5, 6, 7, 8]> extends true // check `tfield` value length
? All<ValidCharacters<ChunkChars, AlphaNumber>, true> extends true // check `tfield` value characters
? _ParsePuExtension<
RemainChunks,
[...Push<Exts, Chunk>]
>
: Exts
: Exts

// TODO:
type ParseOtherExtension<T extends string> = true

0 comments on commit b88f011

Please sign in to comment.