accept-language
Advanced tools
Comparing version 1.0.0 to 1.0.2
47
index.js
@@ -6,10 +6,13 @@ | ||
var codes = [] | ||
, regions = [] | ||
, acceptLanguagueSyntax = /((([a-zA-Z]+(-[a-zA-Z]+)?)|\*)(;q=[0-1](\.[0-9]+)?)?)*/g | ||
var acceptLanguageSyntax = /((([a-zA-Z]+(-[a-zA-Z]+)?)|\*)(;q=[0-1](\.[0-9]+)?)?)*/g | ||
, localeSyntax = /^[a-z]{2}$/ | ||
, regionSyntax = /^[a-z]{2}$/i | ||
, exports = module.exports; | ||
/** | ||
* Languague codes | ||
*/ | ||
var codes = exports.codes = []; | ||
/** | ||
* Prune locales that aren't defined | ||
@@ -21,13 +24,15 @@ * | ||
function prune() { | ||
this.filter(function(locale) { | ||
var filter = false; | ||
if(codes.indexOf(locale.code) === -1) { | ||
return false; | ||
} | ||
function prune(locales) { | ||
if(codes.length > 0) { | ||
locales = locales.filter(function(locale) { | ||
var filter = false; | ||
if(codes.indexOf(locale.code) === -1) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
return true; | ||
}); | ||
} | ||
return this; | ||
return locales; | ||
}; | ||
@@ -42,12 +47,12 @@ | ||
exports.define = function(locales) { | ||
exports.codes = function(locales) { | ||
locales.forEach(function(locale) { | ||
if(typeof locale.code !== 'string') { | ||
throw new TypeError('Locale `code` should be a string'); | ||
if(typeof locale !== 'string') { | ||
throw new TypeError('First parameter must be an array of strings'); | ||
} | ||
if(localeSyntax.test(locale.code)) { | ||
throw new TypeError('Locale `code` is wrong in ' + JSON.stringify(locale)); | ||
if(!localeSyntax.test(locale)) { | ||
throw new TypeError('First parameter must be an array consisting of languague codes. Wrong syntax if locale: ' + locale); | ||
} | ||
// Store code | ||
codes.push(locale.code); | ||
codes.push(locale); | ||
}); | ||
@@ -84,6 +89,4 @@ }; | ||
locale.prune = prune; | ||
return locales; | ||
return prune(locales); | ||
}; | ||
{ | ||
"name": "accept-language", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"description": "Parse the HTTP Accept-Language header", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt test" | ||
"test": "./bin/test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/andyroyle/accept-language-parser.git" | ||
"url": "git://github.com/tinganho/accept-language.git" | ||
}, | ||
@@ -18,15 +18,17 @@ "keywords": [ | ||
], | ||
"author": "Andy Royle <ajroyle@gmail.com>", | ||
"author": "Tingan Ho <tingan87@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/andyroyle/accept-language-parser/issues" | ||
"url": "https://github.com/tinganho/accept-language/issues" | ||
}, | ||
"homepage": "https://github.com/andyroyle/accept-language-parser", | ||
"homepage": "https://github.com/tinganho/accept-language", | ||
"devDependencies": { | ||
"sinon": "~1.9.1", | ||
"sinon-chai": "~2.5.0", | ||
"grunt": "^0.4.4", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-mocha-test": "^0.10.0", | ||
"should": "^3.2.0", | ||
"grunt-contrib-jshint": "^0.9.2" | ||
"grunt-contrib-jshint": "^0.9.2", | ||
"mocha": "~1.18.2", | ||
"chai": "~1.9.1" | ||
} | ||
} |
@@ -1,6 +0,4 @@ | ||
accept-language-parser | ||
accept-language | ||
====================== | ||
[![Build Status](https://travis-ci.org/andyroyle/accept-language-parser.png?branch=master)](https://travis-ci.org/andyroyle/accept-language-parser) [![NPM version](https://badge.fury.io/js/accept-language-parser.png)](http://badge.fury.io/js/accept-language-parser) ![Dependencies](https://david-dm.org/andyroyle/accept-language-parser.png) | ||
Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality. | ||
@@ -13,3 +11,3 @@ | ||
``` | ||
npm install accept-language-parser | ||
npm install accept-language | ||
``` | ||
@@ -20,6 +18,6 @@ | ||
``` | ||
var parser = require('accept-language-parser'); | ||
var acceptLanguage = require('accept-language'); | ||
acceptLanguage.codes(['en', 'zh']); | ||
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv'); | ||
var language = parser.parse('en-GB,en;q=0.8'); | ||
console.log(language); | ||
@@ -38,2 +36,7 @@ ``` | ||
{ | ||
code: "sv", | ||
region: undefined, | ||
quality: 1.0 | ||
}, | ||
{ | ||
code: "en", | ||
@@ -46,11 +49,29 @@ region: undefined, | ||
Output is always sorted in quality order from highest -> lowest. as per the http spec, omitting the quality value implies 1.0. | ||
Pruning non-defined langnague codes: | ||
__Known issues__ | ||
- Cannot cope with multi-part region codes, i.e. 'az-AZ-Cyrl' will be treated as 'az-AZ' | ||
``` | ||
var acceptLanguage = require('accept-language'); | ||
acceptLanguage.codes(['en', 'zh']); | ||
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv').prune(); | ||
__Running tests__ | ||
console.log(language); | ||
``` | ||
npm install | ||
grunt test | ||
Output will be: | ||
``` | ||
[ | ||
{ | ||
code: "en", | ||
region: "GB", | ||
quality: 1.0 | ||
}, | ||
{ | ||
code: "en", | ||
region: undefined, | ||
quality: 0.8 | ||
} | ||
]; | ||
``` | ||
Output is always sorted in quality order from highest -> lowest. as per the http spec, omitting the quality value implies 1.0. |
Sorry, the diff of this file is not supported yet
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
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
9961
251
74
7
1