From 4b367dcc8b07736c39dc2970b70cd0d0676b465e Mon Sep 17 00:00:00 2001 From: dave caruso Date: Wed, 6 Mar 2024 18:22:50 -0800 Subject: [PATCH 1/2] feat: implement util.styleText --- src/js/node/util.js | 14 ++++++++++++++ test/js/node/util/util.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/js/node/util.js b/src/js/node/util.js index e8adc966c8c53..470e48d4e23e6 100644 --- a/src/js/node/util.js +++ b/src/js/node/util.js @@ -240,6 +240,20 @@ var toUSVString = input => { return (input + "").toWellFormed(); }; +function ERR_INVALID_ARG_VALUE(name, value, reason) { + return new Error(`The value "${value}" is invalid for argument '${name}'. Reason: ${reason}`); +} + +function styleText(format, text) { + const formatCodes = inspect.colors[format]; + if (formatCodes == null) { + throw new Error( + `The value "${format}" is invalid for argument 'format'. Reason: must be one of: ${Object.keys(inspect.colors).join(", ")}`, + ); + } + return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`; +} + export default Object.assign(cjs_exports, { format, formatWithOptions, diff --git a/test/js/node/util/util.test.js b/test/js/node/util/util.test.js index df995955a2cce..c6014ba752d7e 100644 --- a/test/js/node/util/util.test.js +++ b/test/js/node/util/util.test.js @@ -326,3 +326,35 @@ describe("util", () => { ); }); }); + +test("util.styleText", () => { + [undefined, null, false, 5n, 5, Symbol(), () => {}, {}, []].forEach(invalidOption => { + assert.throws( + () => { + util.styleText(invalidOption, "test"); + }, + { + code: "ERR_INVALID_ARG_VALUE", + }, + ); + assert.throws( + () => { + util.styleText("red", invalidOption); + }, + { + code: "ERR_INVALID_ARG_TYPE", + }, + ); + }); + + assert.throws( + () => { + util.styleText("invalid", "text"); + }, + { + code: "ERR_INVALID_ARG_VALUE", + }, + ); + + assert.strictEqual(util.styleText("red", "test"), "\u001b[31mtest\u001b[39m"); +}); From af881273aef7441ef1c012f682538a63a718ccca Mon Sep 17 00:00:00 2001 From: dave caruso Date: Wed, 6 Mar 2024 18:34:03 -0800 Subject: [PATCH 2/2] i forgot to test --- src/js/node/util.js | 16 +++++++----- test/js/node/util/util.test.js | 46 +++++++++++++++++----------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/js/node/util.js b/src/js/node/util.js index 470e48d4e23e6..548b663f8d10d 100644 --- a/src/js/node/util.js +++ b/src/js/node/util.js @@ -240,16 +240,19 @@ var toUSVString = input => { return (input + "").toWellFormed(); }; -function ERR_INVALID_ARG_VALUE(name, value, reason) { - return new Error(`The value "${value}" is invalid for argument '${name}'. Reason: ${reason}`); -} - function styleText(format, text) { + if (typeof text !== "string") { + const e = new Error(`The text argument must be of type string. Received type ${typeof text}`); + e.code = "ERR_INVALID_ARG_TYPE"; + throw e; + } const formatCodes = inspect.colors[format]; if (formatCodes == null) { - throw new Error( - `The value "${format}" is invalid for argument 'format'. Reason: must be one of: ${Object.keys(inspect.colors).join(", ")}`, + 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(", ")}`, ); + e.code = "ERR_INVALID_ARG_VALUE"; + throw e; } return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`; } @@ -287,4 +290,5 @@ export default Object.assign(cjs_exports, { TextDecoder, TextEncoder, parseArgs, + styleText, }); diff --git a/test/js/node/util/util.test.js b/test/js/node/util/util.test.js index c6014ba752d7e..4c1c51511e08c 100644 --- a/test/js/node/util/util.test.js +++ b/test/js/node/util/util.test.js @@ -325,36 +325,36 @@ describe("util", () => { "wow({ obj: \u001B[33mtrue\u001B[39m })", ); }); -}); -test("util.styleText", () => { - [undefined, null, false, 5n, 5, Symbol(), () => {}, {}, []].forEach(invalidOption => { + it("styleText", () => { + [undefined, null, false, 5n, 5, Symbol(), () => {}, {}, []].forEach(invalidOption => { + assert.throws( + () => { + util.styleText(invalidOption, "test"); + }, + { + code: "ERR_INVALID_ARG_VALUE", + }, + ); + assert.throws( + () => { + util.styleText("red", invalidOption); + }, + { + code: "ERR_INVALID_ARG_TYPE", + }, + ); + }); + assert.throws( () => { - util.styleText(invalidOption, "test"); + util.styleText("invalid", "text"); }, { code: "ERR_INVALID_ARG_VALUE", }, ); - assert.throws( - () => { - util.styleText("red", invalidOption); - }, - { - code: "ERR_INVALID_ARG_TYPE", - }, - ); - }); - - assert.throws( - () => { - util.styleText("invalid", "text"); - }, - { - code: "ERR_INVALID_ARG_VALUE", - }, - ); - assert.strictEqual(util.styleText("red", "test"), "\u001b[31mtest\u001b[39m"); + assert.strictEqual(util.styleText("red", "test"), "\u001b[31mtest\u001b[39m"); + }); });