Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

uint8arrays

achingbrain5mApache-2.0 OR MIT5.1.0TypeScript support: included

Utility functions to make dealing with Uint8Arrays easier

readme

uint8arrays

codecov CI

Utility functions to make dealing with Uint8Arrays easier

About

Uint8Arrays bring memory-efficient(ish) byte handling to browsers - they are similar to Node.js Buffers but lack a lot of the utility methods present on that class.

This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc.

Since Node.js Buffers are also Uint8Arrays, it falls back to Buffer internally where it makes sense for performance reasons.

alloc(size)

Create a new Uint8Array. When running under Node.js, Buffer will be used in preference to Uint8Array.

Example

import { alloc } from 'uint8arrays/alloc'

const buf = alloc(100)

allocUnsafe(size)

Create a new Uint8Array. When running under Node.js, Buffer will be used in preference to Uint8Array.

On platforms that support it, memory referenced by the returned Uint8Array will not be initialized.

Example

import { allocUnsafe } from 'uint8arrays/alloc'

const buf = allocUnsafe(100)

compare(a, b)

Compare two Uint8Arrays

Example

import { compare } from 'uint8arrays/compare'

const arrays = [
  Uint8Array.from([3, 4, 5]),
  Uint8Array.from([0, 1, 2])
]

const sorted = arrays.sort(compare)

console.info(sorted)
// [
//    Uint8Array[0, 1, 2]
//    Uint8Array[3, 4, 5]
// ]

concat(arrays, [length])

Concatenate one or more Uint8Arrays 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

import { concat } from 'uint8arrays/concat'

const arrays = [
  Uint8Array.from([0, 1, 2]),
  Uint8Array.from([3, 4, 5])
]

const all = concat(arrays, 6)

console.info(all)
// Uint8Array[0, 1, 2, 3, 4, 5]

equals(a, b)

Returns true if the two arrays are the same array or if they have the same length and contents.

Example

import { equals } from '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)) // false
console.info(equals(a, c)) // true
console.info(equals(a, a)) // true

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 multibase encodings as implemented by the multiformats module.

Example

import { fromString } from 'uint8arrays/from-string'

console.info(fromString('hello world')) // Uint8Array[104, 101 ...
console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ...
console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ...
console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ...

toString(array, encoding = 'utf8')

Returns a string created from the passed Uint8Array in the passed encoding.

Supports utf8 and any of the multibase encodings as implemented by the multiformats module.

Example

import { toString } from 'uint8arrays/to-string'

console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world'
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc'
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234'

xor(a, b)

Returns a Uint8Array containing a and b xored together.

Example

import { xor } from 'uint8arrays/xor'

console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]

xorCompare(a, b)

Compares the distances between two xor Uint8Arrays.

Example

import { xor } from 'uint8arrays/xor'
import { xorCompare } from 'uint8arrays/xor-compare'

const target = Uint8Array.from([1, 1])
const val1 = Uint8Array.from([1, 0])
const xor1 = xor(target, val1)

const val2 = Uint8Array.from([0, 1])
const xor2 = xor(target, val2)

console.info(xorCompare(xor1, xor2)) // -1 or 0 or 1

Install

$ npm i uint8arrays

Browser <script> tag

Loading this module through a script tag will make it's exports available as Uint8arrays in the global namespace.

<script src="https://unpkg.com/uint8arrays/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

changelog

5.1.0 (2024-05-10)

Features

Trivial Changes

5.0.3 (2024-03-13)

Bug Fixes

  • Use subpath imports to choose Node or vanilla JS environment (#88) (e43f1f4)

5.0.2 (2024-02-08)

Dependencies

  • dev: bump aegir from 41.3.5 to 42.2.3 (#86) (e6c1f80)

5.0.1 (2023-12-28)

Dependencies

  • bump multiformats from 12.1.3 to 13.0.0 (#77) (ca9ca44)

5.0.0 (2023-12-07)

⚠ BREAKING CHANGES

  • concat now expects an array of Uint8Arrays

Features

4.0.10 (2023-12-07)

Bug Fixes

  • revert use of buffer.concat on node.js (#75) (bf6d760)

Trivial Changes

4.0.9 (2023-11-24)

Bug Fixes

  • use Buffer.concat on node as it is faster (#73) (8d6c24b)

4.0.8 (2023-11-24)

Dependencies

  • dev: bump aegir from 40.0.13 to 41.1.9 (#72) (2a9a0bf)

4.0.7 (2023-11-24)

Documentation

4.0.6 (2023-08-03)

Dependencies

  • dev: bump aegir from 39.0.13 to 40.0.8 (#58) (2678ca8)

4.0.5 (2023-08-03)

Dependencies

  • bump multiformats from 11.0.2 to 12.0.1 (#54) (522d795)

4.0.4 (2023-06-07)

Dependencies

  • dev: bump aegir from 37.12.1 to 38.1.8 (#50) (15d15d6)

Documentation

  • Remove duplicate content section in readme. (#51) (686b669)

4.0.3 (2023-01-06)

Dependencies

  • bump multiformats from 10.0.3 to 11.0.0 (#43) (1bfbcaa)

4.0.2 (2022-10-12)

Bug Fixes

4.0.1 (2022-10-12)

Bug Fixes

  • export SupportedEncodings from to-string and from-string (#40) (d3f1412)

4.0.0 (2022-10-12)

⚠ BREAKING CHANGES

  • this module is now ESM-only

Features

  • convert to typescript and release as ESM-only (#28) (e4c9627)

Documentation

3.1.1 (2022-10-10)

Bug Fixes

3.1.0 (2022-08-02)

Bug Fixes

  • use allocUnsafe where possible (#34) (8b740e0)
  • use Buffer.from for stringification where available (#36) (0a18849)

Features

3.0.0 (2021-08-17)

3.0.0-rc.1 (2021-08-16)

3.0.0-rc.0 (2021-08-16)

chore

BREAKING CHANGES

  • built content includes ESM and CJS and has switched to named exports for the external API

Co-authored-by: Alex Potsides alex@achingbrain.net

2.1.10 (2021-08-01)

2.1.8 (2021-07-27)

Bug Fixes

  • include dist/utils/* files for distribution (#24) (611f1f4), closes #23

2.1.7 (2021-07-15)

2.1.6 (2021-07-15)

2.1.5 (2021-04-15)

Features

  • export supported string encodings (#18) (4b971ec)

2.1.4 (2021-04-06)

2.1.3 (2021-03-01)

Bug Fixes

2.1.2 (2021-02-04)

Bug Fixes

2.1.1 (2021-02-04)

2.1.0 (2021-02-04)

Features

2.0.5 (2020-12-18)

Bug Fixes

  • fixes prepare script and tsconfig extends field (#10) (48c3099), closes #11

2.0.4 (2020-12-18)

Bug Fixes

2.0.3 (2020-12-18)

2.0.2 (2020-12-18)

2.0.1 (2020-12-18)

2.0.0 (2020-12-18)

Features

1.1.0 (2020-08-07)

Features

1.0.0 (2020-07-31)

0.0.2 (2020-07-31)

0.0.1 (2020-07-31)

Features