@allex/base64
Version:
Lightweight performance optimized base64 library by Allex
11 lines (10 loc) • 2.28 kB
JavaScript
/*!
* Lightweight performance optimized base64 library by Allex
*
* Copyright 2019, Allex Wang (https://iallex.com)
*
* Licensed under the MIT license:
* https://opensource.org/licenses/MIT
*/
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).Base64={})}(this,(function(e){"use strict";var r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".split(""),n=String.fromCharCode,t=null,o=function(e){var r,n=-1,t=e.length,o=[];if(/^[\x00-\x7f]*$/.test(e))for(;++n<t;)o.push(e.charCodeAt(n));else for(;++n<t;)(r=e.charCodeAt(n))<128?o.push(r):r<2048?o.push(r>>6|192,63&r|128):o.push(r>>12|224,r>>6&63|128,63&r|128);return o},i=function(e){var n,o=[],i=new Array(4),a=-1;if(!t){for(n=r.length,t={};++a<n;)t[r[a]]=a;a=-1}for(n=e.length;++a<n&&(i[0]=t[e.charAt(a)],i[1]=t[e.charAt(++a)],o.push(i[0]<<2|i[1]>>4),i[2]=t[e.charAt(++a)],64!==i[2])&&(o.push((15&i[1])<<4|i[2]>>2),i[3]=t[e.charAt(++a)],64!==i[3]);)o.push((3&i[2])<<6|i[3]);return o},a=function(e){for(var n,t,i,a=o(e),c=a.length,f=new Array(4),u="",s=-1;++s<c;)n=a[s],t=a[++s],f[0]=n>>2,f[1]=(3&n)<<4|t>>4,isNaN(t)?f[2]=f[3]=64:(i=a[++s],f[2]=(15&t)<<2|i>>6,f[3]=isNaN(i)?64:63&i),u+=r[f[0]]+r[f[1]]+r[f[2]]+r[f[3]];return u},c=function(e,r){return r?a(String(e)).replace(/[+\/]/g,(function(e){return"+"===e?"-":"_"})).replace(/=/g,""):a(String(e))};e.decode=function(e){var r=(e=String(e)).length%4;return r&&(e+=Array(5-r).join("=")),function(e){if((e=e.replace(/\s/g,"")).length%4)throw new Error("InvalidLengthError: decode failed: The string to be decoded is not the correct length for a base64 encoded string.");if(/[^A-Za-z0-9+/=\s]/g.test(e))throw new Error("InvalidCharacterError: decode failed: The string contains characters invalid in a base64 encoded string.");for(var r=i(e),t=r.length,o=0,a="";o<t;)r[o]<128?a+=n(r[o++]):r[o]>191&&r[o]<224?a+=n((31&r[o++])<<6|63&r[o++]):a+=n((15&r[o++])<<12|(63&r[o++])<<6|63&r[o++]);return a}(e.replace(/[-_]/g,(function(e){return"-"===e?"+":"/"})).replace(/[^A-Za-z0-9=\+\/]/g,""))},e.encode=c,e.encodeURI=function(e){return c(e,!0)},e.fromUtf8=i,e.toUtf8=o,Object.defineProperty(e,"__esModule",{value:!0})}));
/* f80d3b81ff881ec2f0788fd5f84e1cf9 */