|
1 | 1 | import { filterMatches } from "@fluent/langneg";
|
2 | 2 |
|
3 |
| -export type LocaleNegotiator<Locale> = ( |
| 3 | +const typedFilterMatches = filterMatches as < |
| 4 | + Requested extends string, |
| 5 | + Available extends string, |
| 6 | +>( |
| 7 | + requested: readonly Requested[], |
| 8 | + available: readonly Available[], |
| 9 | + strategy: "filtering" | "lookup" | "matching", |
| 10 | +) => Available[]; |
| 11 | + |
| 12 | +export type LocaleNegotiator<TypeParamLocale extends string = string> = < |
| 13 | + Locale extends TypeParamLocale = TypeParamLocale, |
| 14 | +>( |
4 | 15 | availableLocales: readonly Locale[],
|
5 | 16 | ) => Locale | undefined;
|
6 | 17 |
|
7 |
| -export type LocaleNegotiators<Locale> = |
| 18 | +export type LocaleNegotiators<Locale extends string> = |
8 | 19 | | readonly [...(LocaleNegotiator<Locale> | false)[], Locale]
|
9 | 20 | | readonly [];
|
10 | 21 |
|
11 |
| -type Algorithm = ( |
| 22 | +type Algorithm = <Available extends string = string>( |
12 | 23 | requestedLocales: readonly string[],
|
13 |
| - availableLocales: readonly string[], |
14 |
| -) => string[]; |
| 24 | + availableLocales: readonly Available[], |
| 25 | +) => Available[]; |
15 | 26 |
|
16 | 27 | export const lookup: Algorithm = (requestedLocales, availableLocales) =>
|
17 |
| - filterMatches( |
18 |
| - // @info `filterMatches` expects mutable arrays |
19 |
| - Array.from(requestedLocales), |
20 |
| - Array.from(availableLocales), |
21 |
| - "lookup", |
22 |
| - ); |
| 28 | + typedFilterMatches(requestedLocales, availableLocales, "lookup"); |
23 | 29 |
|
24 | 30 | export const negotiator: (
|
25 | 31 | requestedLocales: readonly string[],
|
26 | 32 | algorithm: Algorithm,
|
27 |
| -) => LocaleNegotiator<string> = |
28 |
| - (requestedLocales, algorithm) => (availableLocales) => |
29 |
| - algorithm(requestedLocales, availableLocales)[0]; |
| 33 | +) => LocaleNegotiator = (requestedLocales, algorithm) => (availableLocales) => |
| 34 | + algorithm(requestedLocales, availableLocales)[0]; |
30 | 35 |
|
31 | 36 | export const browser = () =>
|
32 | 37 | negotiator(globalThis.navigator?.languages ?? [], lookup);
|
0 commit comments