util: enforce shouldColorize in styleText array arg · nodejs/node@77294d8
@@ -119,6 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
119119validateString(text, 'text');
120120validateBoolean(validateStream, 'options.validateStream');
121121122+let skipColorize;
122123if (validateStream) {
123124if (
124125!isReadableStream(stream) &&
@@ -127,40 +128,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
127128) {
128129throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
129130}
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-}
143131144-return `${left}${text}${right}`;
132+// If the stream is falsy or should not be colorized, set skipColorize to true
133+skipColorize = !lazyUtilColors().shouldColorize(stream);
145134}
146135147-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];
151138152-// 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}`;
161150}
162151163-return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`;
152+return skipColorize ? text : `${left}${text}${right}`;
164153}
165154166155/**