Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node:util): support array type in format for the styleText util #13807

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions src/js/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const { promisify } = require("internal/promisify");
const internalErrorName = $newZigFunction("node_util_binding.zig", "internalErrorName", 1);

const NumberIsSafeInteger = Number.isSafeInteger;
const ArrayIsArray = Array.isArray;
const ObjectKeys = Object.keys;

const ArrayPrototypeIncludes = Array.prototype.includes;
const ArrayPrototypeJoin = Array.prototype.join;
const ArrayPrototypeMap = Array.prototype.map;

var cjs_exports;

Expand Down Expand Up @@ -200,21 +206,71 @@ var toUSVString = input => {
return (input + "").toWellFormed();
};

// BEGIN DUPLICATED FROM src/js/node/child_process.ts
function ERR_INVALID_ARG_VALUE(name, value, reason) {
const err = new Error(`The value "${value}" is invalid for argument '${name}'. Reason: ${reason}`);
err.code = "ERR_INVALID_ARG_VALUE";
return err;
}

/**
* @callback validateOneOf
* @template T
* @param {T} value
* @param {string} name
* @param {T[]} oneOf
*/

/** @type {validateOneOf} */
const validateOneOf = (value, name, oneOf) => {
// const validateOneOf = hideStackFrames((value, name, oneOf) => {
if (!ArrayPrototypeIncludes.$call(oneOf, value)) {
const allowed = ArrayPrototypeJoin.$call(
ArrayPrototypeMap.$call(oneOf, v => (typeof v === "string" ? `'${v}'` : String(v))),
", ",
);
const reason = "must be one of: " + allowed;
throw ERR_INVALID_ARG_VALUE(name, value, reason);
}
};
// END DUPLICATED FROM src/js/node/child_process.ts

/**
* @param {string} code
* @returns {string}
*/
function escapeStyleCode(code) {
return `\u001b[${code}m`;
}

function styleText(format, text) {
if (typeof text !== "string") {
const e = new Error(`The text argument must be of type string. Received type ${typeof text}`);
e.code = "ERR_INVALID_ARG_TYPE";
throw e;
}

if (ArrayIsArray(format)) {
let left = "";
let right = "";
for (const key of format) {
const formatCodes = inspect.colors[key];
if (formatCodes == null) {
validateOneOf(key, "format", ObjectKeys(inspect.colors));
}
left += escapeStyleCode(formatCodes[0]);
right = `${escapeStyleCode(formatCodes[1])}${right}`;
}

return `${left}${text}${right}`;
}

const formatCodes = inspect.colors[format];
if (formatCodes == null) {
const e = new Error(
`The value "${typeof format === "symbol" ? format.description : format}" is invalid for argument 'format'. Reason: must be one of: ${Object.keys(inspect.colors).join(", ")}`,
);
e.code = "ERR_INVALID_ARG_VALUE";
throw e;
validateOneOf(format, "format", ObjectKeys(inspect.colors));
}
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;

return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`;
}

function getSystemErrorName(err: any) {
Expand Down
21 changes: 20 additions & 1 deletion test/js/node/util/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe("util", () => {
});

it("styleText", () => {
[undefined, null, false, 5n, 5, Symbol(), () => {}, {}, []].forEach(invalidOption => {
[undefined, null, false, 5n, 5, Symbol(), () => {}, {}].forEach(invalidOption => {
assert.throws(
() => {
util.styleText(invalidOption, "test");
Expand Down Expand Up @@ -370,6 +370,25 @@ describe("util", () => {
);

assert.strictEqual(util.styleText("red", "test"), "\u001b[31mtest\u001b[39m");

assert.strictEqual(
util.styleText(["bold", "red"], "test"),
"\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m",
);

assert.strictEqual(
util.styleText(["bold", "red"], "test"),
util.styleText("bold", util.styleText("red", "test")),
);

assert.throws(
() => {
util.styleText(["invalid"], "text");
},
{
code: "ERR_INVALID_ARG_VALUE",
},
);
});

describe("getSystemErrorName", () => {
Expand Down