Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 409b4ca

Browse files
authored
Merge pull request #26 from sheerun/loose
Add loose mode, fixes #24
2 parents f3bdc3b + 8a29370 commit 409b4ca

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Output will be:
4343

4444
Output is always sorted in quality order from highest -> lowest. as per the http spec, omitting the quality value implies 1.0.
4545

46-
#### parser.pick(supportedLangugagesArray, acceptLanguageHeader)
46+
#### parser.pick(supportedLangugagesArray, acceptLanguageHeader, options = {})
4747

4848
*Alias*: parser.pick(supportedLanguagesArray, parsedAcceptLanguageHeader)
4949

@@ -61,7 +61,22 @@ Output will be:
6161
"fr-CA"
6262
```
6363

64-
__Running tests__
64+
The `options` currently supports only `loose` option that allows partial matching on supported languages. For example:
65+
66+
67+
```
68+
parser.pick(['fr', 'en'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');
69+
```
70+
71+
Would return:
72+
73+
```
74+
"fr"
75+
```
76+
77+
In loose mode the order of `supportedLanguagesArray` matters, as it is the first partially matching language that is returned. It means that if you want to pick more specific langauge first, you should list it first as well, for example: `['fr-CA', 'fr']`.
78+
79+
### Running test
6580
```
6681
npm install
6782
npm test

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function parse(al){
2828
});
2929
}
3030

31-
function pick(supportedLanguages, acceptLanguage){
31+
function pick(supportedLanguages, acceptLanguage, options){
32+
options = options || {};
33+
3234
if (!supportedLanguages || !supportedLanguages.length || !acceptLanguage) {
3335
return null;
3436
}
@@ -58,8 +60,8 @@ function pick(supportedLanguages, acceptLanguage){
5860
var supportedScript = supported[j].script ? supported[j].script.toLowerCase() : supported[j].script;
5961
var supportedRegion = supported[j].region ? supported[j].region.toLowerCase() : supported[j].region;
6062
if (langCode === supportedCode &&
61-
(!langScript || langScript === supportedScript) &&
62-
(!langRegion || langRegion === supportedRegion)) {
63+
(options.loose || !langScript || langScript === supportedScript) &&
64+
(options.loose || !langRegion || langRegion === supportedRegion)) {
6365
return supportedLanguages[j];
6466
}
6567
}

tests/tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,19 @@ describe('accept-language#pick()', function(){
144144
var result = parser.pick(['en']);
145145
assert.equal(result, null);
146146
});
147+
148+
it('by default should be strict when selecting language', function(){
149+
var result = parser.pick(['en', 'pl'], 'en-US;q=0.6');
150+
assert.equal(result, null);
151+
});
152+
153+
it('can select language loosely with an option', function(){
154+
var result = parser.pick(['en', 'pl'], 'en-US;q=0.6', { loose: true });
155+
assert.equal(result, 'en');
156+
});
157+
158+
it('selects most matching language in loose mode', function(){
159+
var result = parser.pick(['en-US', 'en', 'pl'], 'en-US;q=0.6', { loose: true });
160+
assert.equal(result, 'en-US');
161+
});
147162
});

0 commit comments

Comments
 (0)