Skip to content

Commit

Permalink
v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwhy committed Jun 12, 2019
1 parent f3aad2f commit 679a242
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
.nyc_output
coverage
coverage
*.tgz
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js
sudo: false
node_js:
- "6"
- "7"
- "8"
- "9"
- "10"
- "11"
- "12"
before_script:

script: npm run test-cov
# Run test script, depending on istanbul install
# - "test -n $(npm -ps ls istanbul) || npm test"
# - "test -z $(npm -ps ls istanbul) || npm run-script test-travis"

after_script:
- "npm install coveralls && nyc report --reporter=text-lcov | coveralls"
23 changes: 17 additions & 6 deletions dist/Base64.es.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @cnwhy/base64 v0.2.0
* @cnwhy/base64 v0.2.1
* Homepage https://github.com/cnwhy/Base64.js#readme
* License MIT
*/
Expand Down Expand Up @@ -169,6 +169,11 @@ function checkTable(table) {
}
}
function createEncode(table, pad, strEncode) {
if (typeof table == 'function') {
strEncode = table;
table = undefined;
pad = undefined;
}
const TABLE = getTable(table);
const PAD = getPad(pad, TABLE);
return function (u8arr) {
Expand Down Expand Up @@ -207,9 +212,20 @@ function createEncode(table, pad, strEncode) {
};
}
function createDecode(table, pad, strDecode) {
if (typeof table == 'function') {
strDecode = table;
table = undefined;
pad = undefined;
}
const TABLE = getTable(table);
const PAD = getPad(pad, TABLE);
const TABLE_JOIN = TABLE.join('');
let _strDecode, toString = typeof strDecode == 'function'
? ((_strDecode = strDecode),
function () {
return _strDecode(this);
})
: null;
const getV = function (char) {
let index = TABLE_JOIN.indexOf(char);
if (index == -1)
Expand All @@ -224,11 +240,6 @@ function createDecode(table, pad, strDecode) {
}
return pads;
};
const toString = typeof strDecode == 'function'
? function () {
return strDecode(this);
}
: null;
return function (base64Str) {
let length = base64Str.length;
let indexMax = length - getPads(base64Str);
Expand Down
22 changes: 18 additions & 4 deletions dist/Base64.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}(this, function (exports) { 'use strict';

/*!
* @cnwhy/base64 v0.2.0
* @cnwhy/base64 v0.2.1
* Homepage https://github.com/cnwhy/Base64.js#readme
* License MIT
*/
Expand Down Expand Up @@ -196,6 +196,12 @@
}

function createEncode(table, pad, strEncode) {
if (typeof table == 'function') {
strEncode = table;
table = undefined;
pad = undefined;
}

var TABLE = getTable(table);
var PAD = getPad(pad, TABLE);
return function (u8arr) {
Expand Down Expand Up @@ -238,10 +244,21 @@
}

function createDecode(table, pad, strDecode) {
if (typeof table == 'function') {
strDecode = table;
table = undefined;
pad = undefined;
}

var TABLE = getTable(table);
var PAD = getPad(pad, TABLE);
var TABLE_JOIN = TABLE.join('');

var _strDecode,
toString = typeof strDecode == 'function' ? (_strDecode = strDecode, function () {
return _strDecode(this);
}) : null;

var getV = function getV(_char2) {
var index = TABLE_JOIN.indexOf(_char2);
if (index == -1) throw new TypeError("\"".concat(_char2, "\" not base64 char"));
Expand All @@ -259,9 +276,6 @@
return pads;
};

var toString = typeof strDecode == 'function' ? function () {
return strDecode(this);
} : null;
return function (base64Str) {
var length = base64Str.length;
var indexMax = length - getPads(base64Str);
Expand Down
4 changes: 2 additions & 2 deletions lib/Base64.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { utf8Decode, utf8Encode } from './utf8';
import { createEncode, createDecode, BASE64_TABLE, PAD } from './main';
declare const BASE64_URL_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
declare const encode: (input: any) => string;
declare const decode: (base64Str: string) => number[] | Uint8Array;
declare const decode: (base64str: string) => number[] | Uint8Array;
declare const encodeURL: (input: any) => string;
declare const decodeURL: (base64Str: string) => number[] | Uint8Array;
declare const decodeURL: (base64str: string) => number[] | Uint8Array;
export { createEncode, createDecode, BASE64_TABLE, BASE64_URL_TABLE, PAD, utf8Decode, utf8Encode, encode, decode, encodeURL, decodeURL };
28 changes: 21 additions & 7 deletions lib/main.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
declare const BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
declare const PAD = "=";
/**
* 创建Base64编码函数
*
* @param {Function} strEncode // 字符串编函数
* @returns {(input: any) => string}
*/
declare function createEncode(strEncode: Function): (input: any) => string;
/**
* 创建Base64编码函数
*
* @param {(string[] | string)} Base64 的编码表
* @param {string} PAD
* @param {Function} strEncode
* @param {string} PAD //填充符
* @param {Function} strEncode // 字符串编函数, 不设置则不支持编码字符串
* @returns {(input: any) => string}
*/
declare function createEncode(table: string[] | string, pad: string, strEncode?: Function): (input: any) => string;
declare function createEncode(table?: string[] | string, pad?: string, strEncode?: Function): (input: any) => string;
/**
* 创建Base64解码函数
*
* @param {Function} strDecode
* @returns {((base64str: string) => Uint8Array | number[])}
*/
declare function createDecode(strDecode: Function): (base64str: string) => Uint8Array | number[];
/**
* 创建Base64解码函数
*
* @param {(string[] | string)} table
* @param {string} pad
* @param {(string[] | string)} [table]
* @param {string} [pad]
* @param {Function} [strDecode]
* @returns
* @returns {((base64str: string) => Uint8Array | number[])}
*/
declare function createDecode(table: string[] | string, pad: string, strDecode?: Function): (base64Str: string) => number[] | Uint8Array;
declare function createDecode(table?: string[] | string, pad?: string, strDecode?: Function): (base64str: string) => Uint8Array | number[];
export { createEncode, createDecode, BASE64_TABLE, PAD };
37 changes: 16 additions & 21 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ function checkTable(table) {
}
}
}
/**
* 创建Base64编码函数
*
* @param {(string[] | string)} Base64 的编码表
* @param {string} PAD
* @param {Function} strEncode
* @returns {(input: any) => string}
*/
function createEncode(table, pad, strEncode) {
if (typeof table == 'function') {
strEncode = table;
table = undefined;
pad = undefined;
}
const TABLE = getTable(table);
const PAD = getPad(pad, TABLE);
return function (u8arr) {
Expand Down Expand Up @@ -97,18 +94,21 @@ function createEncode(table, pad, strEncode) {
};
}
exports.createEncode = createEncode;
/**
* 创建Base64解码函数
*
* @param {(string[] | string)} table
* @param {string} pad
* @param {Function} [strDecode]
* @returns
*/
function createDecode(table, pad, strDecode) {
if (typeof table == 'function') {
strDecode = table;
table = undefined;
pad = undefined;
}
const TABLE = getTable(table);
const PAD = getPad(pad, TABLE);
const TABLE_JOIN = TABLE.join('');
let _strDecode, toString = typeof strDecode == 'function'
? ((_strDecode = strDecode),
function () {
return _strDecode(this);
})
: null;
const getV = function (char) {
let index = TABLE_JOIN.indexOf(char);
if (index == -1)
Expand All @@ -123,11 +123,6 @@ function createDecode(table, pad, strDecode) {
}
return pads;
};
const toString = typeof strDecode == 'function'
? function () {
return strDecode(this);
}
: null;
// const padreg = new RegExp(`${pad}*$`);
return function (base64Str) {
// base64Str = base64Str.trim();
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cnwhy/base64",
"version": "0.2.0",
"version": "0.2.1",
"description": "Base64 library, lossless transcoding.",
"main": "dist/Base64.umd.js",
"module": "dist/Base64.es.js",
Expand All @@ -25,8 +25,7 @@
"*.json",
"src",
"lib",
"dist",
"test"
"dist"
],
"author": "cnwhy",
"license": "MIT",
Expand All @@ -40,7 +39,7 @@
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-typescript": "^1.0.1",
"ts-node": "^8.1.0",
"tslib": "^1.9.3",
"tslib": "^1.10.0",
"typescript": "^3.4.4"
},
"directories": {
Expand All @@ -54,7 +53,6 @@
"url": "https://github.com/cnwhy/Base64.js/issues"
},
"homepage": "https://github.com/cnwhy/Base64.js#readme",
"dependencies": {},
"ava": {
"compileEnhancements": false,
"extensions": [
Expand Down
26 changes: 26 additions & 0 deletions test/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,29 @@ encodes.forEach(([name, encode, decode]) => {
var b64 = encode(thestr);
console.log(name + ':\n' + b64 + ':\n' + decode(b64).toString());
});

console.log('== DIY ==');
const BASE64_TABLE = 'xQh}s7*y~A|nkj4Bf%z1R,P+)mMS{(&EWCKegp6r!OX</LuY-l9^ZJ#cTU[vHda$';
const PAD = '.';
const b64Encode = Base64.createEncode(BASE64_TABLE,PAD,Base64.utf8Encode);
const b64Decode = Base64.createDecode(BASE64_TABLE,PAD,Base64.utf8Decode);
var os = '中国\u{10121}美国';
var os_l = os.substr(0,4);
var os_r = os.substr(4);
var os_b64 = b64Encode(os);
var os_l_b64 = b64Encode(os_l);
var os_r_b64 = b64Encode(os_r);
console.log(os)
console.log(os_b64)
console.log(os_l_b64)
console.log(os_r_b64)
console.log(b64Decode(os_b64).toString());
console.log(b64Decode(os_l_b64) + b64Decode(os_r_b64) == os);

let bytes = new Uint8Array([1,2,3,4,5]);
let u16arr = new Uint16Array([1,2,3,4,5]);
let buffer = u16arr.buffer;
let b64_bytes = Base64.encode(bytes);
let b64_buffer = Base64.encode(buffer);
console.log(Array.from(Base64.decode(b64_bytes)));
console.log(Array.from(Base64.decode(b64_buffer)));
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
"importHelpers": true, /* Import emit helpers from 'tslib'. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

Expand Down

0 comments on commit 679a242

Please sign in to comment.