byteview
v2.0.1
Published
TypedArray. Work with bytes. Can be used as a Buffer, TypedArray, BitView, DataView. Client side and server side support.
Downloads
84
Maintainers
Readme
ByteView
Work with bytes. Can be used as a Buffer, TypedArray, BitView, DataView. Client side and server side support. Encode strings & serialize objects.
Table of Contents
Install
npm i byteview
Usage
static ByteView.from:
import ByteView from 'byteview'
const bv = ByteView.from('Hello World!')
/*
* nodejs prints: <ByteView SGVsbG8gV29ybGQh />
* browser prints: ByteView(12) [
* 72, 101, 108, 108, 111,
* 32, 87, 111, 114, 108,
* 100, 33
* ]
*/
console.log(bv)
class ByteView extends Uint8Array {
static from (
input: ArrayLike | ArrayBufferView | ArrayBuffer | string | bigint,
encodingOrOffset?: string | number,
length?: number
): ByteView
static alloc (size: number, encoding?: string): ByteView
static allocBits (size: number, encoding?: string): ByteView
static concat (list: Array<ByteView>, length?: number): ByteView
/**
*
* Returns true if instance of ByteView.
*/
static isByteView (byteView: any): boolean
/**
*
* Returns true if instance of TypedArray or DataView.
*/
static isView (input: any): boolean
/**
*
* Returns true if instance of ArrayBuffer.
*/
static isNativeBuffer (input: any): boolean
/**
*
* Returns a base64 encoded string.
*/
static serialize (input: object): string
/**
*
* Get byteLength of string.
*/
static byteLength (
string: string,
encoding = 'utf8'
): number
/**
*
* If running process is little endian.
*/
static isOSLittleEndian: boolean
/**
*
* Get real size of ByteView.
* Useful when being used as a bit array.
*/
get size (): number
/**
*
* Get bitOffset of ByteView.
* Useful when being used as a bit array.
*/
get bitOffset (): number
get encoding (): string
set encoding (encoding: string)
/**
*
* Interprets byteView as an array of unsigned 16-bit integers and swaps the byte order in-place.
*/
swap16 (): this
/**
*
* Interprets byteView as an array of unsigned 32-bit integers and swaps the byte order in-place.
*/
swap32 (): this
/**
*
* Interprets byteView as an array of unsigned 64-bit integers and swaps the byte order in-place.
*/
swap64 (): this
/**
*
* Gets an array of 3 valid colors.
*/
getRGB (index: number): [number, number, number]
/**
*
* Gets a color hex at the specified index.
*/
getColorHex (index: number): string
/**
*
* Returns the bit at the specified index.
* Accounts for ByteView.bitOffset.
*/
getBit (index: number): 1 | 0
/**
*
* Sets the bit at the specified index.
* Accounts for ByteView.bitOffset.
*/
setBit (index: number, value: number): number
/**
*
* Returns the bits from start to end.
*/
sliceBits (start: number, end: number): number
/**
*
* Get an unsigned 5-bit integer.
*/
getUint5 (index: number): number
/**
*
* Get an unsigned 6-bit integer.
*/
getUint6 (index: number): number
/**
*
* Get an unsigned 8-bit integer.
*/
getUint8 (offset: number): number
/**
*
* Set an unsigned 8-bit integer.
*/
setUint8 (
offset: number,
value: number
): number
/**
*
* Get an unsigned 16-bit integer.
*/
getUint16 (
offset: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Set an unsigned 16-bit integer.
*/
setUint16 (
offset: number,
value: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Get an unsigned 32-bit integer.
*/
getUint32 (
offset: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Set an unsigned 32-bit integer.
*/
setUint32 (
offset: number,
value: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Get an unsigned 64-bit bigint.
*/
getBigUint64 (
offset: number,
littleEndian = ByteView.isOSLittleEndian
): bigint
/**
*
* Set an unsigned 64-bit bigint.
*/
setBigUint64 (
offset: number,
value: bigint,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Get a signed integer.
*/
getInt (
offset: number,
byteLength: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Get a signed 8-bit integer.
*/
getInt8 (offset: number): number
/**
*
* Set a signed 8-bit integer.
*/
setInt8 (offset: number, value: number): number
/**
*
* Get a signed 16-bit integer.
*/
getInt16 (
offset: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Set a signed 16-bit integer.
*/
setInt16 (
offset: number,
value: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Get a signed 32-bit integer.
*/
getInt32 (
offset: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Set a signed 32-bit integer.
*/
setInt32 (
offset: number,
value: number,
littleEndian = ByteView.isOSLittleEndian
): number
/**
*
* Set a signed 6-bit integer.
*/
getInt6 (index: number): number
/**
*
* Get a JSON object representation of a ByteView.
*/
toJSON (): {
type: 'ByteView',
data: Array<number>
}
/**
*
* Prints a ByteView string.
*/
inspect (): string
toString (
encoding?: 'utf8' |
'hex' |
'base32' |
'base32hex' |
'base32crockford' |
'base64' |
'base64url' |
'ascii' |
'latin1' |
'binary' |
'ucs2' |
'ucs-2' |
'utf16le' |
'utf-16le'
): string
subarray (start: number, end?: number): ByteView
copy (
target: ByteView | ArrayBufferView,
targetStart?: number,
start?: number,
end?: number
): number
fill (
val: ByteView | number | Array,
start: number,
end?: number,
encoding?: number
): this
equals (byteView: ByteView): boolean
compare (
target: ByteView,
start?: number,
end?: number,
thisStart?: number,
thisEnd?: number
): -1 | 0 | 1
write (
string: string,
offset: number,
length?: number,
encoding?: string
): number
toBigInt (): bigint
padStart (targetLength: number): this
}