Use \d in regex (as applicable) (#1539)

* Use \d in regex (as applicable)

* Adjust
This commit is contained in:
Jaco
2022-06-16 17:13:34 +03:00
committed by GitHub
parent d5ce0ff4d1
commit 2dd2f78f7e
4 changed files with 21 additions and 21 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ import type { HexString } from '../types';
import { hexStripPrefix } from './stripPrefix';
const CHARS = '0123456789abcdef';
const UNHEX = new Array<number>(255);
const UNHEX = new Array<number>(256);
for (let i = 0; i < CHARS.length; i++) {
UNHEX[CHARS[i].charCodeAt(0)] = i;
+17 -13
View File
@@ -12,20 +12,24 @@ for (let i = 0; i < CC_TO_UP.length; i++) {
}
/** @internal */
function formatRuns (w: string): string {
function formatAllCaps (w: string): string {
return w.slice(0, w.length - 1).toLowerCase() + CC_TO_UP[w.charCodeAt(w.length - 1)];
}
// Inspired from https://stackoverflow.com/a/2970667
//
// this is not as optimal as the original answer (we split into multiple),
// however it does pass the tests (which the original doesn't) and it is still
// a major improvement over the original camelcase npm package (at running)
//
// original: 20.88 μs/op
// this: 1.00 μs/op
//
// Caveat of this: only Ascii, but acceptable for the intended usecase
/**
* @internal
*
* Inspired by https://stackoverflow.com/a/2970667
*
* This is not as optimal as the original SO answer (we split into per-word),
* however it does pass the tests (which the SO version doesn't) and is still
* a major improvement over the original camelcase npm package -
*
* camelcase: 20.88 μs/op
* this: 1.00 μs/op
*
* Caveat of this: only Ascii, but acceptable for the intended usecase
*/
function converter (format: (w: string, i: number) => string): (value: AnyString) => string {
return (value: AnyString): string => {
const parts = value
@@ -43,12 +47,12 @@ function converter (format: (w: string, i: number) => string): (value: AnyString
// apply the formatting
result += format(
/^[A-Z0-9]+$/.test(w)
/^[\dA-Z]+$/.test(w)
// all full uppercase + letters are changed to lowercase
? w.toLowerCase()
// all consecutive capitals + letters are changed to lowercase
// e.g. UUID64 -> uuid64, while preserving splits, eg. NFTOrder -> nftOrder
: w.replace(/^[A-Z0-9]{2,}[^a-z]/, formatRuns),
: w.replace(/^[\dA-Z]{2,}[^a-z]/, formatAllCaps),
i
);
}
+1
View File
@@ -5,6 +5,7 @@ import type { AnyString } from '../types';
import { CC_TO_LO, CC_TO_UP } from './camelCase';
/** @internal */
function converter (map: string[]): (value?: AnyString | null) => string {
return (value?: AnyString | null): string =>
value
+2 -7
View File
@@ -7,19 +7,14 @@ const U8 = new Array<string>(256);
const U16 = new Array<string>(256 * 256);
for (let n = 0; n < 256; n++) {
const hex = n.toString(16).padStart(2, '0');
U8[n] = hex;
U8[n] = n.toString(16).padStart(2, '0');
}
for (let i = 0; i < 256; i++) {
const s = i << 8;
for (let j = 0; j < 256; j++) {
const hex = U8[i] + U8[j];
const n = s | j;
U16[n] = hex;
U16[s | j] = U8[i] + U8[j];
}
}