{"name":"uint8array-extras","version":"0.5.0","description":"Useful utilities for working with Uint8Array (and Buffer)","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/uint8array-extras.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./index.d.ts","default":"./index.js"},"engines":{"node":">=18"},"sideEffects":false,"scripts":{"test":"xo && ava && tsc index.d.ts"},"keywords":["uint8array","uint8","typedarray","buffer","typedarray","arraybuffer","is","assert","concat","equals","compare","base64","string","atob","btoa","hex","hexadecimal"],"devDependencies":{"ava":"^5.3.1","typescript":"^5.2.2","xo":"^0.56.0"},"types":"./index.d.ts","gitHead":"fd1bccba32d81eb9b64ad51f8e5de37f0edf73a6","bugs":{"url":"https://github.com/sindresorhus/uint8array-extras/issues"},"homepage":"https://github.com/sindresorhus/uint8array-extras#readme","_id":"uint8array-extras@0.5.0","_nodeVersion":"18.18.2","_npmVersion":"9.2.0","dist":{"integrity":"sha512-CKAKa+/rjCblsuGrcmIJLluHcZMYKcMwgdhAZZEnSplFFGCuGKF6e2L+CeVXK0zX5rYyerrqG2dnHIANm3i9qA==","shasum":"6b997348064a46609adb13412f155bfce9830c30","tarball":"http://registry-npm.ywwl.com/uint8array-extras/download/uint8array-extras-0.5.0.tgz","fileCount":5,"unpackedSize":20898,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIB0cSi7xQUfKI8nafvuxku0SoRCMW1/RJK8YgBRrWlA0AiA3NzFNSsmUd1yn3eP1geQ5Me20w8ybrNKIe1elZ5X8Xg=="}],"size":4773,"noattachment":false,"key":"/uint8array-extras/-/uint8array-extras-0.5.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/uint8array-extras_0.5.0_1698563607016_0.32739327037052357"},"_hasShrinkwrap":false,"readme":"# uint8array-extras\n\n> Useful utilities for working with [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) (and [`Buffer`](https://nodejs.org/api/buffer.html))\n\nIt's time to [transition from `Buffer` to `Uint8Array`](https://sindresorhus.com/blog/goodbye-nodejs-buffer), and this package helps fill in the gaps.\n\nNote that `Buffer` is a `Uint8Array` subclass, so you can use this package with `Buffer` too.\n\nThis package is tree-shakeable and browser-compatible.\n\nThis package also includes methods to convert a string to Base64 and back.\n\nNote: In the browser, do not use [`globalThis.atob()`](https://developer.mozilla.org/en-US/docs/Web/API/atob) / [`globalThis.btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/btoa) because they [do not support Unicode](https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem). This package does.\n\n## Install\n\n```sh\nnpm install uint8array-extras\n```\n\n## Usage\n\n```js\nimport {concatUint8Arrays} from 'uint8array-extras';\n\nconst a = new Uint8Array([1, 2, 3]);\nconst b = new Uint8Array([4, 5, 6]);\n\nconsole.log(concatUint8Arrays([a, b]));\n//=> Uint8Array [1, 2, 3, 4, 5, 6]\n```\n\n## API\n\n### `isUint8Array(value: unknown): boolean`\n\nCheck if the given value is an instance of `Uint8Array`.\n\nReplacement for [`Buffer.isBuffer()`](https://nodejs.org/api/buffer.html#static-method-bufferisbufferobj).\n\n```js\nimport {isUint8Array} from 'uint8array-extras';\n\nconsole.log(isUint8Array(new Uint8Array()));\n//=> true\n\nconsole.log(isUint8Array(Buffer.from('x')));\n//=> true\n\nconsole.log(isUint8Array(new ArrayBuffer(10)));\n//=> false\n```\n\n### `assertUint8Array(value: unknown)`\n\nThrow a `TypeError` if the given value is not an instance of `Uint8Array`.\n\n```js\nimport {assertUint8Array} from 'uint8array-extras';\n\ntry {\n\tassertUint8Array(new ArrayBuffer(10)); // Throws a TypeError\n} catch (error) {\n\tconsole.error(error.message);\n}\n```\n\n### `toUint8Array(value: TypedArray | ArrayBuffer | DataView): Uint8Array`\n\nConvert a value to a `Uint8Array` without copying its data.\n\nThis can be useful for converting a `Buffer` to a pure `Uint8Array`. `Buffer` is already an `Uint8Array` subclass, but [`Buffer` alters some behavior](https://sindresorhus.com/blog/goodbye-nodejs-buffer), so it can be useful to cast it to a pure `Uint8Array` before returning it.\n\nTip: If you want a copy, just call `.slice()` on the return value.\n\n### `concatUint8Arrays(arrays: Uint8Array[], totalLength?: number): Uint8Array`\n\nConcatenate the given arrays into a new array.\n\nIf `arrays` is empty, it will return a zero-sized `Uint8Array`.\n\nIf `totalLength` is not specified, it is calculated from summing the lengths of the given arrays.\n\nReplacement for [`Buffer.concat()`](https://nodejs.org/api/buffer.html#static-method-bufferconcatlist-totallength).\n\n```js\nimport {concatUint8Arrays} from 'uint8array-extras';\n\nconst a = new Uint8Array([1, 2, 3]);\nconst b = new Uint8Array([4, 5, 6]);\n\nconsole.log(concatUint8Arrays([a, b]));\n//=> Uint8Array [1, 2, 3, 4, 5, 6]\n```\n\n### `areUint8ArraysEqual(a: Uint8Array, b: Uint8Array): boolean`\n\nCheck if two arrays are identical by verifying that they contain the same bytes in the same sequence.\n\nReplacement for [`Buffer#equals()`](https://nodejs.org/api/buffer.html#bufequalsotherbuffer).\n\n```js\nimport {areUint8ArraysEqual} from 'uint8array-extras';\n\nconst a = new Uint8Array([1, 2, 3]);\nconst b = new Uint8Array([1, 2, 3]);\nconst c = new Uint8Array([4, 5, 6]);\n\nconsole.log(areUint8ArraysEqual(a, b));\n//=> true\n\nconsole.log(areUint8ArraysEqual(a, c));\n//=> false\n```\n\n### `compareUint8Arrays(a: Uint8Array, b: Uint8Array): 0 | 1 | -1`\n\nCompare two arrays and indicate their relative order or equality. Useful for sorting.\n\nReplacement for [`Buffer.compare()`](https://nodejs.org/api/buffer.html#static-method-buffercomparebuf1-buf2).\n\n```js\nimport {compareUint8Arrays} from 'uint8array-extras';\n\nconst array1 = new Uint8Array([1, 2, 3]);\nconst array2 = new Uint8Array([4, 5, 6]);\nconst array3 = new Uint8Array([7, 8, 9]);\n\n[array3, array1, array2].sort(compareUint8Arrays);\n//=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n```\n\n### `uint8ArrayToString(array: Uint8Array): string`\n\nConvert a `Uint8Array` (containing a UTF-8 string) to a string.\n\nReplacement for [`Buffer#toString()`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end).\n\n```js\nimport {uint8ArrayToString} from 'uint8array-extras';\n\nconst byteArray = new Uint8Array([72, 101, 108, 108, 111]);\n\nconsole.log(uint8ArrayToString(byteArray));\n//=> 'Hello'\n```\n\n### `stringToUint8Array(string: string): Uint8Array`\n\nConvert a string to a `Uint8Array` (using UTF-8 encoding).\n\nReplacement for [`Buffer.from('Hello')`](https://nodejs.org/api/buffer.html#static-method-bufferfromstring-encoding).\n\n```js\nimport {stringToUint8Array} from 'uint8array-extras';\n\nconsole.log(stringToUint8Array('Hello'));\n//=> Uint8Array [72, 101, 108, 108, 111]\n```\n\n### `uint8ArrayToBase64(array: Uint8Array, options?: {urlSafe: boolean}): string`\n\nConvert a `Uint8Array` to a Base64-encoded string.\n\nSpecify `{urlSafe: true}` to get a [Base64URL](https://base64.guru/standards/base64url)-encoded string.\n\nReplacement for [`Buffer#toString('base64')`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end).\n\n```js\nimport {uint8ArrayToBase64} from 'uint8array-extras';\n\nconst byteArray = new Uint8Array([72, 101, 108, 108, 111]);\n\nconsole.log(uint8ArrayToBase64(byteArray));\n//=> 'SGVsbG8='\n```\n\n### `base64ToUint8Array(string: string): Uint8Array`\n\nConvert a Base64-encoded or [Base64URL](https://base64.guru/standards/base64url)-encoded string to a `Uint8Array`.\n\nReplacement for [`Buffer.from('SGVsbG8=', 'base64')`](https://nodejs.org/api/buffer.html#static-method-bufferfromstring-encoding).\n\n```js\nimport {base64ToUint8Array} from 'uint8array-extras';\n\nconsole.log(base64ToUint8Array('SGVsbG8='));\n//=> Uint8Array [72, 101, 108, 108, 111]\n```\n\n### `stringToBase64(string: string, options?: {urlSafe: boolean}): string`\n\nEncode a string to Base64-encoded string.\n\nSpecify `{urlSafe: true}` to get a [Base64URL](https://base64.guru/standards/base64url)-encoded string.\n\nReplacement for `Buffer.from('Hello').toString('base64')` and [`btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/btoa).\n\n```js\nimport {stringToBase64} from 'uint8array-extras';\n\nconsole.log(stringToBase64('Hello'));\n//=> 'SGVsbG8='\n```\n\n### `base64ToString(base64String: string): string`\n\nDecode a Base64-encoded or [Base64URL](https://base64.guru/standards/base64url)-encoded string to a string.\n\nReplacement for `Buffer.from('SGVsbG8=', 'base64').toString()` and [`atob()`](https://developer.mozilla.org/en-US/docs/Web/API/atob).\n\n```js\nimport {base64ToString} from 'uint8array-extras';\n\nconsole.log(base64ToString('SGVsbG8='));\n//=> 'Hello'\n```\n\n### `uint8ArrayToHex(array: Uint8Array): string`\n\nConvert a `Uint8Array` to a Hex string.\n\nReplacement for [`Buffer#toString('hex')`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end).\n\n```js\nimport {uint8ArrayToHex} from 'uint8array-extras';\n\nconst byteArray = new Uint8Array([72, 101, 108, 108, 111]);\n\nconsole.log(uint8ArrayToHex(byteArray));\n//=> '48656c6c6f'\n```\n\n### `hexToUint8Array(hexString: string): Uint8Array`\n\nConvert a Hex string to a `Uint8Array`.\n\nReplacement for [`Buffer.from('48656c6c6f', 'hex')`](https://nodejs.org/api/buffer.html#static-method-bufferfromstring-encoding).\n\n```js\nimport {hexToUint8Array} from 'uint8array-extras';\n\nconsole.log(hexToUint8Array('48656c6c6f'));\n//=> Uint8Array [72, 101, 108, 108, 111]\n```\n","publish_time":1698563607193,"_cnpm_publish_time":1698563607193,"dist-tags":{"latest":"1.6.0"}}