{"id":13560713,"url":"https://github.com/sindresorhus/uint8array-extras","last_synced_at":"2025-05-14T21:05:26.371Z","repository":{"id":203234393,"uuid":"709150027","full_name":"sindresorhus/uint8array-extras","owner":"sindresorhus","description":"Useful utilities for working with Uint8Array (and Buffer)","archived":false,"fork":false,"pushed_at":"2025-01-24T06:33:17.000Z","size":32,"stargazers_count":251,"open_issues_count":2,"forks_count":7,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-06T14:06:09.510Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","buy_me_a_coffee":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2023-10-24T05:55:45.000Z","updated_at":"2025-03-09T23:30:01.000Z","dependencies_parsed_at":"2023-12-19T05:56:14.612Z","dependency_job_id":"78a372a9-b646-41db-96a6-0b1077edab54","html_url":"https://github.com/sindresorhus/uint8array-extras","commit_stats":{"total_commits":28,"total_committers":4,"mean_commits":7.0,"dds":0.1428571428571429,"last_synced_commit":"733444ac8059552cdc48acf0ad0c8fc902f95431"},"previous_names":["sindresorhus/uint8array-extras"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fuint8array-extras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fuint8array-extras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fuint8array-extras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fuint8array-extras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/uint8array-extras/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248714754,"owners_count":21149961,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:00:48.924Z","updated_at":"2025-04-13T16:53:21.243Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://buymeacoffee.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["Packages","JavaScript","包"],"sub_categories":["Miscellaneous","杂项"],"readme":"# uint8array-extras\n\n\u003e 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//=\u003e 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//=\u003e true\n\nconsole.log(isUint8Array(Buffer.from('x')));\n//=\u003e true\n\nconsole.log(isUint8Array(new ArrayBuffer(10)));\n//=\u003e 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//=\u003e 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//=\u003e true\n\nconsole.log(areUint8ArraysEqual(a, c));\n//=\u003e 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//=\u003e [[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//=\u003e 'Hello'\n\nconst zh = new Uint8Array([167, 65, 166, 110]);\nconsole.log(uint8ArrayToString(zh, 'big5'));\n//=\u003e '你好'\n\nconst ja = new Uint8Array([130, 177, 130, 241, 130, 201, 130, 191, 130, 205]);\nconsole.log(uint8ArrayToString(ja, 'shift-jis'));\n//=\u003e 'こんにちは'\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//=\u003e 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//=\u003e '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//=\u003e 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//=\u003e '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//=\u003e '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//=\u003e '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//=\u003e 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#bufreadintbeoffset-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//=\u003e 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//=\u003e 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//=\u003e true\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fuint8array-extras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fuint8array-extras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fuint8array-extras/lists"}