What is uint8arrays?
The uint8arrays npm package provides utilities for working with Uint8Array objects in JavaScript. It includes functions for concatenation, comparison, conversion to and from strings, and more.
What are uint8arrays's main functionalities?
Concatenation
This feature allows you to concatenate multiple Uint8Array objects into a single Uint8Array.
const uint8arrays = require('uint8arrays');
const arr1 = new Uint8Array([1, 2, 3]);
const arr2 = new Uint8Array([4, 5, 6]);
const concatenated = uint8arrays.concat([arr1, arr2]);
console.log(concatenated); // Uint8Array [1, 2, 3, 4, 5, 6]
Comparison
This feature allows you to compare two Uint8Array objects for equality.
const uint8arrays = require('uint8arrays');
const arr1 = new Uint8Array([1, 2, 3]);
const arr2 = new Uint8Array([1, 2, 3]);
const arr3 = new Uint8Array([4, 5, 6]);
console.log(uint8arrays.equals(arr1, arr2)); // true
console.log(uint8arrays.equals(arr1, arr3)); // false
Conversion to and from Strings
This feature allows you to convert Uint8Array objects to and from strings using various encodings.
const uint8arrays = require('uint8arrays');
const arr = new Uint8Array([72, 101, 108, 108, 111]);
const str = uint8arrays.toString(arr, 'utf8');
console.log(str); // 'Hello'
const newArr = uint8arrays.fromString('Hello', 'utf8');
console.log(newArr); // Uint8Array [72, 101, 108, 108, 111]
Other packages similar to uint8arrays
buffer
The buffer package provides a way of handling binary data in Node.js. It includes many similar functionalities to uint8arrays, such as concatenation, comparison, and conversion to and from strings. However, buffer is more tightly integrated with Node.js and provides additional features like reading and writing to files.
typedarray
The typedarray package provides polyfills for JavaScript's typed array functionality, including Uint8Array. While it doesn't offer as many utility functions as uint8arrays, it ensures compatibility with older JavaScript environments that do not support typed arrays natively.
Uint8Arrays
Some utility functions to make dealing with Uint8Array
s more pleasant.
API
compare(a, b)
Compare two Uint8Arrays
Example
const compare = require('uint8arrays/compare')
const arrays = [
Uint8Array.from([3, 4, 5]),
Uint8Array.from([0, 1, 2])
]
const sorted = arrays.sort(compare)
console.info(sorted)
concat(arrays, [length])
Concatenate one or more array-likes and return a Uint8Array
with their contents.
If you know the length of the arrays, pass it as a second parameter, otherwise it will be calculated by traversing the list of arrays.
Example
const concat = require('uint8arrays/concat')
const arrays = [
Uint8Array.from([0, 1, 2]),
Uint8Array.from([3, 4, 5])
]
const all = concat(arrays, 6)
console.info(all)
equals(a, b)
Returns true if the two arrays are the same array or if they have the same length and contents.
Example
const equals = require('uint8arrays/equals')
const a = Uint8Array.from([0, 1, 2])
const b = Uint8Array.from([3, 4, 5])
const c = Uint8Array.from([0, 1, 2])
console.info(equals(a, b))
console.info(equals(a, c))
console.info(equals(a, a))
fromString(string, encoding = 'utf8')
Returns a new Uint8Array
created from the passed string and interpreted as the passed encoding.
Supports utf8
and any of the multiformats encodings.
Example
const fromString = require('uint8arrays/from-string')
console.info(fromString('hello world'))
console.info(fromString('00010203aabbcc', 'base16'))
console.info(fromString('AAECA6q7zA', 'base64'))
console.info(fromString('01234', 'ascii'))
toString(array, encoding = 'utf8')
Returns a string created from the passed Uint8Array
in the passed encoding.
Supports utf8
and any of the multiformats encodings.
Example
const toString = require('uint8arrays/to-string')
console.info(toString(Uint8Array.from([104, 101...])))
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16'))
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64'))
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii'))
xor(a, b)
Returns a Uint8Array
containing a
and b
xored together.
Example
const xor = require('uint8arrays/xor')
console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1])))