Skip to content

Commit

Permalink
feat(base64): add encode/decode
Browse files Browse the repository at this point in the history
  • Loading branch information
novlan1 committed Sep 8, 2024
1 parent b094acc commit 0184a16
Show file tree
Hide file tree
Showing 26 changed files with 533 additions and 52 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/dist/
/lib/
/es/
/node_modules/
/public/
/config/
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ dist
# TernJS port file
.tern-port

lib
lib/
es/
temp
*.bak

Expand Down
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no -- commitlint@18 --edit
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] };
63 changes: 63 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
# 更新日志

### [1.3.101](https://github.com/novlan1/t-comm/compare/v1.3.100...v1.3.101) (2024-09-07)


### Chore 🚀

* add es module ([f03724d](https://github.com/novlan1/t-comm/commit/f03724d68e12afb8855e369389a09eb5a454afcc))

### [1.3.100](https://github.com/novlan1/t-comm/compare/v1.3.99...v1.3.100) (2024-08-28)


### Features 🎉

* **mp-ci:** update mp-cli modify ([d939c1b](https://github.com/novlan1/t-comm/commit/d939c1be2cb8b0f663666adf4cf16dd6798b258a))

### [1.3.99](https://github.com/novlan1/t-comm/compare/v1.3.98...v1.3.99) (2024-08-26)


### Features 🎉

* **mp-ci:** 优化修改配置 ([03149d3](https://github.com/novlan1/t-comm/commit/03149d35c126739e58828b806bd9bb3eddf53659))

### [1.3.98](https://github.com/novlan1/t-comm/compare/v1.3.97...v1.3.98) (2024-08-26)


### Features 🎉

* **base64:** encode, decode 支持非拉丁字符 ([e9ec697](https://github.com/novlan1/t-comm/commit/e9ec697faef416c024d6ab8ed2e8b4a961e6f655))
* **mp-ci:** 优化修改配置 ([82f74da](https://github.com/novlan1/t-comm/commit/82f74da9e7bbecdc763da99f69bb4238d9477145))
* **mp-ci:** 优化修改mp-ci配置 ([1fbabac](https://github.com/novlan1/t-comm/commit/1fbabac83b8cc45d961ecf879863317085d77b27))

### [1.3.97](https://github.com/novlan1/t-comm/compare/v1.3.96...v1.3.97) (2024-08-15)


### Bug Fixes 🐞

* **bump-version:** 修复bump-version ([ebb3cb4](https://github.com/novlan1/t-comm/commit/ebb3cb4fe802a2b2832ecf25527ff49d1bafaba0))

### [1.3.96](https://github.com/novlan1/t-comm/compare/v1.3.95...v1.3.96) (2024-08-15)


### Chore 🚀

* add commit-msg husky ([5a082a3](https://github.com/novlan1/t-comm/commit/5a082a386d10211fa5397b1e0c8c72114e0bf1dc))


### Features 🎉

* add base64 encode & decode ([7b0aa6a](https://github.com/novlan1/t-comm/commit/7b0aa6ab0c679fa75f34b1545432549a1ef179d8))
* **bump-version:** add bump-version ([762a219](https://github.com/novlan1/t-comm/commit/762a2190745c0ec26636d2bc4241e5c9f496127a))

### [1.3.95](https://github.com/novlan1/t-comm/compare/v1.3.94...v1.3.95) (2024-08-07)


### Tests ✅

* update test case ([a8761c6](https://github.com/novlan1/t-comm/commit/a8761c6cb9f86399aebc1253c83792c2f849cca3))


### Features 🎉

* **devops-ci:** support useDevopsWXCIPlugin ([2b72ba3](https://github.com/novlan1/t-comm/commit/2b72ba343184e7401321e8ea19cf0bd7b9bad4fb))
* **list:** add split long list ([406c543](https://github.com/novlan1/t-comm/commit/406c54374abd3e0b7cc283eacdf932eb4386113d))

### [1.3.94](https://github.com/novlan1/t-comm/compare/v1.3.93...v1.3.94) (2024-07-29)


Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "t-comm",
"version": "1.3.94",
"version": "1.3.101",
"description": "丰富易用的工具库",
"main": "lib/index.js",
"module": "lib/index.esm.js",
"typings": "lib/index.d.ts",
"files": [
"lib",
"es",
"bin",
"barcode",
"LICENSE",
Expand Down Expand Up @@ -123,6 +124,7 @@
},
"dependencies": {
"@babel/runtime": "^7.17.2",
"@commitlint/config-conventional": "^17.1.0",
"axios": "*",
"cheerio": "^1.0.0-rc.12",
"commander": "^10.0.1",
Expand Down
16 changes: 16 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const pkg = require('./package.json');
const PATHS = {
input: path.join(__dirname, '/src/index.ts'),
output: path.join(__dirname, '/lib'),
outputES: path.join(__dirname, '/es'),
};


Expand Down Expand Up @@ -86,6 +87,21 @@ const allRollupConfig = [
...ROLLUP_PLUGINS,
],
},
{
input: getAllSourceInput(),
output: [{
format: 'es',
exports: 'named',
dir: PATHS.outputES,
}],
external: ROLLUP_EXTERNALS,
plugins: [
typescript({
tsconfig: './tsconfig-es.json',
}),
...ROLLUP_PLUGINS,
],
},
{
input: PATHS.input,
output: [{
Expand Down
1 change: 1 addition & 0 deletions src/base/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export {
flattenPreData,
compareTwoList,
} from './parse-list';
export { splitLongList } from './split';
29 changes: 29 additions & 0 deletions src/base/list/split.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function getStrListLength(list: Array<string>) {
return list.reduce((acc, item) => acc + item.length, 0);
}

export function splitLongList(list: Array<string>, threshold = 3800, max = 3) {
const strLen = getStrListLength(list);
if (strLen <= threshold) {
return [list];
}
const result = [];
let temp = [];
for (let i = 0; i < list.length;i++) {
const item = list[i];
temp.push(item);

const tempLen = getStrListLength(temp);
if (tempLen >= threshold) {
result.push(temp);
temp = [];

if (result.length >= max - 1) {
temp = list.slice(i + 1);
break;
}
}
}
result.push(temp);
return result;
}
3 changes: 3 additions & 0 deletions src/base64/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
export const B64RE = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
export const _fromCC = String.fromCharCode.bind(String);
59 changes: 59 additions & 0 deletions src/base64/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable */
import { B64, B64RE, _fromCC } from './config';

const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;


const _tidyB64 = function (s: string) {
return s.replace(/[^A-Za-z0-9\+\/]/g, '');
};

const cb_btou = function (cccc: string) {
switch (cccc.length) {
case 4:
var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
| ((0x3f & cccc.charCodeAt(1)) << 12)
| ((0x3f & cccc.charCodeAt(2)) << 6)
| (0x3f & cccc.charCodeAt(3)); var offset = cp - 0x10000;
return (_fromCC((offset >>> 10) + 0xD800)
+ _fromCC((offset & 0x3FF) + 0xDC00));
case 3:
return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
| ((0x3f & cccc.charCodeAt(1)) << 6)
| (0x3f & cccc.charCodeAt(2)));
default:
return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
| (0x3f & cccc.charCodeAt(1)));
}
};

const btou = function (b: string) {
return b.replace(re_btou, cb_btou);
};


const _unURI = function (a: string) {
return _tidyB64(a.replace(/[-_]/g, m0 => (m0 == '-' ? '+' : '/')));
};


export const decode = (str: string) => btou(innerDecode(_unURI(str)));


// 小程序版本的atob
export const innerDecode = function (string: string) {
string = String(string).replace(/[\t\n\f\r ]+/g, '');
if (!B64RE.test(string)) throw new TypeError('Failed to execute \'atob\' on \'Window\': The string to be decoded is not correctly encoded.');
string += '=='.slice(2 - (string.length & 3));
let bitmap; let result = '';
let r1; let r2; let i = 0;
for (; i < string.length;) {
bitmap = B64.indexOf(string.charAt(i++)) << 18 | B64.indexOf(string.charAt(i++)) << 12
| (r1 = B64.indexOf(string.charAt(i++))) << 6 | (r2 = B64.indexOf(string.charAt(i++)));

result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
: r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
: String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
}
return result;
};
52 changes: 52 additions & 0 deletions src/base64/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable */
import { B64, _fromCC } from './config';
const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;

const utob = function (u: string) {
return u.replace(re_utob, cb_utob);
};


const cb_utob = function (c: string) {
if (c.length < 2) {
var cc: any = c.charCodeAt(0);
return cc < 0x80 ? c
: cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
+ _fromCC(0x80 | (cc & 0x3f)))
: (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | (cc & 0x3f)));
}

var cc: any = 0x10000
+ (c.charCodeAt(0) - 0xD800) * 0x400
+ (c.charCodeAt(1) - 0xDC00);
return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
+ _fromCC(0x80 | ((cc >>> 12) & 0x3f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | (cc & 0x3f)));
};

export const encode = (str: string) => innerEncode(utob(str));


// 小程序版本的btoa
export const innerEncode = function (string: string) {
string = String(string);
let bitmap; let a; let b; let c;
let result = '';
let i = 0;
const rest = string.length % 3;

for (; i < string.length;) {
if ((a = string.charCodeAt(i++)) > 255
|| (b = string.charCodeAt(i++)) > 255
|| (c = string.charCodeAt(i++)) > 255) throw new TypeError('Failed to execute \'btoa\' on \'Window\': The string to be encoded contains characters outside of the Latin1 range.');

bitmap = (a << 16) | (b << 8) | c;
result += B64.charAt(bitmap >> 18 & 63) + B64.charAt(bitmap >> 12 & 63)
+ B64.charAt(bitmap >> 6 & 63) + B64.charAt(bitmap & 63);
}

return rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
};
2 changes: 2 additions & 0 deletions src/base64/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { encode } from './encode';
export { decode } from './decode';
1 change: 1 addition & 0 deletions src/devops/mp-ci/devops-mp-ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async function realUpdateOrCreatePipeline({
rainbowConfigKey: mpCIKey,
repo: newConfig.ci.repo,
isWxCI,
devopsParams: newConfig.devopsParams || {},
}),
],
})
Expand Down
9 changes: 9 additions & 0 deletions src/devops/mp-ci/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export function getPipelineParam({
rainbowConfigKey,
repo,
isWxCI,
devopsParams,
}: Record<string, any>) {
const res = [
{
Expand Down Expand Up @@ -159,6 +160,14 @@ export function getPipelineParam({
desc: 'Tam上报ID',
readOnly: false,
},
{
id: 'useDevopsWXCIPlugin',
required: true,
type: 'STRING',
defaultValue: devopsParams?.useDevopsWXCIPlugin || '0',
desc: '是否使用蓝盾平台小程序CI插件',
readOnly: false,
},
]);
}
return res;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './ast';
export * from './base';
export * from './base64';
export * from './bite';
export * from './build-upload';
export * from './bundle-analyze';
Expand Down
15 changes: 10 additions & 5 deletions src/mp-ci/manage-config/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ export function updateQQTencentSheetLine(rainbowConfig: Record<string, any>) {
Object.keys(qqRobotMap).forEach((branch) => {
const value = qqRobotMap[branch] || {};
if (!value.tencentDocsLine) {
value.tencentDocsLine = {
test: restLines[cur],
release: restLines[cur + 1],
};
cur += 2;
const temp: Record<string, any> = {};
if (value.test) {
temp.test = restLines[cur];
cur += 1;
}
if (value.release) {
temp.release = restLines[cur];
cur += 1;
}
value.tencentDocsLine = temp;
}
});
}
Loading

0 comments on commit 0184a16

Please sign in to comment.