-
Notifications
You must be signed in to change notification settings - Fork 80
make acceptedLanguages order the header by q values descending #262
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
Open
blushingpenguin
wants to merge
9
commits into
projectfluent:main
Choose a base branch
from
blushingpenguin:acceptlanguage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9458970
make acceptedLanguages order the header by q values
blushingpenguin 9c70026
addressing review comments
blushingpenguin 8261356
Merge branch 'master' into acceptlanguage
blushingpenguin da95789
fix lint warning
blushingpenguin 4495e16
Merge branch 'master' into acceptlanguage
blushingpenguin c60aacb
Merge branch 'master' into acceptlanguage
blushingpenguin 6e78127
Merge branch 'master' into acceptlanguage
blushingpenguin adbc32d
Merge branch 'master' into acceptlanguage
blushingpenguin fc62da6
use built in methods that preserve array index rather than adding it …
blushingpenguin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
export default function acceptedLanguages(string = "") { | ||
if (typeof string !== "string") { | ||
function parseAcceptLanguageEntry(entry, index) { | ||
const langWithQ = entry.split(";").map(u => u.trim()); | ||
let q = 1.0; | ||
if (langWithQ.length > 1) { | ||
const qVal = langWithQ[1].split("=").map(u => u.trim()); | ||
if (qVal.length === 2 && qVal[0].toLowerCase() === "q") { | ||
const qn = Number(qVal[1]); | ||
q = isNaN(qn) ? 0.0 : qn; | ||
} | ||
} | ||
return { index: index, lang: langWithQ[0], q }; | ||
} | ||
|
||
export default function acceptedLanguages(acceptLanguageHeader = "") { | ||
if (typeof acceptLanguageHeader !== "string") { | ||
throw new TypeError("Argument must be a string"); | ||
} | ||
const tokens = string.split(",").map(t => t.trim()); | ||
return tokens.filter(t => t !== "").map(t => t.split(";")[0]); | ||
const tokens = acceptLanguageHeader.split(",").map(t => t.trim()) | ||
.filter(t => t !== ""); | ||
const langsWithQ = []; | ||
tokens.forEach((t, index) => | ||
langsWithQ.push(parseAcceptLanguageEntry(t, index))); | ||
// order by q descending, keeping the header order for equal weights | ||
langsWithQ.sort((a, b) => a.q === b.q ? a.index - b.index : b.q - a.q); | ||
return langsWithQ.map(t => t.lang); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Non-blocking cleanup suggestion:
I hand-written it so it may not launch but I hope the intention is expressed in my snippet. I'm trying to save us from reallocating the array in the loop. Alternatively, you could also likely do
const langsWithQ = Array(tokens.length);
which I guess is simpler.The other thing I wanted to improve in my snippet is to not carry around the index, since the
langsWithQ
preserves the order oftokens
, so there's no value in storing it on thelangWithQ
struct.@blushingpenguin - do you like the proposed changes or would you prefer to land as-is?
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.
Nicer your way I think -- I wasn't aware of Array.entries. I have updated the code and retested it.