@hapi/accept
Advanced tools
Comparing version 3.2.1 to 3.2.2
@@ -80,2 +80,12 @@ 'use strict'; | ||
// Normalize preferences | ||
const lowers = Object.create(null); | ||
if (preferences) { | ||
for (let i = 0; i < preferences.length; ++i) { | ||
const preference = preferences[i]; | ||
lowers[preference.toLowerCase()] = { orig: preference, pos: i }; | ||
} | ||
} | ||
// Parse selections | ||
@@ -87,3 +97,4 @@ | ||
for (const part of parts) { | ||
for (let i = 0; i < parts.length; ++i) { | ||
const part = parts[i]; | ||
if (!part) { // Ignore empty parts or leading commas | ||
@@ -111,3 +122,15 @@ continue; | ||
const selection = { token, q: 1 }; | ||
const selection = { | ||
token, | ||
pos: i, | ||
q: 1, | ||
specificity: options.specificity ? token.split('-') : null | ||
}; | ||
if (preferences && | ||
lowers[token]) { | ||
selection.pref = lowers[token].pos; | ||
} | ||
map[selection.token] = selection; | ||
@@ -147,2 +170,4 @@ | ||
// Extract tokens | ||
const values = selections.map((selection) => selection.token); | ||
@@ -156,31 +181,14 @@ | ||
return internals.preferences(map, values, preferences); | ||
}; | ||
internals.sort = function (a, b) { | ||
return b.q - a.q; | ||
}; | ||
internals.preferences = function (map, selections, preferences) { | ||
if (!preferences || | ||
!preferences.length) { | ||
return selections; | ||
return values; | ||
} | ||
const lowers = Object.create(null); | ||
for (const preference of preferences) { | ||
lowers[preference.toLowerCase()] = preference; | ||
} | ||
const preferred = []; | ||
for (const selection of selections) { | ||
for (const selection of values) { | ||
if (selection === '*') { | ||
for (const preference of Object.keys(lowers)) { | ||
if (!map[preference]) { | ||
preferred.push(lowers[preference]); | ||
preferred.push(lowers[preference].orig); | ||
} | ||
@@ -192,3 +200,3 @@ } | ||
if (lowers[lower]) { | ||
preferred.push(lowers[lower]); | ||
preferred.push(lowers[lower].orig); | ||
} | ||
@@ -200,1 +208,33 @@ } | ||
}; | ||
internals.sort = function (a, b) { | ||
const aFirst = -1; | ||
const bFirst = 1; | ||
if (b.q !== a.q) { | ||
return b.q - a.q; | ||
} | ||
if (b.pref !== a.pref) { | ||
if (a.pref === undefined) { | ||
return bFirst; | ||
} | ||
if (b.pref === undefined) { | ||
return aFirst; | ||
} | ||
return a.pref - b.pref; | ||
} | ||
if (a.specificity && | ||
a.specificity[0] === b.specificity[0] && | ||
a.specificity.length !== b.specificity.length) { | ||
return b.specificity.length - a.specificity.length; | ||
} | ||
return a.pos - b.pos; | ||
}; |
@@ -21,3 +21,4 @@ 'use strict'; | ||
language: { | ||
type: 'accept-language' | ||
type: 'accept-language', | ||
specificity: true | ||
} | ||
@@ -24,0 +25,0 @@ } |
{ | ||
"name": "@hapi/accept", | ||
"description": "HTTP Accept-* headers parsing", | ||
"version": "3.2.1", | ||
"version": "3.2.2", | ||
"repository": "git://github.com/hapijs/accept", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
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
19630
434