Comparing version
import { decode, encode } from './text'; | ||
const defaultByteLength = 1024 * 8; | ||
const typedArrays = { | ||
int8: Int8Array, | ||
uint8: Uint8Array, | ||
int16: Int16Array, | ||
uint16: Uint16Array, | ||
int32: Int32Array, | ||
uint32: Uint32Array, | ||
uint64: BigUint64Array, | ||
int64: BigInt64Array, | ||
float32: Float32Array, | ||
float64: Float64Array, | ||
}; | ||
export class IOBuffer { | ||
@@ -200,9 +212,19 @@ /** | ||
readBytes(n = 1) { | ||
const bytes = new Uint8Array(n); | ||
for (let i = 0; i < n; i++) { | ||
bytes[i] = this.readByte(); | ||
} | ||
return bytes; | ||
return this.readArray(n, 'uint8'); | ||
} | ||
/** | ||
* Creates an array of corresponding to the type `type` and size `size`. | ||
* For example type `uint8` will create a `Uint8Array`. | ||
* @param size - size of the resulting array | ||
* @param type - number type of elements to read | ||
*/ | ||
readArray(size, type) { | ||
const bytes = typedArrays[type].BYTES_PER_ELEMENT * size; | ||
const offset = this.byteOffset + this.offset; | ||
const slice = this.buffer.slice(offset, offset + bytes); | ||
const returnArray = new typedArrays[type](slice); | ||
this.offset += bytes; | ||
return returnArray; | ||
} | ||
/** | ||
* Read a 16-bit signed integer and move pointer forward by 2 bytes. | ||
@@ -209,0 +231,0 @@ */ |
/// <reference types="node" /> | ||
declare type InputData = number | ArrayBufferLike | ArrayBufferView | IOBuffer | Buffer; | ||
type InputData = number | ArrayBufferLike | ArrayBufferView | IOBuffer | Buffer; | ||
declare const typedArrays: { | ||
int8: Int8ArrayConstructor; | ||
uint8: Uint8ArrayConstructor; | ||
int16: Int16ArrayConstructor; | ||
uint16: Uint16ArrayConstructor; | ||
int32: Int32ArrayConstructor; | ||
uint32: Uint32ArrayConstructor; | ||
uint64: BigUint64ArrayConstructor; | ||
int64: BigInt64ArrayConstructor; | ||
float32: Float32ArrayConstructor; | ||
float64: Float64ArrayConstructor; | ||
}; | ||
type TypedArrays = typeof typedArrays; | ||
interface IOBufferOptions { | ||
@@ -140,2 +153,9 @@ /** | ||
/** | ||
* Creates an array of corresponding to the type `type` and size `size`. | ||
* For example type `uint8` will create a `Uint8Array`. | ||
* @param size - size of the resulting array | ||
* @param type - number type of elements to read | ||
*/ | ||
readArray<T extends keyof typeof typedArrays>(size: number, type: T): InstanceType<TypedArrays[T]>; | ||
/** | ||
* Read a 16-bit signed integer and move pointer forward by 2 bytes. | ||
@@ -142,0 +162,0 @@ */ |
@@ -6,2 +6,14 @@ "use strict"; | ||
const defaultByteLength = 1024 * 8; | ||
const typedArrays = { | ||
int8: Int8Array, | ||
uint8: Uint8Array, | ||
int16: Int16Array, | ||
uint16: Uint16Array, | ||
int32: Int32Array, | ||
uint32: Uint32Array, | ||
uint64: BigUint64Array, | ||
int64: BigInt64Array, | ||
float32: Float32Array, | ||
float64: Float64Array, | ||
}; | ||
class IOBuffer { | ||
@@ -204,9 +216,19 @@ /** | ||
readBytes(n = 1) { | ||
const bytes = new Uint8Array(n); | ||
for (let i = 0; i < n; i++) { | ||
bytes[i] = this.readByte(); | ||
} | ||
return bytes; | ||
return this.readArray(n, 'uint8'); | ||
} | ||
/** | ||
* Creates an array of corresponding to the type `type` and size `size`. | ||
* For example type `uint8` will create a `Uint8Array`. | ||
* @param size - size of the resulting array | ||
* @param type - number type of elements to read | ||
*/ | ||
readArray(size, type) { | ||
const bytes = typedArrays[type].BYTES_PER_ELEMENT * size; | ||
const offset = this.byteOffset + this.offset; | ||
const slice = this.buffer.slice(offset, offset + bytes); | ||
const returnArray = new typedArrays[type](slice); | ||
this.offset += bytes; | ||
return returnArray; | ||
} | ||
/** | ||
* Read a 16-bit signed integer and move pointer forward by 2 bytes. | ||
@@ -213,0 +235,0 @@ */ |
{ | ||
"name": "iobuffer", | ||
"version": "5.2.1", | ||
"version": "5.3.0", | ||
"description": "Read and write binary data on ArrayBuffers", | ||
@@ -43,3 +43,3 @@ "main": "./lib/IOBuffer.js", | ||
"@types/jest": "^27.0.3", | ||
"@types/node": "^17.0.0", | ||
"@types/node": "^18.11.9", | ||
"eslint": "^8.4.1", | ||
@@ -51,4 +51,4 @@ "eslint-config-cheminfo-typescript": "^10.3.0", | ||
"ts-jest": "^27.1.2", | ||
"typescript": "^4.5.4" | ||
"typescript": "^4.9.3" | ||
} | ||
} |
@@ -7,2 +7,17 @@ import { decode, encode } from './text'; | ||
const typedArrays = { | ||
int8: Int8Array, | ||
uint8: Uint8Array, | ||
int16: Int16Array, | ||
uint16: Uint16Array, | ||
int32: Int32Array, | ||
uint32: Uint32Array, | ||
uint64: BigUint64Array, | ||
int64: BigInt64Array, | ||
float32: Float32Array, | ||
float64: Float64Array, | ||
}; | ||
type TypedArrays = typeof typedArrays; | ||
interface IOBufferOptions { | ||
@@ -265,10 +280,23 @@ /** | ||
public readBytes(n = 1): Uint8Array { | ||
const bytes = new Uint8Array(n); | ||
for (let i = 0; i < n; i++) { | ||
bytes[i] = this.readByte(); | ||
} | ||
return bytes; | ||
return this.readArray(n, 'uint8'); | ||
} | ||
/** | ||
* Creates an array of corresponding to the type `type` and size `size`. | ||
* For example type `uint8` will create a `Uint8Array`. | ||
* @param size - size of the resulting array | ||
* @param type - number type of elements to read | ||
*/ | ||
public readArray<T extends keyof typeof typedArrays>( | ||
size: number, | ||
type: T, | ||
): InstanceType<TypedArrays[T]> { | ||
const bytes = typedArrays[type].BYTES_PER_ELEMENT * size; | ||
const offset = this.byteOffset + this.offset; | ||
const slice = this.buffer.slice(offset, offset + bytes); | ||
const returnArray = new typedArrays[type](slice); | ||
this.offset += bytes; | ||
return returnArray as InstanceType<TypedArrays[T]>; | ||
} | ||
/** | ||
* Read a 16-bit signed integer and move pointer forward by 2 bytes. | ||
@@ -275,0 +303,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2447
3.82%115812
-0.92%27
-3.57%