Skip to content

Instantly share code, notes, and snippets.

@lac5
Created October 10, 2019 18:21
Show Gist options
  • Save lac5/c51053f32e2e529b17827b24d8fceb38 to your computer and use it in GitHub Desktop.
Save lac5/c51053f32e2e529b17827b24d8fceb38 to your computer and use it in GitHub Desktop.
Conversion methods between Base64-URL and standard Base64.
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