| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 861e040 commit c501346
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1792,6 +1792,42 @@ console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m')); | |||
| 1792 | 1792 | // Prints "value" | |
| 1793 | 1793 | ``` | |
| 1794 | 1794 | ||
| 1795 | + ## `util.styleText(format, text)` | ||
| 1796 | + | ||
| 1797 | + > Stability: 1.1 - Active development | ||
| 1798 | + | ||
| 1799 | + <!-- YAML | ||
| 1800 | + added: REPLACEME | ||
| 1801 | + --> | ||
| 1802 | + | ||
| 1803 | + * `format` {string} A text format defined in `util.inspect.colors`. | ||
| 1804 | + * `text` {string} The text to to be formatted. | ||
| 1805 | + | ||
| 1806 | + This function returns a formatted text considering the `format` passed. | ||
| 1807 | + | ||
| 1808 | + ```mjs | ||
| 1809 | + import { styleText } from 'node:util'; | ||
| 1810 | + const errorMessage = styleText('red', 'Error! Error!'); | ||
| 1811 | + console.log(errorMessage); | ||
| 1812 | + ``` | ||
| 1813 | + | ||
| 1814 | + ```cjs | ||
| 1815 | + const { styleText } = require('node:util'); | ||
| 1816 | + const errorMessage = styleText('red', 'Error! Error!'); | ||
| 1817 | + console.log(errorMessage); | ||
| 1818 | + ``` | ||
| 1819 | + | ||
| 1820 | + `util.inspect.colors` also provides text formats such as `italic`, and | ||
| 1821 | + `underline` and you can combine both: | ||
| 1822 | + | ||
| 1823 | + ```cjs | ||
| 1824 | + console.log( | ||
| 1825 | + util.styleText('underline', util.styleText('italic', 'My italic underlined message')), | ||
| 1826 | + ); | ||
| 1827 | + ``` | ||
| 1828 | + | ||
| 1829 | + The full list of formats can be found in [modifiers][]. | ||
| 1830 | + | ||
| 1795 | 1831 | ## Class: `util.TextDecoder` | |
| 1796 | 1832 | ||
| 1797 | 1833 | <!-- YAML | |
@@ -3393,6 +3429,7 @@ util.log('Timestamped message.'); | |||
| 3393 | 3429 | [default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | |
| 3394 | 3430 | [global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for | |
| 3395 | 3431 | [list of deprecated APIS]: deprecations.md#list-of-deprecated-apis | |
| 3432 | + [modifiers]: #modifiers | ||
| 3396 | 3433 | [realm]: https://tc39.es/ecma262/#realm | |
| 3397 | 3434 | [semantically incompatible]: https://github.com/nodejs/node/issues/4179 | |
| 3398 | 3435 | [util.inspect.custom]: #utilinspectcustom | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,6 +68,7 @@ const { | |||
| 68 | 68 | validateFunction, | |
| 69 | 69 | validateNumber, | |
| 70 | 70 | validateString, | |
| 71 | + validateOneOf, | ||
| 71 | 72 | } = require('internal/validators'); | |
| 72 | 73 | const { isBuffer } = require('buffer').Buffer; | |
| 73 | 74 | const types = require('internal/util/types'); | |
@@ -197,6 +198,20 @@ function pad(n) { | |||
| 197 | 198 | return StringPrototypePadStart(n.toString(), 2, '0'); | |
| 198 | 199 | } | |
| 199 | 200 | ||
| 201 | + /** | ||
| 202 | + * @param {string} format | ||
| 203 | + * @param {string} text | ||
| 204 | + * @returns {string} | ||
| 205 | + */ | ||
| 206 | + function styleText(format, text) { | ||
| 207 | + validateString(text, 'text'); | ||
| 208 | + const formatCodes = inspect.colors[format]; | ||
| 209 | + if (formatCodes == null) { | ||
| 210 | + validateOneOf(format, 'format', ObjectKeys(inspect.colors)); | ||
| 211 | + } | ||
| 212 | + return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`; | ||
| 213 | + } | ||
| 214 | + | ||
| 200 | 215 | const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', | |
| 201 | 216 | 'Oct', 'Nov', 'Dec']; | |
| 202 | 217 | ||
@@ -393,6 +408,7 @@ module.exports = { | |||
| 393 | 408 | debuglog, | |
| 394 | 409 | deprecate, | |
| 395 | 410 | format, | |
| 411 | + styleText, | ||
| 396 | 412 | formatWithOptions, | |
| 397 | 413 | getSystemErrorMap, | |
| 398 | 414 | getSystemErrorName, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const util = require('util'); | ||
| 5 | + | ||
| 6 | + [ | ||
| 7 | + undefined, | ||
| 8 | + null, | ||
| 9 | + false, | ||
| 10 | + 5n, | ||
| 11 | + 5, | ||
| 12 | + Symbol(), | ||
| 13 | + () => {}, | ||
| 14 | + {}, | ||
| 15 | + [], | ||
| 16 | + ].forEach((invalidOption) => { | ||
| 17 | + assert.throws(() => { | ||
| 18 | + util.styleText(invalidOption, 'test'); | ||
| 19 | + }, { | ||
| 20 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 21 | + }); | ||
| 22 | + assert.throws(() => { | ||
| 23 | + util.styleText('red', invalidOption); | ||
| 24 | + }, { | ||
| 25 | + code: 'ERR_INVALID_ARG_TYPE' | ||
| 26 | + }); | ||
| 27 | + }); | ||
| 28 | + | ||
| 29 | + assert.throws(() => { | ||
| 30 | + util.styleText('invalid', 'text'); | ||
| 31 | + }, { | ||
| 32 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 33 | + }); | ||
| 34 | + | ||
| 35 | + assert.strictEqual(util.styleText('red', 'test'), '\u001b[31mtest\u001b[39m'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments