Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
util: gracefully handle unknown colors
This makes sure colors that are unknown won't cause an error. This
is especially important in case a library wants to use colors
defined by Node.js core, if available and fall back to the default
otherwise.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
BridgeAR committed Jun 8, 2020
commit b2fc61759e7f5c15018dcbbf15f2cdc001cd8aaa
3 changes: 2 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ function stylizeWithColor(str, styleType) {
const style = inspect.styles[styleType];
if (style !== undefined) {
const color = inspect.colors[style];
return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
if (color !== undefined)
return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
}
return str;
}
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2245,6 +2245,12 @@ assert.strictEqual(
assert.deepStrictEqual(inspect.colors[bgColor], [40 + i, 49]);
assert.deepStrictEqual(inspect.colors[`${bgColor}Bright`], [100 + i, 49]);
});

// Unknown colors are handled gracefully:
const stringStyle = inspect.styles.string;
inspect.styles.string = 'UNKNOWN';
assert.strictEqual(inspect('foobar', { colors: true }), 'foobar');
inspect.styles.string = stringStyle;
}

assert.strictEqual(
Expand Down