@nxtedition/lib 21.5.11 → 21.6.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/sequence.js +36 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "21.5.11",
3
+ "version": "21.6.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/sequence.js CHANGED
@@ -23,9 +23,25 @@ export class Sequence {
23
23
  #value
24
24
  #parts = []
25
25
  #identity
26
+ #count
26
27
 
27
28
  // TODO (perf): Optimize
28
- static compare(a, b) {
29
+ /**
30
+ *
31
+ * @param {string|Sequence|null|undefined} a
32
+ * @param {string|Sequence|null|undefined} b
33
+ * @param {boolean} [strict=true]
34
+ * @returns
35
+ */
36
+ static compare(a, b, strict) {
37
+ if (a && typeof a === 'string') {
38
+ a = Sequence.tryParse(a)
39
+ }
40
+
41
+ if (a && typeof b === 'string') {
42
+ b = Sequence.tryParse(b)
43
+ }
44
+
29
45
  if (!a && !b) {
30
46
  return 0
31
47
  }
@@ -38,15 +54,7 @@ export class Sequence {
38
54
  return -1
39
55
  }
40
56
 
41
- if (typeof a === 'string') {
42
- a = new Sequence(a)
43
- }
44
-
45
- if (typeof b === 'string') {
46
- b = new Sequence(b)
47
- }
48
-
49
- return a.compare(b)
57
+ return a.compare(b, strict)
50
58
  }
51
59
 
52
60
  /**
@@ -72,6 +80,7 @@ export class Sequence {
72
80
  this.#value = '0'
73
81
  } else if (Array.isArray(value)) {
74
82
  this.#identity = identity
83
+ this.#count = 0
75
84
  for (const part of value) {
76
85
  if (typeof part === 'string') {
77
86
  const [sequenceStr, id] = part.split(ID_SEP)
@@ -79,15 +88,18 @@ export class Sequence {
79
88
  assert(Number.isFinite(sequence) && sequence >= 0)
80
89
  this.#parts.push(id, sequence)
81
90
  this.#identity = null
91
+ this.#count += part
82
92
  } else if (typeof part === 'number') {
83
93
  assert(Number.isFinite(part) && part >= 0)
84
94
  assert(Number.isFinite(identity))
85
95
  this.#parts.push('', part)
96
+ this.#count += part
86
97
  } else if (part != null) {
87
98
  assert(Number.isFinite(part.sequence) && part.sequence >= 0)
88
99
  assert(typeof part.id === 'string' && part.id.length > 0)
89
100
  this.#parts.push(part.id, part.sequence)
90
101
  this.#identity = null
102
+ this.#count += part.sequence
91
103
  } else {
92
104
  assert(false, part)
93
105
  }
@@ -103,8 +115,10 @@ export class Sequence {
103
115
  assert(Number.isFinite(sequence) && sequence >= 0)
104
116
  this.#parts.push(id, sequence)
105
117
  }
118
+ this.#count = count
106
119
  this.#value = value
107
120
  } else if (value instanceof Sequence) {
121
+ this.#count = value.#count
108
122
  this.#value = value.#value
109
123
  this.#parts = value.#parts.slice()
110
124
  this.#identity = value.#identity
@@ -145,6 +159,17 @@ export class Sequence {
145
159
  return this.#identity
146
160
  }
147
161
 
162
+ get count() {
163
+ if (this.#count == null) {
164
+ let count = 0
165
+ for (let n = 0; n < this.#parts.length; n += 2) {
166
+ count += this.#parts[n + 1]
167
+ }
168
+ this.#count = count
169
+ }
170
+ return this.#count
171
+ }
172
+
148
173
  get length() {
149
174
  return this.#parts.length / 2
150
175
  }
@@ -160,6 +185,7 @@ export class Sequence {
160
185
  if (this.#parts[index * 2 + 1] !== sequence) {
161
186
  this.#parts[index * 2 + 1] = sequence
162
187
  this.#value = undefined
188
+ this.#count = undefined
163
189
  }
164
190
  }
165
191