Skip to content

Commit

Permalink
refactor: shrink several bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Dec 21, 2024
1 parent a01f594 commit ee515e8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
proxyOverride,
quote,
quotePowerShell,
snakeToCamel,
toCamelCase,
} from './util.js'

const CWD = Symbol('processCwd')
Expand Down Expand Up @@ -929,7 +929,7 @@ export function resolveDefaults(

return Object.entries(env).reduce<Options>((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
}
Expand Down
4 changes: 2 additions & 2 deletions src/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
isStringLiteral,
parseBool,
parseDuration,
snakeToCamel,
toCamelCase,
} from './util.js'
import {
chalk,
Expand All @@ -42,7 +42,7 @@ export const parseArgv = (
): minimist.ParsedArgs =>
Object.entries(minimist(args, opts)).reduce<minimist.ParsedArgs>(
(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
Expand Down
8 changes: 1 addition & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,7 @@ export const proxyOverride = <T extends object>(
},
}) 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()
})
Expand Down
21 changes: 6 additions & 15 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import {
tempdir,
tempfile,
preferLocalBin,
camelToSnake,
snakeToCamel,
toCamelCase,
} from '../build/util.js'

describe('util', () => {
Expand Down Expand Up @@ -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')
})
})

0 comments on commit ee515e8

Please sign in to comment.