Useful utilities for working with
Uint8Array
(andBuffer
)
It's time to transition from Buffer
to Uint8Array
, and this package helps fill in the gaps.
Note that Buffer
is a Uint8Array
subclass, so you can use this package with Buffer
too.
This package is tree-shakeable and browser-compatible.
This package also includes methods to convert a string to Base64 and back.
Note: In the browser, do not use globalThis.atob()
/ globalThis.btoa()
because they do not support Unicode. This package does.
npm install @npmshivamkalsi/dolores-laborum-ipsam-minima
import {concatUint8Arrays} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([4, 5, 6]);
console.log(concatUint8Arrays([a, b]));
//=> Uint8Array [1, 2, 3, 4, 5, 6]
Check if the given value is an instance of Uint8Array
.
Replacement for Buffer.isBuffer()
.
import {isUint8Array} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
console.log(isUint8Array(new Uint8Array()));
//=> true
console.log(isUint8Array(Buffer.from('x')));
//=> true
console.log(isUint8Array(new ArrayBuffer(10)));
//=> false
Throw a TypeError
if the given value is not an instance of Uint8Array
.
import {assertUint8Array} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
try {
assertUint8Array(new ArrayBuffer(10)); // Throws a TypeError
} catch (error) {
console.error(error.message);
}
Convert a value to a Uint8Array
without copying its data.
This can be useful for converting a Buffer
to a pure Uint8Array
. Buffer
is already an Uint8Array
subclass, but Buffer
alters some behavior, so it can be useful to cast it to a pure Uint8Array
before returning it.
Tip: If you want a copy, just call .slice()
on the return value.
Concatenate the given arrays into a new array.
If arrays
is empty, it will return a zero-sized Uint8Array
.
If totalLength
is not specified, it is calculated from summing the lengths of the given arrays.
Replacement for Buffer.concat()
.
import {concatUint8Arrays} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([4, 5, 6]);
console.log(concatUint8Arrays([a, b]));
//=> Uint8Array [1, 2, 3, 4, 5, 6]
Check if two arrays are identical by verifying that they contain the same bytes in the same sequence.
Replacement for Buffer#equals()
.
import {areUint8ArraysEqual} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([1, 2, 3]);
const c = new Uint8Array([4, 5, 6]);
console.log(areUint8ArraysEqual(a, b));
//=> true
console.log(areUint8ArraysEqual(a, c));
//=> false
Compare two arrays and indicate their relative order or equality. Useful for sorting.
Replacement for Buffer.compare()
.
import {compareUint8Arrays} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
const array1 = new Uint8Array([1, 2, 3]);
const array2 = new Uint8Array([4, 5, 6]);
const array3 = new Uint8Array([7, 8, 9]);
[array3, array1, array2].sort(compareUint8Arrays);
//=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Convert a Uint8Array
(containing a UTF-8 string) to a string.
Replacement for Buffer#toString()
.
import {uint8ArrayToString} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
console.log(uint8ArrayToString(byteArray));
//=> 'Hello'
Convert a string to a Uint8Array
(using UTF-8 encoding).
Replacement for Buffer.from('Hello')
.
import {stringToUint8Array} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
console.log(stringToUint8Array('Hello'));
//=> Uint8Array [72, 101, 108, 108, 111]
Convert a Uint8Array
to a Base64-encoded string.
Specify {urlSafe: true}
to get a Base64URL-encoded string.
Replacement for Buffer#toString('base64')
.
import {uint8ArrayToBase64} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
console.log(uint8ArrayToBase64(byteArray));
//=> 'SGVsbG8='
Convert a Base64-encoded or Base64URL-encoded string to a Uint8Array
.
Replacement for Buffer.from('SGVsbG8=', 'base64')
.
import {base64ToUint8Array} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
console.log(base64ToUint8Array('SGVsbG8='));
//=> Uint8Array [72, 101, 108, 108, 111]
Encode a string to Base64-encoded string.
Specify {urlSafe: true}
to get a Base64URL-encoded string.
Replacement for Buffer.from('Hello').toString('base64')
and btoa()
.
import {stringToBase64} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
console.log(stringToBase64('Hello'));
//=> 'SGVsbG8='
Decode a Base64-encoded or Base64URL-encoded string to a string.
Replacement for Buffer.from('SGVsbG8=', 'base64').toString()
and atob()
.
import {base64ToString} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
console.log(base64ToString('SGVsbG8='));
//=> 'Hello'
Convert a Uint8Array
to a Hex string.
Replacement for Buffer#toString('hex')
.
import {uint8ArrayToHex} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
console.log(uint8ArrayToHex(byteArray));
//=> '48656c6c6f'
Convert a Hex string to a Uint8Array
.
Replacement for Buffer.from('48656c6c6f', 'hex')
.
import {hexToUint8Array} from '@npmshivamkalsi/dolores-laborum-ipsam-minima';
console.log(hexToUint8Array('48656c6c6f'));
//=> Uint8Array [72, 101, 108, 108, 111]