|
| 1 | +# Uint8Arrays <!-- omit in toc --> |
| 2 | + |
| 3 | +Some utility functions to make dealing with `Uint8Array`s more pleasant. |
| 4 | + |
| 5 | +- [API](#api) |
| 6 | + - [compare(a, b)](#comparea-b) |
| 7 | + - [Example](#example) |
| 8 | + - [concat(arrays, [length])](#concatarrays-length) |
| 9 | + - [Example](#example-1) |
| 10 | + - [equals(a, b)](#equalsa-b) |
| 11 | + - [Example](#example-2) |
| 12 | + - [fromString(string, encoding = 'utf8')](#fromstringstring-encoding--utf8) |
| 13 | + - [Example](#example-3) |
| 14 | + - [toString(array, encoding = 'utf8')](#tostringarray-encoding--utf8) |
| 15 | + - [Example](#example-4) |
| 16 | + |
| 17 | +## API |
| 18 | + |
| 19 | +### compare(a, b) |
| 20 | + |
| 21 | +Compare two `Uint8Arrays` |
| 22 | + |
| 23 | +#### Example |
| 24 | + |
| 25 | +```js |
| 26 | +const compare = require('uint8arrays/compare') |
| 27 | + |
| 28 | +const arrays = [ |
| 29 | + Uint8Array.from([3, 4, 5]), |
| 30 | + Uint8Array.from([0, 1, 2]) |
| 31 | +] |
| 32 | + |
| 33 | +const sorted = arrays.sort(compare) |
| 34 | + |
| 35 | +console.info(sorted) |
| 36 | +// [ |
| 37 | +// Uint8Array[0, 1, 2] |
| 38 | +// Uint8Array[3, 4, 5] |
| 39 | +// ] |
| 40 | +``` |
| 41 | + |
| 42 | +### concat(arrays, [length]) |
| 43 | + |
| 44 | +Concatenate one or more array-likes and return a `Uint8Array` with their contents. |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +#### Example |
| 49 | + |
| 50 | +```js |
| 51 | +const concat = require('uint8arrays/concat') |
| 52 | + |
| 53 | +const arrays = [ |
| 54 | + Uint8Array.from([0, 1, 2]), |
| 55 | + Uint8Array.from([3, 4, 5]) |
| 56 | +] |
| 57 | + |
| 58 | +const all = concat(arrays, 6) |
| 59 | + |
| 60 | +console.info(all) |
| 61 | +// Uint8Array[0, 1, 2, 3, 4, 5] |
| 62 | +``` |
| 63 | + |
| 64 | +### equals(a, b) |
| 65 | + |
| 66 | +Returns true if the two arrays are the same array or if they have the same length and contents. |
| 67 | + |
| 68 | +#### Example |
| 69 | + |
| 70 | +```js |
| 71 | +const equals = require('uint8arrays/equals') |
| 72 | + |
| 73 | +const a = Uint8Array.from([0, 1, 2]) |
| 74 | +const b = Uint8Array.from([3, 4, 5]) |
| 75 | +const c = Uint8Array.from([0, 1, 2]) |
| 76 | + |
| 77 | +console.info(equals(a, b)) // false |
| 78 | +console.info(equals(a, c)) // true |
| 79 | +console.info(equals(a, a)) // true |
| 80 | +``` |
| 81 | + |
| 82 | +### fromString(string, encoding = 'utf8') |
| 83 | + |
| 84 | +Returns a new `Uint8Array` created from the passed string and interpreted as the passed encoding. |
| 85 | + |
| 86 | +Supports `utf8` and any of the [multiformats encodings](https://www.npmjs.com/package/multibase#supported-encodings-see-srcconstantsjs). |
| 87 | + |
| 88 | +#### Example |
| 89 | + |
| 90 | +```js |
| 91 | +const fromString = require('uint8arrays/from-string') |
| 92 | + |
| 93 | +console.info(fromString('hello world')) // Uint8Array[104, 101 ... |
| 94 | +console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ... |
| 95 | +console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ... |
| 96 | +``` |
| 97 | + |
| 98 | +### toString(array, encoding = 'utf8') |
| 99 | + |
| 100 | +Returns a string created from the passed `Uint8Array` in the passed encoding. |
| 101 | + |
| 102 | +Supports `utf8` and any of the [multiformats encodings](https://www.npmjs.com/package/multibase#supported-encodings-see-srcconstantsjs). |
| 103 | + |
| 104 | +#### Example |
| 105 | + |
| 106 | +```js |
| 107 | +const fromString = require('uint8arrays/from-string') |
| 108 | + |
| 109 | +console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world' |
| 110 | +console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc' |
| 111 | +console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA' |
| 112 | +``` |
0 commit comments