accept-language
Advanced tools
Comparing version 1.0.2 to 1.0.3
47
index.js
@@ -7,3 +7,4 @@ | ||
var acceptLanguageSyntax = /((([a-zA-Z]+(-[a-zA-Z]+)?)|\*)(;q=[0-1](\.[0-9]+)?)?)*/g | ||
, localeSyntax = /^[a-z]{2}$/ | ||
, isCode = /^[a-z]{2}$/ | ||
, isRegion = /^[a-z]{2}$/i | ||
, exports = module.exports; | ||
@@ -18,4 +19,11 @@ | ||
/** | ||
* Default code | ||
*/ | ||
var defaultLanguage = exports.defaultLanguage = null; | ||
/** | ||
* Prune locales that aren't defined | ||
* | ||
* @param {Array.<locale>} locales | ||
* @return {Array.<locale>} | ||
@@ -37,2 +45,8 @@ * @api public | ||
// If no locales matches the defined set. Return | ||
// the default locale if it is set | ||
if(locales.length === 0 && defaultLanguage) { | ||
return defaultLanguage; | ||
} | ||
return locales; | ||
@@ -44,2 +58,3 @@ }; | ||
* | ||
* @param {Array.<locale>} locales | ||
* @return {void} | ||
@@ -54,3 +69,3 @@ * @api public | ||
} | ||
if(!localeSyntax.test(locale)) { | ||
if(!isCode.test(locale)) { | ||
throw new TypeError('First parameter must be an array consisting of languague codes. Wrong syntax if locale: ' + locale); | ||
@@ -64,2 +79,28 @@ } | ||
/** | ||
* Default locale if no-match occurs | ||
* | ||
* @param {String} language | ||
* @returns {void} | ||
* @throws {TypeError} | ||
* @api public | ||
*/ | ||
exports.default = function(language) { | ||
if(typeof language !== 'object') { | ||
throw new TypeError('First parameter must be an object'); | ||
} | ||
if(typeof language.code !== 'string') { | ||
throw new TypeError('Property code must be a string and can\'t be undefined'); | ||
} | ||
if(!isCode.test(language.code)) { | ||
throw new TypeError('Property code must consist of two lowercase letters [a-z]'); | ||
} | ||
if(typeof language.region === 'string' && !isRegion.test(language.region)) { | ||
throw new TypeError('Property region must consist of two case-insensitive letters [a-zA-Z]'); | ||
} | ||
defaultLanguage = language; | ||
}; | ||
/** | ||
* Define locales | ||
@@ -71,3 +112,3 @@ * | ||
exports.parse = function(acceptLanguage){ | ||
exports.parse = function(acceptLanguage) { | ||
var strings = (acceptLanguage || '').match(acceptLanguageSyntax); | ||
@@ -74,0 +115,0 @@ var locales = strings.map(function(match) { |
{ | ||
"name": "accept-language", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Parse the HTTP Accept-Language header", | ||
@@ -28,4 +28,2 @@ "main": "index.js", | ||
"grunt": "^0.4.4", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-contrib-jshint": "^0.9.2", | ||
"mocha": "~1.18.2", | ||
@@ -32,0 +30,0 @@ "chai": "~1.9.1" |
@@ -1,19 +0,18 @@ | ||
accept-language | ||
====================== | ||
accept-language [![Build Status](https://travis-ci.org/tinganho/node-accept-language.png)](https://travis-ci.org/tinganho/node-accept-language) | ||
======================== | ||
Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality. | ||
[![NPM](https://nodei.co/npm/accept-language.png?downloads=true&stars=true)](https://nodei.co/npm/accept-language/) | ||
dependencies: none | ||
`accept-language` is an Node package that parses HTTP Accept-Language header and returns a consumable array of language codes. | ||
installation: | ||
### Installation: | ||
``` | ||
npm install accept-language | ||
npm install accept-language --save | ||
``` | ||
usage: | ||
### Usage: | ||
``` | ||
var acceptLanguage = require('accept-language'); | ||
acceptLanguage.codes(['en', 'zh']); | ||
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv'); | ||
@@ -24,3 +23,3 @@ | ||
Output will be: | ||
Output: | ||
@@ -47,3 +46,3 @@ ``` | ||
Pruning non-defined langnague codes: | ||
Filter non-defined language codes: | ||
@@ -53,3 +52,3 @@ ``` | ||
acceptLanguage.codes(['en', 'zh']); | ||
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv').prune(); | ||
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv'); | ||
@@ -59,3 +58,3 @@ console.log(language); | ||
Output will be: | ||
Output: | ||
``` | ||
@@ -77,2 +76,2 @@ [ | ||
Output is always sorted in quality order from highest -> lowest. as per the http spec, omitting the quality value implies 1.0. | ||
The output is always sorted with the highest quality first. |
/** | ||
@@ -21,6 +19,5 @@ * Modules | ||
describe('accept-language', function() { | ||
describe('#parse', function() { | ||
it('should be able to parse a single langague', function() { | ||
it('should be able to parse a single language', function() { | ||
expect(acceptLangague.parse('en')).to.eql([{ | ||
@@ -33,3 +30,3 @@ code : 'en', | ||
it('should be able to parse multiple langagues without regions', function() { | ||
it('should be able to parse multiple languages without regions', function() { | ||
expect(acceptLangague.parse('en,zh')).to.eql([{ | ||
@@ -174,2 +171,80 @@ code : 'en', | ||
}); | ||
describe('#default', function() { | ||
it('should throw an error if the first parameeter is not an object', function() { | ||
var sample = function() { | ||
acceptLangague.default(1); | ||
}; | ||
expect(sample).to.throw(TypeError, 'First parameter must be an object'); | ||
}); | ||
it('should throw an error if language object don\'t contain a code property', function() { | ||
var language = { | ||
region : 'US', | ||
quality : 1 | ||
}; | ||
var sample = function() { | ||
acceptLangague.default(language); | ||
}; | ||
expect(sample).to.throw(TypeError, 'Property code must be a string and can\'t be undefined'); | ||
}); | ||
it('should throw an error if language object contain a code property but with wrong type', function() { | ||
var language = { | ||
code : 1, | ||
region : 'US', | ||
quality : 1 | ||
}; | ||
var sample = function() { | ||
acceptLangague.default(language); | ||
}; | ||
expect(sample).to.throw(TypeError, 'Property code must be a string and can\'t be undefined'); | ||
}); | ||
it('should throw an error if language object contain a code property but with wrong syntax', function() { | ||
var language = { | ||
code : 'US', | ||
region : 'US', | ||
quality : 1 | ||
}; | ||
var sample = function() { | ||
acceptLangague.default(language); | ||
}; | ||
expect(sample).to.throw(TypeError, 'Property code must consist of two lowercase letters [a-z]'); | ||
}); | ||
it('should throw an error if language object contain a region property with wrong syntax', function() { | ||
var language = { | ||
code : 'en', | ||
region : 'USS', | ||
quality : 1 | ||
}; | ||
var sample = function() { | ||
acceptLangague.default(language); | ||
}; | ||
expect(sample).to.throw(TypeError, 'Property region must consist of two case-insensitive letters [a-zA-Z]'); | ||
}); | ||
it('should return the default language if there is no match', function() { | ||
var language = { | ||
code : 'en', | ||
region : 'US', | ||
quality : 1 | ||
}; | ||
acceptLangague.default(language); | ||
acceptLangague.codes(['en', 'zh']); | ||
expect(acceptLangague.parse('fr-CA')).to.eql(language); | ||
}); | ||
it('shouldn\'t return the default language if there is a match', function() { | ||
var language = { | ||
code : 'en', | ||
region : 'US', | ||
quality : 1 | ||
}; | ||
acceptLangague.default(language); | ||
acceptLangague.codes(['en', 'zh']); | ||
expect(acceptLangague.parse('zh-CN')).to.not.eql(language); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
13625
5
357
0
73