Skip to content

Commit

Permalink
Merge pull request #26 from sheerun/loose
Browse files Browse the repository at this point in the history
Add loose mode, fixes #24
  • Loading branch information
matteofigus authored Mar 20, 2018
2 parents f3bdc3b + 8a29370 commit 409b4ca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Output will be:

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

#### parser.pick(supportedLangugagesArray, acceptLanguageHeader)
#### parser.pick(supportedLangugagesArray, acceptLanguageHeader, options = {})

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

Expand All @@ -61,7 +61,22 @@ Output will be:
"fr-CA"
```

__Running tests__
The `options` currently supports only `loose` option that allows partial matching on supported languages. For example:


```
parser.pick(['fr', 'en'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');
```

Would return:

```
"fr"
```

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']`.

### Running test
```
npm install
npm test
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ function parse(al){
});
}

function pick(supportedLanguages, acceptLanguage){
function pick(supportedLanguages, acceptLanguage, options){
options = options || {};

if (!supportedLanguages || !supportedLanguages.length || !acceptLanguage) {
return null;
}
Expand Down Expand Up @@ -58,8 +60,8 @@ function pick(supportedLanguages, acceptLanguage){
var supportedScript = supported[j].script ? supported[j].script.toLowerCase() : supported[j].script;
var supportedRegion = supported[j].region ? supported[j].region.toLowerCase() : supported[j].region;
if (langCode === supportedCode &&
(!langScript || langScript === supportedScript) &&
(!langRegion || langRegion === supportedRegion)) {
(options.loose || !langScript || langScript === supportedScript) &&
(options.loose || !langRegion || langRegion === supportedRegion)) {
return supportedLanguages[j];
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,19 @@ describe('accept-language#pick()', function(){
var result = parser.pick(['en']);
assert.equal(result, null);
});

it('by default should be strict when selecting language', function(){
var result = parser.pick(['en', 'pl'], 'en-US;q=0.6');
assert.equal(result, null);
});

it('can select language loosely with an option', function(){
var result = parser.pick(['en', 'pl'], 'en-US;q=0.6', { loose: true });
assert.equal(result, 'en');
});

it('selects most matching language in loose mode', function(){
var result = parser.pick(['en-US', 'en', 'pl'], 'en-US;q=0.6', { loose: true });
assert.equal(result, 'en-US');
});
});

0 comments on commit 409b4ca

Please sign in to comment.