From 7cd400fa4f799634643d886f4be5480d64149578 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Mon, 13 Jun 2022 22:10:47 +0000 Subject: [PATCH 1/7] util: add colorText method --- lib/util.js | 17 ++++++++++++++ test/parallel/test-util-colorText.js | 33 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 test/parallel/test-util-colorText.js diff --git a/lib/util.js b/lib/util.js index fbd8c550318650..f4f7b107639ff6 100644 --- a/lib/util.js +++ b/lib/util.js @@ -64,6 +64,7 @@ const { debuglog } = require('internal/util/debuglog'); const { validateFunction, validateNumber, + validateString, } = require('internal/validators'); const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; @@ -331,12 +332,28 @@ function getSystemErrorName(err) { return internalErrorName(err); } +/** + * @param {string} format + * @param {string} text + * @returns {string} + */ +function colorText(format, text) { + validateString(format, 'format'); + validateString(text, 'text'); + const formatCodes = inspect.colors[format]; + if (!ArrayIsArray(formatCodes)) { + return text; + } + return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`; +} + // Keep the `exports =` so that various functions can still be monkeypatched module.exports = { _errnoException: errnoException, _exceptionWithHostPort: exceptionWithHostPort, _extend, callbackify, + colorText, debug: debuglog, debuglog, deprecate, diff --git a/test/parallel/test-util-colorText.js b/test/parallel/test-util-colorText.js new file mode 100644 index 00000000000000..f218554e197fe4 --- /dev/null +++ b/test/parallel/test-util-colorText.js @@ -0,0 +1,33 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const util = require('util'); + +[ + undefined, + null, + false, + 5n, + 5, + Symbol(), +].forEach((invalidOption) => { + assert.throws(() => { + util.colorText(invalidOption, 'test'); + }, { + code: 'ERR_INVALID_ARG_TYPE' + }); + assert.throws(() => { + util.colorText('red', invalidOption); + }, { + code: 'ERR_INVALID_ARG_TYPE' + }); +}); + +assert.throws(() => { + util.colorText('red', undefined); +}, { + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "text" argument must be of type string. Received undefined' +}); + +assert.strictEqual(util.colorText('red', 'test'), '\u001b[31mtest\u001b[39m'); From 9da85fe3148f7fa8197c16458c78ce982bd3f163 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Tue, 14 Jun 2022 23:44:43 +0000 Subject: [PATCH 2/7] doc: add colorText --- doc/api/util.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/doc/api/util.md b/doc/api/util.md index 8cafd120503764..542c6f7b836ffd 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -71,6 +71,25 @@ callbackFunction((err, ret) => { }); ``` +## `util.colorText(format, text)` + + + +* `format` {string} `format` one of the color format from `util.inspect.colors` +* `text` {string} The text you would like to color +* Returns: {string} colored text string + +Takes `format` and `text` and retuns the colored text form + +```js +const util = require('node:util'); + +console.log(util.colorText('red', 'This text shall be in red color')); +// ^ '\u001b[31mThis text shall be in red color\u001b[39m' +``` + ## `util.debuglog(section[, callback])` * `format` {string} `format` one of the color format from `util.inspect.colors` From f4814d4b11ea94b4c10d9596282eb57131e86883 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Mon, 20 Jun 2022 21:53:06 -0700 Subject: [PATCH 4/7] Update doc/api/util.md Co-authored-by: Colin Ihrig --- doc/api/util.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index a2e9aed79ee6e8..9f899702b6ac3d 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -77,7 +77,7 @@ callbackFunction((err, ret) => { added: REPLACEME --> -* `format` {string} `format` one of the color format from `util.inspect.colors` +* `format` {string} A color format defined in `util.inspect.colors`. * `text` {string} The text you would like to color * Returns: {string} colored text string From d21bd40c55dd6d1cb0f5b7c58d38f765ec605bec Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Mon, 20 Jun 2022 21:53:42 -0700 Subject: [PATCH 5/7] Update doc/api/util.md Co-authored-by: Colin Ihrig --- doc/api/util.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index 9f899702b6ac3d..79b84b0f0c0772 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -78,7 +78,7 @@ added: REPLACEME --> * `format` {string} A color format defined in `util.inspect.colors`. -* `text` {string} The text you would like to color +* `text` {string} The text to color. * Returns: {string} colored text string Takes `format` and `text` and retuns the colored text form From d48eb4f17c6eaa4d305f14f14a466041f2839543 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Mon, 20 Jun 2022 21:53:49 -0700 Subject: [PATCH 6/7] Update doc/api/util.md Co-authored-by: Colin Ihrig --- doc/api/util.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index 79b84b0f0c0772..38f574d931749b 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -79,7 +79,7 @@ added: REPLACEME * `format` {string} A color format defined in `util.inspect.colors`. * `text` {string} The text to color. -* Returns: {string} colored text string +* Returns: {string} Colored text string. Takes `format` and `text` and retuns the colored text form From 3bcbc5d690011b9acd3b5d4862815c0bbb68a29b Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Mon, 20 Jun 2022 21:55:31 -0700 Subject: [PATCH 7/7] Update doc/api/util.md Co-authored-by: Jordan Harband --- doc/api/util.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index 38f574d931749b..9c0c010f86875b 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -81,7 +81,7 @@ added: REPLACEME * `text` {string} The text to color. * Returns: {string} Colored text string. -Takes `format` and `text` and retuns the colored text form +Takes `format` and `text` and returns the colored text form ```js const util = require('node:util');