@nxtedition/lib 28.0.3 → 28.0.5

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 (3) hide show
  1. package/package.json +2 -2
  2. package/sequence.js +48 -11
  3. package/slice.js +16 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "28.0.3",
3
+ "version": "28.0.5",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -92,5 +92,5 @@
92
92
  "pino": ">=7.0.0",
93
93
  "rxjs": "^7.0.0"
94
94
  },
95
- "gitHead": "15f4fd00f86a558ae56672b1efa3e909b23feb3f"
95
+ "gitHead": "f778b4c525a240411f681137d8dd0fe19031c7b0"
96
96
  }
package/sequence.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import assert from 'node:assert'
2
+ import util from 'node:util'
2
3
 
3
4
  export const ID_SEP = '~'
4
5
 
@@ -46,12 +47,12 @@ export class Sequence {
46
47
  return -1
47
48
  }
48
49
 
49
- if (a && typeof a === 'string') {
50
- a = new Sequence(a)
50
+ if (a === b) {
51
+ return 0
51
52
  }
52
53
 
53
- if (typeof b === 'string') {
54
- b = new Sequence(b)
54
+ if (typeof a === 'string') {
55
+ a = new Sequence(a)
55
56
  }
56
57
 
57
58
  return a.compare(b, strict)
@@ -205,13 +206,29 @@ export class Sequence {
205
206
  }
206
207
 
207
208
  at(index) {
208
- assert(Number.isFinite(index) && index >= 0 && index * 2 < this.#parts.length)
209
+ if (!Number.isInteger(index)) {
210
+ throw new TypeError('index must be an integer')
211
+ }
212
+ if (index < 0 || index >= this.#parts.length / 2) {
213
+ throw new RangeError('index out of range')
214
+ }
215
+
209
216
  return this.#parts[index * 2 + 1]
210
217
  }
211
218
 
212
219
  set(index, sequence) {
213
- assert(Number.isFinite(index) && index >= 0 && index < this.#parts.length)
214
- assert(Number.isFinite(sequence) && sequence >= 0)
220
+ if (!Number.isInteger(index)) {
221
+ throw new TypeError('index must be an integer')
222
+ }
223
+ if (index < 0 || index >= this.#parts.length / 2) {
224
+ throw new RangeError('index out of range')
225
+ }
226
+ if (!Number.isInteger(sequence)) {
227
+ throw new TypeError('sequence must be an integer')
228
+ }
229
+ if (sequence < 0) {
230
+ throw new RangeError('sequence must be non-negative')
231
+ }
215
232
  if (this.#parts[index * 2 + 1] !== sequence) {
216
233
  this.#parts[index * 2 + 1] = sequence
217
234
  this.#value = ''
@@ -224,11 +241,23 @@ export class Sequence {
224
241
  strict = true
225
242
  }
226
243
 
227
- assert(other instanceof Sequence)
228
- assert(typeof strict === 'boolean')
244
+ if (typeof other === 'string') {
245
+ if (this.#value && this.#value === other) {
246
+ return 0
247
+ }
248
+ other = new Sequence(other)
249
+ }
250
+
251
+ if (!(other instanceof Sequence)) {
252
+ throw new TypeError('Can only compare with another Sequence')
253
+ }
254
+
255
+ if (typeof strict !== 'boolean') {
256
+ throw new TypeError('strict must be a boolean')
257
+ }
229
258
 
230
- if (strict && other.identity && this.identity) {
231
- assert.strictEqual(other.identity, this.identity)
259
+ if (strict && other.identity && this.identity && other.identity !== this.identity) {
260
+ throw new Error('Cannot compare sequences with different identities')
232
261
  }
233
262
 
234
263
  if (this.#parts.length !== other.#parts.length) {
@@ -256,4 +285,12 @@ export class Sequence {
256
285
  }
257
286
  return this.#value
258
287
  }
288
+
289
+ [Symbol.toStringTag]() {
290
+ return this.toString()
291
+ }
292
+
293
+ [util.inspect.custom](depth, options, inspect) {
294
+ return `Sequence: "${this.toString()}"`
295
+ }
259
296
  }
package/slice.js CHANGED
@@ -60,6 +60,22 @@ export class Slice {
60
60
  maxByteLength = byteLength
61
61
  }
62
62
 
63
+ if (byteOffset < 0) {
64
+ throw new RangeError('byteOffset must be non-negative')
65
+ }
66
+
67
+ if (byteLength < 0) {
68
+ throw new RangeError('byteLength must be non-negative')
69
+ }
70
+
71
+ if (byteLength > maxByteLength) {
72
+ throw new RangeError('byteLength cannot be greater than maxByteLength')
73
+ }
74
+
75
+ if (byteOffset + maxByteLength > buffer.byteLength) {
76
+ throw new RangeError('Slice exceeds buffer length')
77
+ }
78
+
63
79
  const slice = POOL.pop()
64
80
 
65
81
  if (slice) {