We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent efe272d commit 6f78e74Copy full SHA for 6f78e74
2 files changed
fluent-langneg/src/accepted_languages.ts
@@ -1,7 +1,20 @@
1
-export function acceptedLanguages(str: string = ""): Array<string> {
2
- if (typeof str !== "string") {
+const entryRegexp =
+ // locale ; q = qval
3
+ /(?:^|,)([^,;]+)(?:;\s*[qQ]\s*=([^,;]+))?/g;
4
+
5
+export function acceptedLanguages(acceptLanguageHeader = ""): string[] {
6
+ if (typeof acceptLanguageHeader !== "string") {
7
throw new TypeError("Argument must be a string");
8
}
- const tokens = str.split(",").map(t => t.trim());
- return tokens.filter(t => t !== "").map(t => t.split(";")[0]);
9
10
+ const langsWithQ: Array<{ lang: string; q: number; index: number }> = [];
11
+ for (const token of acceptLanguageHeader.matchAll(entryRegexp)) {
12
+ const q = token[2] ? parseFloat(token[2]) || 0 : 1;
13
+ langsWithQ.push({ lang: token[1].trim(), q, index: token.index });
14
+ }
15
16
+ // order by q descending, keeping the header order for equal weights
17
+ langsWithQ.sort((a, b) => (a.q === b.q ? a.index - b.index : b.q - a.q));
18
19
+ return langsWithQ.map(val => val.lang);
20
fluent-langneg/test/headers_test.js
@@ -18,6 +18,36 @@ suite("parse headers", () => {
);
});
21
+ test("with out of order quality values", () => {
22
+ assert.deepStrictEqual(
23
+ acceptedLanguages("en;q=0.8, fr;q=0.9, de;q=0.7, *;q=0.5, fr-CH"),
24
+ ["fr-CH", "fr", "en", "de", "*"]
25
+ );
26
+ });
27
28
+ test("with equal q values", () => {
29
30
+ acceptedLanguages("en;q=0.1, fr;q=0.1, de;q=0.1, *;q=0.1"),
31
+ ["en", "fr", "de", "*"]
32
33
34
35
+ test("with duff q values", () => {
36
37
+ acceptedLanguages(
38
+ "en;q=no, fr;z=0.9, de;q=0.7;q=9, *;q=0.5, fr-CH;q=a=0.1"
39
+ ),
40
+ ["fr", "de", "*", "en", "fr-CH"]
41
42
43
44
+ test("with empty entries", () => {
45
46
+ acceptedLanguages("en;q=0.8,,, fr;q=0.9,, de;q=0.7, *;q=0.5, fr-CH"),
47
48
49
50
51
test("edge cases", () => {
52
const args = [null, NaN, Infinity, [], {}];
53
0 commit comments