| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1800,7 +1800,8 @@ console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m')); | |||
| 1800 | 1800 | added: v21.7.0 | |
| 1801 | 1801 | --> | |
| 1802 | 1802 | ||
| 1803 | - * `format` {string} A text format defined in `util.inspect.colors`. | ||
| 1803 | + * `format` {string | Array} A text format or an Array | ||
| 1804 | + of text formats defined in `util.inspect.colors`. | ||
| 1804 | 1805 | * `text` {string} The text to to be formatted. | |
| 1805 | 1806 | ||
| 1806 | 1807 | This function returns a formatted text considering the `format` passed. | |
@@ -1822,7 +1823,16 @@ console.log(errorMessage); | |||
| 1822 | 1823 | ||
| 1823 | 1824 | ```cjs | |
| 1824 | 1825 | console.log( | |
| 1825 | - util.styleText('underline', util.styleText('italic', 'My italic underlined message')), | ||
| 1826 | + util.styleText(['underline', 'italic'], 'My italic underlined message'), | ||
| 1827 | + ); | ||
| 1828 | + ``` | ||
| 1829 | + | ||
| 1830 | + When passing an array of formats, the order of the format applied | ||
| 1831 | + is left to right so the following style might overwrite the previous one. | ||
| 1832 | + | ||
| 1833 | + ```cjs | ||
| 1834 | + console.log( | ||
| 1835 | + util.styleText(['red', 'green'], 'text'), // green | ||
| 1826 | 1836 | ); | |
| 1827 | 1837 | ``` | |
| 1828 | 1838 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -199,17 +199,40 @@ function pad(n) { | |||
| 199 | 199 | } | |
| 200 | 200 | ||
| 201 | 201 | /** | |
| 202 | - * @param {string} format | ||
| 202 | + * @param {string} code | ||
| 203 | + * @returns {string} | ||
| 204 | + */ | ||
| 205 | + function escapeStyleCode(code) { | ||
| 206 | + return `\u001b[${code}m`; | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + /** | ||
| 210 | + * @param {string | string[]} format | ||
| 203 | 211 | * @param {string} text | |
| 204 | 212 | * @returns {string} | |
| 205 | 213 | */ | |
| 206 | 214 | function styleText(format, text) { | |
| 207 | 215 | validateString(text, 'text'); | |
| 216 | + if (ArrayIsArray(format)) { | ||
| 217 | + let left = ''; | ||
| 218 | + let right = ''; | ||
| 219 | + for (const key of format) { | ||
| 220 | + const formatCodes = inspect.colors[key]; | ||
| 221 | + if (formatCodes == null) { | ||
| 222 | + validateOneOf(key, 'format', ObjectKeys(inspect.colors)); | ||
| 223 | + } | ||
| 224 | + left += escapeStyleCode(formatCodes[0]); | ||
| 225 | + right = `${escapeStyleCode(formatCodes[1])}${right}`; | ||
| 226 | + } | ||
| 227 | + | ||
| 228 | + return `${left}${text}${right}`; | ||
| 229 | + } | ||
| 230 | + | ||
| 208 | 231 | const formatCodes = inspect.colors[format]; | |
| 209 | 232 | if (formatCodes == null) { | |
| 210 | 233 | validateOneOf(format, 'format', ObjectKeys(inspect.colors)); | |
| 211 | 234 | } | |
| 212 | - return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`; | ||
| 235 | + return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`; | ||
| 213 | 236 | } | |
| 214 | 237 | ||
| 215 | 238 | const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,6 @@ const util = require('util'); | |||
| 12 | 12 | Symbol(), | |
| 13 | 13 | () => {}, | |
| 14 | 14 | {}, | |
| 15 | - [], | ||
| 16 | 15 | ].forEach((invalidOption) => { | |
| 17 | 16 | assert.throws(() => { | |
| 18 | 17 | util.styleText(invalidOption, 'test'); | |
@@ -33,3 +32,12 @@ assert.throws(() => { | |||
| 33 | 32 | }); | |
| 34 | 33 | ||
| 35 | 34 | assert.strictEqual(util.styleText('red', 'test'), '\u001b[31mtest\u001b[39m'); | |
| 35 | + | ||
| 36 | + assert.strictEqual(util.styleText(['bold', 'red'], 'test'), '\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m'); | ||
| 37 | + assert.strictEqual(util.styleText(['bold', 'red'], 'test'), util.styleText('bold', util.styleText('red', 'test'))); | ||
| 38 | + | ||
| 39 | + assert.throws(() => { | ||
| 40 | + util.styleText(['invalid'], 'text'); | ||
| 41 | + }, { | ||
| 42 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 43 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments