diff --git a/src/js/node/util.ts b/src/js/node/util.ts index 3541d1b806..562aad9d15 100644 --- a/src/js/node/util.ts +++ b/src/js/node/util.ts @@ -206,7 +206,28 @@ function styleText(format, text) { e.code = "ERR_INVALID_ARG_TYPE"; throw e; } - const formatCodes = inspect.colors[format]; + + if ($isJSArray(format)) { + let left = ""; + let right = ""; + for (const key of format) { + const formatCodes = inspect.colors[key]; + if (formatCodes == null) { + const e = new Error( + `The value "${typeof key === "symbol" ? key.description : key}" is invalid for argument 'format'. Reason: must be one of: ${Object.keys(inspect.colors).join(", ")}`, + ); + e.code = "ERR_INVALID_ARG_VALUE"; + throw e; + } + left += `\u001b[${formatCodes[0]}m`; + right = `\u001b[${formatCodes[1]}m${right}`; + } + + return `${left}${text}${right}`; + } + + let formatCodes = inspect.colors[format]; + if (formatCodes == null) { const e = new Error( `The value "${typeof format === "symbol" ? format.description : format}" is invalid for argument 'format'. Reason: must be one of: ${Object.keys(inspect.colors).join(", ")}`, diff --git a/test/js/node/util/util.test.js b/test/js/node/util/util.test.js index dd55d12b1a..3f02c2b183 100644 --- a/test/js/node/util/util.test.js +++ b/test/js/node/util/util.test.js @@ -341,7 +341,13 @@ describe("util", () => { }); it("styleText", () => { - [undefined, null, false, 5n, 5, Symbol(), () => {}, {}, []].forEach(invalidOption => { + it("multiplecolors", () => { + expect(util.styleText(["bold", "red"], "test")).toBe("\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m"); + expect(util.styleText("bold"), "test").toBe("\u001b[1mtest\u001b[22m"); + expect(util.styleText("red", "test")).toBe("\u001b[31mtest\u001b[39m"); + }); + + [undefined, null, false, 5n, 5, Symbol(), () => {}, {}].forEach(invalidOption => { assert.throws( () => { util.styleText(invalidOption, "test");