@cnwhy/base64
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,5 +0,1 @@ | ||
/*! | ||
* Base64.js | ||
* https://github.com/cnwhy/Base64.js#readme | ||
*/ | ||
const BASE64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(''); | ||
@@ -9,2 +5,18 @@ const BASE64_URL_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'.split(''); | ||
const ERR_CODE = '\ufffd'; | ||
// poliyfill | ||
const isArray = | ||
Array.isArray || | ||
function(obj) { | ||
Object.prototype.toString.call(obj) == '[object Array]'; | ||
}; | ||
const hasArrayBuffer = typeof ArrayBuffer === 'function'; | ||
const MyArrayBuffer = hasArrayBuffer ? ArrayBuffer : function() {}; | ||
const myUint8arrayClass = hasArrayBuffer ? Uint8Array : Array; | ||
const getUint8Array = hasArrayBuffer | ||
? function(arr: any) { | ||
return new Uint8Array(arr); | ||
} | ||
: function(arr: any) { | ||
return typeof arr === 'number' ? new Array(arr) : arr; | ||
}; | ||
@@ -35,6 +47,6 @@ function u2utf8(codePoint: number): number[] { | ||
*/ | ||
function utf8Encode(str: string):Uint8Array { | ||
function utf8Encode(str: string): Uint8Array { | ||
let utf8: number[] = []; | ||
let codePoints: number[] = []; | ||
for (var i = 0; i < str.length; i++) { | ||
for (let i = 0; i < str.length; i++) { | ||
let code = str.charCodeAt(i); | ||
@@ -53,6 +65,11 @@ let cod1; | ||
} | ||
codePoints.forEach(v => { | ||
// codePoints.forEach(v => { | ||
// utf8.push.apply(utf8, u2utf8(v)); | ||
// }); | ||
for (let i = 0; i < codePoints.length; i++) { | ||
let v = codePoints[i]; | ||
utf8.push.apply(utf8, u2utf8(v)); | ||
}); | ||
return new Uint8Array(utf8); | ||
} | ||
return getUint8Array(utf8); | ||
} | ||
@@ -67,11 +84,11 @@ | ||
function utf8Decode(buffer: ArrayBuffer | Uint8Array | number[]): string { | ||
let u8: Uint8Array; | ||
let u8: Uint8Array | number[]; | ||
let str = ''; | ||
let index = 0; | ||
if (buffer instanceof Uint8Array) { | ||
if (buffer instanceof myUint8arrayClass) { | ||
// Uint8Array & Buffer | ||
u8 = buffer; | ||
} else if (buffer instanceof ArrayBuffer || Array.isArray(buffer)) { | ||
} else if (buffer instanceof MyArrayBuffer || isArray(buffer)) { | ||
// ArrayBuffer & number[] | ||
u8 = new Uint8Array(buffer); | ||
u8 = getUint8Array(buffer); | ||
} else { | ||
@@ -133,3 +150,3 @@ return String(buffer); | ||
function toStringUTF8(this: Uint8Array):string { | ||
function toStringUTF8(this: Uint8Array): string { | ||
return utf8Decode(this); | ||
@@ -141,6 +158,6 @@ } | ||
let _u8arr; | ||
if (u8arr instanceof Uint8Array) { | ||
if (u8arr instanceof myUint8arrayClass) { | ||
_u8arr = u8arr; | ||
} else if (u8arr instanceof ArrayBuffer || Array.isArray(u8arr)) { | ||
_u8arr = new Uint8Array(u8arr); | ||
} else if (u8arr instanceof MyArrayBuffer || isArray(u8arr)) { | ||
_u8arr = getUint8Array(u8arr); | ||
} else { | ||
@@ -162,5 +179,11 @@ _u8arr = utf8Encode(u8arr.toString()); | ||
} | ||
return codes.reduce((d, code, i) => { | ||
return (d += i > bitLength - 1 ? pad : table[code]); | ||
}, ''); | ||
let base64 = ''; | ||
for (let i = 0; i < codes.length; i++) { | ||
const code = codes[i]; | ||
base64 += i > bitLength - 1 ? pad : table[code]; | ||
} | ||
return base64; | ||
// return codes.reduce((d, code, i) => { | ||
// return (d += i > bitLength - 1 ? pad : table[code]); | ||
// }, ''); | ||
}; | ||
@@ -170,4 +193,5 @@ } | ||
function getDecode(table: string[], pad: string) { | ||
const tableStr = table.join(''); | ||
const getV = function(char: string): number { | ||
let index = table.indexOf(char); | ||
let index = tableStr.indexOf(char); | ||
if (index == -1) throw new TypeError(`"${char}" not base64 char`); | ||
@@ -184,5 +208,5 @@ return index; | ||
}; | ||
const padreg = new RegExp(`${pad}*$`); | ||
return function(base64Str: string): Uint8Array { | ||
base64Str = base64Str.trim(); | ||
// const padreg = new RegExp(`${pad}*$`); | ||
return function(base64Str: string): Uint8Array | number[] { | ||
// base64Str = base64Str.trim(); | ||
let length = base64Str.length; | ||
@@ -192,3 +216,3 @@ let indexMax = length - getPads(base64Str); | ||
if (mc4 === 1) throw new TypeError('The parameter is not a base64 string!'); | ||
let buffer = new Uint8Array(Math.floor((indexMax * 6) / 8)); | ||
let buffer = new myUint8arrayClass(Math.floor((indexMax * 6) / 8)); | ||
let index = 0; | ||
@@ -201,11 +225,11 @@ let i = 0; | ||
let [c0, c1, c2, c3] = [next(), next(), next(), next()]; | ||
buffer[index++] = (c0 << 2) | (c1 >> 4); | ||
buffer[index++] = (c1 << 4) | (c2 >> 2); | ||
buffer[index++] = (c2 << 6) | c3; | ||
buffer[index++] = ((c0 << 2) | (c1 >> 4)) & 0xff; | ||
buffer[index++] = ((c1 << 4) | (c2 >> 2)) & 0xff; | ||
buffer[index++] = ((c2 << 6) | c3) & 0xff; | ||
} | ||
if (mc4) { | ||
let c0,c1; | ||
buffer[index++] = ((c0 = next()) << 2) | ((c1 = next()) >> 4); | ||
let c1; | ||
buffer[index++] = ((next() << 2) | ((c1 = next()) >> 4)) & 0xff; | ||
if (mc4 === 3) { | ||
buffer[index++] = (c1 << 4) | ((next()) >> 2); | ||
buffer[index++] = ((c1 << 4) | (next() >> 2)) & 0xff; | ||
} | ||
@@ -224,3 +248,3 @@ } | ||
const decodeURL = getDecode(BASE64_URL_TABLE, PAD); | ||
export { decode, encode, encodeURL, decodeURL, utf8Encode, utf8Decode }; | ||
// const __esModule = true; | ||
export { decode, encode, encodeURL, decodeURL, utf8Encode, utf8Decode}; |
@@ -16,5 +16,5 @@ /** | ||
declare const encode: (u8arr: string | number[] | ArrayBuffer | Uint8Array) => string; | ||
declare const decode: (base64Str: string) => Uint8Array; | ||
declare const decode: (base64Str: string) => number[] | Uint8Array; | ||
declare const encodeURL: (u8arr: string | number[] | ArrayBuffer | Uint8Array) => string; | ||
declare const decodeURL: (base64Str: string) => Uint8Array; | ||
declare const decodeURL: (base64Str: string) => number[] | Uint8Array; | ||
export { decode, encode, encodeURL, decodeURL, utf8Encode, utf8Decode }; |
/*! | ||
* Base64.js | ||
* https://github.com/cnwhy/Base64.js#readme | ||
* @cnwhy/base64 v0.1.1 | ||
* Homepage https://github.com/cnwhy/Base64.js#readme | ||
* License MIT | ||
*/ | ||
const BASE64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(''); | ||
@@ -9,5 +11,17 @@ const BASE64_URL_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'.split(''); | ||
const ERR_CODE = '\ufffd'; | ||
const isArray = Array.isArray || | ||
function (obj) { | ||
Object.prototype.toString.call(obj) == '[object Array]'; | ||
}; | ||
const hasArrayBuffer = typeof ArrayBuffer === 'function'; | ||
const MyArrayBuffer = hasArrayBuffer ? ArrayBuffer : function () { }; | ||
const myUint8arrayClass = hasArrayBuffer ? Uint8Array : Array; | ||
const getUint8Array = hasArrayBuffer | ||
? function (arr) { | ||
return new Uint8Array(arr); | ||
} | ||
: function (arr) { | ||
return typeof arr === 'number' ? new Array(arr) : arr; | ||
}; | ||
function u2utf8(codePoint) { | ||
// 未暴露的方法, 内部调用无需判断; | ||
// if (codePoint < 0 || codePoint > 0x7fffffff) throw new SyntaxError('Undefined Unicode code-point'); | ||
if (codePoint < 0x80) | ||
@@ -29,12 +43,6 @@ return [codePoint]; | ||
} | ||
/** | ||
* 字符串utf8编码 | ||
* | ||
* @param {string} str | ||
* @returns | ||
*/ | ||
function utf8Encode(str) { | ||
let utf8 = []; | ||
let codePoints = []; | ||
for (var i = 0; i < str.length; i++) { | ||
for (let i = 0; i < str.length; i++) { | ||
let code = str.charCodeAt(i); | ||
@@ -46,3 +54,2 @@ let cod1; | ||
else if (code < 0xdc00 && (cod1 = str.charCodeAt(i + 1)) >= 0xdc00 && cod1 < 0xe000) { | ||
//四字节字符处理 | ||
i++; | ||
@@ -52,17 +59,11 @@ codePoints.push(0x10000 + (((code & 0x3ff) << 10) | (cod1 & 0x3ff))); | ||
else { | ||
//不自行处理 不正常编码 | ||
codePoints.push(code); | ||
} | ||
} | ||
codePoints.forEach(v => { | ||
for (let i = 0; i < codePoints.length; i++) { | ||
let v = codePoints[i]; | ||
utf8.push.apply(utf8, u2utf8(v)); | ||
}); | ||
return new Uint8Array(utf8); | ||
} | ||
return getUint8Array(utf8); | ||
} | ||
/** | ||
* buffer以utf8转字符串 | ||
* | ||
* @param {(ArrayBuffer | Uint8Array | number[])} buffer | ||
* @returns {string} | ||
*/ | ||
function utf8Decode(buffer) { | ||
@@ -72,9 +73,7 @@ let u8; | ||
let index = 0; | ||
if (buffer instanceof Uint8Array) { | ||
// Uint8Array & Buffer | ||
if (buffer instanceof myUint8arrayClass) { | ||
u8 = buffer; | ||
} | ||
else if (buffer instanceof ArrayBuffer || Array.isArray(buffer)) { | ||
// ArrayBuffer & number[] | ||
u8 = new Uint8Array(buffer); | ||
else if (buffer instanceof MyArrayBuffer || isArray(buffer)) { | ||
u8 = getUint8Array(buffer); | ||
} | ||
@@ -93,4 +92,2 @@ else { | ||
else if (c0 < 0xc2 || c0 > 0xfd) { | ||
// 多字节 `u+0080` 转第一位最小值是 1100 0010 , 0000 0000 | ||
// 多字节 第一字节 最大位是 `1111 1101` | ||
throw 'code err'; | ||
@@ -105,3 +102,2 @@ } | ||
let cn = u8[_i++]; | ||
// if(cn < 0x80 || cn > 0xfb) throw 'code err'; | ||
if ((cn & 0xc0) ^ 0x80) | ||
@@ -131,5 +127,2 @@ throw 'code err'; | ||
catch (e) { | ||
// str += String.fromCharCode(c0); | ||
// 不正常的UTF8字节数据, 替换为 � | ||
// console.log(e); | ||
str += ERR_CODE; | ||
@@ -150,7 +143,7 @@ return i + 1; | ||
let _u8arr; | ||
if (u8arr instanceof Uint8Array) { | ||
if (u8arr instanceof myUint8arrayClass) { | ||
_u8arr = u8arr; | ||
} | ||
else if (u8arr instanceof ArrayBuffer || Array.isArray(u8arr)) { | ||
_u8arr = new Uint8Array(u8arr); | ||
else if (u8arr instanceof MyArrayBuffer || isArray(u8arr)) { | ||
_u8arr = getUint8Array(u8arr); | ||
} | ||
@@ -173,10 +166,14 @@ else { | ||
} | ||
return codes.reduce((d, code, i) => { | ||
return (d += i > bitLength - 1 ? pad : table[code]); | ||
}, ''); | ||
let base64 = ''; | ||
for (let i = 0; i < codes.length; i++) { | ||
const code = codes[i]; | ||
base64 += i > bitLength - 1 ? pad : table[code]; | ||
} | ||
return base64; | ||
}; | ||
} | ||
function getDecode(table, pad) { | ||
const tableStr = table.join(''); | ||
const getV = function (char) { | ||
let index = table.indexOf(char); | ||
let index = tableStr.indexOf(char); | ||
if (index == -1) | ||
@@ -195,3 +192,2 @@ throw new TypeError(`"${char}" not base64 char`); | ||
return function (base64Str) { | ||
base64Str = base64Str.trim(); | ||
let length = base64Str.length; | ||
@@ -202,3 +198,3 @@ let indexMax = length - getPads(base64Str); | ||
throw new TypeError('The parameter is not a base64 string!'); | ||
let buffer = new Uint8Array(Math.floor((indexMax * 6) / 8)); | ||
let buffer = new myUint8arrayClass(Math.floor((indexMax * 6) / 8)); | ||
let index = 0; | ||
@@ -211,14 +207,13 @@ let i = 0; | ||
let [c0, c1, c2, c3] = [next(), next(), next(), next()]; | ||
buffer[index++] = (c0 << 2) | (c1 >> 4); | ||
buffer[index++] = (c1 << 4) | (c2 >> 2); | ||
buffer[index++] = (c2 << 6) | c3; | ||
buffer[index++] = ((c0 << 2) | (c1 >> 4)) & 0xff; | ||
buffer[index++] = ((c1 << 4) | (c2 >> 2)) & 0xff; | ||
buffer[index++] = ((c2 << 6) | c3) & 0xff; | ||
} | ||
if (mc4) { | ||
let c0, c1; | ||
buffer[index++] = ((c0 = next()) << 2) | ((c1 = next()) >> 4); | ||
let c1; | ||
buffer[index++] = ((next() << 2) | ((c1 = next()) >> 4)) & 0xff; | ||
if (mc4 === 3) { | ||
buffer[index++] = (c1 << 4) | ((next()) >> 2); | ||
buffer[index++] = ((c1 << 4) | (next() >> 2)) & 0xff; | ||
} | ||
} | ||
// 复写toString以UTF8编码输出; | ||
buffer.toString = toStringUTF8; | ||
@@ -225,0 +220,0 @@ return buffer; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.Base64 = {})); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.Base64 = {})); | ||
}(this, function (exports) { 'use strict'; | ||
/*! | ||
* Base64.js | ||
* https://github.com/cnwhy/Base64.js#readme | ||
*/ | ||
const BASE64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(''); | ||
const BASE64_URL_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'.split(''); | ||
const PAD = '='; | ||
const ERR_CODE = '\ufffd'; | ||
function u2utf8(codePoint) { | ||
// 未暴露的方法, 内部调用无需判断; | ||
// if (codePoint < 0 || codePoint > 0x7fffffff) throw new SyntaxError('Undefined Unicode code-point'); | ||
if (codePoint < 0x80) | ||
return [codePoint]; | ||
let n = 11; | ||
while (codePoint >= Math.pow(2, n)) { | ||
n += 5; | ||
} | ||
let length = Math.ceil(n / 6); | ||
let u8 = new Array(length); | ||
let i = 0; | ||
u8[0] = (0xff ^ (Math.pow(2, (8 - length)) - 1)) | (codePoint >> (6 * (length - 1))); | ||
while (i < length - 1) { | ||
u8[length - 1 - i] = 0x80 | ((codePoint >> (i * 6)) & 0x3f); | ||
i++; | ||
} | ||
return u8; | ||
} | ||
/** | ||
* 字符串utf8编码 | ||
* | ||
* @param {string} str | ||
* @returns | ||
*/ | ||
function utf8Encode(str) { | ||
let utf8 = []; | ||
let codePoints = []; | ||
for (var i = 0; i < str.length; i++) { | ||
let code = str.charCodeAt(i); | ||
let cod1; | ||
if (code < 0xd800 || code > 0xdfff) { | ||
codePoints.push(code); | ||
} | ||
else if (code < 0xdc00 && (cod1 = str.charCodeAt(i + 1)) >= 0xdc00 && cod1 < 0xe000) { | ||
//四字节字符处理 | ||
i++; | ||
codePoints.push(0x10000 + (((code & 0x3ff) << 10) | (cod1 & 0x3ff))); | ||
} | ||
else { | ||
//不自行处理 不正常编码 | ||
codePoints.push(code); | ||
} | ||
} | ||
codePoints.forEach(v => { | ||
utf8.push.apply(utf8, u2utf8(v)); | ||
}); | ||
return new Uint8Array(utf8); | ||
} | ||
/** | ||
* buffer以utf8转字符串 | ||
* | ||
* @param {(ArrayBuffer | Uint8Array | number[])} buffer | ||
* @returns {string} | ||
*/ | ||
function utf8Decode(buffer) { | ||
let u8; | ||
let str = ''; | ||
let index = 0; | ||
if (buffer instanceof Uint8Array) { | ||
// Uint8Array & Buffer | ||
u8 = buffer; | ||
} | ||
else if (buffer instanceof ArrayBuffer || Array.isArray(buffer)) { | ||
// ArrayBuffer & number[] | ||
u8 = new Uint8Array(buffer); | ||
} | ||
else { | ||
return String(buffer); | ||
} | ||
function setChar(i) { | ||
let _i = i; | ||
let c0 = u8[_i++]; | ||
try { | ||
if (c0 < 0x80) { | ||
str += String.fromCharCode(c0); | ||
return _i; | ||
} | ||
else if (c0 < 0xc2 || c0 > 0xfd) { | ||
// 多字节 `u+0080` 转第一位最小值是 1100 0010 , 0000 0000 | ||
// 多字节 第一字节 最大位是 `1111 1101` | ||
throw 'code err'; | ||
} | ||
else { | ||
let mk = 0x80; | ||
let w = 6; | ||
let cs = []; | ||
let code = 0; | ||
while (c0 >= (mk | (Math.pow(2, w)))) { | ||
let cn = u8[_i++]; | ||
// if(cn < 0x80 || cn > 0xfb) throw 'code err'; | ||
if ((cn & 0xc0) ^ 0x80) | ||
throw 'code err'; | ||
cs.push(cn); | ||
mk = mk | (Math.pow(2, w)); | ||
w--; | ||
} | ||
cs = cs.reverse(); | ||
for (let k = 0; k < cs.length; k++) { | ||
let _c = cs[k] & 0x3f; | ||
code |= _c << (k * 6); | ||
} | ||
code |= (c0 & (Math.pow(2, w) - 1)) << (cs.length * 6); | ||
if (code > 0xffff) { | ||
let _code = code - 0x10000; | ||
str += String.fromCharCode(0xd800 | (_code >> 10)); | ||
str += String.fromCharCode(0xdc00 | (_code & 0x3ff)); | ||
} | ||
else { | ||
str += String.fromCharCode(code & 0xffff); | ||
} | ||
return _i; | ||
} | ||
} | ||
catch (e) { | ||
// str += String.fromCharCode(c0); | ||
// 不正常的UTF8字节数据, 替换为 � | ||
// console.log(e); | ||
str += ERR_CODE; | ||
return i + 1; | ||
} | ||
} | ||
while (index < u8.length) { | ||
index = setChar(index); | ||
} | ||
return str; | ||
} | ||
function toStringUTF8() { | ||
return utf8Decode(this); | ||
} | ||
function getEncode(table, pad) { | ||
return function (u8arr) { | ||
let _u8arr; | ||
if (u8arr instanceof Uint8Array) { | ||
_u8arr = u8arr; | ||
} | ||
else if (u8arr instanceof ArrayBuffer || Array.isArray(u8arr)) { | ||
_u8arr = new Uint8Array(u8arr); | ||
} | ||
else { | ||
_u8arr = utf8Encode(u8arr.toString()); | ||
} | ||
let bitLength = Math.ceil((_u8arr.length * 8) / 6); | ||
let str64Length = Math.ceil(_u8arr.length / 3) * 4; | ||
let codes = new Array(str64Length); | ||
let index = 0; | ||
for (let i = 0; i < _u8arr.length;) { | ||
let a0 = _u8arr[i++]; | ||
let a1 = _u8arr[i++]; | ||
let a2 = _u8arr[i++]; | ||
codes[index++] = a0 >> 2; | ||
codes[index++] = ((a0 << 4) | (a1 >> 4)) & 0x3f; | ||
codes[index++] = ((a1 << 2) | (a2 >> 6)) & 0x3f; | ||
codes[index++] = a2 & 0x3f; | ||
} | ||
return codes.reduce((d, code, i) => { | ||
return (d += i > bitLength - 1 ? pad : table[code]); | ||
}, ''); | ||
}; | ||
} | ||
function getDecode(table, pad) { | ||
const getV = function (char) { | ||
let index = table.indexOf(char); | ||
if (index == -1) | ||
throw new TypeError(`"${char}" not base64 char`); | ||
return index; | ||
}; | ||
const getPads = function (base64Str) { | ||
let index = base64Str.length; | ||
let pads = 0; | ||
while (index-- > 0 && base64Str.charAt(index) === pad) { | ||
pads++; | ||
} | ||
return pads; | ||
}; | ||
return function (base64Str) { | ||
base64Str = base64Str.trim(); | ||
let length = base64Str.length; | ||
let indexMax = length - getPads(base64Str); | ||
let mc4 = indexMax % 4; | ||
if (mc4 === 1) | ||
throw new TypeError('The parameter is not a base64 string!'); | ||
let buffer = new Uint8Array(Math.floor((indexMax * 6) / 8)); | ||
let index = 0; | ||
let i = 0; | ||
const next = function () { | ||
return getV(base64Str.charAt(i++)); | ||
}; | ||
for (let loopLength = indexMax - mc4; i < loopLength;) { | ||
let [c0, c1, c2, c3] = [next(), next(), next(), next()]; | ||
buffer[index++] = (c0 << 2) | (c1 >> 4); | ||
buffer[index++] = (c1 << 4) | (c2 >> 2); | ||
buffer[index++] = (c2 << 6) | c3; | ||
} | ||
if (mc4) { | ||
let c0, c1; | ||
buffer[index++] = ((c0 = next()) << 2) | ((c1 = next()) >> 4); | ||
if (mc4 === 3) { | ||
buffer[index++] = (c1 << 4) | ((next()) >> 2); | ||
} | ||
} | ||
// 复写toString以UTF8编码输出; | ||
buffer.toString = toStringUTF8; | ||
return buffer; | ||
}; | ||
} | ||
const encode = getEncode(BASE64_TABLE, PAD); | ||
const decode = getDecode(BASE64_TABLE, PAD); | ||
const encodeURL = getEncode(BASE64_URL_TABLE, PAD); | ||
const decodeURL = getDecode(BASE64_URL_TABLE, PAD); | ||
/*! | ||
* @cnwhy/base64 v0.1.1 | ||
* Homepage https://github.com/cnwhy/Base64.js#readme | ||
* License MIT | ||
*/ | ||
var BASE64_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(''); | ||
var BASE64_URL_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'.split(''); | ||
var PAD = '='; | ||
var ERR_CODE = "\uFFFD"; | ||
exports.decode = decode; | ||
exports.decodeURL = decodeURL; | ||
exports.encode = encode; | ||
exports.encodeURL = encodeURL; | ||
exports.utf8Decode = utf8Decode; | ||
exports.utf8Encode = utf8Encode; | ||
var isArray = Array.isArray || function (obj) { | ||
Object.prototype.toString.call(obj) == '[object Array]'; | ||
}; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var hasArrayBuffer = typeof ArrayBuffer === 'function'; | ||
var MyArrayBuffer = hasArrayBuffer ? ArrayBuffer : function () {}; | ||
var myUint8arrayClass = hasArrayBuffer ? Uint8Array : Array; | ||
var getUint8Array = hasArrayBuffer ? function (arr) { | ||
return new Uint8Array(arr); | ||
} : function (arr) { | ||
return typeof arr === 'number' ? new Array(arr) : arr; | ||
}; | ||
function u2utf8(codePoint) { | ||
if (codePoint < 0x80) return [codePoint]; | ||
var n = 11; | ||
while (codePoint >= Math.pow(2, n)) { | ||
n += 5; | ||
} | ||
var length = Math.ceil(n / 6); | ||
var u8 = new Array(length); | ||
var i = 0; | ||
u8[0] = 0xff ^ Math.pow(2, 8 - length) - 1 | codePoint >> 6 * (length - 1); | ||
while (i < length - 1) { | ||
u8[length - 1 - i] = 0x80 | codePoint >> i * 6 & 0x3f; | ||
i++; | ||
} | ||
return u8; | ||
} | ||
function utf8Encode(str) { | ||
var utf8 = []; | ||
var codePoints = []; | ||
for (var i = 0; i < str.length; i++) { | ||
var code = str.charCodeAt(i); | ||
var cod1 = void 0; | ||
if (code < 0xd800 || code > 0xdfff) { | ||
codePoints.push(code); | ||
} else if (code < 0xdc00 && (cod1 = str.charCodeAt(i + 1)) >= 0xdc00 && cod1 < 0xe000) { | ||
i++; | ||
codePoints.push(0x10000 + ((code & 0x3ff) << 10 | cod1 & 0x3ff)); | ||
} else { | ||
codePoints.push(code); | ||
} | ||
} | ||
for (var _i2 = 0; _i2 < codePoints.length; _i2++) { | ||
var v = codePoints[_i2]; | ||
utf8.push.apply(utf8, u2utf8(v)); | ||
} | ||
return getUint8Array(utf8); | ||
} | ||
function utf8Decode(buffer) { | ||
var u8; | ||
var str = ''; | ||
var index = 0; | ||
if (buffer instanceof myUint8arrayClass) { | ||
u8 = buffer; | ||
} else if (buffer instanceof MyArrayBuffer || isArray(buffer)) { | ||
u8 = getUint8Array(buffer); | ||
} else { | ||
return String(buffer); | ||
} | ||
function setChar(i) { | ||
var _i = i; | ||
var c0 = u8[_i++]; | ||
try { | ||
if (c0 < 0x80) { | ||
str += String.fromCharCode(c0); | ||
return _i; | ||
} else if (c0 < 0xc2 || c0 > 0xfd) { | ||
throw 'code err'; | ||
} else { | ||
var mk = 0x80; | ||
var w = 6; | ||
var cs = []; | ||
var code = 0; | ||
while (c0 >= (mk | Math.pow(2, w))) { | ||
var cn = u8[_i++]; | ||
if (cn & 0xc0 ^ 0x80) throw 'code err'; | ||
cs.push(cn); | ||
mk = mk | Math.pow(2, w); | ||
w--; | ||
} | ||
cs = cs.reverse(); | ||
for (var k = 0; k < cs.length; k++) { | ||
var _c = cs[k] & 0x3f; | ||
code |= _c << k * 6; | ||
} | ||
code |= (c0 & Math.pow(2, w) - 1) << cs.length * 6; | ||
if (code > 0xffff) { | ||
var _code = code - 0x10000; | ||
str += String.fromCharCode(0xd800 | _code >> 10); | ||
str += String.fromCharCode(0xdc00 | _code & 0x3ff); | ||
} else { | ||
str += String.fromCharCode(code & 0xffff); | ||
} | ||
return _i; | ||
} | ||
} catch (e) { | ||
str += ERR_CODE; | ||
return i + 1; | ||
} | ||
} | ||
while (index < u8.length) { | ||
index = setChar(index); | ||
} | ||
return str; | ||
} | ||
function toStringUTF8() { | ||
return utf8Decode(this); | ||
} | ||
function getEncode(table, pad) { | ||
return function (u8arr) { | ||
var _u8arr; | ||
if (u8arr instanceof myUint8arrayClass) { | ||
_u8arr = u8arr; | ||
} else if (u8arr instanceof MyArrayBuffer || isArray(u8arr)) { | ||
_u8arr = getUint8Array(u8arr); | ||
} else { | ||
_u8arr = utf8Encode(u8arr.toString()); | ||
} | ||
var bitLength = Math.ceil(_u8arr.length * 8 / 6); | ||
var str64Length = Math.ceil(_u8arr.length / 3) * 4; | ||
var codes = new Array(str64Length); | ||
var index = 0; | ||
for (var i = 0; i < _u8arr.length;) { | ||
var a0 = _u8arr[i++]; | ||
var a1 = _u8arr[i++]; | ||
var a2 = _u8arr[i++]; | ||
codes[index++] = a0 >> 2; | ||
codes[index++] = (a0 << 4 | a1 >> 4) & 0x3f; | ||
codes[index++] = (a1 << 2 | a2 >> 6) & 0x3f; | ||
codes[index++] = a2 & 0x3f; | ||
} | ||
var base64 = ''; | ||
for (var _i3 = 0; _i3 < codes.length; _i3++) { | ||
var code = codes[_i3]; | ||
base64 += _i3 > bitLength - 1 ? pad : table[code]; | ||
} | ||
return base64; | ||
}; | ||
} | ||
function getDecode(table, pad) { | ||
var tableStr = table.join(''); | ||
var getV = function getV(_char) { | ||
var index = tableStr.indexOf(_char); | ||
if (index == -1) throw new TypeError("\"".concat(_char, "\" not base64 char")); | ||
return index; | ||
}; | ||
var getPads = function getPads(base64Str) { | ||
var index = base64Str.length; | ||
var pads = 0; | ||
while (index-- > 0 && base64Str.charAt(index) === pad) { | ||
pads++; | ||
} | ||
return pads; | ||
}; | ||
return function (base64Str) { | ||
var length = base64Str.length; | ||
var indexMax = length - getPads(base64Str); | ||
var mc4 = indexMax % 4; | ||
if (mc4 === 1) throw new TypeError('The parameter is not a base64 string!'); | ||
var buffer = new myUint8arrayClass(Math.floor(indexMax * 6 / 8)); | ||
var index = 0; | ||
var i = 0; | ||
var next = function next() { | ||
return getV(base64Str.charAt(i++)); | ||
}; | ||
for (var loopLength = indexMax - mc4; i < loopLength;) { | ||
var _ref = [next(), next(), next(), next()], | ||
c0 = _ref[0], | ||
c1 = _ref[1], | ||
c2 = _ref[2], | ||
c3 = _ref[3]; | ||
buffer[index++] = (c0 << 2 | c1 >> 4) & 0xff; | ||
buffer[index++] = (c1 << 4 | c2 >> 2) & 0xff; | ||
buffer[index++] = (c2 << 6 | c3) & 0xff; | ||
} | ||
if (mc4) { | ||
var c1; | ||
buffer[index++] = (next() << 2 | (c1 = next()) >> 4) & 0xff; | ||
if (mc4 === 3) { | ||
buffer[index++] = (c1 << 4 | next() >> 2) & 0xff; | ||
} | ||
} | ||
buffer.toString = toStringUTF8; | ||
return buffer; | ||
}; | ||
} | ||
var encode = getEncode(BASE64_TABLE, PAD); | ||
var decode = getDecode(BASE64_TABLE, PAD); | ||
var encodeURL = getEncode(BASE64_URL_TABLE, PAD); | ||
var decodeURL = getDecode(BASE64_URL_TABLE, PAD); | ||
exports.decode = decode; | ||
exports.decodeURL = decodeURL; | ||
exports.encode = encode; | ||
exports.encodeURL = encodeURL; | ||
exports.utf8Decode = utf8Decode; | ||
exports.utf8Encode = utf8Encode; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); |
{ | ||
"name": "@cnwhy/base64", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Base64 library, lossless transcoding.", | ||
@@ -32,2 +32,3 @@ "main": "dist/Base64.umd.js", | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.4.3", | ||
"ava": "^1.4.1", | ||
@@ -37,2 +38,3 @@ "nyc": "^14.0.0", | ||
"rollup": "^1.10.1", | ||
"rollup-plugin-babel": "^4.3.2", | ||
"rollup-plugin-typescript": "^1.0.1", | ||
@@ -39,0 +41,0 @@ "ts-node": "^8.1.0", |
@@ -19,6 +19,2 @@ **为什么重复造轮子?** | ||
1. 二进制数据 与 Base64 互转 | ||
### 兼容 | ||
IE10+ (ES2015) | ||
### Demo | ||
@@ -25,0 +21,0 @@ ```js |
import typescript from 'rollup-plugin-typescript'; | ||
import babel from 'rollup-plugin-babel' | ||
// import es3 from 'rollup-plugin-es3' | ||
import pkg from './package.json' | ||
let banner =`/*! | ||
* ${pkg.name} v${pkg.version} | ||
* Homepage ${pkg.homepage} | ||
* License ${pkg.license} | ||
*/ | ||
` | ||
export default [ | ||
@@ -10,3 +19,4 @@ { | ||
target: 'ES2015', | ||
module: 'ES2015' | ||
module: 'ES2015', | ||
removeComments: true, | ||
}) | ||
@@ -18,7 +28,25 @@ ], | ||
// file: outDir + 'Base64.es.js', | ||
format: 'es' | ||
format: 'es', | ||
banner: banner | ||
}, | ||
// { | ||
// file: pkg.main, | ||
// // file: outDir + 'Base64.umd.js', | ||
// name: 'Base64', | ||
// format: 'umd' | ||
// } | ||
] | ||
}, | ||
{ | ||
input: pkg.module, | ||
plugins: [ | ||
babel({ | ||
babelrc: false, | ||
presets: [['@babel/env', { targets: "ie 6" }]], | ||
}), | ||
// es3() | ||
], | ||
output: [ | ||
{ | ||
file: pkg.main, | ||
// file: outDir + 'Base64.umd.js', | ||
name: 'Base64', | ||
@@ -25,0 +53,0 @@ format: 'umd' |
@@ -25,3 +25,3 @@ { | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */ | ||
// "noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */ | ||
// "strictNullChecks": true, /* Enable strict null checks. */ | ||
@@ -28,0 +28,0 @@ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37747
11
941
10
67