From ee515e84629e78a6d92890b95ea6d31d29e05b08 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sun, 22 Dec 2024 00:57:37 +0300 Subject: [PATCH] refactor: shrink several bytes --- .size-limit.json | 4 ++-- src/core.ts | 4 ++-- src/goods.ts | 4 ++-- src/util.ts | 8 +------- test/util.test.js | 21 ++++++--------------- 5 files changed, 13 insertions(+), 28 deletions(-) diff --git a/.size-limit.json b/.size-limit.json index 14c81174b3..27e6430280 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -2,14 +2,14 @@ { "name": "zx/core", "path": ["build/core.cjs", "build/util.cjs", "build/vendor-core.cjs"], - "limit": "76 kB", + "limit": "75 kB", "brotli": false, "gzip": false }, { "name": "zx/index", "path": "build/*.{js,cjs}", - "limit": "804 kB", + "limit": "803 kB", "brotli": false, "gzip": false }, diff --git a/src/core.ts b/src/core.ts index e0c3d13299..d0f2a12d02 100644 --- a/src/core.ts +++ b/src/core.ts @@ -56,7 +56,7 @@ import { proxyOverride, quote, quotePowerShell, - snakeToCamel, + toCamelCase, } from './util.js' const CWD = Symbol('processCwd') @@ -929,7 +929,7 @@ export function resolveDefaults( return Object.entries(env).reduce((m, [k, v]) => { if (v && k.startsWith(prefix)) { - const _k = snakeToCamel(k.slice(prefix.length)) + const _k = toCamelCase(k.slice(prefix.length)) const _v = parseBool(v) if (allowed.has(_k)) (m as any)[_k] = _v } diff --git a/src/goods.ts b/src/goods.ts index 1a5888fe70..476b991b2b 100644 --- a/src/goods.ts +++ b/src/goods.ts @@ -21,7 +21,7 @@ import { isStringLiteral, parseBool, parseDuration, - snakeToCamel, + toCamelCase, } from './util.js' import { chalk, @@ -42,7 +42,7 @@ export const parseArgv = ( ): minimist.ParsedArgs => Object.entries(minimist(args, opts)).reduce( (m, [k, v]) => { - const kTrans = opts.camelCase ? snakeToCamel : identity + const kTrans = opts.camelCase ? toCamelCase : identity const vTrans = opts.parseBoolean ? parseBool : identity const [_k, _v] = k === '--' || k === '_' ? [k, v] : [kTrans(k), vTrans(v)] m[_k] = _v diff --git a/src/util.ts b/src/util.ts index 7296e0a24e..68208e4302 100644 --- a/src/util.ts +++ b/src/util.ts @@ -286,13 +286,7 @@ export const proxyOverride = ( }, }) as T -export const camelToSnake = (str: string) => - str - .split(/(?=[A-Z])/) - .map((s) => s.toUpperCase()) - .join('_') - -export const snakeToCamel = (str: string) => +export const toCamelCase = (str: string) => str.toLowerCase().replace(/([a-z])[_-]+([a-z])/g, (_, p1, p2) => { return p1 + p2.toUpperCase() }) diff --git a/test/util.test.js b/test/util.test.js index e7d10e3e81..7eef6914dd 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -28,8 +28,7 @@ import { tempdir, tempfile, preferLocalBin, - camelToSnake, - snakeToCamel, + toCamelCase, } from '../build/util.js' describe('util', () => { @@ -133,18 +132,10 @@ describe('util', () => { ) }) - test('camelToSnake()', () => { - assert.equal(camelToSnake('verbose'), 'VERBOSE') - assert.equal(camelToSnake('nothrow'), 'NOTHROW') - assert.equal(camelToSnake('preferLocal'), 'PREFER_LOCAL') - assert.equal(camelToSnake('someMoreBigStr'), 'SOME_MORE_BIG_STR') - }) - - test('snakeToCamel()', () => { - assert.equal(snakeToCamel('VERBOSE'), 'verbose') - assert.equal(snakeToCamel('NOTHROW'), 'nothrow') - assert.equal(snakeToCamel('PREFER_LOCAL'), 'preferLocal') - assert.equal(snakeToCamel('SOME_MORE_BIG_STR'), 'someMoreBigStr') - assert.equal(snakeToCamel('kebab-input-str'), 'kebabInputStr') + test('toCamelCase()', () => { + assert.equal(toCamelCase('VERBOSE'), 'verbose') + assert.equal(toCamelCase('PREFER_LOCAL'), 'preferLocal') + assert.equal(toCamelCase('SOME_MORE_BIG_STR'), 'someMoreBigStr') + assert.equal(toCamelCase('kebab-input-str'), 'kebabInputStr') }) })