Skip to content

Commit

Permalink
Cleanup (#372)
Browse files Browse the repository at this point in the history
* Modernize code

* Improve error description

* Revert incorrect refactor
  • Loading branch information
kanongil authored Apr 23, 2022
1 parent e3210f2 commit 3964633
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 54 deletions.
1 change: 1 addition & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const AssertError = require('./error');


const internals = {};


Expand Down
34 changes: 17 additions & 17 deletions lib/escapeHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {

Expand All @@ -79,7 +79,7 @@ internals.safeCharCodes = (function () {
i === 58 || // :
i === 95) { // _

safe[i] = null;
safe.add(i);
}
}

Expand Down
37 changes: 12 additions & 25 deletions lib/escapeJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]);
8 changes: 4 additions & 4 deletions lib/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
12 changes: 7 additions & 5 deletions lib/once.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
'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;
method(...args);
}
};

wrapped._hoekOnce = true;
return wrapped;
wrappedFn[internals.wrapped] = true;
return wrappedFn;
};
2 changes: 1 addition & 1 deletion lib/reach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ']';
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down

0 comments on commit 3964633

Please sign in to comment.