@@ -2,6 +2,48 @@ 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
+
5
47
export function convertBase (
6
48
{
7
49
value, fromBase, toBase,
@@ -42,20 +84,26 @@ export function convertBase(
42
84
}
43
85
const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/' . split ( '' ) ;
44
86
const fromRange = range . slice ( 0 , finalFromBase ) ;
87
+ const fromRange = range . slice ( 0 , finalFromBase ) ;
45
88
const toRange = range . slice ( 0 , toBase ) ;
89
+ let decValue = cleanedValue
46
90
let decValue = cleanedValue
47
91
. split ( '' )
48
92
. reverse ( )
93
+ . reduce ( ( carry : bigint , digit : string , index : number ) => {
49
94
. reduce ( ( carry : bigint , digit : string , index : number ) => {
50
95
if ( ! fromRange . includes ( digit ) ) {
51
96
throw new Error ( `Invalid digit "${ digit } " for base ${ finalFromBase } .` ) ;
97
+ throw new Error ( `Invalid digit "${ digit } " for base ${ finalFromBase } .` ) ;
52
98
}
53
99
return ( carry += BigInt ( fromRange . indexOf ( digit ) ) * BigInt ( fromBase ) ** BigInt ( index ) ) ;
54
100
} , 0n ) ;
55
101
let newValue = '' ;
56
102
while ( decValue > 0 ) {
57
103
newValue = toRange [ Number ( decValue % BigInt ( toBase ) ) ] + newValue ;
58
104
decValue = ( decValue - ( decValue % BigInt ( toBase ) ) ) / BigInt ( toBase ) ;
105
+ newValue = toRange [ Number ( decValue % BigInt ( toBase ) ) ] + newValue ;
106
+ decValue = ( decValue - ( decValue % BigInt ( toBase ) ) ) / BigInt ( toBase ) ;
59
107
}
60
108
return newValue || '0' ;
61
109
}
0 commit comments