Preserve all-caps word splits in stringCamelCase (#1287)

* Preserve all-caps word splits in `stringCamelCase`

* Additional tests

* Additional test

* Additional
This commit is contained in:
Jaco
2021-12-06 09:02:03 +02:00
committed by GitHub
parent faa6734329
commit e97410df52
3 changed files with 27 additions and 2 deletions
@@ -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 => {
+5 -2
View File
@@ -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
)
)