@algorithm.ts/base64 2.0.14 → 3.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <header>
2
2
  <h1 align="center">
3
- <a href="https://github.com/guanghechen/algorithm.ts/tree/release-2.x.x/packages/base64/#readme">@algorithm.ts/base64</a>
3
+ <a href="https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/base64/#readme">@algorithm.ts/base64</a>
4
4
  </h1>
5
5
  <div align="center">
6
6
  <a href="https://www.npmjs.com/package/@algorithm.ts/base64">
@@ -75,12 +75,16 @@ project uses `Uint8Array` to represent byte streams.
75
75
  * `encode`: Encode a Uint8Array into base64 encoded string.
76
76
 
77
77
  ```typescript
78
+ // node env: import { TextEncoder } from 'util'
78
79
  import { encode } from '@algorithm.ts/base64'
79
80
 
80
- const textEncoder = new TextEncoder('utf-8')
81
- const data: Uint8Array = textEncoder.encode(text)
81
+ const getBytes = (text: string): Uint8Array => {
82
+ const textEncoder = new TextEncoder()
83
+ const data: Uint8Array = textEncoder.encode(text)
84
+ return data
85
+ }
82
86
 
83
- encode('Hello, world!') // => 'SGVsbG8sIHdvcmxkIQ=='
87
+ encode(getBytes('Hello, world!')) // => 'SGVsbG8sIHdvcmxkIQ=='
84
88
  ```
85
89
 
86
90
  * `decode`: Decode a base64 encoded string to Uint8Array
@@ -89,7 +93,7 @@ project uses `Uint8Array` to represent byte streams.
89
93
  import { decode } from '@algorithm.ts/base64'
90
94
 
91
95
  const data: Uint8Array = decode('SGVsbG8sIHdvcmxkIQ==')
92
- const textDecoder = new TextDecoder('utf-8')
96
+ const textDecoder = new TextDecoder()
93
97
  textDecoder.decode(data) // => 'Hello, world!'
94
98
  ```
95
99
 
@@ -97,7 +101,24 @@ project uses `Uint8Array` to represent byte streams.
97
101
 
98
102
  ```typescript
99
103
  validate('SGVsbG8sIHdvcmxkIQ==') // => true
