@nxtedition/lib 28.0.23 → 28.0.24

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/slice.js +48 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "28.0.23",
3
+ "version": "28.0.24",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -92,5 +92,5 @@
92
92
  "canvas": "^3.1.0",
93
93
  "rxjs": "^7.0.0"
94
94
  },
95
- "gitHead": "2fbd54c297b3887f26f8c8f4384e1b0fa8fe1345"
95
+ "gitHead": "73f8163622322cb2cd3d0a1f6e9473c6d3b4693b"
96
96
  }
package/slice.js CHANGED
@@ -3,15 +3,22 @@ import util from 'node:util'
3
3
  const EMPTY_BUF = Buffer.alloc(0)
4
4
 
5
5
  export class Slice {
6
- buffer
6
+ /** @type {Buffer} */
7
+ buffer = EMPTY_BUF
8
+
9
+ /** @type {number} */
7
10
  byteOffset = 0
11
+
12
+ /** @type {number} */
8
13
  byteLength = 0
14
+
15
+ /** @type {number} */
9
16
  maxByteLength = 0
10
17
 
18
+ /** @type {Buffer} */
11
19
  static EMPTY_BUF = EMPTY_BUF
12
20
 
13
21
  /**
14
- *
15
22
  * @param {Buffer} [buffer]
16
23
  * @param {number} [byteOffset]
17
24
  * @param {number} [byteLength]
@@ -49,6 +56,9 @@ export class Slice {
49
56
  this.maxByteLength = maxByteLength
50
57
  }
51
58
 
59
+ /**
60
+ * @returns {void}
61
+ */
52
62
  reset() {
53
63
  this.buffer = Slice.EMPTY_BUF
54
64
  this.byteOffset = 0
@@ -56,12 +66,15 @@ export class Slice {
56
66
  this.maxByteLength = 0
57
67
  }
58
68
 
69
+ /**
70
+ * @returns {number}
71
+ */
59
72
  get length() {
60
73
  return this.byteLength
61
74
  }
62
75
 
63
76
  /**
64
- * @param {Buffer|Slice} target
77
+ * @param {Uint8Array} target
65
78
  * @param {number} [targetStart = 0]
66
79
  * @param {number} [sourceStart = 0]
67
80
  * @param {number} [sourceEnd = this.byteOffset]
@@ -94,12 +107,12 @@ export class Slice {
94
107
  }
95
108
 
96
109
  /**
97
- * @param {Buffer|Slice} target
110
+ * @param {Uint8Array} target
98
111
  * @param {number} [targetStart = 0]
99
112
  * @param {number} [targetEnd = target.byteOffset]
100
113
  * @param {number} [sourceStart = 0]
101
114
  * @param {number} [sourceEnd = this.byteOffset]
102
- * @returns
115
+ * @returns {-1 | 0 | 1}
103
116
  */
104
117
  compare(target, targetStart, targetEnd, sourceStart, sourceEnd) {
105
118
  if (target instanceof Slice) {
@@ -134,7 +147,6 @@ export class Slice {
134
147
  }
135
148
 
136
149
  /**
137
- *
138
150
  * @param {string} string
139
151
  * @param {number} [offset=0]
140
152
  * @param {number} [length=this.byteLength - offset]
@@ -155,8 +167,13 @@ export class Slice {
155
167
  return this.buffer.write(string, offset, length, encoding)
156
168
  }
157
169
 
158
- set(array, offset) {
159
- if (array == null) {
170
+ /**
171
+ * @param {Buffer|Slice} source
172
+ * @param {number} offset
173
+ * @returns
174
+ */
175
+ set(source, offset) {
176
+ if (source == null) {
160
177
  return
161
178
  }
162
179
 
@@ -166,31 +183,28 @@ export class Slice {
166
183
  offset += this.byteOffset
167
184
  }
168
185
 
169
- array?.copy(this.buffer, offset)
186
+ source.copy(this.buffer, offset)
170
187
  }
171
188
 
189
+ /**
190
+ * @param {number} index
191
+ * @returns {number}
192
+ */
172
193
  at(index) {
173
194
  return index >= 0
174
195
  ? this.buffer[this.byteOffset + index]
175
196
  : this.buffer[this.byteOffset + this.byteLength + index]
176
197
  }
177
198
 
199
+ /**
200
+ * @param {{ test: (Buffer: buffer, byteOffset: number, byteLength: number) => boolean }} expr
201
+ * @returns
202
+ */
178
203
  test(expr) {
179
204
  return expr.test(this.buffer, this.byteOffset, this.byteLength)
180
205
  }
181
206
 
182
- readBigUInt64BE(offset = 0) {
183
- if (offset === undefined) {
184
- offset = this.byteOffset
185
- } else {
186
- offset += this.byteOffset
187
- }
188
-
189
- return this.buffer.readBigUInt64BE(offset)
190
- }
191
-
192
207
  /**
193
- *
194
208
  * @param {BufferEncoding} [encoding='utf8']
195
209
  * @param {number} [start=0]
196
210
  * @param {number} [end=this.byteLength]
@@ -212,6 +226,11 @@ export class Slice {
212
226
  return this.buffer.toString(encoding, start, end)
213
227
  }
214
228
 
229
+ /**
230
+ * @param {number} [start=0]
231
+ * @param {number} [end=this.byteLength]
232
+ * @returns {Buffer}
233
+ */
215
234
  toBuffer(start, end) {
216
235
  if (start === undefined) {
217
236
  start = this.byteOffset
@@ -228,10 +247,19 @@ export class Slice {
228
247
  return this.buffer.subarray(start, end)
229
248
  }
230
249
 
250
+ /**
251
+ * @returns {string}
252
+ */
231
253
  [Symbol.toStringTag]() {
232
254
  return this.toString()
233
255
  }
234
256
 
257
+ /**
258
+ * @param {*} depth
259
+ * @param {*} options
260
+ * @param {*} inspect
261
+ * @returns {string}
262
+ */
235
263
  [util.inspect.custom](depth, options, inspect) {
236
264
  const bytes = []
237
265
  for (let i = 0; i < this.byteLength; i++) {