Skip to content

Commit 0592b1f

Browse files
committed
Code refactoring
1 parent 28e21c4 commit 0592b1f

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
const runningOnBrowser = typeof window !== 'undefined'
22

3-
// Pad out with standard base64 required padding characters if missing
4-
const base64AddPadding = base64String => {
5-
const missingPadding = '='.repeat((4 - base64String.length % 4) % 4)
6-
return base64String + missingPadding
7-
}
8-
9-
// Replace non-url compatible chars with base64 standard chars
10-
const base64UrlDecode = base64UrlString => {
11-
return base64UrlString
3+
const base64urlToBase64 = base64UrlString => {
4+
// Replace non-url compatible chars with base64 standard chars
5+
const base64 = base64UrlString
126
.replace(/-/g, '+')
137
.replace(/_/g, '/')
8+
9+
// Pad out with standard base64 required padding characters if missing
10+
const missingPadding = '='.repeat((4 - base64.length % 4) % 4)
11+
12+
return base64 + missingPadding
1413
}
1514

16-
const atob = data => {
15+
const base64Decode = data => {
1716
if (runningOnBrowser) {
18-
return window.atob(data)
17+
return atob(data)
1918
}
2019
// atob polyfill for Node
2120
return Buffer.from(data, 'base64').toString('binary')
2221
}
2322

2423
const base64ToUint8Array = base64String => {
2524
// base64 sanitizing
26-
const base64 = base64UrlDecode(base64AddPadding(base64String))
25+
const base64 = base64urlToBase64(base64String)
2726

2827
// base64 decoding
29-
const rawData = atob(base64)
28+
const rawData = base64Decode(base64)
3029

3130
// Converting raw data to Uint8Array
3231
return Uint8Array.from(rawData, char => char.charCodeAt(0))

0 commit comments

Comments
 (0)