Skip to content

Commit

Permalink
util: refactor inspect.js to use more primodials
Browse files Browse the repository at this point in the history
PR-URL: #36730
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Pooja D P <Pooja.D.P@ibm.com>
  • Loading branch information
rchougule authored and danielleadams committed Jan 12, 2021
1 parent b996e3b commit baa8064
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
Array,
ArrayIsArray,
ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeSort,
ArrayPrototypeUnshift,
Expand Down Expand Up @@ -462,12 +463,13 @@ function strEscape(str) {
// instead wrap the text in double quotes. If double quotes exist, check for
// backticks. If they do not exist, use those as fallback instead of the
// double quotes.
if (str.includes("'")) {
if (StringPrototypeIncludes(str, "'")) {
// This invalidates the charCode and therefore can not be matched for
// anymore.
if (!str.includes('"')) {
if (!StringPrototypeIncludes(str, '"')) {
singleQuote = -1;
} else if (!str.includes('`') && !str.includes('${')) {
} else if (!StringPrototypeIncludes(str, '`') &&
!StringPrototypeIncludes(str, '${')) {
singleQuote = -2;
}
if (singleQuote !== 39) {
Expand All @@ -488,7 +490,7 @@ function strEscape(str) {
let last = 0;
const lastIndex = str.length;
for (let i = 0; i < lastIndex; i++) {
const point = str.charCodeAt(i);
const point = StringPrototypeCharCodeAt(str, i);
if (point === singleQuote ||
point === 92 ||
point < 32 ||
Expand Down Expand Up @@ -609,13 +611,13 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
if (depth === 0) {
keySet = new SafeSet();
} else {
keys.forEach((key) => keySet.add(key));
ArrayPrototypeForEach(keys, (key) => keySet.add(key));
}
// Get all own property names and symbols.
keys = ObjectGetOwnPropertyNames(obj);
const symbols = ObjectGetOwnPropertySymbols(obj);
if (symbols.length !== 0) {
keys.push(...symbols);
ArrayPrototypePush(keys, ...symbols);
}
for (const key of keys) {
// Ignore the `constructor` property and keys that exist on layers above.
Expand All @@ -632,9 +634,9 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
ctx, obj, recurseTimes, key, kObjectType, desc, main);
if (ctx.colors) {
// Faint!
output.push(`\u001b[2m${value}\u001b[22m`);
ArrayPrototypePush(output, `\u001b[2m${value}\u001b[22m`);
} else {
output.push(value);
ArrayPrototypePush(output, value);
}
}
// Limit the inspection to up to three prototype layers. Using `recurseTimes`
Expand Down

0 comments on commit baa8064

Please sign in to comment.