Skip to content

Commit

Permalink
Switch to maps. Closes #55
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Feb 13, 2020
1 parent 3865ae9 commit 08c4989
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions lib/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ internals.parse = function (raw, preferences, options) {

// Normalize preferences

const lowers = Object.create(null);
const lowers = new Map();
if (preferences) {
for (let i = 0; i < preferences.length; ++i) {
const preference = preferences[i];
lowers[preference.toLowerCase()] = { orig: preference, pos: i };
lowers.set(preference.toLowerCase(), { orig: preference, pos: i });
}
}

// Parse selections

const parts = header.split(',');
const selections = [];
const map = {};
const map = new Set();

for (let i = 0; i < parts.length; ++i) {
const part = parts[i];
Expand All @@ -112,9 +112,9 @@ internals.parse = function (raw, preferences, options) {
}

if (options.equivalents &&
options.equivalents[token]) {
options.equivalents.has(token)) {

token = options.equivalents[token];
token = options.equivalents.get(token);
}

const selection = {
Expand All @@ -125,12 +125,12 @@ internals.parse = function (raw, preferences, options) {
};

if (preferences &&
lowers[token]) {
lowers.has(token)) {

selection.pref = lowers[token].pos;
selection.pref = lowers.get(token).pos;
}

map[selection.token] = selection;
map.add(selection.token);

// Parse q=value

Expand All @@ -139,7 +139,7 @@ internals.parse = function (raw, preferences, options) {
const [key, value] = q.split('=');

if (!value ||
(key !== 'q' && key !== 'Q')) {
key !== 'q' && key !== 'Q') {

throw Boom.badRequest(`Invalid ${options.type} header`);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ internals.parse = function (raw, preferences, options) {
const values = selections.map((selection) => selection.token);

if (options.default &&
!map[options.default]) {
!map.has(options.default)) {

values.push(options.default);
}
Expand All @@ -183,16 +183,16 @@ internals.parse = function (raw, preferences, options) {
const preferred = [];
for (const selection of values) {
if (selection === '*') {
for (const preference of Object.keys(lowers)) {
if (!map[preference]) {
preferred.push(lowers[preference].orig);
for (const [preference, value] of lowers) {
if (!map.has(preference)) {
preferred.push(value.orig);
}
}
}
else {
const lower = selection.toLowerCase();
if (lowers[lower]) {
preferred.push(lowers[lower].orig);
if (lowers.has(lower)) {
preferred.push(lowers.get(lower).orig);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const internals = {
encoding: {
type: 'accept-encoding',
default: 'identity',
equivalents: {
'x-compress': 'compress',
'x-gzip': 'gzip'
}
equivalents: new Map([
['x-compress', 'compress'],
['x-gzip', 'gzip']
])
},
language: {
type: 'accept-language',
Expand Down

0 comments on commit 08c4989

Please sign in to comment.