Skip to content

Commit ee515e8

Browse files
committed
refactor: shrink several bytes
1 parent a01f594 commit ee515e8

File tree

5 files changed

+13
-28
lines changed

5 files changed

+13
-28
lines changed

.size-limit.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
{
33
"name": "zx/core",
44
"path": ["build/core.cjs", "build/util.cjs", "build/vendor-core.cjs"],
5-
"limit": "76 kB",
5+
"limit": "75 kB",
66
"brotli": false,
77
"gzip": false
88
},
99
{
1010
"name": "zx/index",
1111
"path": "build/*.{js,cjs}",
12-
"limit": "804 kB",
12+
"limit": "803 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},

src/core.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import {
5656
proxyOverride,
5757
quote,
5858
quotePowerShell,
59-
snakeToCamel,
59+
toCamelCase,
6060
} from './util.js'
6161

6262
const CWD = Symbol('processCwd')
@@ -929,7 +929,7 @@ export function resolveDefaults(
929929

930930
return Object.entries(env).reduce<Options>((m, [k, v]) => {
931931
if (v && k.startsWith(prefix)) {
932-
const _k = snakeToCamel(k.slice(prefix.length))
932+
const _k = toCamelCase(k.slice(prefix.length))
933933
const _v = parseBool(v)
934934
if (allowed.has(_k)) (m as any)[_k] = _v
935935
}

src/goods.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
isStringLiteral,
2222
parseBool,
2323
parseDuration,
24-
snakeToCamel,
24+
toCamelCase,
2525
} from './util.js'
2626
import {
2727
chalk,
@@ -42,7 +42,7 @@ export const parseArgv = (
4242
): minimist.ParsedArgs =>
4343
Object.entries(minimist(args, opts)).reduce<minimist.ParsedArgs>(
4444
(m, [k, v]) => {
45-
const kTrans = opts.camelCase ? snakeToCamel : identity
45+
const kTrans = opts.camelCase ? toCamelCase : identity
4646
const vTrans = opts.parseBoolean ? parseBool : identity
4747
const [_k, _v] = k === '--' || k === '_' ? [k, v] : [kTrans(k), vTrans(v)]
4848
m[_k] = _v

src/util.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,7 @@ export const proxyOverride = <T extends object>(
286286
},
287287
}) as T
288288

289-
export const camelToSnake = (str: string) =>
290-
str
291-
.split(/(?=[A-Z])/)
292-
.map((s) => s.toUpperCase())
293-
.join('_')
294-
295-
export const snakeToCamel = (str: string) =>
289+
export const toCamelCase = (str: string) =>
296290
str.toLowerCase().replace(/([a-z])[_-]+([a-z])/g, (_, p1, p2) => {
297291
return p1 + p2.toUpperCase()
298292
})

test/util.test.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ import {
2828
tempdir,
2929
tempfile,
3030
preferLocalBin,
31-
camelToSnake,
32-
snakeToCamel,
31+
toCamelCase,
3332
} from '../build/util.js'
3433

3534
describe('util', () => {
@@ -133,18 +132,10 @@ describe('util', () => {
133132
)
134133
})
135134

136-
test('camelToSnake()', () => {
137-
assert.equal(camelToSnake('verbose'), 'VERBOSE')
138-
assert.equal(camelToSnake('nothrow'), 'NOTHROW')
139-
assert.equal(camelToSnake('preferLocal'), 'PREFER_LOCAL')
140-
assert.equal(camelToSnake('someMoreBigStr'), 'SOME_MORE_BIG_STR')
141-
})
142-
143-
test('snakeToCamel()', () => {
144-
assert.equal(snakeToCamel('VERBOSE'), 'verbose')
145-
assert.equal(snakeToCamel('NOTHROW'), 'nothrow')
146-
assert.equal(snakeToCamel('PREFER_LOCAL'), 'preferLocal')
147-
assert.equal(snakeToCamel('SOME_MORE_BIG_STR'), 'someMoreBigStr')
148-
assert.equal(snakeToCamel('kebab-input-str'), 'kebabInputStr')
135+
test('toCamelCase()', () => {
136+
assert.equal(toCamelCase('VERBOSE'), 'verbose')
137+
assert.equal(toCamelCase('PREFER_LOCAL'), 'preferLocal')
138+
assert.equal(toCamelCase('SOME_MORE_BIG_STR'), 'someMoreBigStr')
139+
assert.equal(toCamelCase('kebab-input-str'), 'kebabInputStr')
149140
})
150141
})

0 commit comments

Comments
 (0)