Skip to content

Instantly share code, notes, and snippets.

@dchest
Last active August 2, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchest/13c9a1a60238dafb45eea925e7a0414a to your computer and use it in GitHub Desktop.
Save dchest/13c9a1a60238dafb45eea925e7a0414a to your computer and use it in GitHub Desktop.
Short Utils
// 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