Adjust camelCase with simplified version (#1230)

* Adjust camelCase with simplified version

* Adjust count

* Adjust

* readability

* Ascii-only

* single-line callbacks

* CHANGELOG

* Dedupe string{Lower, Upper}First

* replacer -> fn
This commit is contained in:
Jaco
2021-11-22 16:12:29 +02:00
committed by GitHub
parent b390714285
commit 391ab49b8f
21 changed files with 187 additions and 102 deletions
+32 -3
View File
@@ -1,7 +1,34 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { stringCamelCase } from '.';
import { randomAsNumber } from '@polkadot/util-crypto';
import words from '@polkadot/util-crypto/mnemonic/bip39-en';
import { performanceJs } from '../../test/performance';
import { arrayRange } from '../array';
import { stringCamelCase, stringUpperFirst } from '.';
const SEPS = [' ', '_', '-'];
const randomWords = arrayRange(256).map((): [string] => {
let result = '';
for (let i = 0; i < 6; i++) {
let word = words[randomAsNumber() % words.length];
if (randomAsNumber() % 2) {
word = stringUpperFirst(word);
}
if (i === 0) {
result = word;
} else {
result += `${SEPS[randomAsNumber() % SEPS.length]}${word}`;
}
}
return [result];
});
describe('stringCamelCase', (): void => {
it('works correctly', (): void => {
@@ -12,7 +39,9 @@ describe('stringCamelCase', (): void => {
it('works correctly for String (class', (): void => {
expect(
stringCamelCase(String('Foo_bar-baz---test'))
).toBe('fooBarBazTest');
stringCamelCase(String('Foo_bar-baz---test-_ spaced'))
).toBe('fooBarBazTestSpaced');
});
performanceJs('stringCamelCase', 64000, randomWords, stringCamelCase);
});
+30 -5
View File
@@ -3,12 +3,37 @@
import type { AnyString } from '../types';
import camelcase from 'camelcase';
const RE_WHITE = /[-_ ]+/g;
const RE_WORDS = /(?:^\w|[A-Z]|\b\w)/g;
const RE_JOIN = /\s/g;
// Adapted from https://stackoverflow.com/a/2970667
//
// this is not as optimal as the original (we split into multiple), however
// it does pass the tests (which the original doesn't) and it is still 10+x
// improvement over the original camelcase npm package (at running)
//
// original: 20.88 μs/op
// this: 1.67 μs/op
//
// Caveat of this: only Ascii, but acceptable for the intended usecase
function converter (fn: (w: string, i: number) => string): (value: AnyString) => string {
return (value: AnyString): string =>
value
.toString()
.replace(RE_WHITE, ' ')
.replace(RE_WORDS, fn)
.replace(RE_JOIN, '');
}
/**
* @name stringCamelCase
* @summary Convert a dash/dot/underscore/space separated string/String to camelCase
* @summary Convert a dash/dot/underscore/space separated Ascii string/String to camelCase
*/
export function stringCamelCase (value: AnyString): string {
return camelcase(value.toString());
}
export const stringCamelCase = converter((w, i) => i ? w.toUpperCase() : w.toLowerCase());
/**
* @name stringPascalCase
* @summary Convert a dash/dot/underscore/space separated Ascii string/String to PascalCase
*/
export const stringPascalCase = converter((w) => w.toUpperCase());
+2 -4
View File
@@ -5,10 +5,8 @@
* @summary Utility methods to convert to work with `string` values
*/
export { stringCamelCase } from './camelCase';
export { stringLowerFirst } from './lowerFirst';
export { stringPascalCase } from './pascalCase';
export { stringCamelCase, stringPascalCase } from './camelCase';
export { stringLowerFirst, stringUpperFirst } from './lowerFirst';
export { stringShorten } from './shorten';
export { stringToHex } from './toHex';
export { stringToU8a } from './toU8a';
export { stringUpperFirst } from './upperFirst';
+24 -5
View File
@@ -3,6 +3,13 @@
import type { AnyString } from '../types';
function converter (fn: (value: string) => string): (value?: AnyString | null) => string {
return (value?: AnyString | null): string =>
value
? fn(value[0]) + value.slice(1)
: '';
}
/**
* @name stringLowerFirst
* @summary Lowercase the first letter of a string
@@ -17,8 +24,20 @@ import type { AnyString } from '../types';
* stringLowerFirst('ABC'); // => 'aBC'
* ```
*/
export function stringLowerFirst (value?: AnyString | null): string {
return value
? value.charAt(0).toLowerCase() + value.slice(1)
: '';
}
export const stringLowerFirst = converter((s) => s.toLowerCase());
/**
* @name stringUpperFirst
* @summary Uppercase the first letter of a string
* @description
* Lowercase the first letter of a string
* @example
* <BR>
*
* ```javascript
* import { stringUpperFirst } from '@polkadot/util';
*
* stringUpperFirst('abc'); // => 'Abc'
* ```
*/
export const stringUpperFirst = converter((s) => s.toUpperCase());
+2 -2
View File
@@ -12,7 +12,7 @@ describe('stringPascalCase', (): void => {
it('works correctly for String (class)', (): void => {
expect(
stringPascalCase(String('foo_bar-baz'))
).toBe('FooBarBaz');
stringPascalCase(String('foo_bar-- __baz Bob'))
).toBe('FooBarBazBob');
});
});
-16
View File
@@ -1,16 +0,0 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import camelcase from 'camelcase';
const OPTS = { pascalCase: true };
/**
* @name stringPascalCase
* @summary Convert a dash/dot/underscore/space separated string/String to PascalCase
*/
export function stringPascalCase (value: AnyString): string {
return camelcase(value.toString(), OPTS);
}
+3 -5
View File
@@ -18,9 +18,7 @@ import type { AnyString } from '../types';
* ```
*/
export function stringShorten (value: AnyString, prefixLength = 6): string {
if (value.length <= 2 + 2 * prefixLength) {
return value.toString();
}
return `${value.substr(0, prefixLength)}…${value.slice(-prefixLength)}`;
return value.length <= 2 + 2 * prefixLength
? value.toString()
: `${value.substr(0, prefixLength)}…${value.slice(-prefixLength)}`;
}
-25
View File
@@ -1,25 +0,0 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name stringUpperFirst
* @summary Lowercase the first letter of a string
* @description
* Lowercase the first letter of a string
* @example
* <BR>
*
* ```javascript
* import { stringUpperFirst } from '@polkadot/util';
*
* stringUpperFirst('abc'); // => 'Abc'
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringUpperFirst (value?: AnyString | null): string {
return value
? value.charAt(0).toUpperCase() + value.slice(1)
: '';
}