100
- validate('SGVsbG8sIHdvcmxkIQ===') // => false
104
+ ```
105
+
106
+ * Custom code map.
107
+
108
+ ```typescript
109
+ // node env: import { TextEncoder } from 'util'
110
+ import { Base64 } from '@algorithm.ts/base64'
111
+
112
+ const getBytes = (text: string): Uint8Array => {
113
+ const textEncoder = new TextEncoder()
114
+ const data: Uint8Array = textEncoder.encode(text)
115
+ return data
116
+ }
117
+
118
+ const base64 = new Base64({
119
+ CODES: '#ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+'
120
+ })
121
+ base64.encode(getBytes('Hello, world!')) // => RFUraF7rHGcublwjHP==
101
122
  ```
102
123
 
103
124
 
@@ -133,4 +154,4 @@ project uses `Uint8Array` to represent byte streams.
133
154
  * [`btoa()` | MDN](https://developer.mozilla.org/en-US/docs/Web/API/btoa)
134
155
 
135
156
 
136
- [homepage]: https://github.com/guanghechen/algorithm.ts/tree/release-2.x.x/packages/base64#readme
157
+ [homepage]: https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/base64#readme
package/lib/cjs/index.js CHANGED
@@ -2,86 +2,108 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- const BASE64_CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
6
- const BASE64_CODE_PADDING = '=';
7
- const BASE64_CODE_LIST = BASE64_CODES.split('');
8
- const BASE64_CODE_MAP = BASE64_CODE_LIST.reduce((acc, v, idx) => ({ ...acc, [v]: idx }), {
9
- [BASE64_CODE_PADDING]: 0,
10
- });
11
- const BASE64_VALIDATE_REGEX = new RegExp(`^[${BASE64_CODES}]+={0,2}$`);
12
- var index = { encode, decode, validate };
13
- function encode(data) {
14
- let result = '';
15
- const _end = Math.floor(data.length / 3) * 3;
16
- for (let i = 0; i < _end; i += 3) {
17
- result += encodeUnit((data[i] << 16) | (data[i + 1] << 8) | data[i + 2]);
5
+ const sanitizeRegex = /([*^$\\])/g;
6
+ class Base64 {
7
+ CODE_LIST;
8
+ CODE_REFLECT;
9
+ CODE_PADDING;
10
+ VALIDATE_REGEX;
11
+ constructor(props = {}) {
12
+ const CODES = props.CODES?.length === 64
13
+ ? props.CODES
14
+ : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
15
+ const CODE_PADDING = props.CODE_PADDING?.length === 1 ? props.CODE_PADDING : '=';
16
+ this.CODE_LIST = CODES.split('');
17
+ this.CODE_REFLECT = this.CODE_LIST.reduce((acc, v, idx) => ({ ...acc, [v]: idx }), {
18
+ [CODE_PADDING]: 0,
19
+ });
20
+ this.VALIDATE_REGEX = new RegExp(`^[${CODES.replace(sanitizeRegex, '\\$1')}]+${CODE_PADDING.replace(sanitizeRegex, '\\$1')}{0,2}$`);
21
+ this.CODE_PADDING = CODE_PADDING;
18
22
  }
19
- switch (data.length - _end) {
20
- case 1: {
21
- const tail = encodeUnit(data[_end] << 16);
22
- result += tail.slice(0, 2) + BASE64_CODE_PADDING + BASE64_CODE_PADDING;
23
- break;
24
- }
25
- case 2: {
26
- const tail = encodeUnit((data[_end] << 16) | (data[_end + 1] << 8));
27
- result += tail.slice(0, 3) + BASE64_CODE_PADDING;
23
+ encode(data) {
24
+ let result = '';
25
+ const _end = Math.floor(data.length / 3) * 3;
26
+ for (let i = 0; i < _end; i += 3) {
27
+ result += this._encodeUnit((data[i] << 16) | (data[i + 1] << 8) | data[i + 2]);
28
28
  }
29
- }
30
- return result;
31
- }
32
- function decode(text) {
33
- if (text.length <= 0 || !validate(text)) {
34
- throw new TypeError('Invalid base64 string.');
35
- }
36
- let countOfPadding = 0;
37
- for (let i = text.length - 1; i >= 0 && text[i] === BASE64_CODE_PADDING; --i)
38
- countOfPadding += 1;
39
- const _size = (text.length >> 2) * 3 - countOfPadding;
40
- const result = new Uint8Array(_size);
41
- const _end = ((text.length - countOfPadding) >> 2) << 2;
42
- let i = 0;
43
- let j = 0;
44
- for (; i < _end; i += 4, j += 3) {
45
- const v = decodeUint(text, i);
46
- result[j] = v >> 16;
47
- result[j + 1] = (v >> 8) & 0xff;
48
- result[j + 2] = v & 0xff;
49
- }
50
- if (countOfPadding > 0) {
51
- const v = decodeUint(text, i);
52
- switch (countOfPadding) {
29
+ const { CODE_PADDING } = this;
30
+ switch (data.length - _end) {
53
31
  case 1: {
54
- result[j] = v >> 16;
55
- result[j + 1] = (v >> 8) & 0xff;
32
+ const tail = this._encodeUnit(data[_end] << 16);
33
+ result += tail.slice(0, 2) + CODE_PADDING + CODE_PADDING;
56
34
  break;
57
35
  }
58
36
  case 2: {
59
- result[j] = v >> 16;
60
- break;
37
+ const tail = this._encodeUnit((data[_end] << 16) | (data[_end + 1] << 8));
38
+ result += tail.slice(0, 3) + CODE_PADDING;
61
39
  }
62
40
  }
41
+ return result;
42
+ }
43
+ decode(text) {
44
+ if (text.length <= 0 || !this.validate(text)) {
45
+ throw new TypeError('Invalid base64 string.');
46
+ }
47
+ let countOfPadding = 0;
48
+ const { CODE_PADDING } = this;
49
+ for (let i = text.length - 1; i >= 0 && text[i] === CODE_PADDING; --i)
50
+ countOfPadding += 1;
51
+ const _size = (text.length >> 2) * 3 - countOfPadding;
52
+ const result = new Uint8Array(_size);
53
+ const _end = ((text.length - countOfPadding) >> 2) << 2;
54
+ let i = 0;
55
+ let j = 0;
56
+ for (; i < _end; i += 4, j += 3) {
57
+ const v = this._decodeUint(text, i);
58
+ result[j] = v >> 16;
59
+ result[j + 1] = (v >> 8) & 0xff;
60
+ result[j + 2] = v & 0xff;
61
+ }
62
+ if (countOfPadding > 0) {
63
+ const v = this._decodeUint(text, i);
64
+ switch (countOfPadding) {
65
+ case 1: {
66
+ result[j] = v >> 16;
67
+ result[j + 1] = (v >> 8) & 0xff;
68
+ break;
69
+ }
70
+ case 2: {
71
+ result[j] = v >> 16;
72
+ break;
73
+ }
74
+ }
75
+ }
76
+ return result;
77
+ }
78
+ validate(text) {
79
+ if (text.length & 3)
80
+ return false;
81
+ return this.VALIDATE_REGEX.test(text);
82
+ }
83
+ _encodeUnit(v) {
84
+ const { CODE_LIST } = this;
85
+ return (CODE_LIST[(v >> 18) & 0x3f] +
86
+ CODE_LIST[(v >> 12) & 0x3f] +
87
+ CODE_LIST[(v >> 6) & 0x3f] +
88
+ CODE_LIST[v & 0x3f]);
89
+ }
90
+ _decodeUint(text, startIdx) {
91
+ const { CODE_REFLECT } = this;
92
+ const v = (CODE_REFLECT[text.charAt(startIdx)] << 18) |
93
+ (CODE_REFLECT[text.charAt(startIdx | 1)] << 12) |
94
+ (CODE_REFLECT[text.charAt(startIdx | 2)] << 6) |
95
+ CODE_REFLECT[text.charAt(startIdx | 3)];
96
+ return v;
63
97
  }
64
- return result;
65
- }
66
- function validate(text) {
67
- if (text.length & 3)
68
- return false;
69
- return BASE64_VALIDATE_REGEX.test(text);
70
- }
71
- function encodeUnit(v) {
72
- return (BASE64_CODE_LIST[(v >> 18) & 0x3f] +
73
- BASE64_CODE_LIST[(v >> 12) & 0x3f] +
74
- BASE64_CODE_LIST[(v >> 6) & 0x3f] +
75
- BASE64_CODE_LIST[v & 0x3f]);
76
- }
77
- function decodeUint(text, startIdx) {
78
- const v = (BASE64_CODE_MAP[text.charAt(startIdx)] << 18) |
79
- (BASE64_CODE_MAP[text.charAt(startIdx | 1)] << 12) |
80
- (BASE64_CODE_MAP[text.charAt(startIdx | 2)] << 6) |
81
- BASE64_CODE_MAP[text.charAt(startIdx | 3)];
82
- return v;
83
98
  }
84
99
 
100
+ const base64 = new Base64();
101
+ const encode = base64.encode.bind(base64);
102
+ const decode = base64.decode.bind(base64);
103
+ const validate = base64.validate.bind(base64);
104
+ var index = { encode, decode, validate };
105
+
106
+ exports.Base64 = Base64;
85
107
  exports.decode = decode;
86
108
  exports["default"] = index;
87
109
  exports.encode = encode;
package/lib/esm/index.js CHANGED
@@ -1,81 +1,102 @@
1
- const BASE64_CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
2
- const BASE64_CODE_PADDING = '=';
3
- const BASE64_CODE_LIST = BASE64_CODES.split('');
4
- const BASE64_CODE_MAP = BASE64_CODE_LIST.reduce((acc, v, idx) => ({ ...acc, [v]: idx }), {
5
- [BASE64_CODE_PADDING]: 0,
6
- });
7
- const BASE64_VALIDATE_REGEX = new RegExp(`^[${BASE64_CODES}]+={0,2}$`);
8
- var index = { encode, decode, validate };
9
- function encode(data) {
10
- let result = '';
11
- const _end = Math.floor(data.length / 3) * 3;
12
- for (let i = 0; i < _end; i += 3) {
13
- result += encodeUnit((data[i] << 16) | (data[i + 1] << 8) | data[i + 2]);
1
+ const sanitizeRegex = /([*^$\\])/g;
2
+ class Base64 {
3
+ CODE_LIST;
4
+ CODE_REFLECT;
5
+ CODE_PADDING;
6
+ VALIDATE_REGEX;
7
+ constructor(props = {}) {
8
+ const CODES = props.CODES?.length === 64
9
+ ? props.CODES
10
+ : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
11
+ const CODE_PADDING = props.CODE_PADDING?.length === 1 ? props.CODE_PADDING : '=';
12
+ this.CODE_LIST = CODES.split('');
13
+ this.CODE_REFLECT = this.CODE_LIST.reduce((acc, v, idx) => ({ ...acc, [v]: idx }), {
14
+ [CODE_PADDING]: 0,
15
+ });
16
+ this.VALIDATE_REGEX = new RegExp(`^[${CODES.replace(sanitizeRegex, '\\$1')}]+${CODE_PADDING.replace(sanitizeRegex, '\\$1')}{0,2}$`);
17
+ this.CODE_PADDING = CODE_PADDING;
14
18
  }
15
- switch (data.length - _end) {
16
- case 1: {
17
- const tail = encodeUnit(data[_end] << 16);
18
- result += tail.slice(0, 2) + BASE64_CODE_PADDING + BASE64_CODE_PADDING;
19
- break;
20
- }
21
- case 2: {
22
- const tail = encodeUnit((data[_end] << 16) | (data[_end + 1] << 8));
23
- result += tail.slice(0, 3) + BASE64_CODE_PADDING;
19
+ encode(data) {
20
+ let result = '';
21
+ const _end = Math.floor(data.length / 3) * 3;
22
+ for (let i = 0; i < _end; i += 3) {
23
+ result += this._encodeUnit((data[i] << 16) | (data[i + 1] << 8) | data[i + 2]);
24
24
  }
25
- }
26
- return result;
27
- }
28
- function decode(text) {
29
- if (text.length <= 0 || !validate(text)) {
30
- throw new TypeError('Invalid base64 string.');
31
- }
32
- let countOfPadding = 0;
33
- for (let i = text.length - 1; i >= 0 && text[i] === BASE64_CODE_PADDING; --i)
34
- countOfPadding += 1;
35
- const _size = (text.length >> 2) * 3 - countOfPadding;
36
- const result = new Uint8Array(_size);
37
- const _end = ((text.length - countOfPadding) >> 2) << 2;
38
- let i = 0;
39
- let j = 0;
40
- for (; i < _end; i += 4, j += 3) {
41
- const v = decodeUint(text, i);
42
- result[j] = v >> 16;
43
- result[j + 1] = (v >> 8) & 0xff;
44
- result[j + 2] = v & 0xff;
45
- }
46
- if (countOfPadding > 0) {
47
- const v = decodeUint(text, i);
48
- switch (countOfPadding) {
25
+ const { CODE_PADDING } = this;
26
+ switch (data.length - _end) {
49
27
  case 1: {
50
- result[j] = v >> 16;
51
- result[j + 1] = (v >> 8) & 0xff;
28
+ const tail = this._encodeUnit(data[_end] << 16);
29
+ result += tail.slice(0, 2) + CODE_PADDING + CODE_PADDING;
52
30
  break;
53
31
  }
54
32
  case 2: {
55
- result[j] = v >> 16;
56
- break;
33
+ const tail = this._encodeUnit((data[_end] << 16) | (data[_end + 1] << 8));
34
+ result += tail.slice(0, 3) + CODE_PADDING;
57
35
  }
58
36
  }
37
+ return result;
38
+ }
39
+ decode(text) {
40
+ if (text.length <= 0 || !this.validate(text)) {
41
+ throw new TypeError('Invalid base64 string.');
42
+ }
43
+ let countOfPadding = 0;
44
+ const { CODE_PADDING } = this;
45
+ for (let i = text.length - 1; i >= 0 && text[i] === CODE_PADDING; --i)
46
+ countOfPadding += 1;
47
+ const _size = (text.length >> 2) * 3 - countOfPadding;
48
+ const result = new Uint8Array(_size);
49
+ const _end = ((text.length - countOfPadding) >> 2) << 2;
50
+ let i = 0;
51
+ let j = 0;
52
+ for (; i < _end; i += 4, j += 3) {
53
+ const v = this._decodeUint(text, i);
54
+ result[j] = v >> 16;
55
+ result[j + 1] = (v >> 8) & 0xff;
56
+ result[j + 2] = v & 0xff;
57
+ }
58
+ if (countOfPadding > 0) {
59
+ const v = this._decodeUint(text, i);
60
+ switch (countOfPadding) {
61
+ case 1: {
62
+ result[j] = v >> 16;
63
+ result[j + 1] = (v >> 8) & 0xff;
64
+ break;
65
+ }
66
+ case 2: {
67
+ result[j] = v >> 16;
68
+ break;
69
+ }
70
+ }
71
+ }
72
+ return result;
73
+ }
74
+ validate(text) {
75
+ if (text.length & 3)
76
+ return false;
77
+ return this.VALIDATE_REGEX.test(text);
78
+ }
79
+ _encodeUnit(v) {
80
+ const { CODE_LIST } = this;
81
+ return (CODE_LIST[(v >> 18) & 0x3f] +
82
+ CODE_LIST[(v >> 12) & 0x3f] +
83
+ CODE_LIST[(v >> 6) & 0x3f] +
84
+ CODE_LIST[v & 0x3f]);
85
+ }
86
+ _decodeUint(text, startIdx) {
87
+ const { CODE_REFLECT } = this;
88
+ const v = (CODE_REFLECT[text.charAt(startIdx)] << 18) |
89
+ (CODE_REFLECT[text.charAt(startIdx | 1)] << 12) |
90
+ (CODE_REFLECT[text.charAt(startIdx | 2)] << 6) |
91
+ CODE_REFLECT[text.charAt(startIdx | 3)];
92
+ return v;
59
93
  }
60
- return result;
61
- }
62
- function validate(text) {
63
- if (text.length & 3)
64
- return false;
65
- return BASE64_VALIDATE_REGEX.test(text);
66
- }
67
- function encodeUnit(v) {
68
- return (BASE64_CODE_LIST[(v >> 18) & 0x3f] +
69
- BASE64_CODE_LIST[(v >> 12) & 0x3f] +
70
- BASE64_CODE_LIST[(v >> 6) & 0x3f] +
71
- BASE64_CODE_LIST[v & 0x3f]);
72
- }
73
- function decodeUint(text, startIdx) {
74
- const v = (BASE64_CODE_MAP[text.charAt(startIdx)] << 18) |
75
- (BASE64_CODE_MAP[text.charAt(startIdx | 1)] << 12) |
76
- (BASE64_CODE_MAP[text.charAt(startIdx | 2)] << 6) |
77
- BASE64_CODE_MAP[text.charAt(startIdx | 3)];
78
- return v;
79
94
  }
80
95
 
81
- export { decode, index as default, encode, validate };
96
+ const base64 = new Base64();
97
+ const encode = base64.encode.bind(base64);
98
+ const decode = base64.decode.bind(base64);
99
+ const validate = base64.validate.bind(base64);
100
+ var index = { encode, decode, validate };
101
+
102
+ export { Base64, decode, index as default, encode, validate };
@@ -1,11 +1,33 @@
1
+ interface IBase64Props {
2
+ /**
3
+ * @default 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
4
+ */
5
+ CODES?: string;
6
+ /**
7
+ * @default '=''
8
+ */
9
+ CODE_PADDING?: string;
10
+ }
11
+ declare class Base64 {
12
+ protected readonly CODE_LIST: string[];
13
+ protected readonly CODE_REFLECT: Record<string, number>;
14
+ protected readonly CODE_PADDING: string;
15
+ protected readonly VALIDATE_REGEX: RegExp;
16
+ constructor(props?: IBase64Props);
17
+ encode(data: Uint8Array): string;
18
+ decode(text: string): Uint8Array | never;
19
+ validate(text: string): boolean;
20
+ protected _encodeUnit(v: number): string;
21
+ protected _decodeUint(text: string, startIdx: number): number;
22
+ }
23
+
24
+ declare const encode: (data: Uint8Array) => string;
25
+ declare const decode: (text: string) => Uint8Array;
26
+ declare const validate: (text: string) => boolean;
1
27
  declare const _default: {
2
- encode: typeof encode;
3
- decode: typeof decode;
4
- validate: typeof validate;
28
+ encode: (data: Uint8Array) => string;
29
+ decode: (text: string) => Uint8Array;
30
+ validate: (text: string) => boolean;
5
31
  };
6
32
 
7
- declare function encode(data: Uint8Array): string;
8
- declare function decode(text: string): Uint8Array | never;
9
- declare function validate(text: string): boolean;
10
-
11
- export { decode, _default as default, encode, validate };
33
+ export { Base64, IBase64Props, decode, _default as default, encode, validate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@algorithm.ts/base64",
3
- "version": "2.0.14",
3
+ "version": "3.0.0-alpha.0",
4
4
  "description": "A Base64 encoding implementation.",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -8,10 +8,10 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/guanghechen/algorithm.ts/tree/release-2.x.x",
11
+ "url": "https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x",
12
12
  "directory": "packages/base64"
13
13
  },
14
- "homepage": "https://github.com/guanghechen/algorithm.ts/tree/release-2.x.x/packages/base64#readme",
14
+ "homepage": "https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/base64#readme",
15
15
  "keywords": [
16
16
  "algorithm",
17
17
  "base64"
@@ -39,5 +39,5 @@
39
39
  "prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
40
40
  "test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
41
41
  },
42
- "gitHead": "53a24624195c9f09422c9769c552f9066bc22c70"
42
+ "gitHead": "7562a908843d63b6b1bf92e7aa2104e7b294eaa0"
43
43
  }