Last active
August 2, 2019 11:41
-
-
Save dchest/13c9a1a60238dafb45eea925e7a0414a to your computer and use it in GitHub Desktop.
Short Utils
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
// String ranges: strange('A-Za-z0-9+/') -> "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy012345678+/" | |
const strange = s => s.replace(/(.)-(.)/g, (_, x, y) => { for (var i = x.charCodeAt(0) + 1; i < y.charCodeAt(0); i++) x += String.fromCharCode(i); return x }) | |
// Base64 encoding of Uint8Array of ArrayBuffer and decoding to Uint8Array | |
const encodeBase64 = a => btoa(Array.from(a instanceof ArrayBuffer ? new Uint8Array(a) : a).map(x => String.fromCharCode(x)).join('')); | |
const encodeBase64url = a => encodeBase64(a).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); // no padding | |
const decodeBase64 = s => Uint8Array.from(atob(s), x => x.charCodeAt(0)); | |
// TODO: decodeBase64url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment