whichever-data
Description
An ES module with some functions making it easier to accept a wide range of binary-data holding objects like Array, TypedArray, DataView, ArrayBuffer and Node.js's Buffer as input without having to actually know which of them were used as the input.
Example
import {dataToTypedArray} from 'whichever-data'
function giveMeAnyData(data) {
const uint32array = dataToTypedArray(data, Uint32Array)
// do something amazing here
}
Supported platforms
- Node.js
- Deno
- A proper browser (Chromium based usually) or just Babel the shit out of it if you need legacy support.
How to use
NPM
Install usingnpm i whichever-data
Import the ES module into Node.js
import {
dataToArrayBuffer,
dataToTypedArray,
dataToDataView,
compareData
} from 'whichever-data'
Got problems using ES modules? Click here or read this.
Import the ES module into the browser or Deno
import {
dataToArrayBuffer,
dataToTypedArray,
dataToDataView,
compareData
} from '/node_modules/whichever-data/source/whicheverData.js'
Cumbersome? Consider using import maps so you can import it just like in Node.js. Also see the Deno specific documentation for import maps if using Deno.
Funding
If you find this useful then please consider helping me out (I'm jobless and sick). For more information visit my GitHub sponsors page, my profile or my simple website.
Auto-generated API documentation (from JSDoc)
whichever-data
An ES module with some functions making it easier to accept a wide range of binary-data holding objects like Array, TypedArray, DataView, ArrayBuffer and Node.js's Buffer as input without having to actually know which of them were used as the input.
-
whichever-data
-
.dataToArrayBuffer(data) ⇒
ArrayBuffer
-
.dataToTypedArray(data, TypedArray) ⇒
TypedArray
-
.dataToDataView(data) ⇒
DataView
-
.compareData(a, b) ⇒
Boolean
-
.dataToArrayBuffer(data) ⇒
ArrayBuffer
whichever.dataToArrayBuffer(data) ⇒ Copies the viewed part of the underlying ArrayBuffer
from the supplied object (e.g. DataView
, TypedArray
or Node.js Buffer
). Or if supplied an array it treats it like unsigned bytes and creates the ArrayBuffer
from it. If supplied an ArrayBuffer
it creates a copy of it.
Kind: static method of whichever-data
Returns: ArrayBuffer
- A new ArrayBuffer.
Params
- data
ArrayBuffer
|ArrayBufferView
|Buffer
|Array.<number>
TypedArray
whichever.dataToTypedArray(data, TypedArray) ⇒ Creates a TypedArray
of the chosen type from an Array
, another TypedArray
, DataView
, ArrayBuffer
or Node.js Buffer
. When not created from an Array
it doesn't copy any data, instead it will create a view into the underlying ArrayBuffer
(hence changes in the viewed data will also affect the content of this TypedArray
).
Kind: static method of whichever-data
Returns: TypedArray
- A new TypedArray.
Params
- data
ArrayBuffer
|ArrayBufferView
|Buffer
|Array.<number>
- TypedArray
TypedArray
- The TypedArray constructor to use, e.g. Uint8Array.
DataView
whichever.dataToDataView(data) ⇒ Creates a DataView
from a TypedArray
, DataView
, ArrayBuffer
, Node.js Buffer
or an Array
(of unsigned bytes). When not created from an Array
it doesn't copy any data, instead it will create a view into the underlying ArrayBuffer
(hence changes in the viewed data will also affect the content of this DataView
).
Kind: static method of whichever-data
Returns: DataView
- A new DataView.
Params
- data
ArrayBuffer
|ArrayBufferView
|Buffer
|Array.<number>
Boolean
whichever.compareData(a, b) ⇒ Checks if the data given to it is the same binary data. Accepts Array
, TypedArray
, DataView
, ArrayBuffer
or Node.js Buffer
as input. If comparing two Arrays then normal array comparison is done, but if comparing one Array against data of another type then the array is treated as an array of unsigned bytes.
Kind: static method of whichever-data
Returns: Boolean
- true or false
Params
- a
ArrayBuffer
|ArrayBufferView
|Buffer
|Array.<number>
- b
ArrayBuffer
|ArrayBufferView
|Buffer
|Array.<number>
The bottom
You reached the bottom, good. Most people just skim the top... But I guess that you have an attention to detail and a higher intelligence than the average person. I like you!
To get back up click here or find your own way.