@visulima/bytes 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE.md +85 -0
- package/README.md +382 -0
- package/dist/index.cjs +76 -0
- package/dist/index.d.cts +287 -0
- package/dist/index.d.mts +287 -0
- package/dist/index.d.ts +287 -0
- package/dist/index.mjs +59 -0
- package/dist/packem_shared/concat-CrMUrqFR.mjs +18 -0
- package/dist/packem_shared/concat-DO5DD91d.cjs +22 -0
- package/dist/packem_shared/copy-Bx6bRT8N.cjs +18 -0
- package/dist/packem_shared/copy-DrPnASpi.mjs +14 -0
- package/dist/packem_shared/endsWith-0SUmpwo5.mjs +17 -0
- package/dist/packem_shared/endsWith-D8qAxWS-.cjs +21 -0
- package/dist/packem_shared/equals-QKFTA-PW.mjs +37 -0
- package/dist/packem_shared/equals-lBip8nIE.cjs +41 -0
- package/dist/packem_shared/includesNeedle-DrBfDWAu.mjs +10 -0
- package/dist/packem_shared/includesNeedle-rD-vGkdN.cjs +14 -0
- package/dist/packem_shared/indexOfNeedle-ByhaFu9G.cjs +31 -0
- package/dist/packem_shared/indexOfNeedle-DNPShVZm.mjs +27 -0
- package/dist/packem_shared/lastIndexOfNeedle-Bw_wLjkK.cjs +30 -0
- package/dist/packem_shared/lastIndexOfNeedle-YY1XC9fo.mjs +26 -0
- package/dist/packem_shared/repeat-BBMZqtbD.cjs +22 -0
- package/dist/packem_shared/repeat-D9RZrtYr.mjs +18 -0
- package/dist/packem_shared/startsWith-BOQEkHrk.cjs +18 -0
- package/dist/packem_shared/startsWith-CC5eK1Xp.mjs +14 -0
- package/package.json +71 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
## @visulima/bytes 1.0.0 (2025-06-03)
|
|
2
|
+
|
|
3
|
+
### Features
|
|
4
|
+
|
|
5
|
+
* **bytes:** add utility functions for Uint8Array manipulation ([#507](https://github.com/visulima/visulima/issues/507)) ([07f43a0](https://github.com/visulima/visulima/commit/07f43a001a4f33a3ebcce9072d404a8975539608))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 visulima
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
<!-- DEPENDENCIES -->
|
|
24
|
+
|
|
25
|
+
# Licenses of bundled dependencies
|
|
26
|
+
The published @visulima/bytes artifact additionally contains code with the following licenses:
|
|
27
|
+
|
|
28
|
+
# Bundled dependencies:
|
|
29
|
+
## @jsr/std__bytes
|
|
30
|
+
|
|
31
|
+
> MIT License
|
|
32
|
+
>
|
|
33
|
+
> Copyright 2018-2022 the Deno authors.
|
|
34
|
+
>
|
|
35
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
36
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
37
|
+
> in the Software without restriction, including without limitation the rights
|
|
38
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
39
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
40
|
+
> furnished to do so, subject to the following conditions:
|
|
41
|
+
>
|
|
42
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
43
|
+
> copies or substantial portions of the Software.
|
|
44
|
+
>
|
|
45
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
46
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
47
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
48
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
49
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
50
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
51
|
+
> SOFTWARE.
|
|
52
|
+
|
|
53
|
+
<!-- /DEPENDENCIES -->
|
|
54
|
+
|
|
55
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
56
|
+
|
|
57
|
+
# Licenses of bundled types
|
|
58
|
+
The published @visulima/bytes artifact additionally contains code with the following licenses:
|
|
59
|
+
|
|
60
|
+
# Bundled types:
|
|
61
|
+
## @jsr/std__bytes
|
|
62
|
+
|
|
63
|
+
> MIT License
|
|
64
|
+
>
|
|
65
|
+
> Copyright 2018-2022 the Deno authors.
|
|
66
|
+
>
|
|
67
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
68
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
69
|
+
> in the Software without restriction, including without limitation the rights
|
|
70
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
71
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
72
|
+
> furnished to do so, subject to the following conditions:
|
|
73
|
+
>
|
|
74
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
75
|
+
> copies or substantial portions of the Software.
|
|
76
|
+
>
|
|
77
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
78
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
79
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
80
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
81
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
82
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
83
|
+
> SOFTWARE.
|
|
84
|
+
|
|
85
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h3>visulima bytes</h3>
|
|
3
|
+
<p>
|
|
4
|
+
Utility functions to make dealing with Uint8Arrays easier
|
|
5
|
+
</p>
|
|
6
|
+
</div>
|
|
7
|
+
|
|
8
|
+
<br />
|
|
9
|
+
|
|
10
|
+
<div align="center">
|
|
11
|
+
|
|
12
|
+
[![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
<p>
|
|
20
|
+
<sup>
|
|
21
|
+
Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
|
|
22
|
+
</sup>
|
|
23
|
+
</p>
|
|
24
|
+
</div>
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install @visulima/bytes
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
yarn add @visulima/bytes
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
pnpm add @visulima/bytes
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
Here's how you can use the functions from the `@visulima/bytes` package:
|
|
45
|
+
|
|
46
|
+
### `concat`
|
|
47
|
+
|
|
48
|
+
Concatenates an array of byte slices into a single slice.
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { concat } from "@visulima/bytes";
|
|
52
|
+
import assert from "node:assert";
|
|
53
|
+
|
|
54
|
+
const a = new Uint8Array([0, 1, 2]);
|
|
55
|
+
const b = new Uint8Array([3, 4, 5]);
|
|
56
|
+
|
|
57
|
+
assert.deepStrictEqual(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### `copy`
|
|
61
|
+
|
|
62
|
+
Copies bytes from the source array to the destination array.
|
|
63
|
+
|
|
64
|
+
**Basic usage:**
|
|
65
|
+
```typescript
|
|
66
|
+
import { copy } from "@visulima/bytes";
|
|
67
|
+
import assert from "node:assert";
|
|
68
|
+
|
|
69
|
+
const src = new Uint8Array([9, 8, 7]);
|
|
70
|
+
const dst = new Uint8Array([0, 1, 2, 3, 4, 5]);
|
|
71
|
+
|
|
72
|
+
assert.deepStrictEqual(copy(src, dst), 3);
|
|
73
|
+
assert.deepStrictEqual(dst, new Uint8Array([9, 8, 7, 3, 4, 5]));
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Copy with offset:**
|
|
77
|
+
```typescript
|
|
78
|
+
import { copy } from "@visulima/bytes";
|
|
79
|
+
import assert from "node:assert";
|
|
80
|
+
|
|
81
|
+
const src = new Uint8Array([1, 1, 1, 1]);
|
|
82
|
+
const dst = new Uint8Array([0, 0, 0, 0]);
|
|
83
|
+
|
|
84
|
+
assert.deepStrictEqual(copy(src, dst, 1), 3);
|
|
85
|
+
assert.deepStrictEqual(dst, new Uint8Array([0, 1, 1, 1]));
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### `endsWith`
|
|
89
|
+
|
|
90
|
+
Checks if a byte slice ends with a given suffix.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { endsWith } from "@visulima/bytes";
|
|
94
|
+
import assert from "node:assert";
|
|
95
|
+
|
|
96
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
97
|
+
const suffix = new Uint8Array([1, 2, 3]);
|
|
98
|
+
|
|
99
|
+
assert.deepStrictEqual(endsWith(source, suffix), true);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### `equals`
|
|
103
|
+
|
|
104
|
+
Checks if two byte slices are equal.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { equals } from "@visulima/bytes";
|
|
108
|
+
import assert from "node:assert";
|
|
109
|
+
|
|
110
|
+
const a = new Uint8Array([1, 2, 3]);
|
|
111
|
+
const b = new Uint8Array([1, 2, 3]);
|
|
112
|
+
const c = new Uint8Array([4, 5, 6]);
|
|
113
|
+
|
|
114
|
+
assert.deepStrictEqual(equals(a, b), true);
|
|
115
|
+
assert.deepStrictEqual(equals(a, c), false);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### `includesNeedle`
|
|
119
|
+
|
|
120
|
+
Determines whether a byte slice contains a specific sequence of bytes.
|
|
121
|
+
|
|
122
|
+
**Basic usage:**
|
|
123
|
+
```typescript
|
|
124
|
+
import { includesNeedle } from "@visulima/bytes";
|
|
125
|
+
import assert from "node:assert";
|
|
126
|
+
|
|
127
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
128
|
+
const needle = new Uint8Array([1, 2]);
|
|
129
|
+
|
|
130
|
+
assert.deepStrictEqual(includesNeedle(source, needle), true);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**With start index:**
|
|
134
|
+
```typescript
|
|
135
|
+
import { includesNeedle } from "@visulima/bytes";
|
|
136
|
+
import assert from "node:assert";
|
|
137
|
+
|
|
138
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
139
|
+
const needle = new Uint8Array([1, 2]);
|
|
140
|
+
|
|
141
|
+
assert.deepStrictEqual(includesNeedle(source, needle, 3), true);
|
|
142
|
+
assert.deepStrictEqual(includesNeedle(source, needle, 6), false);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### `indexOfNeedle`
|
|
146
|
+
|
|
147
|
+
Finds the first index of a specific sequence of bytes within a byte slice.
|
|
148
|
+
|
|
149
|
+
**Basic usage:**
|
|
150
|
+
```typescript
|
|
151
|
+
import { indexOfNeedle } from "@visulima/bytes";
|
|
152
|
+
import assert from "node:assert";
|
|
153
|
+
|
|
154
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
155
|
+
const needle = new Uint8Array([1, 2]);
|
|
156
|
+
const notNeedle = new Uint8Array([5, 0]);
|
|
157
|
+
|
|
158
|
+
assert.deepStrictEqual(indexOfNeedle(source, needle), 1);
|
|
159
|
+
assert.deepStrictEqual(indexOfNeedle(source, notNeedle), -1);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**With start index:**
|
|
163
|
+
```typescript
|
|
164
|
+
import { indexOfNeedle } from "@visulima/bytes";
|
|
165
|
+
import assert from "node:assert";
|
|
166
|
+
|
|
167
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
168
|
+
const needle = new Uint8Array([1, 2]);
|
|
169
|
+
|
|
170
|
+
assert.deepStrictEqual(indexOfNeedle(source, needle, 2), 3);
|
|
171
|
+
assert.deepStrictEqual(indexOfNeedle(source, needle, 6), -1);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### `lastIndexOfNeedle`
|
|
175
|
+
|
|
176
|
+
Finds the last index of a specific sequence of bytes within a byte slice.
|
|
177
|
+
|
|
178
|
+
**Basic usage:**
|
|
179
|
+
```typescript
|
|
180
|
+
import { lastIndexOfNeedle } from "@visulima/bytes";
|
|
181
|
+
import assert from "node:assert";
|
|
182
|
+
|
|
183
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
184
|
+
const needle = new Uint8Array([1, 2]);
|
|
185
|
+
const notNeedle = new Uint8Array([5, 0]);
|
|
186
|
+
|
|
187
|
+
assert.deepStrictEqual(lastIndexOfNeedle(source, needle), 5);
|
|
188
|
+
assert.deepStrictEqual(lastIndexOfNeedle(source, notNeedle), -1);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**With start index:**
|
|
192
|
+
```typescript
|
|
193
|
+
import { lastIndexOfNeedle } from "@visulima/bytes";
|
|
194
|
+
import assert from "node:assert";
|
|
195
|
+
|
|
196
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
197
|
+
const needle = new Uint8Array([1, 2]);
|
|
198
|
+
|
|
199
|
+
assert.deepStrictEqual(lastIndexOfNeedle(source, needle, 2), 1);
|
|
200
|
+
assert.deepStrictEqual(lastIndexOfNeedle(source, needle, 6), 5);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `repeat`
|
|
204
|
+
|
|
205
|
+
Repeats a byte slice a specified number of times.
|
|
206
|
+
|
|
207
|
+
**Basic usage:**
|
|
208
|
+
```typescript
|
|
209
|
+
import { repeat } from "@visulima/bytes";
|
|
210
|
+
import assert from "node:assert";
|
|
211
|
+
|
|
212
|
+
const source = new Uint8Array([0, 1, 2]);
|
|
213
|
+
|
|
214
|
+
assert.deepStrictEqual(repeat(source, 3), new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 2]));
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**Zero count:**
|
|
218
|
+
```typescript
|
|
219
|
+
import { repeat } from "@visulima/bytes";
|
|
220
|
+
import assert from "node:assert";
|
|
221
|
+
|
|
222
|
+
const source = new Uint8Array([0, 1, 2]);
|
|
223
|
+
|
|
224
|
+
assert.deepStrictEqual(repeat(source, 0), new Uint8Array([]));
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### `startsWith`
|
|
228
|
+
|
|
229
|
+
Checks if a byte slice starts with a given prefix.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { startsWith } from "@visulima/bytes";
|
|
233
|
+
import assert from "node:assert";
|
|
234
|
+
|
|
235
|
+
const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
236
|
+
const prefix = new Uint8Array([0, 1, 2]);
|
|
237
|
+
|
|
238
|
+
assert.deepStrictEqual(startsWith(source, prefix), true);
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## API / Features
|
|
242
|
+
|
|
243
|
+
This package provides the following utility functions for working with `Uint8Array`s, similar to the Deno standard library module [`@std/bytes`](https://github.com/denoland/std/tree/main/bytes):
|
|
244
|
+
|
|
245
|
+
* **`concat`**: Concatenate an array of byte slices into a single slice.
|
|
246
|
+
* **`copy`**: Copy bytes from the source array to the destination array and returns the number of bytes copied.
|
|
247
|
+
* **`endsWith`**: Returns true if the suffix array appears at the end of the source array, false otherwise.
|
|
248
|
+
* **`equals`**: Check whether byte slices are equal to each other.
|
|
249
|
+
* **`includesNeedle`**: Determines whether the source array contains the needle array.
|
|
250
|
+
* **`indexOfNeedle`**: Returns the index of the first occurrence of the needle array in the source array, or -1 if it is not present.
|
|
251
|
+
* **`lastIndexOfNeedle`**: Returns the index of the last occurrence of the needle array in the source array, or -1 if it is not present.
|
|
252
|
+
* **`repeat`**: Returns a new byte slice composed of count repetitions of the source array.
|
|
253
|
+
* **`startsWith`**: Returns true if the prefix array appears at the start of the source array, false otherwise.
|
|
254
|
+
* **`bufferToUint8Array(buf: Buffer): Uint8Array`**: Converts a Node.js `Buffer` to a `Uint8Array`.
|
|
255
|
+
* **`isUint8Array(x: unknown): x is Uint8Array`**: Checks if a value is a `Uint8Array` or (in Node.js) a `Buffer`.
|
|
256
|
+
* **`asciiToUint8Array(txt: TemplateStringsArray | string | [string]): Uint8Array`**: Converts an ASCII string to a `Uint8Array`.
|
|
257
|
+
* **`utf8ToUint8Array(txt: TemplateStringsArray | string | [string]): Uint8Array`**: Converts a UTF-8 string to a `Uint8Array` (requires Node.js `Buffer` support).
|
|
258
|
+
* **`toUint8Array(data: unknown): Uint8Array`**: Attempts to convert various data types (like `ArrayBuffer`, `Array` of numbers, `Buffer`, strings via `Buffer.from`) to a `Uint8Array`.
|
|
259
|
+
|
|
260
|
+
## Related
|
|
261
|
+
|
|
262
|
+
### `bufferToUint8Array`
|
|
263
|
+
|
|
264
|
+
Converts a Node.js `Buffer` to a `Uint8Array`.
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import { bufferToUint8Array } from "@visulima/bytes";
|
|
268
|
+
import { Buffer } from "buffer"; // Or from 'node:buffer'
|
|
269
|
+
import assert from "node:assert";
|
|
270
|
+
|
|
271
|
+
const nodeBuffer = Buffer.from("Hello");
|
|
272
|
+
const u8Array = bufferToUint8Array(nodeBuffer);
|
|
273
|
+
|
|
274
|
+
assert.ok(u8Array instanceof Uint8Array);
|
|
275
|
+
assert.deepStrictEqual(u8Array, new Uint8Array([72, 101, 108, 108, 111]));
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### `isUint8Array`
|
|
279
|
+
|
|
280
|
+
Checks if a value is a `Uint8Array` or a Node.js `Buffer`.
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
import { isUint8Array } from "@visulima/bytes";
|
|
284
|
+
import { Buffer } from "buffer";
|
|
285
|
+
import assert from "node:assert";
|
|
286
|
+
|
|
287
|
+
assert.ok(isUint8Array(new Uint8Array([1, 2])));
|
|
288
|
+
assert.ok(isUint8Array(Buffer.from("test")));
|
|
289
|
+
assert.ok(!isUint8Array("not a Uint8Array"));
|
|
290
|
+
assert.ok(!isUint8Array([1, 2])); // Plain array is not
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### `asciiToUint8Array`
|
|
294
|
+
|
|
295
|
+
Converts an ASCII string to a `Uint8Array`.
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
import { asciiToUint8Array } from "@visulima/bytes";
|
|
299
|
+
import assert from "node:assert";
|
|
300
|
+
|
|
301
|
+
const asciiArray = asciiToUint8Array("Hello!");
|
|
302
|
+
assert.deepStrictEqual(asciiArray, new Uint8Array([72, 101, 108, 108, 111, 33]));
|
|
303
|
+
|
|
304
|
+
const templateAscii = asciiToUint8Array`World`;
|
|
305
|
+
assert.deepStrictEqual(templateAscii, new Uint8Array([87, 111, 114, 108, 100]));
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### `utf8ToUint8Array`
|
|
309
|
+
|
|
310
|
+
Converts a UTF-8 string to a `Uint8Array`.
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
import { utf8ToUint8Array } from "@visulima/bytes";
|
|
314
|
+
import assert from "node:assert";
|
|
315
|
+
|
|
316
|
+
const utf8Array = utf8ToUint8Array("你好"); // "Hello" in Chinese
|
|
317
|
+
assert.deepStrictEqual(utf8Array, new Uint8Array([228, 189, 160, 229, 165, 189]));
|
|
318
|
+
|
|
319
|
+
const templateUtf8 = utf8ToUint8Array`🌍`; // Globe emoji
|
|
320
|
+
assert.deepStrictEqual(templateUtf8, new Uint8Array([240, 159, 140, 141]));
|
|
321
|
+
|
|
322
|
+
Attempts to convert various data types to a `Uint8Array`.
|
|
323
|
+
|
|
324
|
+
```typescript
|
|
325
|
+
import { toUint8Array } from "@visulima/bytes";
|
|
326
|
+
import { Buffer } from "buffer";
|
|
327
|
+
import assert from "node:assert";
|
|
328
|
+
|
|
329
|
+
// From Uint8Array
|
|
330
|
+
const u8 = new Uint8Array([1, 2, 3]);
|
|
331
|
+
assert.deepStrictEqual(toUint8Array(u8), u8);
|
|
332
|
+
|
|
333
|
+
// From ArrayBuffer
|
|
334
|
+
const buffer = new ArrayBuffer(3);
|
|
335
|
+
const view = new Uint8Array(buffer);
|
|
336
|
+
view[0] = 1; view[1] = 2; view[2] = 3;
|
|
337
|
+
assert.deepStrictEqual(toUint8Array(buffer), new Uint8Array([1, 2, 3]));
|
|
338
|
+
|
|
339
|
+
// From Array of numbers
|
|
340
|
+
assert.deepStrictEqual(toUint8Array([4, 5, 6]), new Uint8Array([4, 5, 6]));
|
|
341
|
+
|
|
342
|
+
// From Node.js Buffer
|
|
343
|
+
const nodeBuf = Buffer.from("Node");
|
|
344
|
+
assert.deepStrictEqual(toUint8Array(nodeBuf), new Uint8Array([78, 111, 100, 101]));
|
|
345
|
+
|
|
346
|
+
// From string (via Buffer.from in Node.js)
|
|
347
|
+
assert.deepStrictEqual(toUint8Array("String"), new Uint8Array([83, 116, 114, 105, 110, 103]));
|
|
348
|
+
|
|
349
|
+
try {
|
|
350
|
+
toUint8Array(123); // Not convertible
|
|
351
|
+
} catch (e: any) {
|
|
352
|
+
assert.strictEqual(e.message, "UINT8ARRAY_INCOMPATIBLE: Cannot convert data to Uint8Array");
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## Supported Node.js Versions
|
|
357
|
+
|
|
358
|
+
Libraries in this ecosystem make the best effort to track [Node.js' release schedule](https://github.com/nodejs/release#release-schedule).
|
|
359
|
+
Here's [a post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
|
|
360
|
+
|
|
361
|
+
## Contributing
|
|
362
|
+
|
|
363
|
+
If you would like to help take a look at the [list of issues](https://github.com/visulima/visulima/issues) and check our [Contributing](.github/CONTRIBUTING.md) guidelines.
|
|
364
|
+
|
|
365
|
+
> **Note:** please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
|
|
366
|
+
|
|
367
|
+
## Credits
|
|
368
|
+
|
|
369
|
+
- The Deno Standard Library authors and contributors for their work on `@std/bytes`.
|
|
370
|
+
- [Daniel Bannert](https://github.com/prisis)
|
|
371
|
+
- [All Contributors](https://github.com/visulima/visulima/graphs/contributors)
|
|
372
|
+
|
|
373
|
+
## License
|
|
374
|
+
|
|
375
|
+
The visulima bytes is open-sourced software licensed under the [MIT][license-url]
|
|
376
|
+
|
|
377
|
+
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
378
|
+
[typescript-url]: https://www.typescriptlang.org/ "TypeScript"
|
|
379
|
+
[license-image]: https://img.shields.io/npm/l/@visulima/bytes?color=blueviolet&style=for-the-badge
|
|
380
|
+
[license-url]: LICENSE.md "license"
|
|
381
|
+
[npm-image]: https://img.shields.io/npm/v/@visulima/bytes/latest.svg?style=for-the-badge&logo=npm
|
|
382
|
+
[npm-url]: https://www.npmjs.com/package/@visulima/bytes/v/latest "npm"
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
|
+
|
|
5
|
+
const node_buffer = require('node:buffer');
|
|
6
|
+
const concat = require('./packem_shared/concat-DO5DD91d.cjs');
|
|
7
|
+
const copy = require('./packem_shared/copy-Bx6bRT8N.cjs');
|
|
8
|
+
const endsWith = require('./packem_shared/endsWith-D8qAxWS-.cjs');
|
|
9
|
+
const equals = require('./packem_shared/equals-lBip8nIE.cjs');
|
|
10
|
+
const includesNeedle = require('./packem_shared/includesNeedle-rD-vGkdN.cjs');
|
|
11
|
+
const indexOfNeedle = require('./packem_shared/indexOfNeedle-ByhaFu9G.cjs');
|
|
12
|
+
const lastIndexOfNeedle = require('./packem_shared/lastIndexOfNeedle-Bw_wLjkK.cjs');
|
|
13
|
+
const repeat = require('./packem_shared/repeat-BBMZqtbD.cjs');
|
|
14
|
+
const startsWith = require('./packem_shared/startsWith-BOQEkHrk.cjs');
|
|
15
|
+
|
|
16
|
+
var __defProp = Object.defineProperty;
|
|
17
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
18
|
+
const bufferToUint8Array = /* @__PURE__ */ __name((buf) => new Uint8Array(buf.buffer, buf.byteOffset, buf.length), "bufferToUint8Array");
|
|
19
|
+
const isUint8Array = typeof node_buffer.Buffer === "function" ? (x) => x instanceof Uint8Array || node_buffer.Buffer.isBuffer(x) : (x) => x instanceof Uint8Array;
|
|
20
|
+
const asciiToUint8Array = /* @__PURE__ */ __name((txt) => {
|
|
21
|
+
if (typeof txt === "string")
|
|
22
|
+
return asciiToUint8Array([txt]);
|
|
23
|
+
const [input] = Array.isArray(txt) ? txt : [String.raw(txt)];
|
|
24
|
+
const inputLength = input.length;
|
|
25
|
+
const result = new Uint8Array(inputLength);
|
|
26
|
+
for (let index = 0; index < inputLength; index += 1) {
|
|
27
|
+
result[index] = input.charCodeAt(index) & 255;
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}, "asciiToUint8Array");
|
|
31
|
+
const utf8ToUint8Array = /* @__PURE__ */ __name((txt) => {
|
|
32
|
+
if (typeof txt === "string")
|
|
33
|
+
return utf8ToUint8Array([txt]);
|
|
34
|
+
const [input] = Array.isArray(txt) ? txt : [String.raw(txt)];
|
|
35
|
+
return bufferToUint8Array(node_buffer.Buffer.from(input, "utf8"));
|
|
36
|
+
}, "utf8ToUint8Array");
|
|
37
|
+
const toUint8Array = /* @__PURE__ */ __name((data) => {
|
|
38
|
+
if (typeof node_buffer.Buffer === "function" && node_buffer.Buffer.isBuffer(data)) {
|
|
39
|
+
return bufferToUint8Array(data);
|
|
40
|
+
}
|
|
41
|
+
if (data instanceof Uint8Array) {
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
if (data instanceof ArrayBuffer)
|
|
45
|
+
return new Uint8Array(data);
|
|
46
|
+
if (Array.isArray(data) && data.every((item) => typeof item === "number")) {
|
|
47
|
+
return new Uint8Array(data);
|
|
48
|
+
}
|
|
49
|
+
if (typeof node_buffer.Buffer === "function") {
|
|
50
|
+
if (node_buffer.Buffer.isBuffer(data)) {
|
|
51
|
+
return bufferToUint8Array(data);
|
|
52
|
+
}
|
|
53
|
+
if (typeof data === "string") {
|
|
54
|
+
try {
|
|
55
|
+
return bufferToUint8Array(node_buffer.Buffer.from(data));
|
|
56
|
+
} catch {
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
throw new Error("UINT8ARRAY_INCOMPATIBLE: Cannot convert data to Uint8Array");
|
|
61
|
+
}, "toUint8Array");
|
|
62
|
+
|
|
63
|
+
exports.concat = concat.concat;
|
|
64
|
+
exports.copy = copy.copy;
|
|
65
|
+
exports.endsWith = endsWith.endsWith;
|
|
66
|
+
exports.equals = equals.equals;
|
|
67
|
+
exports.includesNeedle = includesNeedle.includesNeedle;
|
|
68
|
+
exports.indexOfNeedle = indexOfNeedle.indexOfNeedle;
|
|
69
|
+
exports.lastIndexOfNeedle = lastIndexOfNeedle.lastIndexOfNeedle;
|
|
70
|
+
exports.repeat = repeat.repeat;
|
|
71
|
+
exports.startsWith = startsWith.startsWith;
|
|
72
|
+
exports.asciiToUint8Array = asciiToUint8Array;
|
|
73
|
+
exports.bufferToUint8Array = bufferToUint8Array;
|
|
74
|
+
exports.isUint8Array = isUint8Array;
|
|
75
|
+
exports.toUint8Array = toUint8Array;
|
|
76
|
+
exports.utf8ToUint8Array = utf8ToUint8Array;
|