| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4606a5f commit 77294d8
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,6 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou | |||
| 119 | 119 | validateString(text, 'text'); | |
| 120 | 120 | validateBoolean(validateStream, 'options.validateStream'); | |
| 121 | 121 | ||
| 122 | + let skipColorize; | ||
| 122 | 123 | if (validateStream) { | |
| 123 | 124 | if ( | |
| 124 | 125 | !isReadableStream(stream) && | |
@@ -127,40 +128,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou | |||
| 127 | 128 | ) { | |
| 128 | 129 | throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream); | |
| 129 | 130 | } | |
| 130 | - } | ||
| 131 | - | ||
| 132 | - if (ArrayIsArray(format)) { | ||
| 133 | - let left = ''; | ||
| 134 | - let right = ''; | ||
| 135 | - for (const key of format) { | ||
| 136 | - const formatCodes = inspect.colors[key]; | ||
| 137 | - if (formatCodes == null) { | ||
| 138 | - validateOneOf(key, 'format', ObjectKeys(inspect.colors)); | ||
| 139 | - } | ||
| 140 | - left += escapeStyleCode(formatCodes[0]); | ||
| 141 | - right = `${escapeStyleCode(formatCodes[1])}${right}`; | ||
| 142 | - } | ||
| 143 | 131 | ||
| 144 | - return `${left}${text}${right}`; | ||
| 132 | + // If the stream is falsy or should not be colorized, set skipColorize to true | ||
| 133 | + skipColorize = !lazyUtilColors().shouldColorize(stream); | ||
| 145 | 134 | } | |
| 146 | 135 | ||
| 147 | - const formatCodes = inspect.colors[format]; | ||
| 148 | - if (formatCodes == null) { | ||
| 149 | - validateOneOf(format, 'format', ObjectKeys(inspect.colors)); | ||
| 150 | - } | ||
| 136 | + // If the format is not an array, convert it to an array | ||
| 137 | + const formatArray = ArrayIsArray(format) ? format : [format]; | ||
| 151 | 138 | ||
| 152 | - // Check colorize only after validating arg type and value | ||
| 153 | - if ( | ||
| 154 | - validateStream && | ||
| 155 | - ( | ||
| 156 | - !stream || | ||
| 157 | - !lazyUtilColors().shouldColorize(stream) | ||
| 158 | - ) | ||
| 159 | - ) { | ||
| 160 | - return text; | ||
| 139 | + let left = ''; | ||
| 140 | + let right = ''; | ||
| 141 | + for (const key of formatArray) { | ||
| 142 | + const formatCodes = inspect.colors[key]; | ||
| 143 | + // If the format is not a valid style, throw an error | ||
| 144 | + if (formatCodes == null) { | ||
| 145 | + validateOneOf(key, 'format', ObjectKeys(inspect.colors)); | ||
| 146 | + } | ||
| 147 | + if (skipColorize) continue; | ||
| 148 | + left += escapeStyleCode(formatCodes[0]); | ||
| 149 | + right = `${escapeStyleCode(formatCodes[1])}${right}`; | ||
| 161 | 150 | } | |
| 162 | 151 | ||
| 163 | - return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`; | ||
| 152 | + return skipColorize ? text : `${left}${text}${right}`; | ||
| 164 | 153 | } | |
| 165 | 154 | ||
| 166 | 155 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,8 +95,15 @@ if (fd !== -1) { | |||
| 95 | 95 | ...process.env, | |
| 96 | 96 | ...testCase.env | |
| 97 | 97 | }; | |
| 98 | - const output = util.styleText('red', 'test', { stream: writeStream }); | ||
| 99 | - assert.strictEqual(output, testCase.expected); | ||
| 98 | + { | ||
| 99 | + const output = util.styleText('red', 'test', { stream: writeStream }); | ||
| 100 | + assert.strictEqual(output, testCase.expected); | ||
| 101 | + } | ||
| 102 | + { | ||
| 103 | + // Check that when passing an array of styles, the output behaves the same | ||
| 104 | + const output = util.styleText(['red'], 'test', { stream: writeStream }); | ||
| 105 | + assert.strictEqual(output, testCase.expected); | ||
| 106 | + } | ||
| 100 | 107 | process.env = originalEnv; | |
| 101 | 108 | }); | |
| 102 | 109 | } else { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments