@nuintun/buffer 0.3.2 → 0.4.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 +63 -40
- package/cjs/{Binary.cjs → binary.cjs} +2 -2
- package/{esm/Binary.d.ts → cjs/binary.d.cts} +1 -1
- package/cjs/encoding.cjs +94 -0
- package/cjs/encoding.d.cts +25 -0
- package/cjs/enum.cjs +1 -1
- package/cjs/errors.cjs +2 -2
- package/cjs/errors.d.cts +1 -1
- package/cjs/index.cjs +31 -28
- package/cjs/index.d.cts +27 -9
- package/cjs/utils.cjs +1 -1
- package/{cjs/Binary.d.cts → esm/binary.d.ts} +1 -1
- package/esm/{Binary.js → binary.js} +2 -2
- package/esm/encoding.d.ts +25 -0
- package/esm/encoding.js +91 -0
- package/esm/enum.js +1 -1
- package/esm/errors.d.ts +1 -1
- package/esm/errors.js +2 -2
- package/esm/index.d.ts +27 -9
- package/esm/index.js +28 -25
- package/esm/utils.js +1 -1
- package/package.json +1 -1
- package/cjs/Encoding/UTF8.cjs +0 -33
- package/cjs/Encoding/UTF8.d.cts +0 -15
- package/cjs/Encoding/Unicode.cjs +0 -45
- package/cjs/Encoding/Unicode.d.cts +0 -18
- package/cjs/Encoding/index.cjs +0 -65
- package/cjs/Encoding/index.d.cts +0 -19
- package/esm/Encoding/UTF8.d.ts +0 -15
- package/esm/Encoding/UTF8.js +0 -30
- package/esm/Encoding/Unicode.d.ts +0 -18
- package/esm/Encoding/Unicode.js +0 -42
- package/esm/Encoding/index.d.ts +0 -19
- package/esm/Encoding/index.js +0 -62
package/esm/Encoding/UTF8.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module UTF8
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* @function encode
|
|
6
|
-
* @param {string} input
|
|
7
|
-
* @returns {Uint8Array}
|
|
8
|
-
*/
|
|
9
|
-
export declare const encode: (input?: string) => Uint8Array;
|
|
10
|
-
/**
|
|
11
|
-
* @function decode
|
|
12
|
-
* @param {BufferSource} input
|
|
13
|
-
* @returns {string}
|
|
14
|
-
*/
|
|
15
|
-
export declare const decode: (input?: BufferSource) => string;
|
package/esm/Encoding/UTF8.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package @nuintun/buffer
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.3.2
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A buffer tool for javascript.
|
|
7
|
-
* @see https://github.com/nuintun/Buffer#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @module UTF8
|
|
12
|
-
*/
|
|
13
|
-
// 编码器实例
|
|
14
|
-
const encoder = new TextEncoder();
|
|
15
|
-
// 解码器实例
|
|
16
|
-
const decoder = new TextDecoder();
|
|
17
|
-
/**
|
|
18
|
-
* @function encode
|
|
19
|
-
* @param {string} input
|
|
20
|
-
* @returns {Uint8Array}
|
|
21
|
-
*/
|
|
22
|
-
const encode = encoder.encode.bind(encoder);
|
|
23
|
-
/**
|
|
24
|
-
* @function decode
|
|
25
|
-
* @param {BufferSource} input
|
|
26
|
-
* @returns {string}
|
|
27
|
-
*/
|
|
28
|
-
const decode = decoder.decode.bind(decoder);
|
|
29
|
-
|
|
30
|
-
export { decode, encode };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Unicode
|
|
3
|
-
*/
|
|
4
|
-
export type TypeArray = typeof Uint16Array | typeof Uint32Array;
|
|
5
|
-
/**
|
|
6
|
-
* @function encode
|
|
7
|
-
* @param {string} input
|
|
8
|
-
* @param {TypeArray} Buffer
|
|
9
|
-
* @returns {Uint8Array}
|
|
10
|
-
*/
|
|
11
|
-
export declare function encode(input: string, TypeArray: TypeArray): Uint8Array;
|
|
12
|
-
/**
|
|
13
|
-
* @function decode
|
|
14
|
-
* @param {BufferSource} input
|
|
15
|
-
* @param {TypeArray} Buffer
|
|
16
|
-
* @returns {string}
|
|
17
|
-
*/
|
|
18
|
-
export declare function decode(input: BufferSource, TypeArray: TypeArray): string;
|
package/esm/Encoding/Unicode.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package @nuintun/buffer
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.3.2
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A buffer tool for javascript.
|
|
7
|
-
* @see https://github.com/nuintun/Buffer#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @module Unicode
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* @function encode
|
|
15
|
-
* @param {string} input
|
|
16
|
-
* @param {TypeArray} Buffer
|
|
17
|
-
* @returns {Uint8Array}
|
|
18
|
-
*/
|
|
19
|
-
function encode(input, TypeArray) {
|
|
20
|
-
const { length } = input;
|
|
21
|
-
const array = new TypeArray(length);
|
|
22
|
-
for (let i = 0; i < length; i++) {
|
|
23
|
-
array[i] = input.codePointAt(i) || 0;
|
|
24
|
-
}
|
|
25
|
-
return new Uint8Array(array.buffer);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* @function decode
|
|
29
|
-
* @param {BufferSource} input
|
|
30
|
-
* @param {TypeArray} Buffer
|
|
31
|
-
* @returns {string}
|
|
32
|
-
*/
|
|
33
|
-
function decode(input, TypeArray) {
|
|
34
|
-
let result = '';
|
|
35
|
-
const array = new TypeArray(ArrayBuffer.isView(input) ? input.buffer : input);
|
|
36
|
-
for (const code of array) {
|
|
37
|
-
result += String.fromCodePoint(code);
|
|
38
|
-
}
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export { decode, encode };
|
package/esm/Encoding/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Encoding
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* @function encode
|
|
6
|
-
* @description 用指定编码编码字符串
|
|
7
|
-
* @param {string} input 需要编码的字符串
|
|
8
|
-
* @param {string} [encoding] 字符串编码
|
|
9
|
-
* @returns {Uint8Array}
|
|
10
|
-
*/
|
|
11
|
-
export declare function encode(input: string, encoding?: string): Uint8Array;
|
|
12
|
-
/**
|
|
13
|
-
* @function decode
|
|
14
|
-
* @description 用指定编码解码字符串数据
|
|
15
|
-
* @param {BufferSource} input 需要解码的字符串数据
|
|
16
|
-
* @param {string} [encoding] 字符串编码
|
|
17
|
-
* @returns {string}
|
|
18
|
-
*/
|
|
19
|
-
export declare function decode(input: BufferSource, encoding?: string): string;
|
package/esm/Encoding/index.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @package @nuintun/buffer
|
|
3
|
-
* @license MIT
|
|
4
|
-
* @version 0.3.2
|
|
5
|
-
* @author nuintun <nuintun@qq.com>
|
|
6
|
-
* @description A buffer tool for javascript.
|
|
7
|
-
* @see https://github.com/nuintun/Buffer#readme
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { encode as encode$2, decode as decode$2 } from './UTF8.js';
|
|
11
|
-
import { encodingInvalid } from '../errors.js';
|
|
12
|
-
import { encode as encode$1, decode as decode$1 } from './Unicode.js';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @module Encoding
|
|
16
|
-
*/
|
|
17
|
-
/**
|
|
18
|
-
* @function encode
|
|
19
|
-
* @description 用指定编码编码字符串
|
|
20
|
-
* @param {string} input 需要编码的字符串
|
|
21
|
-
* @param {string} [encoding] 字符串编码
|
|
22
|
-
* @returns {Uint8Array}
|
|
23
|
-
*/
|
|
24
|
-
function encode(input, encoding = 'UTF8') {
|
|
25
|
-
switch (encoding.toUpperCase()) {
|
|
26
|
-
case 'UTF8':
|
|
27
|
-
case 'UTF-8':
|
|
28
|
-
return encode$2(input);
|
|
29
|
-
case 'UTF16':
|
|
30
|
-
case 'UTF-16':
|
|
31
|
-
return encode$1(input, Uint16Array);
|
|
32
|
-
case 'UTF32':
|
|
33
|
-
case 'UTF-32':
|
|
34
|
-
return encode$1(input, Uint32Array);
|
|
35
|
-
default:
|
|
36
|
-
throw new TypeError(encodingInvalid(encoding));
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* @function decode
|
|
41
|
-
* @description 用指定编码解码字符串数据
|
|
42
|
-
* @param {BufferSource} input 需要解码的字符串数据
|
|
43
|
-
* @param {string} [encoding] 字符串编码
|
|
44
|
-
* @returns {string}
|
|
45
|
-
*/
|
|
46
|
-
function decode(input, encoding = 'UTF8') {
|
|
47
|
-
switch (encoding.toUpperCase()) {
|
|
48
|
-
case 'UTF8':
|
|
49
|
-
case 'UTF-8':
|
|
50
|
-
return decode$2(input);
|
|
51
|
-
case 'UTF16':
|
|
52
|
-
case 'UTF-16':
|
|
53
|
-
return decode$1(input, Uint16Array);
|
|
54
|
-
case 'UTF32':
|
|
55
|
-
case 'UTF-32':
|
|
56
|
-
return decode$1(input, Uint32Array);
|
|
57
|
-
default:
|
|
58
|
-
throw new TypeError(encodingInvalid(encoding));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export { decode, encode };
|