houdini/app/javascript/legacy/components/b64.js

14 lines
571 B
JavaScript
Raw Normal View History

2019-11-06 20:36:28 +00:00
// License: LGPL-3.0-or-later
// see https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
// used for encoded and decoding data for email text
module.exports = {
encode: str =>
btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g
, (match, p1) => String.fromCharCode('0x' + p1))).replace(/\//g,'_').replace(/\+/g,'-')
, decode: str =>
decodeURIComponent(Array.prototype.map.call(atob(str.replace(/-/g, '+').replace(/_/g, '/'))
, c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''))
}