diff --git a/lib/assert.js b/lib/assert.js index 6a11e93..6ed635a 100755 --- a/lib/assert.js +++ b/lib/assert.js @@ -2,6 +2,7 @@ const AssertError = require('./error'); + const internals = {}; diff --git a/lib/escapeHtml.js b/lib/escapeHtml.js index ddef2b6..c2dd443 100755 --- a/lib/escapeHtml.js +++ b/lib/escapeHtml.js @@ -29,8 +29,8 @@ module.exports = function (input) { internals.escapeHtmlChar = function (charCode) { - const namedEscape = internals.namedHtml[charCode]; - if (typeof namedEscape !== 'undefined') { + const namedEscape = internals.namedHtml.get(charCode); + if (namedEscape) { return namedEscape; } @@ -45,27 +45,27 @@ internals.escapeHtmlChar = function (charCode) { internals.isSafe = function (charCode) { - return (typeof internals.safeCharCodes[charCode] !== 'undefined'); + return internals.safeCharCodes.has(charCode); }; -internals.namedHtml = { - '38': '&', - '60': '<', - '62': '>', - '34': '"', - '160': ' ', - '162': '¢', - '163': '£', - '164': '¤', - '169': '©', - '174': '®' -}; +internals.namedHtml = new Map([ + [38, '&'], + [60, '<'], + [62, '>'], + [34, '"'], + [160, ' '], + [162, '¢'], + [163, '£'], + [164, '¤'], + [169, '©'], + [174, '®'] +]); internals.safeCharCodes = (function () { - const safe = {}; + const safe = new Set(); for (let i = 32; i < 123; ++i) { @@ -79,7 +79,7 @@ internals.safeCharCodes = (function () { i === 58 || // : i === 95) { // _ - safe[i] = null; + safe.add(i); } } diff --git a/lib/escapeJson.js b/lib/escapeJson.js index e6e94b3..243edfb 100755 --- a/lib/escapeJson.js +++ b/lib/escapeJson.js @@ -9,33 +9,20 @@ module.exports = function (input) { return ''; } - const lessThan = 0x3C; - const greaterThan = 0x3E; - const andSymbol = 0x26; - const lineSeperator = 0x2028; - - // replace method - let charCode; - return input.replace(/[<>&\u2028\u2029]/g, (match) => { - - charCode = match.charCodeAt(0); + return input.replace(/[<>&\u2028\u2029]/g, internals.escape); +}; - if (charCode === lessThan) { - return '\\u003c'; - } - if (charCode === greaterThan) { - return '\\u003e'; - } +internals.escape = function (char) { - if (charCode === andSymbol) { - return '\\u0026'; - } + return internals.replacements.get(char); +}; - if (charCode === lineSeperator) { - return '\\u2028'; - } - return '\\u2029'; - }); -}; +internals.replacements = new Map([ + ['<', '\\u003c'], + ['>', '\\u003e'], + ['&', '\\u0026'], + ['\u2028', '\\u2028'], + ['\u2029', '\\u2029'] +]); diff --git a/lib/flatten.js b/lib/flatten.js index 726e231..a5ea622 100755 --- a/lib/flatten.js +++ b/lib/flatten.js @@ -7,12 +7,12 @@ module.exports = internals.flatten = function (array, target) { const result = target || []; - for (let i = 0; i < array.length; ++i) { - if (Array.isArray(array[i])) { - internals.flatten(array[i], result); + for (const entry of array) { + if (Array.isArray(entry)) { + internals.flatten(entry, result); } else { - result.push(array[i]); + result.push(entry); } } diff --git a/lib/once.js b/lib/once.js index de94ea0..c825767 100755 --- a/lib/once.js +++ b/lib/once.js @@ -1,16 +1,18 @@ 'use strict'; -const internals = {}; +const internals = { + wrapped: Symbol('wrapped') +}; module.exports = function (method) { - if (method._hoekOnce) { + if (method[internals.wrapped]) { return method; } let once = false; - const wrapped = function (...args) { + const wrappedFn = function (...args) { if (!once) { once = true; @@ -18,6 +20,6 @@ module.exports = function (method) { } }; - wrapped._hoekOnce = true; - return wrapped; + wrappedFn[internals.wrapped] = true; + return wrappedFn; }; diff --git a/lib/reach.js b/lib/reach.js index 3791b37..53b7c24 100755 --- a/lib/reach.js +++ b/lib/reach.js @@ -22,7 +22,7 @@ module.exports = function (obj, chain, options) { const isChainArray = Array.isArray(chain); - Assert(!isChainArray || !options.separator, 'Separator option no valid for array-based chain'); + Assert(!isChainArray || !options.separator, 'Separator option is not valid for array-based chain'); const path = isChainArray ? chain : chain.split(options.separator || '.'); let ref = obj; diff --git a/lib/stringify.js b/lib/stringify.js index 88d0fc4..82152cf 100755 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -6,7 +6,7 @@ const internals = {}; module.exports = function (...args) { try { - return JSON.stringify.apply(null, args); + return JSON.stringify(...args); } catch (err) { return '[Cannot display object: ' + err.message + ']'; diff --git a/test/index.js b/test/index.js index 9e9747f..f5db161 100755 --- a/test/index.js +++ b/test/index.js @@ -2141,7 +2141,7 @@ describe('assert()', () => { expect(() => { - Hoek.assert(false, 'This', 'is', new Error('spinal'), new Error('tap')); + Hoek.assert(false, new Error('This'), 'is', 'spinal', new Error('tap')); }).to.throw('This is spinal tap'); });