{"name":"uint8array-extras","version":"1.4.1","keywords":["uint8array","uint8","typedarray","buffer","typedarray","arraybuffer","is","assert","concat","equals","compare","base64","string","atob","btoa","hex","hexadecimal"],"author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"license":"MIT","_id":"uint8array-extras@1.4.1","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"homepage":"https://github.com/sindresorhus/uint8array-extras#readme","bugs":{"url":"https://github.com/sindresorhus/uint8array-extras/issues"},"dist":{"shasum":"6d6e88363a414577c133aca1e63b2a6c5524be21","tarball":"http://registry-npm.ywwl.com/uint8array-extras/download/uint8array-extras-1.4.1.tgz","fileCount":5,"integrity":"sha512-+NWHrac9dvilNgme+gP4YrBSumsaMZP0fNBtXXFIf33RLLKEcBUKaQZ7ULUbS0sBfcjxIZ4V96OTRkCbM7hxpw==","signatures":[{"sig":"MEUCIQDuR9ory3/4M/+mLSCCZndOeQbf6A+LFBIn1gCpGOICFgIgai76JXX5BHPfhoEdThe2wgTV/Q5BIUasexHP3I9wdqI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":28384,"size":6192,"noattachment":false,"key":"/uint8array-extras/-/uint8array-extras-1.4.1.tgz"},"type":"module","types":"./index.d.ts","engines":{"node":">=18"},"exports":{"types":"./index.d.ts","default":"./index.js"},"funding":"https://github.com/sponsors/sindresorhus","gitHead":"c3b4ff1c7909271ef887c4251f63d7dd4e796dc9","scripts":{"test":"xo && ava && tsc index.d.ts"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/uint8array-extras.git","type":"git"},"_npmVersion":"10.9.2","description":"Useful utilities for working with Uint8Array (and Buffer)","directories":{},"sideEffects":false,"_nodeVersion":"20.19.1","_hasShrinkwrap":false,"devDependencies":{"xo":"^0.56.0","ava":"^6.0.1","benchmark":"2.1.4","typescript":"^5.3.3"},"_npmOperationalInternal":{"tmp":"tmp/uint8array-extras_1.4.1_1754980758300_0.3161858972437377","host":"s3://npm-registry-packages-npm-production"},"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### `assertUint8ArrayOrArrayBuffer(value: unknown)`\n\nThrow a `TypeError` if the given value is not an instance of `Uint8Array` or `ArrayBuffer`.\n\nUseful as a guard for functions that accept either a `Uint8Array` or `ArrayBuffer`.\n\n```js\nimport {assertUint8ArrayOrArrayBuffer} from 'uint8array-extras';\n\nassertUint8ArrayOrArrayBuffer(new Uint8Array());\nassertUint8ArrayOrArrayBuffer(new ArrayBuffer(8));\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 a `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 | ArrayBuffer, encoding?: string = 'utf8'): string`\n\nConvert a `Uint8Array` to a string.\n\n- Parameter: `encoding` - The [encoding](https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings) to convert from.\n\nReplacement for [`Buffer#toString()`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end). For the `encoding` parameter, `latin1` should be used instead of `binary` and `utf-16le` instead of `utf16le`.\n\n```js\nimport {uint8ArrayToString} from 'uint8array-extras';\n\nconst byteArray = new Uint8Array([72, 101, 108, 108, 111]);\nconsole.log(uint8ArrayToString(byteArray));\n//=> 'Hello'\n\nconst zh = new Uint8Array([167, 65, 166, 110]);\nconsole.log(uint8ArrayToString(zh, 'big5'));\n//=> '你好'\n\nconst ja = new Uint8Array([130, 177, 130, 241, 130, 201, 130, 191, 130, 205]);\nconsole.log(uint8ArrayToString(ja, 'shift-jis'));\n//=> 'こんにちは'\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\nAccepts Base64URL with or without padding.\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 a 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\nAccepts Base64URL with or without padding.\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\n### `getUintBE(view: DataView): number`\n\nRead `DataView#byteLength` number of bytes from the given view, up to 48-bit.\n\nReplacement for [`Buffer#readUIntBE`](https://nodejs.org/api/buffer.html#bufreaduintbeoffset-bytelength)\n\n```js\nimport {getUintBE} from 'uint8array-extras';\n\nconst byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(getUintBE(new DataView(byteArray.buffer)));\n//=> 20015998341291\n```\n\n### `indexOf(array: Uint8Array, value: Uint8Array): number`\n\nFind the index of the first occurrence of the given sequence of bytes (`value`) within the given `Uint8Array` (`array`).\n\nReplacement for [`Buffer#indexOf`](https://nodejs.org/api/buffer.html#bufindexofvalue-byteoffset-encoding). `Uint8Array#indexOf` only takes a number which is different from Buffer's `indexOf` implementation.\n\n```js\nimport {indexOf} from 'uint8array-extras';\n\nconst byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]);\n\nconsole.log(indexOf(byteArray, new Uint8Array([0x78, 0x90])));\n//=> 3\n```\n\n### `includes(array: Uint8Array, value: Uint8Array): boolean`\n\nChecks if the given sequence of bytes (`value`) is within the given `Uint8Array` (`array`).\n\nReturns true if the value is included, otherwise false.\n\nReplacement for [`Buffer#includes`](https://nodejs.org/api/buffer.html#bufincludesvalue-byteoffset-encoding). `Uint8Array#includes` only takes a number which is different from Buffer's `includes` implementation.\n\n```js\nimport {includes} from 'uint8array-extras';\n\nconst byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]);\n\nconsole.log(includes(byteArray, new Uint8Array([0x78, 0x90])));\n//=> true\n```\n","publish_time":1754980758482,"_cnpm_publish_time":1754980758482,"dist-tags":{"latest":"1.6.0"}}