From e97410df5244a2de4bd3d0399f8e0aba0135fe6a Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 6 Dec 2021 08:02:03 +0100 Subject: [PATCH] Preserve all-caps word splits in `stringCamelCase` (#1287) * Preserve all-caps word splits in `stringCamelCase` * Additional tests * Additional test * Additional --- CHANGELOG.md | 1 + packages/util/src/string/camelCase.spec.ts | 21 +++++++++++++++++++++ packages/util/src/string/camelCase.ts | 7 +++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7043f3099..d37cb0d51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Changes: - Swap to upstream `@noble/hashes` +- Preserve all-caps word splits in `stringCamelCase` ## 8.1.2 Dec 5, 2021 diff --git a/packages/util/src/string/camelCase.spec.ts b/packages/util/src/string/camelCase.spec.ts index f037a8951..e19c72f33 100644 --- a/packages/util/src/string/camelCase.spec.ts +++ b/packages/util/src/string/camelCase.spec.ts @@ -68,6 +68,27 @@ describe('stringCamelCase', (): void => { expect( stringCamelCase('BLAKE2B') ).toEqual('blake2b'); + expect( + stringCamelCase('NFTOrder') + ).toEqual('nftOrder'); + expect( + stringCamelCase('EVM') + ).toEqual('evm'); + expect( + stringCamelCase('A') + ).toEqual('a'); + expect( + stringCamelCase('A1') + ).toEqual('a1'); + expect( + stringCamelCase('A1b') + ).toEqual('a1b'); + expect( + stringCamelCase('A1B') + ).toEqual('a1b'); + expect( + stringCamelCase('RawVRFOutput') + ).toEqual('rawVRFOutput'); }); it('adjusts with leading _', (): void => { diff --git a/packages/util/src/string/camelCase.ts b/packages/util/src/string/camelCase.ts index 8fec871ec..d5ed2f919 100644 --- a/packages/util/src/string/camelCase.ts +++ b/packages/util/src/string/camelCase.ts @@ -10,7 +10,7 @@ import type { AnyString } from '../types'; // a 10+x improvement over the original camelcase npm package (at running) // // original: 20.88 μs/op -// this: 2.97 μs/op +// this: 2.86 μs/op // // Caveat of this: only Ascii, but acceptable for the intended usecase function converter (fn: (w: string, i: number) => string): (value: AnyString) => string { @@ -32,7 +32,10 @@ function converter (fn: (w: string, i: number) => string): (value: AnyString) => // all full uppercase + letters are changed to lowercase ? w.toLowerCase() // all consecutive capitals + letters are changed to lowercase - : w.replace(/[A-Z]{2,}[0-9]{2,}/g, (w) => w.toLowerCase()), + // e.g. UUID64 -> uuid64, while preserving splits, eg. NFTOrder -> nftOrder + : w.replace(/^[A-Z0-9]{2,}[^a-z]/, (w) => + w.slice(0, w.length - 1).toLowerCase() + w.slice(-1).toUpperCase() + ), i ) )