|
3 | 3 |
|
4 | 4 | > Utility functions to make dealing with Uint8Arrays easier |
5 | 5 |
|
| 6 | +# About |
| 7 | + |
| 8 | +`Uint8Array`s bring memory-efficient(ish) byte handling to browsers - they are similar to Node.js `Buffer`s but lack a lot of the utility methods present on that class. |
| 9 | + |
| 10 | +This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc. |
| 11 | + |
| 12 | +Since Node.js `Buffer`s are also `Uint8Array`s, it falls back to `Buffer` internally where it makes sense for performance reasons. |
| 13 | + |
| 14 | +## alloc(size) |
| 15 | + |
| 16 | +Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`. |
| 17 | + |
| 18 | +### Example |
| 19 | + |
| 20 | +```js |
| 21 | +import { alloc } from 'uint8arrays/alloc' |
| 22 | + |
| 23 | +const buf = alloc(100) |
| 24 | +``` |
| 25 | + |
| 26 | +## allocUnsafe(size) |
| 27 | + |
| 28 | +Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`. |
| 29 | + |
| 30 | +On platforms that support it, memory referenced by the returned `Uint8Array` will not be initialized. |
| 31 | + |
| 32 | +### Example |
| 33 | + |
| 34 | +```js |
| 35 | +import { allocUnsafe } from 'uint8arrays/alloc' |
| 36 | + |
| 37 | +const buf = allocUnsafe(100) |
| 38 | +``` |
| 39 | + |
| 40 | +## compare(a, b) |
| 41 | + |
| 42 | +Compare two `Uint8Arrays` |
| 43 | + |
| 44 | +### Example |
| 45 | + |
| 46 | +```js |
| 47 | +import { compare } from 'uint8arrays/compare' |
| 48 | + |
| 49 | +const arrays = [ |
| 50 | + Uint8Array.from([3, 4, 5]), |
| 51 | + Uint8Array.from([0, 1, 2]) |
| 52 | +] |
| 53 | + |
| 54 | +const sorted = arrays.sort(compare) |
| 55 | + |
| 56 | +console.info(sorted) |
| 57 | +// [ |
| 58 | +// Uint8Array[0, 1, 2] |
| 59 | +// Uint8Array[3, 4, 5] |
| 60 | +// ] |
| 61 | +``` |
| 62 | + |
| 63 | +## concat(arrays, \[length]) |
| 64 | + |
| 65 | +Concatenate one or more `Uint8Array`s and return a `Uint8Array` with their contents. |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +### Example |
| 70 | + |
| 71 | +```js |
| 72 | +import { concat } from 'uint8arrays/concat' |
| 73 | + |
| 74 | +const arrays = [ |
| 75 | + Uint8Array.from([0, 1, 2]), |
| 76 | + Uint8Array.from([3, 4, 5]) |
| 77 | +] |
| 78 | + |
| 79 | +const all = concat(arrays, 6) |
| 80 | + |
| 81 | +console.info(all) |
| 82 | +// Uint8Array[0, 1, 2, 3, 4, 5] |
| 83 | +``` |
| 84 | + |
| 85 | +## equals(a, b) |
| 86 | + |
| 87 | +Returns true if the two arrays are the same array or if they have the same length and contents. |
| 88 | + |
| 89 | +### Example |
| 90 | + |
| 91 | +```js |
| 92 | +import { equals } from 'uint8arrays/equals' |
| 93 | + |
| 94 | +const a = Uint8Array.from([0, 1, 2]) |
| 95 | +const b = Uint8Array.from([3, 4, 5]) |
| 96 | +const c = Uint8Array.from([0, 1, 2]) |
| 97 | + |
| 98 | +console.info(equals(a, b)) // false |
| 99 | +console.info(equals(a, c)) // true |
| 100 | +console.info(equals(a, a)) // true |
| 101 | +``` |
| 102 | + |
| 103 | +## fromString(string, encoding = 'utf8') |
| 104 | + |
| 105 | +Returns a new `Uint8Array` created from the passed string and interpreted as the passed encoding. |
| 106 | + |
| 107 | +Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats). |
| 108 | + |
| 109 | +### Example |
| 110 | + |
| 111 | +```js |
| 112 | +import { fromString } from 'uint8arrays/from-string' |
| 113 | + |
| 114 | +console.info(fromString('hello world')) // Uint8Array[104, 101 ... |
| 115 | +console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ... |
| 116 | +console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ... |
| 117 | +console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ... |
| 118 | +``` |
| 119 | + |
| 120 | +## toString(array, encoding = 'utf8') |
| 121 | + |
| 122 | +Returns a string created from the passed `Uint8Array` in the passed encoding. |
| 123 | + |
| 124 | +Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats). |
| 125 | + |
| 126 | +### Example |
| 127 | + |
| 128 | +```js |
| 129 | +import { toString } from 'uint8arrays/to-string' |
| 130 | + |
| 131 | +console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world' |
| 132 | +console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc' |
| 133 | +console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA' |
| 134 | +console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234' |
| 135 | +``` |
| 136 | + |
| 137 | +## xor(a, b) |
| 138 | + |
| 139 | +Returns a `Uint8Array` containing `a` and `b` xored together. |
| 140 | + |
| 141 | +### Example |
| 142 | + |
| 143 | +```js |
| 144 | +import { xor } from 'uint8arrays/xor' |
| 145 | + |
| 146 | +console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1] |
| 147 | +``` |
| 148 | + |
6 | 149 | # Install |
7 | 150 |
|
8 | 151 | ```console |
|
0 commit comments