Skip to content

Commit c89762f

Browse files
committed
Merge branch 'fix/integer-base-converter-bigint' into chore/all-my-stuffs
# Conflicts: # src/tools/integer-base-converter/integer-base-converter.model.test.ts # src/tools/integer-base-converter/integer-base-converter.model.ts
2 parents 2ce4d1b + 6749d81 commit c89762f

File tree

2 files changed

+11
-59
lines changed

2 files changed

+11
-59
lines changed

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/integer-base-converter/integer-base-converter.model.ts

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,6 @@ export function hasNumberPrefix(value: string) {
22
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
33
}
44

5-
export function convertBase(
6-
{
7-
value, fromBase, toBase,
8-
ignorePunctuationsRegexChars = ' \u00A0_\.,-',
9-
handlePrefixSuffix = true,
10-
ignoreCase = true,
11-
}: {
12-
value: string
13-
fromBase: number
14-
toBase: number
15-
ignorePunctuationsRegexChars?: string
16-
handlePrefixSuffix?: boolean
17-
ignoreCase?: boolean
18-
}) {
19-
let cleanedValue = (value ?? '0').trim();
20-
if (ignorePunctuationsRegexChars) {
21-
cleanedValue = cleanedValue.replace(new RegExp(`[${ignorePunctuationsRegexChars}]`, 'g'), '');
22-
}
23-
let finalFromBase = fromBase;
24-
if (handlePrefixSuffix) {
25-
for (const regBase of [
26-
{ base: 2, regex: /^(&b|0b)?([01]+)([IULZn]*)$/i },
27-
{ base: 8, regex: /^(&o|0o)?([0-7]+)([IULZn]*)$/i },
28-
{ base: 16, regex: /^(&h|0x)?([a-f0-9]+)([IULZn]*)$/i },
29-
]) {
30-
const match = cleanedValue.match(regBase.regex);
31-
if (match) {
32-
if (match[1]) {
33-
finalFromBase = regBase.base;
34-
}
35-
cleanedValue = match[2];
36-
break;
37-
}
38-
}
39-
}
40-
if (ignoreCase && finalFromBase <= 36) {
41-
cleanedValue = cleanedValue.toLowerCase();
42-
}
43-
export function hasNumberPrefix(value: string) {
44-
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
45-
}
46-
475
export function convertBase(
486
{
497
value, fromBase, toBase,
@@ -84,26 +42,20 @@ export function convertBase(
8442
}
8543
const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split('');
8644
const fromRange = range.slice(0, finalFromBase);
87-
const fromRange = range.slice(0, finalFromBase);
8845
const toRange = range.slice(0, toBase);
89-
let decValue = cleanedValue
9046
let decValue = cleanedValue
9147
.split('')
9248
.reverse()
93-
.reduce((carry: bigint, digit: string, index: number) => {
94-
.reduce((carry: bigint, digit: string, index: number) => {
49+
.reduce((carry: number, digit: string, index: number) => {
9550
if (!fromRange.includes(digit)) {
9651
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
97-
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
9852
}
99-
return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(fromBase) ** BigInt(index));
100-
}, 0n);
53+
return (carry += fromRange.indexOf(digit) * finalFromBase ** index);
54+
}, 0);
10155
let newValue = '';
10256
while (decValue > 0) {
103-
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
104-
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
105-
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
106-
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
57+
newValue = toRange[decValue % toBase] + newValue;
58+
decValue = (decValue - (decValue % toBase)) / toBase;
10759
}
10860
return newValue || '0';
10961
}

0 commit comments

Comments
 (0)