@nuintun/buffer 0.1.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/LICENSE +21 -0
- package/README.md +310 -0
- package/cjs/Binary.cjs +25 -0
- package/cjs/Binary.d.cts +8 -0
- package/cjs/Encoding/UTF8.cjs +33 -0
- package/cjs/Encoding/UTF8.d.cts +15 -0
- package/cjs/Encoding/Unicode.cjs +45 -0
- package/cjs/Encoding/Unicode.d.cts +18 -0
- package/cjs/Encoding/index.cjs +65 -0
- package/cjs/Encoding/index.d.cts +19 -0
- package/cjs/enum.d.cts +15 -0
- package/cjs/errors.cjs +35 -0
- package/cjs/errors.d.cts +9 -0
- package/cjs/index.cjs +559 -0
- package/cjs/index.d.cts +281 -0
- package/cjs/utils.cjs +31 -0
- package/cjs/utils.d.cts +11 -0
- package/esm/Binary.d.ts +8 -0
- package/esm/Binary.js +23 -0
- package/esm/Encoding/UTF8.d.ts +15 -0
- package/esm/Encoding/UTF8.js +30 -0
- package/esm/Encoding/Unicode.d.ts +18 -0
- package/esm/Encoding/Unicode.js +42 -0
- package/esm/Encoding/index.d.ts +19 -0
- package/esm/Encoding/index.js +62 -0
- package/esm/enum.d.ts +15 -0
- package/esm/errors.d.ts +9 -0
- package/esm/errors.js +28 -0
- package/esm/index.d.ts +281 -0
- package/esm/index.js +568 -0
- package/esm/utils.d.ts +11 -0
- package/esm/utils.js +29 -0
- package/package.json +61 -0
package/cjs/index.d.cts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Buffer
|
|
3
|
+
*/
|
|
4
|
+
export declare enum Endian {
|
|
5
|
+
Big = 0,
|
|
6
|
+
Little = 1
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* @function endianness
|
|
10
|
+
* @description 获取系统默认字节序
|
|
11
|
+
* @returns {Endian}
|
|
12
|
+
*/
|
|
13
|
+
export declare function endianness(): Endian;
|
|
14
|
+
/**
|
|
15
|
+
* @class Buffer
|
|
16
|
+
* @classdesc Buffer 类提供用于优化读取,写入以及处理二进制数据的方法和属性
|
|
17
|
+
*/
|
|
18
|
+
export declare class Buffer {
|
|
19
|
+
#private;
|
|
20
|
+
/**
|
|
21
|
+
* @constructor
|
|
22
|
+
* @param {number} [length] 缓冲区初始大小
|
|
23
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
24
|
+
*/
|
|
25
|
+
constructor(length?: number, pageSize?: number);
|
|
26
|
+
/**
|
|
27
|
+
* @constructor
|
|
28
|
+
* @param {Uint8Array} bytes 缓冲区初始数据
|
|
29
|
+
* @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
|
|
30
|
+
*/
|
|
31
|
+
constructor(bytes?: Uint8Array, pageSize?: number);
|
|
32
|
+
/**
|
|
33
|
+
* @public
|
|
34
|
+
* @property {number} offset
|
|
35
|
+
* @description 设置读写指针位置,以字节为单位
|
|
36
|
+
* @description 下一次调用读写方法时将在此位置开始读写
|
|
37
|
+
*/
|
|
38
|
+
set offset(offset: number);
|
|
39
|
+
/**
|
|
40
|
+
* @public
|
|
41
|
+
* @property {number} offset
|
|
42
|
+
* @description 获取读写指针的位置
|
|
43
|
+
* @returns {number}
|
|
44
|
+
*/
|
|
45
|
+
get offset(): number;
|
|
46
|
+
/**
|
|
47
|
+
* @public
|
|
48
|
+
* @property {number} length
|
|
49
|
+
* @description 设置 Buffer 长度
|
|
50
|
+
* @description 如果将长度设置为小于当前长度的值,将会截断该字节数组
|
|
51
|
+
* @description 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧
|
|
52
|
+
*/
|
|
53
|
+
set length(length: number);
|
|
54
|
+
/**
|
|
55
|
+
* @public
|
|
56
|
+
* @property {number} length
|
|
57
|
+
* @description 获取 Buffer 长度
|
|
58
|
+
* @returns {number}
|
|
59
|
+
*/
|
|
60
|
+
get length(): number;
|
|
61
|
+
/**
|
|
62
|
+
* @public
|
|
63
|
+
* @property {ArrayBuffer} buffer
|
|
64
|
+
* @description 获取 ArrayBuffer 缓冲区
|
|
65
|
+
* @returns {ArrayBuffer}
|
|
66
|
+
*/
|
|
67
|
+
get buffer(): ArrayBuffer;
|
|
68
|
+
/**
|
|
69
|
+
* @public
|
|
70
|
+
* @property {Uint8Array} bytes
|
|
71
|
+
* @description 获取 Uint8Array 缓冲区
|
|
72
|
+
* @returns {Uint8Array}
|
|
73
|
+
*/
|
|
74
|
+
get bytes(): Uint8Array;
|
|
75
|
+
/**
|
|
76
|
+
* @public
|
|
77
|
+
* @method writeInt8
|
|
78
|
+
* @description 在缓冲区中写入一个有符号整数
|
|
79
|
+
* @param {number} value 介于 -128 和 127 之间的整数
|
|
80
|
+
*/
|
|
81
|
+
writeInt8(value: number): void;
|
|
82
|
+
/**
|
|
83
|
+
* @public
|
|
84
|
+
* @method writeUint8
|
|
85
|
+
* @description 在缓冲区中写入一个无符号整数
|
|
86
|
+
* @param {number} value 介于 0 和 255 之间的整数
|
|
87
|
+
*/
|
|
88
|
+
writeUint8(value: number): void;
|
|
89
|
+
/**
|
|
90
|
+
* @method writeBoolean
|
|
91
|
+
* @description 在缓冲区中写入布尔值,true 写 1,false写 0
|
|
92
|
+
* @param {boolean} value 布尔值
|
|
93
|
+
*/
|
|
94
|
+
writeBoolean(value: boolean): void;
|
|
95
|
+
/**
|
|
96
|
+
* @method writeInt16
|
|
97
|
+
* @description 在缓冲区中写入一个 16 位有符号整数
|
|
98
|
+
* @param {number} value 要写入的 16 位有符号整数
|
|
99
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
100
|
+
*/
|
|
101
|
+
writeInt16(value: number, littleEndian?: boolean): void;
|
|
102
|
+
/**
|
|
103
|
+
* @method writeUint16
|
|
104
|
+
* @description 在缓冲区中写入一个 16 位无符号整数
|
|
105
|
+
* @param {number} value 要写入的 16 位无符号整数
|
|
106
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
107
|
+
*/
|
|
108
|
+
writeUint16(value: number, littleEndian?: boolean): void;
|
|
109
|
+
/**
|
|
110
|
+
* @method writeInt32
|
|
111
|
+
* @description 在缓冲区中写入一个有符号的 32 位有符号整数
|
|
112
|
+
* @param {number} value 要写入的 32 位有符号整数
|
|
113
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
114
|
+
*/
|
|
115
|
+
writeInt32(value: number, littleEndian?: boolean): void;
|
|
116
|
+
/**
|
|
117
|
+
* @method writeUint32
|
|
118
|
+
* @description 在缓冲区中写入一个无符号的 32 位无符号整数
|
|
119
|
+
* @param {number} value 要写入的 32 位无符号整数
|
|
120
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
121
|
+
*/
|
|
122
|
+
writeUint32(value: number, littleEndian?: boolean): void;
|
|
123
|
+
/**
|
|
124
|
+
* @method writeInt64
|
|
125
|
+
* @description 在缓冲区中写入一个无符号的 64 位有符号整数
|
|
126
|
+
* @param {bigint} value 要写入的 32 位有符号整数
|
|
127
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
128
|
+
*/
|
|
129
|
+
writeInt64(value: bigint, littleEndian?: boolean): void;
|
|
130
|
+
/**
|
|
131
|
+
* @method writeUint64
|
|
132
|
+
* @description 在缓冲区中写入一个无符号的 64 位无符号整数
|
|
133
|
+
* @param {bigint} value 要写入的 64 位无符号整数
|
|
134
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
135
|
+
*/
|
|
136
|
+
writeUint64(value: bigint, littleEndian?: boolean): void;
|
|
137
|
+
/**
|
|
138
|
+
* @method writeFloat32
|
|
139
|
+
* @description 在缓冲区中写入一个 IEEE 754 单精度 32 位浮点数
|
|
140
|
+
* @param {number} value 单精度 32 位浮点数
|
|
141
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
142
|
+
*/
|
|
143
|
+
writeFloat32(value: number, littleEndian?: boolean): void;
|
|
144
|
+
/**
|
|
145
|
+
* @method writeFloat64
|
|
146
|
+
* @description 在缓冲区中写入一个 IEEE 754 双精度 64 位浮点数
|
|
147
|
+
* @param {number} value 双精度 64 位浮点数
|
|
148
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
149
|
+
*/
|
|
150
|
+
writeFloat64(value: number, littleEndian?: boolean): void;
|
|
151
|
+
/**
|
|
152
|
+
* @method write
|
|
153
|
+
* @description 将字符串用指定编码写入字节流
|
|
154
|
+
* @param {string} value 要写入的字符串
|
|
155
|
+
* @param {string} [encoding] 字符串编码
|
|
156
|
+
*/
|
|
157
|
+
write(value: string, encoding?: string): void;
|
|
158
|
+
/**
|
|
159
|
+
* @method write
|
|
160
|
+
* @description 将 Uint8Array 对象写入字节流
|
|
161
|
+
* @param {Uint8Array} bytes 要写入 Uint8Array 对象
|
|
162
|
+
* @param {number} [start] Uint8Array 对象开始索引
|
|
163
|
+
* @param {number} [end] Uint8Array 对象结束索引
|
|
164
|
+
*/
|
|
165
|
+
write(bytes: Uint8Array, start?: number, end?: number): void;
|
|
166
|
+
/**
|
|
167
|
+
* @method readInt8
|
|
168
|
+
* @description 从缓冲区中读取有符号的整数
|
|
169
|
+
* @returns {number} 介于 -128 和 127 之间的整数
|
|
170
|
+
*/
|
|
171
|
+
readInt8(): number;
|
|
172
|
+
/**
|
|
173
|
+
* @method readUint8
|
|
174
|
+
* @description 从缓冲区中读取无符号的整数
|
|
175
|
+
* @returns {number} 介于 0 和 255 之间的无符号整数
|
|
176
|
+
*/
|
|
177
|
+
readUint8(): number;
|
|
178
|
+
/**
|
|
179
|
+
* @method readBoolean
|
|
180
|
+
* @description 从缓冲区中读取布尔值
|
|
181
|
+
* @returns {boolean} 如果字节非零,则返回 true,否则返回 false
|
|
182
|
+
*/
|
|
183
|
+
readBoolean(): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* @method readInt16
|
|
186
|
+
* @description 从缓冲区中读取一个 16 位有符号整数
|
|
187
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
188
|
+
* @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
|
|
189
|
+
*/
|
|
190
|
+
readInt16(littleEndian?: boolean): number;
|
|
191
|
+
/**
|
|
192
|
+
* @method readUint16
|
|
193
|
+
* @description 从缓冲区中读取一个 16 位无符号整数
|
|
194
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
195
|
+
* @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
|
|
196
|
+
*/
|
|
197
|
+
readUint16(littleEndian?: boolean): number;
|
|
198
|
+
/**
|
|
199
|
+
* @method readInt32
|
|
200
|
+
* @description 从缓冲区中读取一个 32 位有符号整数
|
|
201
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
202
|
+
* @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
|
|
203
|
+
*/
|
|
204
|
+
readInt32(littleEndian?: boolean): number;
|
|
205
|
+
/**
|
|
206
|
+
* @method readUint32
|
|
207
|
+
* @description 从缓冲区中读取一个 32 位无符号整数
|
|
208
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
209
|
+
* @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
|
|
210
|
+
*/
|
|
211
|
+
readUint32(littleEndian?: boolean): number;
|
|
212
|
+
/**
|
|
213
|
+
* @method readInt64
|
|
214
|
+
* @description 从缓冲区中读取一个 64 位有符号整数
|
|
215
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
216
|
+
* @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
|
|
217
|
+
*/
|
|
218
|
+
readInt64(littleEndian?: boolean): bigint;
|
|
219
|
+
/**
|
|
220
|
+
* @method readUint64
|
|
221
|
+
* @description 从缓冲区中读取一个 64 位无符号整数
|
|
222
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
223
|
+
* @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
|
|
224
|
+
*/
|
|
225
|
+
readUint64(littleEndian?: boolean): bigint;
|
|
226
|
+
/**
|
|
227
|
+
* @method readFloat32
|
|
228
|
+
* @description 从缓冲区中读取一个 IEEE 754 单精度 32 位浮点数
|
|
229
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
230
|
+
* @returns {number} 单精度 32 位浮点数
|
|
231
|
+
*/
|
|
232
|
+
readFloat32(littleEndian?: boolean): number;
|
|
233
|
+
/**
|
|
234
|
+
* @method readFloat64
|
|
235
|
+
* @description 从缓冲区中读取一个 IEEE 754 双精度 64 位浮点数
|
|
236
|
+
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
237
|
+
* @returns {number} 双精度 64 位浮点数
|
|
238
|
+
*/
|
|
239
|
+
readFloat64(littleEndian?: boolean): number;
|
|
240
|
+
/**
|
|
241
|
+
* @method read
|
|
242
|
+
* @description 从缓冲区中读取指定长度的 Uint8Array 对象
|
|
243
|
+
* @param {number} length 读取的字节长度
|
|
244
|
+
* @returns {Uint8Array}
|
|
245
|
+
*/
|
|
246
|
+
read(length: number): Uint8Array;
|
|
247
|
+
/**
|
|
248
|
+
* @method read
|
|
249
|
+
* @description 从缓冲区中读取一个字符串
|
|
250
|
+
* @param {number} length 读取的字节长度
|
|
251
|
+
* @param {string} encoding 字符串编码
|
|
252
|
+
* @returns {string} 指定编码的字符串
|
|
253
|
+
*/
|
|
254
|
+
read(length: number, encoding: string): string;
|
|
255
|
+
/**
|
|
256
|
+
* @public
|
|
257
|
+
* @method slice
|
|
258
|
+
* @description 从指定开始和结束位置索引截取并返回新的 Buffer 对象
|
|
259
|
+
* @param {number} [start] 截取开始位置索引
|
|
260
|
+
* @param {number} [end] 截取结束位置索引
|
|
261
|
+
* @returns {Buffer}
|
|
262
|
+
*/
|
|
263
|
+
slice(start?: number, end?: number): Buffer;
|
|
264
|
+
/**
|
|
265
|
+
* @public
|
|
266
|
+
* @method copyWithin
|
|
267
|
+
* @description 从 Buffer 对象中将指定位置的数据复制到以 target 起始的位置
|
|
268
|
+
* @param {number} target 粘贴开始位置索引
|
|
269
|
+
* @param {number} start 复制开始位置索引
|
|
270
|
+
* @param {number} [end] 复制结束位置索引
|
|
271
|
+
* @returns {this}
|
|
272
|
+
*/
|
|
273
|
+
copyWithin(target: number, start: number, end?: number): this;
|
|
274
|
+
/**
|
|
275
|
+
* @override
|
|
276
|
+
* @method toString
|
|
277
|
+
* @description 获取 Buffer 对象二进制编码字符串
|
|
278
|
+
* @returns {string}
|
|
279
|
+
*/
|
|
280
|
+
toString(): string;
|
|
281
|
+
}
|
package/cjs/utils.cjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.1.0
|
|
5
|
+
* @author nuintun <nuintun@qq.com>
|
|
6
|
+
* @description A buffer tool for javascript.
|
|
7
|
+
* @see https://github.com/nuintun/Buffer#readme
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @module utils
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @function calcBufferLength
|
|
17
|
+
* @description 计算适合的 Buffer 长度
|
|
18
|
+
* @param {number} length 数据字节总大小
|
|
19
|
+
* @param {number} pageSize 缓冲区页大小
|
|
20
|
+
* @returns {number}
|
|
21
|
+
*/
|
|
22
|
+
function calcBufferLength(length, pageSize) {
|
|
23
|
+
if (length > pageSize) {
|
|
24
|
+
const pages = Math.ceil(length / pageSize);
|
|
25
|
+
return pages * pageSize;
|
|
26
|
+
} else {
|
|
27
|
+
return length;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.calcBufferLength = calcBufferLength;
|
package/cjs/utils.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module utils
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @function calcBufferLength
|
|
6
|
+
* @description 计算适合的 Buffer 长度
|
|
7
|
+
* @param {number} length 数据字节总大小
|
|
8
|
+
* @param {number} pageSize 缓冲区页大小
|
|
9
|
+
* @returns {number}
|
|
10
|
+
*/
|
|
11
|
+
export declare function calcBufferLength(length: number, pageSize: number): number;
|
package/esm/Binary.d.ts
ADDED
package/esm/Binary.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.1.0
|
|
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 Binary
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* @type {string[]}
|
|
15
|
+
* @description 已获得的二进制映射表
|
|
16
|
+
*/
|
|
17
|
+
const mapping = [];
|
|
18
|
+
// 生成映射表
|
|
19
|
+
for (let code = 0; code < 256; code++) {
|
|
20
|
+
mapping[code] = String.fromCharCode(code);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { mapping };
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.1.0
|
|
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 };
|
|
@@ -0,0 +1,18 @@
|
|
|
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;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.1.0
|
|
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 };
|
|
@@ -0,0 +1,19 @@
|
|
|
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;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.1.0
|
|
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 };
|
package/esm/enum.d.ts
ADDED
package/esm/errors.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module errors
|
|
3
|
+
*/
|
|
4
|
+
export declare function encodingInvalid(encoding: string): string;
|
|
5
|
+
export declare const unknownEndianness = 'unknown endianness';
|
|
6
|
+
export declare const lengthInvalid = 'invalid buffer length';
|
|
7
|
+
export declare const offsetInvalid = 'invalid buffer offset';
|
|
8
|
+
export declare const readOverflow = 'read is outside the bounds of the Buffer';
|
|
9
|
+
export declare const offsetOverflow = 'offset is outside the bounds of the Buffer';
|
package/esm/errors.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.1.0
|
|
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 errors
|
|
12
|
+
*/
|
|
13
|
+
// 未支持的编码格式
|
|
14
|
+
function encodingInvalid(encoding) {
|
|
15
|
+
return 'unsupported encoding ' + encoding;
|
|
16
|
+
}
|
|
17
|
+
// 未知字节序
|
|
18
|
+
const unknownEndianness = 'unknown endianness';
|
|
19
|
+
// 非法长度
|
|
20
|
+
const lengthInvalid = 'invalid buffer length';
|
|
21
|
+
// 非法读写指针
|
|
22
|
+
const offsetInvalid = 'invalid buffer offset';
|
|
23
|
+
// 数据读取溢出
|
|
24
|
+
const readOverflow = 'read is outside the bounds of the Buffer';
|
|
25
|
+
// 读写指针溢出
|
|
26
|
+
const offsetOverflow = 'offset is outside the bounds of the Buffer';
|
|
27
|
+
|
|
28
|
+
export { encodingInvalid, lengthInvalid, offsetInvalid, offsetOverflow, readOverflow, unknownEndianness };
|