mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
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
This commit is contained in:
commit
c89762f129
2 files changed
+11
-59
No files matched your search
Generated
+6
-6
@@ -1058,9 +1058,9 @@ packages:
|
||||
resolution: {integrity: sha512-+lcUbnTRhd0jOewtFSedLyiPsD5tswKkbgcezOqqWFUVNEwoUTlpPOBmvhG7OXWLR4jMdv0czPGH5XbflnD1EA==}
|
||||
engines: {node: '>=6.9.0'}
|
||||
dependencies:
|
||||
'@babel/types': 7.23.0
|
||||
'@babel/types': 7.22.5
|
||||
'@jridgewell/gen-mapping': 0.3.3
|
||||
'@jridgewell/trace-mapping': 0.3.20
|
||||
'@jridgewell/trace-mapping': 0.3.18
|
||||
jsesc: 2.5.2
|
||||
dev: true
|
||||
|
||||
@@ -1195,8 +1195,8 @@ packages:
|
||||
resolution: {integrity: sha512-wtHSq6jMRE3uF2otvfuD3DIvVhOsSNshQl0Qrd7qC9oQJzHvOL4qQXlQn2916+CXGywIjpGuIkoyZRRxHPiNQQ==}
|
||||
engines: {node: '>=6.9.0'}
|
||||
dependencies:
|
||||
'@babel/template': 7.22.15
|
||||
'@babel/types': 7.23.0
|
||||
'@babel/template': 7.22.5
|
||||
'@babel/types': 7.22.5
|
||||
dev: true
|
||||
|
||||
/@babel/helper-function-name@7.23.0:
|
||||
@@ -1210,7 +1210,7 @@ packages:
|
||||
resolution: {integrity: sha512-wGjk9QZVzvknA6yKIUURb8zY3grXCcOZt+/7Wcy8O2uctxhplmUPkOdlgoNhmdVee2c92JXbf1xpMtVNbfoxRw==}
|
||||
engines: {node: '>=6.9.0'}
|
||||
dependencies:
|
||||
'@babel/types': 7.23.0
|
||||
'@babel/types': 7.22.5
|
||||
|
||||
/@babel/helper-member-expression-to-functions@7.22.5:
|
||||
resolution: {integrity: sha512-aBiH1NKMG0H2cGZqspNvsaBe6wNGjbJjuLy29aU+eDZjSbbN53BaxlpB02xm9v34pLTZ1nIQPFYn2qMZoa5BQQ==}
|
||||
@@ -1330,7 +1330,7 @@ packages:
|
||||
resolution: {integrity: sha512-thqK5QFghPKWLhAV321lxF95yCg2K3Ob5yw+M3VHWfdia0IkPXUtoLH8x/6Fh486QUvzhb8YOWHChTVen2/PoQ==}
|
||||
engines: {node: '>=6.9.0'}
|
||||
dependencies:
|
||||
'@babel/types': 7.23.0
|
||||
'@babel/types': 7.22.5
|
||||
dev: true
|
||||
|
||||
/@babel/helper-split-export-declaration@7.22.6:
|
||||
|
||||
@@ -2,48 +2,6 @@ export function hasNumberPrefix(value: string) {
|
||||
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
|
||||
}
|
||||
|
||||
export function convertBase(
|
||||
{
|
||||
value, fromBase, toBase,
|
||||
ignorePunctuationsRegexChars = ' \u00A0_\.,-',
|
||||
handlePrefixSuffix = true,
|
||||
ignoreCase = true,
|
||||
}: {
|
||||
value: string
|
||||
fromBase: number
|
||||
toBase: number
|
||||
ignorePunctuationsRegexChars?: string
|
||||
handlePrefixSuffix?: boolean
|
||||
ignoreCase?: boolean
|
||||
}) {
|
||||
let cleanedValue = (value ?? '0').trim();
|
||||
if (ignorePunctuationsRegexChars) {
|
||||
cleanedValue = cleanedValue.replace(new RegExp(`[${ignorePunctuationsRegexChars}]`, 'g'), '');
|
||||
}
|
||||
let finalFromBase = fromBase;
|
||||
if (handlePrefixSuffix) {
|
||||
for (const regBase of [
|
||||
{ base: 2, regex: /^(&b|0b)?([01]+)([IULZn]*)$/i },
|
||||
{ base: 8, regex: /^(&o|0o)?([0-7]+)([IULZn]*)$/i },
|
||||
{ base: 16, regex: /^(&h|0x)?([a-f0-9]+)([IULZn]*)$/i },
|
||||
]) {
|
||||
const match = cleanedValue.match(regBase.regex);
|
||||
if (match) {
|
||||
if (match[1]) {
|
||||
finalFromBase = regBase.base;
|
||||
}
|
||||
cleanedValue = match[2];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ignoreCase && finalFromBase <= 36) {
|
||||
cleanedValue = cleanedValue.toLowerCase();
|
||||
}
|
||||
export function hasNumberPrefix(value: string) {
|
||||
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
|
||||
}
|
||||
|
||||
export function convertBase(
|
||||
{
|
||||
value, fromBase, toBase,
|
||||
@@ -84,26 +42,20 @@ export function convertBase(
|
||||
}
|
||||
const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split('');
|
||||
const fromRange = range.slice(0, finalFromBase);
|
||||
const fromRange = range.slice(0, finalFromBase);
|
||||
const toRange = range.slice(0, toBase);
|
||||
let decValue = cleanedValue
|
||||
let decValue = cleanedValue
|
||||
.split('')
|
||||
.reverse()
|
||||
.reduce((carry: bigint, digit: string, index: number) => {
|
||||
.reduce((carry: bigint, digit: string, index: number) => {
|
||||
.reduce((carry: number, digit: string, index: number) => {
|
||||
if (!fromRange.includes(digit)) {
|
||||
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
|
||||
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
|
||||
}
|
||||
return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(fromBase) ** BigInt(index));
|
||||
}, 0n);
|
||||
return (carry += fromRange.indexOf(digit) * finalFromBase ** index);
|
||||
}, 0);
|
||||
let newValue = '';
|
||||
while (decValue > 0) {
|
||||
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
|
||||
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
|
||||
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
|
||||
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
|
||||
newValue = toRange[decValue % toBase] + newValue;
|
||||
decValue = (decValue - (decValue % toBase)) / toBase;
|
||||
}
|
||||
return newValue || '0';
|
||||
}
|
||||
Reference in new issue
Block a user