-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_19_2025.js
91 lines (74 loc) · 3.13 KB
/
3_19_2025.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const parseAcceptLanguage = (acceptLanguageHeader, supportedLanguageArray) => {
let acceptedLanguageHeadersArray = acceptLanguageHeader.split(', ');
let seenMappings = {};
let supportedLanguagesSet = new Set();
supportedLanguageArray.forEach(supportedLanguage => {
supportedLanguagesSet.add(supportedLanguage);
let languageTag = supportedLanguage.split('-')[0];
if (seenMappings[languageTag]) {
let currentMapping = seenMappings[languageTag]
seenMappings[languageTag] = [...currentMapping, supportedLanguage]
} else {
seenMappings[languageTag] = [supportedLanguage]
}
})
// parseAcceptLanguage("fr-FR, fr, *", ["en-US", "fr-CA", "fr-FR"]),
let intersectLanguages = [];
for (let acceptedLanguage of acceptedLanguageHeadersArray) {
if (acceptedLanguage === '*'){
supportedLanguageArray.forEach(supLang => {
if (!intersectLanguages.includes(supLang)) {
intersectLanguages.push(supLang)
}
})
break;
} else if (!acceptedLanguage.includes('-') && seenMappings[acceptedLanguage]) {
let matchingLanguages = seenMappings[acceptedLanguage];
matchingLanguages.filter(matchLang => !intersectLanguages.includes(matchLang))
intersectLanguages = [...intersectLanguages, ...matchingLanguages];
} else {
if (supportedLanguagesSet.has(acceptedLanguage)) {
intersectLanguages.push(acceptedLanguage);
}
}
}
return intersectLanguages
}
console.log('PART3 testcases')
console.log(
parseAcceptLanguage("en, *", ["en-US", "fr-CA", "fr-FR"]),
'returns: "en-US", "fr-CA", "fr-FR"'
)
console.log(
parseAcceptLanguage("en-US, *", ["en-US", "fr-CA", "fr-FR"]),
'returns: ["en-US", "fr-CA", "fr-FR"]'
)
console.log(
parseAcceptLanguage("fr-FR, fr, *", ["en-US", "fr-CA", "fr-FR"]),
'returns: ["fr-FR", "fr-CA", "en-US"]'
)
console.log('PART2 testcases')
console.log(
parseAcceptLanguage("fr-CA, en", ["en-US", "fr-CA", "fr-FR"]),
'***returns: ["fr-CA","en-US"]'
)
console.log(parseAcceptLanguage("en", ["en-US", "fr-CA", "fr-FR"]), 'expect: ["en-US"]')
console.log(parseAcceptLanguage("fr", ["en-US", "fr-CA", "fr-FR"]),
'returns: ["fr-CA", "fr-FR"]')
console.log(
parseAcceptLanguage("en, fr-CA", ["en-US", "fr-CA", "fr-FR"]),
'returns: ["en-US","fr-CA"]'
)
console.log('PART1 testcases')
console.log(parseAcceptLanguage(
"en-US, fr-CA, fr-FR", // the client's Accept-Language header, a string
["fr-FR", "en-US"] // the server's supported languages, a set of strings
), ' expect: ["en-US", "fr-FR"]')
console.log(parseAcceptLanguage(
"fr-CA, fr-FR, en-US", // the client's Accept-Language header, a string
["fr-FR", "en-US"] // the server's supported languages, a set of strings
), ' expect: [""fr-FR", en-US"]')
console.log(parseAcceptLanguage("fr-CA, fr-FR", ["en-US", "fr-FR"]), 'expect: ["fr-FR"]')
console.log(parseAcceptLanguage("en-US", ["en-US", "fr-CA"]), 'expect: en-US')
// en-US
// console.log("en-US, fr-CA, fr-FR".split(', ').sort())