Skip to content

Commit f292939

Browse files
committed
util: allow color aliases in styleText
Fixes an issue where `util.styleText()` would throw an error for valid color aliases like 'grey' in Node.js >= 25.7.0. It now uses `ObjectGetOwnPropertyNames` instead of `ObjectKeys` to fetch both keys and aliases. Fixes: #62177
1 parent a0d8ea4 commit f292939

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const {
3232
ObjectDefineProperties,
3333
ObjectDefineProperty,
3434
ObjectGetOwnPropertyDescriptors,
35+
ObjectGetOwnPropertyNames,
3536
ObjectKeys,
3637
ObjectSetPrototypeOf,
3738
ObjectValues,
@@ -115,7 +116,7 @@ function getStyleCache() {
115116
if (styleCache === undefined) {
116117
styleCache = { __proto__: null };
117118
const colors = inspect.colors;
118-
for (const key of ObjectKeys(colors)) {
119+
for (const key of ObjectGetOwnPropertyNames(colors)) {
119120
const codes = colors[key];
120121
if (codes) {
121122
const openNum = codes[0];
@@ -206,7 +207,7 @@ function styleText(format, text, options) {
206207
if (key === 'none') continue;
207208
const style = cache[key];
208209
if (style === undefined) {
209-
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
210+
validateOneOf(key, 'format', ObjectGetOwnPropertyNames(inspect.colors));
210211
}
211212
openCodes += style.openSeq;
212213
closeCodes = style.closeSeq + closeCodes;

test/parallel/test-util-styletext.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ assert.strictEqual(
4141
'\u001b[31mtest\u001b[39m',
4242
);
4343

44+
assert.strictEqual(
45+
util.styleText('gray', 'test', { validateStream: false }),
46+
'\u001b[90mtest\u001b[39m',
47+
);
48+
49+
assert.strictEqual(
50+
util.styleText('grey', 'test', { validateStream: false }),
51+
'\u001b[90mtest\u001b[39m',
52+
);
53+
4454
assert.strictEqual(
4555
util.styleText(['bold', 'red'], 'test', { validateStream: false }),
4656
'\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m',

0 commit comments

Comments
 (0)