uint1array
v1.2.0
Published
Uint1Array - JavaScript's missing Typed Array
Downloads
641
Maintainers
Readme
:wind_chime: Uint1Array
JavaScript's missing TypedArray. Bit-level view of any underlying ArrayBuffer.
API
The API simply mirrors a regular TypedArray.
Differences from the TypedArray API
The only two differences are that Uint1Array has special cases of the following standard TypedArray
properties:
Uint1Array.BYTES_PER_ELEMENT
Returns a number value of the element size.
BYTES_PER_ELEMENT
equals 0.125 in the case of an Uint1Array.
Uint1Array.length
Static length property.
Static class member length
value is 0 in the case of Uint1Array.
For the actual length (number of bits), use <Uint1Array>.length
.
Get
npm install --save uint1array
Using
You can use like an ordinary TypedArray:
// pick an import style, either ESM or CommonJS
// ESM
// import Uint1Array from 'uint1array';
// CommonJS
// const Uint1Array = require('uint1array').default;
const message = "JAVASCRIPT ROCKS";
const chars = message.split('').map( c => c.charCodeAt(0) );
const buf = new ArrayBuffer(chars.length);
const bytes = new Uint8Array(buf);
const bits = new Uint1Array(buf);
bytes.set(chars);
console.log(`${bits}`); // Uint1Array [ 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0 ]