Skip to content
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

Add Headers parsing function #38

Merged
merged 3 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fluent-langneg/src/accepted_languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export default function acceptedLanguages(string = '') {
if (typeof string !== '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]);
}
1 change: 1 addition & 0 deletions fluent-langneg/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
*/

export { default as acceptedLanguages } from './accepted_languages';
import filterMatches from './matches';

function GetOption(options, property, type, values, fallback) {
Expand Down
45 changes: 45 additions & 0 deletions fluent-langneg/test/headers_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import assert from 'assert';
import acceptedLanguages from '../src/accepted_languages';

suite('parse headers', () => {
test('without quality values', () => {
assert.deepStrictEqual(
acceptedLanguages('en-US, fr, pl'), [
'en-US',
'fr',
'pl'
]
);
assert.deepStrictEqual(
acceptedLanguages('sr-Latn'), [
'sr-Latn'
]
);
});

test('with quality values', () => {
assert.deepStrictEqual(
acceptedLanguages('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5'), [
'fr-CH',
'fr',
'en',
'de',
'*'
]
);
});

test('edge cases', () => {
const args = [
null,
NaN,
Infinity,
[],
{}
];

args.forEach(arg => {
assert.throws(acceptedLanguages.bind(null, arg), TypeError);
});
});
});