@@ -2,48 +2,6 @@ export function hasNumberPrefix(value: string) {
2
2
return ( value ?? '' ) . trim ( ) . match ( / ^ ( 0 [ x o b ] .| & [ h o b ] .) / i) ;
3
3
}
4
4
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 | 0 b ) ? ( [ 0 1 ] + ) ( [ I U L Z n ] * ) $ / i } ,
27
- { base : 8 , regex : / ^ ( & o | 0 o ) ? ( [ 0 - 7 ] + ) ( [ I U L Z n ] * ) $ / i } ,
28
- { base : 16 , regex : / ^ ( & h | 0 x ) ? ( [ a - f 0 - 9 ] + ) ( [ I U L Z n ] * ) $ / 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 [ x o b ] .| & [ h o b ] .) / i) ;
45
- }
46
-
47
5
export function convertBase (
48
6
{
49
7
value, fromBase, toBase,
@@ -84,26 +42,20 @@ export function convertBase(
84
42
}
85
43
const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/' . split ( '' ) ;
86
44
const fromRange = range . slice ( 0 , finalFromBase ) ;
87
- const fromRange = range . slice ( 0 , finalFromBase ) ;
88
45
const toRange = range . slice ( 0 , toBase ) ;
89
- let decValue = cleanedValue
90
46
let decValue = cleanedValue
91
47
. split ( '' )
92
48
. 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 ) => {
95
50
if ( ! fromRange . includes ( digit ) ) {
96
51
throw new Error ( `Invalid digit "${ digit } " for base ${ finalFromBase } .` ) ;
97
- throw new Error ( `Invalid digit "${ digit } " for base ${ finalFromBase } .` ) ;
98
52
}
99
- return ( carry += BigInt ( fromRange . indexOf ( digit ) ) * BigInt ( fromBase ) ** BigInt ( index ) ) ;
100
- } , 0n ) ;
53
+ return ( carry += fromRange . indexOf ( digit ) * finalFromBase ** index ) ;
54
+ } , 0 ) ;
101
55
let newValue = '' ;
102
56
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 ;
107
59
}
108
60
return newValue || '0' ;
109
61
}
0 commit comments