From e2bc676bd85d11195f761134153c2d80c6d6e4d5 Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Sat, 22 Nov 2025 17:30:22 +0800 Subject: [PATCH] perf: don't use regex --- src/node.ts | 60 +++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/node.ts b/src/node.ts index 6b06fee..3e7ff8a 100644 --- a/src/node.ts +++ b/src/node.ts @@ -23,33 +23,39 @@ const colors: number[] = const inspectOpts: InspectOptions = Object.keys(process.env) .filter((key) => /^debug_/i.test(key)) - .reduce( - (obj, key) => { - // Camel-case - const prop = key - .slice(6) - .toLowerCase() - .replace(/_([a-z])/g, (_, k) => k.toUpperCase()) - - // Coerce string value into JS value - let value: any = process.env[key] - if (value === 'null') { - value = null - // TODO perf: don't use regex - } else if (/^yes|on|true|enabled$/i.test(value)) { - value = true - // TODO perf: don't use regex - } else if (/^no|off|false|disabled$/i.test(value)) { - value = false - } else { - value = Number(value) - } - - obj[prop] = value - return obj - }, - {} as Record, - ) + .reduce>((obj, key) => { + // Camel-case + const prop = key + .slice(6) + .toLowerCase() + .replace(/_([a-z])/g, (_, k) => k.toUpperCase()) + + // Coerce string value into JS value + let value: any = process.env[key] + const lowerCase = typeof value === 'string' && value.toLowerCase() + if (value === 'null') { + value = null + } else if ( + lowerCase === 'yes' || + lowerCase === 'on' || + lowerCase === 'true' || + lowerCase === 'enabled' + ) { + value = true + } else if ( + lowerCase === 'no' || + lowerCase === 'off' || + lowerCase === 'false' || + lowerCase === 'disabled' + ) { + value = false + } else { + value = Number(value) + } + + obj[prop] = value + return obj + }, {}) /** * Is stdout a TTY? Colored output is enabled when `true`. -- 2.51.2