![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
uint8array-extras
Advanced tools
Readme
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.
npm install uint8array-extras
import {concatUint8Arrays} from 'uint8array-extras';
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]
isUint8Array(value: unknown): boolean
Check if the given value is an instance of Uint8Array
.
Replacement for Buffer.isBuffer()
.
import {isUint8Array} from 'uint8array-extras';
console.log(isUint8Array(new Uint8Array()));
//=> true
console.log(isUint8Array(new ArrayBuffer(10)));
//=> false
assertUint8Array(value: unknown)
Throw a TypeError
if the given value is not an instance of Uint8Array
.
import {assertUint8Array} from 'uint8array-extras';
try {
assertUint8Array(new ArrayBuffer(10)); // Throws a TypeError
} catch (error) {
console.error(error.message);
}
concatUint8Arrays(arrays: Uint8Array[], totalLength?: number): Uint8Array
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 'uint8array-extras';
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]
areUint8ArraysEqual(a: Uint8Array, b: Uint8Array): boolean
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 'uint8array-extras';
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
compareUint8Arrays(a: Uint8Array, b: Uint8Array): 0 | 1 | -1
Compare two arrays and indicate their relative order or equality. Useful for sorting.
Replacement for Buffer.compare()
.
import {compareUint8Arrays} from 'uint8array-extras';
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]]
uint8ArrayToString(array: Uint8Array): string
Convert a Uint8Array
(containing a UTF-8 string) to a string.
Replacement for Buffer#toString()
.
import {uint8ArrayToString} from 'uint8array-extras';
const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
console.log(uint8ArrayToString(byteArray));
//=> 'Hello'
stringToUint8Array(string: string): Uint8Array
Convert a string to a Uint8Array
(using UTF-8 encoding).
Replacement for Buffer.from('Hello')
.
import {stringToUint8Array} from 'uint8array-extras';
console.log(stringToUint8Array('Hello'));
//=> Uint8Array [72, 101, 108, 108, 111]
uint8ArrayToBase64(array: Uint8Array): string
Convert a Uint8Array
to a Base64-encoded string.
Replacement for Buffer#toString('base64')
.
import {uint8ArrayToBase64} from 'uint8array-extras';
const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
console.log(uint8ArrayToBase64(byteArray));
//=> 'SGVsbG8='
base64ToUint8Array(string: string): Uint8Array
Convert a Base64-encoded string to a Uint8Array
.
Replacement for Buffer.from('SGVsbG8=', 'base64')
.
import {base64ToUint8Array} from 'uint8array-extras';
console.log(base64ToUint8Array('SGVsbG8='));
//=> Uint8Array [72, 101, 108, 108, 111]
stringToBase64(string: string): string
Encode a string to Base64-encoded string.
Replacement for Buffer.from('Hello').toString('base64')
.
import {stringToBase64} from 'uint8array-extras';
console.log(stringToBase64('Hello'));
//=> 'SGVsbG8='
base64ToString(base64String: string): string
Decode a Base64-encoded string to a string.
Replacement for Buffer.from('SGVsbG8=', 'base64').toString()
.
import {base64ToString} from 'uint8array-extras';
console.log(base64ToString('SGVsbG8='));
//=> 'Hello'
uint8ArrayToHex(array: Uint8Array): string
Convert a Uint8Array
to a Hex string.
Replacement for Buffer#toString('hex')
.
import {uint8ArrayToHex} from 'uint8array-extras';
const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
console.log(uint8ArrayToHex(byteArray));
//=> '48656c6c6f'
hexToUint8Array(hexString: string): Uint8Array
Convert a Hex string to a Uint8Array
.
Replacement for Buffer.from('48656c6c6f', 'hex')
.
import {hexToUint8Array} from 'uint8array-extras';
console.log(hexToUint8Array('48656c6c6f'));
//=> Uint8Array [72, 101, 108, 108, 111]
FAQs
Useful utilities for working with Uint8Array (and Buffer)
The npm package uint8array-extras receives a total of 120,072 weekly downloads. As such, uint8array-extras popularity was classified as popular.
We found that uint8array-extras demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.