-
Notifications
You must be signed in to change notification settings - Fork 79
Migrate @fluent/langneg to TypeScript #462
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| esm/ | ||
| /index.js | ||
| /compat.js |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| .nyc_output | ||
| coverage | ||
| docs | ||
| esm/.compiled | ||
| src | ||
| test | ||
| makefile | ||
| tsconfig.json |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export function acceptedLanguages(str: string = ""): Array<string> { | ||
| if (typeof str !== "string") { | ||
| throw new TypeError("Argument must be a string"); | ||
| } | ||
| const tokens = str.split(",").map(t => t.trim()); | ||
| return tokens.filter(t => t !== "").map(t => t.split(";")[0]); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,8 @@ | ||
| import filterMatches from "./matches"; | ||
| import {filterMatches} from "./matches"; | ||
|
|
||
| function GetOption(options, property, type, values, fallback) { | ||
| let value = options[property]; | ||
|
|
||
| if (value !== undefined) { | ||
| if (type === "boolean") { | ||
| value = new Boolean(value); | ||
| } else if (type === "string") { | ||
| value = String(value); | ||
| } | ||
|
|
||
| if (values !== undefined && values.indexOf(value) === -1) { | ||
| throw new Error("Invalid option value"); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| return fallback; | ||
| interface NegotiateLanguagesOptions { | ||
| strategy?: "filtering" | "matching" | "lookup"; | ||
| defaultLocale?: string; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -63,38 +48,32 @@ function GetOption(options, property, type, values, fallback) { | |
| * | ||
| * This strategy requires defaultLocale option to be set. | ||
| */ | ||
| export default function negotiateLanguages( | ||
| requestedLocales, | ||
| availableLocales, | ||
| options = {} | ||
| ) { | ||
|
|
||
| const defaultLocale = GetOption(options, "defaultLocale", "string"); | ||
| const strategy = GetOption(options, "strategy", "string", | ||
| ["filtering", "matching", "lookup"], "filtering"); | ||
|
|
||
| if (strategy === "lookup" && !defaultLocale) { | ||
| throw new Error("defaultLocale cannot be undefined for strategy `lookup`"); | ||
| } | ||
|
|
||
| const resolvedReqLoc = Array.from(Object(requestedLocales)).map(loc => { | ||
| return String(loc); | ||
| }); | ||
| const resolvedAvailLoc = Array.from(Object(availableLocales)).map(loc => { | ||
| return String(loc); | ||
| }); | ||
| export function negotiateLanguages( | ||
| requestedLocales: Array<string>, | ||
| availableLocales: Array<string>, | ||
| { | ||
| strategy = "filtering", | ||
| defaultLocale, | ||
| }: NegotiateLanguagesOptions = {} | ||
| ): Array<string> { | ||
|
|
||
| const supportedLocales = filterMatches( | ||
| resolvedReqLoc, | ||
| resolvedAvailLoc, strategy | ||
| Array.from(Object(requestedLocales)).map(String), | ||
| Array.from(Object(availableLocales)).map(String), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm tempted to remove the string and array normalization here as well, because again, I think we can make reasonable assumptions about the quality of the input. There are tests which fail if I do that, however. |
||
| strategy | ||
| ); | ||
|
|
||
| if (strategy === "lookup") { | ||
| if (defaultLocale === undefined) { | ||
| throw new Error( | ||
| "defaultLocale cannot be undefined for strategy `lookup`"); | ||
| } | ||
| if (supportedLocales.length === 0) { | ||
| supportedLocales.push(defaultLocale); | ||
| } | ||
| } else if (defaultLocale && !supportedLocales.includes(defaultLocale)) { | ||
| supportedLocales.push(defaultLocale); | ||
| } | ||
|
|
||
| return supportedLocales; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this function because I don't think the implementation of
@fluent/langnegneeds to be 100% defensive. For TypeScript consumers, TS already gives us some of those guaranties. For non-TS consumers, we don't usually verify all inputs in other@fluentpackages as thoroughly as this function did it.