Skip to content

Commit efc49d4

Browse files
authored
⚡️ Faster unmap for ulid (#4091)
* ⚡️ Faster unmap for `ulid` * versions
1 parent 096685c commit efc49d4

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

.yarn/versions/04697a61.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
releases:
2+
fast-check: minor
3+
4+
declined:
5+
- "@fast-check/ava"
6+
- "@fast-check/jest"
7+
- "@fast-check/vitest"
8+
- "@fast-check/worker"

packages/fast-check/src/arbitrary/_internals/mappers/UintToBase32String.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const encodeSymbolLookupTable: Record<number, string> = {
2727
/** @internal */
2828
const decodeSymbolLookupTable: Record<string, number> = {
2929
'0': 0,
30-
O: 0,
3130
'1': 1,
3231
'2': 2,
3332
'3': 3,
@@ -106,21 +105,22 @@ export function paddedUintToBase32StringMapper(paddingLength: number) {
106105
};
107106
}
108107

109-
/** @internal */
110-
const Base32Regex = /^[0-9A-HJKMNP-TV-Z]+$/;
111-
112108
/** @internal */
113109
export function uintToBase32StringUnmapper(value: unknown): number {
114110
if (typeof value !== 'string') {
115111
throw new Error('Unsupported type');
116112
}
117113

118-
const normalizedBase32str = value.toUpperCase();
119-
if (!Base32Regex.test(normalizedBase32str)) {
120-
throw new Error('Unsupported type');
114+
let accumulated = 0;
115+
let power = 1;
116+
for (let index = value.length - 1; index >= 0; --index) {
117+
const char = value[index].toUpperCase();
118+
const numericForChar = decodeSymbolLookupTable[char];
119+
if (numericForChar === undefined) {
120+
throw new Error('Unsupported type');
121+
}
122+
accumulated += numericForChar * power;
123+
power *= 32;
121124
}
122-
123-
const symbols = normalizedBase32str.split('').map((char) => decodeSymbolLookupTable[char]);
124-
125-
return symbols.reduce((prev, curr, i) => prev + curr * Math.pow(32, symbols.length - 1 - i), 0);
125+
return accumulated;
126126
}

0 commit comments

Comments
 (0)