FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Conversions/ArbitraryBase.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Conversions
/
ArbitraryBase.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
119 lines (111 loc) · 3.91 KB
Breadcrumbs
JavaScript
/
Conversions
/
ArbitraryBase.js
Copy path
File metadata and controls
119 lines (111 loc) · 3.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Divide two numbers and get the result of floor division and remainder
*
@param
{
number
} dividend
*
@param
{
number
} divisor
*
@returns
{
[result: number, remainder: number]
}
*/
const
floorDiv
=
(
dividend
,
divisor
)
=>
{
const
remainder
=
dividend
%
divisor
const
result
=
Math
.
floor
(
dividend
/
divisor
)
return
[
result
,
remainder
]
}
/**
* Converts a string from one base to other. Loses accuracy above the value of `Number.MAX_SAFE_INTEGER`.
*
@param
{
string
} stringInBaseOne String in input base
*
@param
{
string
} baseOneCharacters Character set for the input base
*
@param
{
string
} baseTwoCharacters Character set for the output base
*
@returns
{
string
}
*/
const
convertArbitraryBase
=
(
stringInBaseOne
,
baseOneCharacterString
,
baseTwoCharacterString
)
=>
{
if
(
[
stringInBaseOne
,
baseOneCharacterString
,
baseTwoCharacterString
]
.
map
(
(
arg
)
=>
typeof
arg
)
.
some
(
(
type
)
=>
type
!==
'string'
)
)
{
throw
new
TypeError
(
'Only string arguments are allowed'
)
}
const
baseOneCharacters
=
[
...
baseOneCharacterString
]
const
baseTwoCharacters
=
[
...
baseTwoCharacterString
]
for
(
const
charactersInBase
of
[
baseOneCharacters
,
baseTwoCharacters
]
)
{
if
(
charactersInBase
.
length
!==
new
Set
(
charactersInBase
)
.
size
)
{
throw
new
TypeError
(
'Duplicate characters in character set are not allowed'
)
}
}
const
reversedStringOneChars
=
[
...
stringInBaseOne
]
.
reverse
(
)
const
stringOneBase
=
baseOneCharacters
.
length
let
value
=
0
let
placeValue
=
1
for
(
const
digit
of
reversedStringOneChars
)
{
const
digitNumber
=
baseOneCharacters
.
indexOf
(
digit
)
if
(
digitNumber
===
-
1
)
{
throw
new
TypeError
(
`Not a valid character:
${
digit
}
`
)
}
value
+=
digitNumber
*
placeValue
placeValue
*=
stringOneBase
}
const
outputChars
=
[
]
const
stringTwoBase
=
baseTwoCharacters
.
length
while
(
value
>
0
)
{
const
[
divisionResult
,
remainder
]
=
floorDiv
(
value
,
stringTwoBase
)
outputChars
.
push
(
baseTwoCharacters
[
remainder
]
)
value
=
divisionResult
}
return
outputChars
.
reverse
(
)
.
join
(
''
)
||
baseTwoCharacters
[
0
]
}
/**
* Converts a arbitrary-length string from one base to other. Doesn't lose accuracy.
*
@param
{
string
} stringInBaseOne String in input base
*
@param
{
string
} baseOneCharacters Character set for the input base
*
@param
{
string
} baseTwoCharacters Character set for the output base
*
@returns
{
string
}
*/
const
convertArbitraryBaseBigIntVersion
=
(
stringInBaseOne
,
baseOneCharacterString
,
baseTwoCharacterString
)
=>
{
if
(
[
stringInBaseOne
,
baseOneCharacterString
,
baseTwoCharacterString
]
.
map
(
(
arg
)
=>
typeof
arg
)
.
some
(
(
type
)
=>
type
!==
'string'
)
)
{
throw
new
TypeError
(
'Only string arguments are allowed'
)
}
const
baseOneCharacters
=
[
...
baseOneCharacterString
]
const
baseTwoCharacters
=
[
...
baseTwoCharacterString
]
for
(
const
charactersInBase
of
[
baseOneCharacters
,
baseTwoCharacters
]
)
{
if
(
charactersInBase
.
length
!==
new
Set
(
charactersInBase
)
.
size
)
{
throw
new
TypeError
(
'Duplicate characters in character set are not allowed'
)
}
}
const
reversedStringOneChars
=
[
...
stringInBaseOne
]
.
reverse
(
)
const
stringOneBase
=
BigInt
(
baseOneCharacters
.
length
)
let
value
=
0n
let
placeValue
=
1n
for
(
const
digit
of
reversedStringOneChars
)
{
const
digitNumber
=
BigInt
(
baseOneCharacters
.
indexOf
(
digit
)
)
if
(
digitNumber
===
-
1n
)
{
throw
new
TypeError
(
`Not a valid character:
${
digit
}
`
)
}
value
+=
digitNumber
*
placeValue
placeValue
*=
stringOneBase
}
const
outputChars
=
[
]
const
stringTwoBase
=
BigInt
(
baseTwoCharacters
.
length
)
while
(
value
>
0n
)
{
const
divisionResult
=
value
/
stringTwoBase
const
remainder
=
value
%
stringTwoBase
outputChars
.
push
(
baseTwoCharacters
[
remainder
]
)
value
=
divisionResult
}
return
outputChars
.
reverse
(
)
.
join
(
''
)
||
baseTwoCharacters
[
0
]
}
export
{
convertArbitraryBase
,
convertArbitraryBaseBigIntVersion
}
Back
|
FazBrowse Home
|
New Git URL