@nuintun/buffer 0.3.0 → 0.3.2

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/esm/index.js CHANGED
@@ -1,36 +1,31 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
8
8
  */
9
9
 
10
- import { makeUint8Array } from './utils.js';
11
10
  import { mapping } from './Binary.js';
12
- import { unknownEndianness, readOverflow, offsetInvalid, offsetOverflow, lengthInvalid } from './errors.js';
11
+ import { unknownEndianness, readOverflow, offsetInvalid, lengthInvalid, readLengthInvalid } from './errors.js';
13
12
  import { encode, decode } from './Encoding/index.js';
13
+ import { Endian } from './enum.js';
14
+ import { isTypedArray, makeUint8Array, isNaturalNumber } from './utils.js';
14
15
 
15
16
  /**
16
17
  * @module Buffer
17
18
  */
18
- // 字节序类型
19
- var Endian;
20
- (function (Endian) {
21
- Endian[(Endian['Big'] = 0)] = 'Big';
22
- Endian[(Endian['Little'] = 1)] = 'Little';
23
- })(Endian || (Endian = {}));
24
19
  /**
25
20
  * @function endianness
26
21
  * @description 获取系统默认字节序
27
22
  * @returns {Endian}
28
23
  */
29
24
  function endianness() {
30
- switch (new Uint8Array(new Uint32Array([0x12345678]))[0]) {
31
- case 0x12:
25
+ switch (new Uint8Array(new Uint16Array([0x00ff]).buffer)[0]) {
26
+ case 0x00:
32
27
  return Endian.Big;
33
- case 0x78:
28
+ case 0xff:
34
29
  return Endian.Little;
35
30
  default:
36
31
  throw new TypeError(unknownEndianness);
@@ -52,45 +47,64 @@ class Buffer {
52
47
  #offset = 0;
53
48
  // 已使用字节长度
54
49
  #length = 0;
50
+ /**
51
+ * @constructor
52
+ * @param {number | Uint8Array | ArrayBuffer} [input] 缓冲区初始配置
53
+ * @param {number} [pageSize] 缓冲区分页大小,扩容时将按分页大小增加
54
+ */
55
55
  constructor(input = 0, pageSize = 4096) {
56
- this.#pageSize = pageSize;
57
- if (input instanceof Uint8Array) {
58
- this.#bytes = input;
59
- this.#length = input.length;
60
- this.#dataView = new DataView(input.buffer);
56
+ let length;
57
+ let bytes;
58
+ if (isTypedArray(input)) {
59
+ length = input.byteLength;
60
+ bytes = makeUint8Array(length, pageSize);
61
+ if (length > 0) {
62
+ bytes.set(new Uint8Array(input.buffer));
63
+ }
64
+ } else if (input instanceof ArrayBuffer) {
65
+ length = input.byteLength;
66
+ bytes = makeUint8Array(length, pageSize);
67
+ if (length > 0) {
68
+ bytes.set(new Uint8Array(input));
69
+ }
61
70
  } else {
62
- const bytes = makeUint8Array(input, pageSize);
63
- this.#bytes = bytes;
64
- this.#length = input;
65
- this.#dataView = new DataView(bytes.buffer);
71
+ length = input;
72
+ bytes = makeUint8Array(length, pageSize);
66
73
  }
74
+ this.#bytes = bytes;
75
+ this.#length = length;
76
+ this.#pageSize = pageSize;
77
+ this.#dataView = new DataView(bytes.buffer);
67
78
  }
68
79
  /**
69
80
  * @private
70
- * @method grow
71
- * @description 增加长度
72
- * @param {number} length 长度增加量
81
+ * @method seek
82
+ * @description 移动读写指针
83
+ * @param {number} offset 指针位置
73
84
  */
74
- #grow(length) {
75
- this.#length += length;
85
+ #seek(offset) {
86
+ if (offset > this.#length) {
87
+ this.#length = offset;
88
+ }
89
+ this.#offset = offset;
76
90
  }
77
91
  /**
78
92
  * @private
79
- * @method seek
80
- * @description 移动读写指针
81
- * @param {number} offset 指针偏移量
93
+ * @method getOffset
94
+ * @description 根据数据类型获取最新指针位置
95
+ * @param size 数据类型长度
82
96
  */
83
- #seek(offset) {
84
- this.#offset += offset;
97
+ #getOffset(size) {
98
+ return this.#offset + size;
85
99
  }
86
100
  /**
87
101
  * @private
88
102
  * @method assertRead
89
103
  * @description 读取断言,防止越界读取
90
- * @param {number} length 断言字节长度
104
+ * @param {number} size 断言字节长度
91
105
  */
92
106
  #assertRead(length) {
93
- if (length < 0 || this.#offset + length > this.#length) {
107
+ if (length > this.#length) {
94
108
  throw new RangeError(readOverflow);
95
109
  }
96
110
  }
@@ -101,7 +115,6 @@ class Buffer {
101
115
  * @param {number} length 分配字节长度
102
116
  */
103
117
  #alloc(length) {
104
- length += this.#offset;
105
118
  const bytes = this.#bytes;
106
119
  if (length > bytes.length) {
107
120
  const newBytes = makeUint8Array(length, this.#pageSize);
@@ -117,12 +130,9 @@ class Buffer {
117
130
  * @description 下一次调用读写方法时将在此位置开始读写
118
131
  */
119
132
  set offset(offset) {
120
- if (offset < 0) {
133
+ if (!isNaturalNumber(offset)) {
121
134
  throw new RangeError(offsetInvalid);
122
135
  }
123
- if (offset > this.#length) {
124
- throw new RangeError(offsetOverflow);
125
- }
126
136
  this.#offset = offset;
127
137
  }
128
138
  /**
@@ -142,7 +152,7 @@ class Buffer {
142
152
  * @description 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧
143
153
  */
144
154
  set length(length) {
145
- if (length < 0) {
155
+ if (!isNaturalNumber(length)) {
146
156
  throw new RangeError(lengthInvalid);
147
157
  }
148
158
  const currentLength = this.#length;
@@ -169,16 +179,16 @@ class Buffer {
169
179
  /**
170
180
  * @public
171
181
  * @property {ArrayBuffer} buffer
172
- * @description 获取 ArrayBuffer 缓冲区
182
+ * @description 获取全部 ArrayBuffer 原始缓冲区
173
183
  * @returns {ArrayBuffer}
174
184
  */
175
185
  get buffer() {
176
- return this.bytes.buffer;
186
+ return this.#bytes.buffer;
177
187
  }
178
188
  /**
179
189
  * @public
180
190
  * @property {Uint8Array} bytes
181
- * @description 获取 Uint8Array 缓冲区
191
+ * @description 获取已写入 Uint8Array 原始缓冲区
182
192
  * @returns {Uint8Array}
183
193
  */
184
194
  get bytes() {
@@ -191,10 +201,10 @@ class Buffer {
191
201
  * @param {number} value 介于 -128 和 127 之间的整数
192
202
  */
193
203
  writeInt8(value) {
194
- this.#alloc(1 /* SizeOf.INT8 */);
204
+ const offset = this.#getOffset(1 /* SizeOf.INT8 */);
205
+ this.#alloc(offset);
195
206
  this.#dataView.setInt8(this.#offset, value);
196
- this.#grow(1 /* SizeOf.INT8 */);
197
- this.#seek(1 /* SizeOf.INT8 */);
207
+ this.#seek(offset);
198
208
  }
199
209
  /**
200
210
  * @public
@@ -203,10 +213,10 @@ class Buffer {
203
213
  * @param {number} value 介于 0 和 255 之间的整数
204
214
  */
205
215
  writeUint8(value) {
206
- this.#alloc(1 /* SizeOf.UINT8 */);
216
+ const offset = this.#getOffset(1 /* SizeOf.UINT8 */);
217
+ this.#alloc(offset);
207
218
  this.#dataView.setUint8(this.#offset, value);
208
- this.#grow(1 /* SizeOf.UINT8 */);
209
- this.#seek(1 /* SizeOf.UINT8 */);
219
+ this.#seek(offset);
210
220
  }
211
221
  /**
212
222
  * @method writeBoolean
@@ -223,10 +233,10 @@ class Buffer {
223
233
  * @param {boolean} [littleEndian] 是否为小端字节序
224
234
  */
225
235
  writeInt16(value, littleEndian) {
226
- this.#alloc(2 /* SizeOf.INT16 */);
236
+ const offset = this.#getOffset(2 /* SizeOf.INT16 */);
237
+ this.#alloc(offset);
227
238
  this.#dataView.setInt16(this.#offset, value, littleEndian);
228
- this.#grow(2 /* SizeOf.INT16 */);
229
- this.#seek(2 /* SizeOf.INT16 */);
239
+ this.#seek(offset);
230
240
  }
231
241
  /**
232
242
  * @method writeUint16
@@ -235,10 +245,10 @@ class Buffer {
235
245
  * @param {boolean} [littleEndian] 是否为小端字节序
236
246
  */
237
247
  writeUint16(value, littleEndian) {
238
- this.#alloc(2 /* SizeOf.UINT16 */);
248
+ const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
249
+ this.#alloc(offset);
239
250
  this.#dataView.setUint16(this.#offset, value, littleEndian);
240
- this.#grow(2 /* SizeOf.UINT16 */);
241
- this.#seek(2 /* SizeOf.UINT16 */);
251
+ this.#seek(offset);
242
252
  }
243
253
  /**
244
254
  * @method writeInt32
@@ -247,10 +257,10 @@ class Buffer {
247
257
  * @param {boolean} [littleEndian] 是否为小端字节序
248
258
  */
249
259
  writeInt32(value, littleEndian) {
250
- this.#alloc(4 /* SizeOf.INT32 */);
260
+ const offset = this.#getOffset(4 /* SizeOf.INT32 */);
261
+ this.#alloc(offset);
251
262
  this.#dataView.setInt32(this.#offset, value, littleEndian);
252
- this.#grow(4 /* SizeOf.INT32 */);
253
- this.#seek(4 /* SizeOf.INT32 */);
263
+ this.#seek(offset);
254
264
  }
255
265
  /**
256
266
  * @method writeUint32
@@ -259,10 +269,10 @@ class Buffer {
259
269
  * @param {boolean} [littleEndian] 是否为小端字节序
260
270
  */
261
271
  writeUint32(value, littleEndian) {
262
- this.#alloc(4 /* SizeOf.UINT32 */);
272
+ const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
273
+ this.#alloc(offset);
263
274
  this.#dataView.setUint32(this.#offset, value, littleEndian);
264
- this.#grow(4 /* SizeOf.UINT32 */);
265
- this.#seek(4 /* SizeOf.UINT32 */);
275
+ this.#seek(offset);
266
276
  }
267
277
  /**
268
278
  * @method writeInt64
@@ -271,10 +281,10 @@ class Buffer {
271
281
  * @param {boolean} [littleEndian] 是否为小端字节序
272
282
  */
273
283
  writeInt64(value, littleEndian) {
274
- this.#alloc(8 /* SizeOf.INT64 */);
284
+ const offset = this.#getOffset(8 /* SizeOf.INT64 */);
285
+ this.#alloc(offset);
275
286
  this.#dataView.setBigInt64(this.#offset, value, littleEndian);
276
- this.#grow(8 /* SizeOf.INT64 */);
277
- this.#seek(8 /* SizeOf.INT64 */);
287
+ this.#seek(offset);
278
288
  }
279
289
  /**
280
290
  * @method writeUint64
@@ -283,10 +293,10 @@ class Buffer {
283
293
  * @param {boolean} [littleEndian] 是否为小端字节序
284
294
  */
285
295
  writeUint64(value, littleEndian) {
286
- this.#alloc(8 /* SizeOf.UINT64 */);
296
+ const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
297
+ this.#alloc(offset);
287
298
  this.#dataView.setBigUint64(this.#offset, value, littleEndian);
288
- this.#grow(8 /* SizeOf.UINT64 */);
289
- this.#seek(8 /* SizeOf.UINT64 */);
299
+ this.#seek(offset);
290
300
  }
291
301
  /**
292
302
  * @method writeFloat32
@@ -295,10 +305,10 @@ class Buffer {
295
305
  * @param {boolean} [littleEndian] 是否为小端字节序
296
306
  */
297
307
  writeFloat32(value, littleEndian) {
298
- this.#alloc(4 /* SizeOf.FLOAT32 */);
308
+ const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
309
+ this.#alloc(offset);
299
310
  this.#dataView.setFloat32(this.#offset, value, littleEndian);
300
- this.#grow(4 /* SizeOf.FLOAT32 */);
301
- this.#seek(4 /* SizeOf.FLOAT32 */);
311
+ this.#seek(offset);
302
312
  }
303
313
  /**
304
314
  * @method writeFloat64
@@ -307,10 +317,10 @@ class Buffer {
307
317
  * @param {boolean} [littleEndian] 是否为小端字节序
308
318
  */
309
319
  writeFloat64(value, littleEndian) {
310
- this.#alloc(8 /* SizeOf.FLOAT64 */);
320
+ const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
321
+ this.#alloc(offset);
311
322
  this.#dataView.setFloat64(this.#offset, value, littleEndian);
312
- this.#grow(8 /* SizeOf.FLOAT64 */);
313
- this.#seek(8 /* SizeOf.FLOAT64 */);
323
+ this.#seek(offset);
314
324
  }
315
325
  write(input, start, end) {
316
326
  let bytes;
@@ -321,10 +331,10 @@ class Buffer {
321
331
  }
322
332
  const { length } = bytes;
323
333
  if (length > 0) {
324
- this.#alloc(length);
334
+ const offset = this.#getOffset(length);
335
+ this.#alloc(offset);
325
336
  this.#bytes.set(bytes, this.#offset);
326
- this.#grow(length);
327
- this.#seek(length);
337
+ this.#seek(offset);
328
338
  }
329
339
  }
330
340
  /**
@@ -333,9 +343,10 @@ class Buffer {
333
343
  * @returns {number} 介于 -128 和 127 之间的整数
334
344
  */
335
345
  readInt8() {
336
- this.#assertRead(1 /* SizeOf.INT8 */);
346
+ const offset = this.#getOffset(1 /* SizeOf.INT8 */);
347
+ this.#assertRead(offset);
337
348
  const value = this.#dataView.getInt8(this.#offset);
338
- this.#seek(1 /* SizeOf.INT8 */);
349
+ this.#seek(offset);
339
350
  return value;
340
351
  }
341
352
  /**
@@ -344,9 +355,10 @@ class Buffer {
344
355
  * @returns {number} 介于 0 和 255 之间的无符号整数
345
356
  */
346
357
  readUint8() {
347
- this.#assertRead(1 /* SizeOf.UINT8 */);
358
+ const offset = this.#getOffset(1 /* SizeOf.UINT8 */);
359
+ this.#assertRead(offset);
348
360
  const value = this.#dataView.getUint8(this.#offset);
349
- this.#seek(1 /* SizeOf.UINT8 */);
361
+ this.#seek(offset);
350
362
  return value;
351
363
  }
352
364
  /**
@@ -364,9 +376,10 @@ class Buffer {
364
376
  * @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
365
377
  */
366
378
  readInt16(littleEndian) {
367
- this.#assertRead(2 /* SizeOf.INT16 */);
379
+ const offset = this.#getOffset(2 /* SizeOf.INT16 */);
380
+ this.#assertRead(offset);
368
381
  const value = this.#dataView.getInt16(this.#offset, littleEndian);
369
- this.#seek(2 /* SizeOf.INT16 */);
382
+ this.#seek(offset);
370
383
  return value;
371
384
  }
372
385
  /**
@@ -376,9 +389,10 @@ class Buffer {
376
389
  * @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
377
390
  */
378
391
  readUint16(littleEndian) {
379
- this.#assertRead(2 /* SizeOf.UINT16 */);
392
+ const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
393
+ this.#assertRead(offset);
380
394
  const value = this.#dataView.getUint16(this.#offset, littleEndian);
381
- this.#seek(2 /* SizeOf.UINT16 */);
395
+ this.#seek(offset);
382
396
  return value;
383
397
  }
384
398
  /**
@@ -388,9 +402,10 @@ class Buffer {
388
402
  * @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
389
403
  */
390
404
  readInt32(littleEndian) {
391
- this.#assertRead(4 /* SizeOf.INT32 */);
405
+ const offset = this.#getOffset(4 /* SizeOf.INT32 */);
406
+ this.#assertRead(offset);
392
407
  const value = this.#dataView.getInt32(this.#offset, littleEndian);
393
- this.#seek(4 /* SizeOf.INT32 */);
408
+ this.#seek(offset);
394
409
  return value;
395
410
  }
396
411
  /**
@@ -400,9 +415,10 @@ class Buffer {
400
415
  * @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
401
416
  */
402
417
  readUint32(littleEndian) {
403
- this.#assertRead(4 /* SizeOf.UINT32 */);
418
+ const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
419
+ this.#assertRead(offset);
404
420
  const value = this.#dataView.getUint32(this.#offset, littleEndian);
405
- this.#seek(4 /* SizeOf.UINT32 */);
421
+ this.#seek(offset);
406
422
  return value;
407
423
  }
408
424
  /**
@@ -412,9 +428,10 @@ class Buffer {
412
428
  * @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
413
429
  */
414
430
  readInt64(littleEndian) {
415
- this.#assertRead(8 /* SizeOf.INT64 */);
431
+ const offset = this.#getOffset(8 /* SizeOf.INT64 */);
432
+ this.#assertRead(offset);
416
433
  const value = this.#dataView.getBigInt64(this.#offset, littleEndian);
417
- this.#seek(8 /* SizeOf.INT64 */);
434
+ this.#seek(offset);
418
435
  return value;
419
436
  }
420
437
  /**
@@ -424,9 +441,10 @@ class Buffer {
424
441
  * @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
425
442
  */
426
443
  readUint64(littleEndian) {
427
- this.#assertRead(8 /* SizeOf.UINT64 */);
444
+ const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
445
+ this.#assertRead(offset);
428
446
  const value = this.#dataView.getBigUint64(this.#offset, littleEndian);
429
- this.#seek(8 /* SizeOf.UINT64 */);
447
+ this.#seek(offset);
430
448
  return value;
431
449
  }
432
450
  /**
@@ -436,9 +454,10 @@ class Buffer {
436
454
  * @returns {number} 单精度 32 位浮点数
437
455
  */
438
456
  readFloat32(littleEndian) {
439
- this.#assertRead(4 /* SizeOf.FLOAT32 */);
457
+ const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
458
+ this.#assertRead(offset);
440
459
  const value = this.#dataView.getFloat32(this.#offset, littleEndian);
441
- this.#seek(4 /* SizeOf.FLOAT32 */);
460
+ this.#seek(offset);
442
461
  return value;
443
462
  }
444
463
  /**
@@ -448,16 +467,20 @@ class Buffer {
448
467
  * @returns {number} 双精度 64 位浮点数
449
468
  */
450
469
  readFloat64(littleEndian) {
451
- this.#assertRead(8 /* SizeOf.FLOAT64 */);
470
+ const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
471
+ this.#assertRead(offset);
452
472
  const value = this.#dataView.getFloat64(this.#offset, littleEndian);
453
- this.#seek(8 /* SizeOf.FLOAT64 */);
473
+ this.#seek(offset);
454
474
  return value;
455
475
  }
456
476
  read(length, encoding) {
457
- this.#assertRead(length);
458
- const offset = this.#offset;
459
- const bytes = this.#bytes.slice(offset, offset + length);
460
- this.#seek(length);
477
+ if (!isNaturalNumber(length)) {
478
+ throw new RangeError(readLengthInvalid);
479
+ }
480
+ const offset = this.#getOffset(length);
481
+ this.#assertRead(offset);
482
+ const bytes = this.#bytes.slice(this.#offset, offset);
483
+ this.#seek(offset);
461
484
  if (arguments.length >= 2) {
462
485
  return decode(bytes, encoding);
463
486
  }
@@ -488,6 +511,30 @@ class Buffer {
488
511
  this.#bytes.copyWithin(target, start, end);
489
512
  return this;
490
513
  }
514
+ /**
515
+ * @method entries
516
+ * @description 获取迭代器
517
+ * @returns {IterableIterator<[number, number]>}
518
+ */
519
+ *entries() {
520
+ const bytes = this.bytes;
521
+ const length = this.#length;
522
+ for (let i = 0; i < length; i++) {
523
+ yield [i, bytes[i]];
524
+ }
525
+ }
526
+ /**
527
+ * @method values
528
+ * @description 获取迭代器
529
+ * @returns {IterableIterator<number>}
530
+ */
531
+ *values() {
532
+ const bytes = this.bytes;
533
+ const length = this.#length;
534
+ for (let i = 0; i < length; i++) {
535
+ yield bytes[i];
536
+ }
537
+ }
491
538
  /**
492
539
  * @override
493
540
  * @method toString
@@ -506,6 +553,14 @@ class Buffer {
506
553
  // 返回二进制编码
507
554
  return binary;
508
555
  }
556
+ /**
557
+ * @method iterator
558
+ * @description 迭代器
559
+ * @returns {IterableIterator<number>}
560
+ */
561
+ [Symbol.iterator]() {
562
+ return this.values();
563
+ }
509
564
  }
510
565
 
511
566
  export { Buffer, Endian, endianness };
package/esm/utils.d.ts CHANGED
@@ -1,11 +1,37 @@
1
1
  /**
2
2
  * @module utils
3
3
  */
4
+ export type TypedArray =
5
+ | Int8Array
6
+ | Int16Array
7
+ | Int32Array
8
+ | Uint8Array
9
+ | Uint16Array
10
+ | Uint32Array
11
+ | Float32Array
12
+ | Float64Array
13
+ | BigInt64Array
14
+ | BigUint64Array
15
+ | Uint8ClampedArray;
16
+ /**
17
+ * @function isTypedArray
18
+ * @description 检测是否为 TypedArray
19
+ * @param value 待判断的值
20
+ * @returns {boolean}
21
+ */
22
+ export declare function isTypedArray(value: unknown): value is TypedArray;
23
+ /**
24
+ * @function isNaturalNumber
25
+ * @description 判断是否为自然数
26
+ * @param value 待判断的值
27
+ * @returns {boolean}
28
+ */
29
+ export declare function isNaturalNumber(value: unknown): value is number;
4
30
  /**
5
31
  * @function makeUint8Array
6
32
  * @description 创建一个合适长度的 Uint8Array
7
- * @param {number} byteLength 数据字节总大小
33
+ * @param {number} length 数据长度大小
8
34
  * @param {number} pageSize 缓冲区页大小
9
35
  * @returns {Uint8Array}
10
36
  */
11
- export declare function makeUint8Array(byteLength: number, pageSize: number): Uint8Array;
37
+ export declare function makeUint8Array(length: number, pageSize: number): Uint8Array;
package/esm/utils.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @package @nuintun/buffer
3
3
  * @license MIT
4
- * @version 0.3.0
4
+ * @version 0.3.2
5
5
  * @author nuintun <nuintun@qq.com>
6
6
  * @description A buffer tool for javascript.
7
7
  * @see https://github.com/nuintun/Buffer#readme
@@ -10,18 +10,38 @@
10
10
  /**
11
11
  * @module utils
12
12
  */
13
+ // 获取 TypedArray 原型
14
+ const TypedArray = Object.getPrototypeOf(Uint8Array);
15
+ /**
16
+ * @function isTypedArray
17
+ * @description 检测是否为 TypedArray
18
+ * @param value 待判断的值
19
+ * @returns {boolean}
20
+ */
21
+ function isTypedArray(value) {
22
+ return value instanceof TypedArray;
23
+ }
24
+ /**
25
+ * @function isNaturalNumber
26
+ * @description 判断是否为自然数
27
+ * @param value 待判断的值
28
+ * @returns {boolean}
29
+ */
30
+ function isNaturalNumber(value) {
31
+ return Number.isInteger(value) && value >= 0;
32
+ }
13
33
  /**
14
34
  * @function makeUint8Array
15
35
  * @description 创建一个合适长度的 Uint8Array
16
- * @param {number} byteLength 数据字节总大小
36
+ * @param {number} length 数据长度大小
17
37
  * @param {number} pageSize 缓冲区页大小
18
38
  * @returns {Uint8Array}
19
39
  */
20
- function makeUint8Array(byteLength, pageSize) {
21
- if (byteLength > pageSize) {
22
- return new Uint8Array(Math.ceil(byteLength / pageSize) * pageSize);
40
+ function makeUint8Array(length, pageSize) {
41
+ if (length > pageSize) {
42
+ return new Uint8Array(Math.ceil(length / pageSize) * pageSize);
23
43
  }
24
44
  return new Uint8Array(pageSize);
25
45
  }
26
46
 
27
- export { makeUint8Array };
47
+ export { isNaturalNumber, isTypedArray, makeUint8Array };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuintun/buffer",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A buffer tool for javascript.",
5
5
  "type": "module",
6
6
  "sideEffects": false,