Created
October 10, 2019 18:21
-
-
Save lac5/c51053f32e2e529b17827b24d8fceb38 to your computer and use it in GitHub Desktop.
Conversion methods between Base64-URL and standard Base64.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function base64url(base64) { | |
return String(base64).replace(/[^A-Za-z0-9_-]/g, m => | |
m === '+' ? '-' : | |
m === '/' ? '_' : | |
'' | |
); | |
} | |
export function base64standard(base64) { | |
base64 = String(base64).replace(/[^A-Za-z0-9+/]/g, m => | |
m === '-' ? '+' : | |
m === '_' ? '/' : | |
'' | |
); | |
switch (base64.length % 4) { | |
case 1: return base64 + '==='; | |
case 2: return base64 + '=='; | |
case 3: return base64 + '='; | |
default: return base64; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment