express-middleware
Advanced tools
Comparing version
{ | ||
"name": "express-middleware", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Set of middlewares for Chauffeur-Privé", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -22,26 +22,2 @@ 'use strict'; | ||
/** | ||
* Find matching language by exact match. | ||
* | ||
* To match, the language must have the same country code and region. | ||
* | ||
* @param {Array} languages Array of known languages. | ||
* @param {Array} parsedLanguages Array of parsed languages from Accept-Language. | ||
* @returns {String|null} The first matching language if found, null otherwise. | ||
*/ | ||
function findExactLanguageMatch(languages, parsedLanguages) { | ||
let matchingLanguage; | ||
_.find(parsedLanguages, | ||
lang => { | ||
const languageString = languageObjectToString(lang); | ||
if (!lang.region) { | ||
matchingLanguage = findPartialLanguageMatch(languages, [lang]); | ||
} else if (_.includes(languages, languageString)) { | ||
matchingLanguage = languageString; | ||
} | ||
return matchingLanguage; | ||
}); | ||
return matchingLanguage || null; | ||
} | ||
/** | ||
* Find matching language by partial match. | ||
@@ -101,5 +77,3 @@ * | ||
const language = findExactLanguageMatch(languages, validParsedLanguages) | ||
|| findPartialLanguageMatch(languages, validParsedLanguages) | ||
|| languages[0]; | ||
const language = findPartialLanguageMatch(languages, validParsedLanguages) || languages[0]; | ||
@@ -106,0 +80,0 @@ req.language = language; |
@@ -106,2 +106,14 @@ 'use strict'; | ||
it('should match the first language found with a partial matching', function test(done) { | ||
const req = { get: () => 'en-FR;q=1.0, en-GB;q=0.9, fr-FR;q=0.8, vi-FR;q=0.7' }; | ||
const middleware = language({ languages }); | ||
expect(middleware).to.be.instanceof(Function); | ||
middleware(req, null, () => { | ||
expect(req.language).to.equal('en-US'); | ||
done(); | ||
}); | ||
}); | ||
it('should throw if languages is not set', function test() { | ||
@@ -108,0 +120,0 @@ expect(() => language({ languages: null })).to.throw(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47755
-67.93%28
-6.67%1072
-74.09%