uint8arraylist 2.3.0 → 2.3.1
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/README.md +56 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
|
|
10
10
|
- [Install](#install)
|
|
11
11
|
- [Usage](#usage)
|
|
12
|
+
- [Converting Uint8ArrayLists to Uint8Arrays](#converting-uint8arraylists-to-uint8arrays)
|
|
13
|
+
- [slice](#slice)
|
|
14
|
+
- [subarray](#subarray)
|
|
15
|
+
- [sublist](#sublist)
|
|
12
16
|
- [Inspiration](#inspiration)
|
|
13
17
|
- [License](#license)
|
|
14
18
|
- [Contribution](#contribution)
|
|
@@ -28,11 +32,11 @@ const list = new Uint8ArrayList()
|
|
|
28
32
|
list.append(Uint8Array.from([0, 1, 2]))
|
|
29
33
|
list.append(Uint8Array.from([3, 4, 5]))
|
|
30
34
|
|
|
31
|
-
list.
|
|
35
|
+
list.subarray()
|
|
32
36
|
// -> Uint8Array([0, 1, 2, 3, 4, 5])
|
|
33
37
|
|
|
34
38
|
list.consume(3)
|
|
35
|
-
list.
|
|
39
|
+
list.subarray()
|
|
36
40
|
// -> Uint8Array([3, 4, 5])
|
|
37
41
|
|
|
38
42
|
// you can also iterate over the list
|
|
@@ -40,8 +44,57 @@ for (const buf of list) {
|
|
|
40
44
|
// ..do something with `buf`
|
|
41
45
|
}
|
|
42
46
|
|
|
47
|
+
list.subarray(0, 1)
|
|
48
|
+
// -> Uint8Array([0])
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Converting Uint8ArrayLists to Uint8Arrays
|
|
52
|
+
|
|
53
|
+
There are two ways to turn a `Uint8ArrayList` into a `Uint8Array` - `.slice` and `.subarray` and one way to turn a `Uint8ArrayList` into a `Uint8ArrayList` with different contents - `.sublist`.
|
|
54
|
+
|
|
55
|
+
#### slice
|
|
56
|
+
|
|
57
|
+
Slice follows the same semantics as [Uint8Array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice) in that it creates a new `Uint8Array` and copies bytes into it using an optional offset & length.
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
const list = new Uint8ArrayList()
|
|
61
|
+
list.append(Uint8Array.from([0, 1, 2]))
|
|
62
|
+
list.append(Uint8Array.from([3, 4, 5]))
|
|
63
|
+
|
|
64
|
+
list.slice(0, 1)
|
|
65
|
+
// -> Uint8Array([0])
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### subarray
|
|
69
|
+
|
|
70
|
+
Subarray attempts to follow the same semantics as [Uint8Array.subarray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray) with one important different - this is a no-copy operation, unless the requested bytes span two internal buffers in which case it is a copy operation.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const list = new Uint8ArrayList()
|
|
74
|
+
list.append(Uint8Array.from([0, 1, 2]))
|
|
75
|
+
list.append(Uint8Array.from([3, 4, 5]))
|
|
76
|
+
|
|
43
77
|
list.slice(0, 1)
|
|
44
|
-
// ->
|
|
78
|
+
// -> Uint8Array([0]) - no-copy
|
|
79
|
+
|
|
80
|
+
list.slice(2, 5)
|
|
81
|
+
// -> Uint8Array([2, 3]) - copy
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### sublist
|
|
85
|
+
|
|
86
|
+
Sublist creates and returns a new `Uint8ArrayList` that shares the underlying buffers with the original so is always a no-copy operation.
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
const list = new Uint8ArrayList()
|
|
90
|
+
list.append(Uint8Array.from([0, 1, 2]))
|
|
91
|
+
list.append(Uint8Array.from([3, 4, 5]))
|
|
92
|
+
|
|
93
|
+
list.sublist(0, 1)
|
|
94
|
+
// -> Uint8ArrayList([0]) - no-copy
|
|
95
|
+
|
|
96
|
+
list.sublist(2, 5)
|
|
97
|
+
// -> Uint8ArrayList([2, 3]) - no-copy
|
|
45
98
|
```
|
|
46
99
|
|
|
47
100
|
## Inspiration
|