accept-language-parser
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -30,2 +30,2 @@ module.exports = function(grunt) { | ||
grunt.registerTask('default', ['test']); | ||
}; | ||
}; |
16
index.js
@@ -1,2 +0,2 @@ | ||
var regex = /((([a-zA-Z]+(-[a-zA-Z]+)?)|\*)(;q=[0-1](\.[0-9]+)?)?)*/g; | ||
var regex = /((([a-zA-Z]+(-[a-zA-Z]+){0,2})|\*)(;q=[0-1](\.[0-9]+)?)?)*/g; | ||
@@ -12,6 +12,8 @@ module.exports.parse = function(al){ | ||
var ietf = bits[0].split('-'); | ||
var hasScript = ietf.length === 3; | ||
return { | ||
code: ietf[0], | ||
region: ietf[1], | ||
script: hasScript ? ietf[1] : null, | ||
region: hasScript ? ietf[2] : ietf[1], | ||
quality: bits[1] ? parseFloat(bits[1].split('=')[1]) : 1.0 | ||
@@ -35,6 +37,8 @@ }; | ||
var bits = support.split('-'); | ||
var hasScript = bits.length === 3; | ||
return { | ||
code: bits[0], | ||
region: bits[1] | ||
script: hasScript ? bits[1] : null, | ||
region: hasScript ? bits[2] : bits[1] | ||
}; | ||
@@ -47,6 +51,10 @@ }); | ||
var langRegion = lang.region ? lang.region.toLowerCase() : lang.region; | ||
var langScript = lang.script ? lang.script.toLowerCase() : lang.script; | ||
for (var j = 0; j < supported.length; j++) { | ||
var supportedCode = supported[j].code.toLowerCase(); | ||
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 && (!langRegion || langRegion === supportedRegion)) { | ||
if (langCode === supportedCode && | ||
(!langScript || langScript === supportedScript) && | ||
(!langRegion || langRegion === supportedRegion)) { | ||
return supportedLanguages[j]; | ||
@@ -53,0 +61,0 @@ } |
{ | ||
"name": "accept-language-parser", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Parse the accept-language header from a HTTP request", | ||
@@ -27,6 +27,6 @@ "main": "index.js", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-contrib-jshint": "^1.0.0", | ||
"grunt-mocha-test": "^0.10.0", | ||
"should": "^9.0.0", | ||
"grunt-contrib-jshint": "^0.9.2" | ||
"should": "^10.0.0" | ||
} | ||
} |
@@ -61,5 +61,2 @@ accept-language-parser | ||
__Known issues__ | ||
- Cannot cope with multi-part region codes, i.e. 'az-AZ-Cyrl' will be treated as 'az-AZ' | ||
__Running tests__ | ||
@@ -66,0 +63,0 @@ ``` |
@@ -7,2 +7,7 @@ var parser = require("../index.js"); | ||
actual.code.should.eql(expected.code); | ||
if(actual.script || expected.script){ | ||
actual.script.should.eql(expected.script) | ||
} | ||
if(actual.region || expected.region){ | ||
@@ -74,2 +79,14 @@ actual.region.should.eql(expected.region) | ||
}); | ||
it('should correctly identify script', function(){ | ||
var result = parser.parse('zh-Hant-cn'); | ||
assertResult({ code: 'zh', script: 'Hant', region: 'cn', quality: 1.0}, result[0]); | ||
}); | ||
it('should cope with script and a quality value', function(){ | ||
var result = parser.parse('zh-Hant-cn;q=1, zh-cn;q=0.6, zh;q=0.4'); | ||
assertResult({ code: 'zh', script: 'Hant', region: 'cn', quality: 1.0}, result[0]); | ||
assertResult({ code: 'zh', region: 'cn', quality: 0.6}, result[1]); | ||
assertResult({ code: 'zh', quality: 0.4}, result[2]); | ||
}); | ||
}); | ||
@@ -83,2 +100,7 @@ | ||
it('should pick a specific script (if specified)', function(){ | ||
var result = parser.pick(['zh-Hant-cn', 'zh-cn'], 'zh-Hant-cn,zh-cn;q=0.6,zh;q=0.4'); | ||
assert.equal(result, 'zh-Hant-cn'); | ||
}); | ||
it('should pick proper language regardless of casing', function(){ | ||
@@ -85,0 +107,0 @@ var result = parser.pick(['eN-Us', 'Fr-cA'], 'fR-Ca,fr;q=0.2,en-US;q=0.6,en;q=0.4,*;q=0.5'); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12678
196
0
66