Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 168 lines 4.7 kB view raw
1/** 2 * Simple `Uint8Array` utilities for AT Protocol. 3 * 4 * ## alloc(size) 5 * 6 * Create a new `Uint8Array`. 7 * 8 * @example alloc(size) 9 * 10 * ```js 11 * import { alloc } from 'uint8arrays/alloc' 12 * 13 * const buf = alloc(100) 14 * ``` 15 * 16 * ## allocUnsafe(size) 17 * 18 * Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`. 19 * 20 * On platforms that support it, memory referenced by the returned `Uint8Array` will not be initialized. 21 * 22 * @example allocUnsafe(size) 23 * 24 * ```js 25 * import { allocUnsafe } from 'uint8arrays/alloc' 26 * 27 * const buf = allocUnsafe(100) 28 * ``` 29 * 30 * ## compare(a, b) 31 * 32 * Compare two `Uint8Arrays` 33 * 34 * @example compare(a, b) 35 * 36 * ```js 37 * import { compare } from 'uint8arrays/compare' 38 * 39 * const arrays = [ 40 * Uint8Array.from([3, 4, 5]), 41 * Uint8Array.from([0, 1, 2]) 42 * ] 43 * 44 * const sorted = arrays.sort(compare) 45 * 46 * console.info(sorted) 47 * // [ 48 * // Uint8Array[0, 1, 2] 49 * // Uint8Array[3, 4, 5] 50 * // ] 51 * ``` 52 * 53 * ## concat(arrays, \[length]) 54 * 55 * Concatenate one or more `Uint8Array`s and return a `Uint8Array` with their contents. 56 * 57 * 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. 58 * 59 * @example concat(arrays, \[length]) 60 * 61 * ```js 62 * import { concat } from 'uint8arrays/concat' 63 * 64 * const arrays = [ 65 * Uint8Array.from([0, 1, 2]), 66 * Uint8Array.from([3, 4, 5]) 67 * ] 68 * 69 * const all = concat(arrays, 6) 70 * 71 * console.info(all) 72 * // Uint8Array[0, 1, 2, 3, 4, 5] 73 * ``` 74 * 75 * ## equals(a, b) 76 * 77 * Returns true if the two arrays are the same array or if they have the same length and contents. 78 * 79 * @example equals(a, b) 80 * 81 * ```js 82 * import { equals } from 'uint8arrays/equals' 83 * 84 * const a = Uint8Array.from([0, 1, 2]) 85 * const b = Uint8Array.from([3, 4, 5]) 86 * const c = Uint8Array.from([0, 1, 2]) 87 * 88 * console.info(equals(a, b)) // false 89 * console.info(equals(a, c)) // true 90 * console.info(equals(a, a)) // true 91 * ``` 92 * 93 * ## fromString(string, encoding = 'utf8') 94 * 95 * Returns a new `Uint8Array` created from the passed string and interpreted as the passed encoding. 96 * 97 * 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). 98 * 99 * @example fromString(string, encoding = 'utf8') 100 * 101 * ```js 102 * import { fromString } from 'uint8arrays/from-string' 103 * 104 * console.info(fromString('hello world')) // Uint8Array[104, 101 ... 105 * console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ... 106 * console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ... 107 * console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ... 108 * ``` 109 * 110 * ## toString(array, encoding = 'utf8') 111 * 112 * Returns a string created from the passed `Uint8Array` in the passed encoding. 113 * 114 * 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). 115 * 116 * @example toString(array, encoding = 'utf8') 117 * 118 * ```js 119 * import { toString } from 'uint8arrays/to-string' 120 * 121 * console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world' 122 * console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc' 123 * console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA' 124 * console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234' 125 * ``` 126 * 127 * ## xor(a, b) 128 * 129 * Returns a `Uint8Array` containing `a` and `b` xored together. 130 * 131 * @example xor(a, b) 132 * 133 * ```js 134 * import { xor } from 'uint8arrays/xor' 135 * 136 * console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1] 137 * ``` 138 * 139 * ## xorCompare(a, b) 140 * 141 * Compares the distances between two xor `Uint8Array`s. 142 * 143 * @example xorCompare(a, b) 144 * 145 * ```ts 146 * import { xor } from 'uint8arrays/xor' 147 * import { xorCompare } from 'uint8arrays/xor-compare' 148 * 149 * const target = Uint8Array.from([1, 1]) 150 * const val1 = Uint8Array.from([1, 0]) 151 * const xor1 = xor(target, val1) 152 * 153 * const val2 = Uint8Array.from([0, 1]) 154 * const xor2 = xor(target, val2) 155 * 156 * console.info(xorCompare(xor1, xor2)) // -1 or 0 or 1 157 * ``` 158 * @module 159 */ 160 161import { xor } from "./xor.ts"; 162import { compare, equals, xorCompare } from "./compare.ts"; 163import { concat } from "./concat.ts"; 164import { fromString, toString } from "./string.ts"; 165 166export { compare, concat, equals, fromString, toString, xor, xorCompare }; 167 168export type { SupportedEncodings } from "./util.ts";