mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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> PR-URL: https://github.com/nodejs/node/pull/33797 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
1 parent
e8cbefe12d
commit
7af5cfc10e
2 files changed
+8
-1
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user