@nxtedition/lib 21.5.11 → 21.6.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/sequence.js +29 -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.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/sequence.js CHANGED
@@ -23,9 +23,18 @@ 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
+ static compare(a, b, strict) {
30
+ if (a && typeof a === 'string') {
31
+ a = Sequence.tryParse(a)
32
+ }
33
+
34
+ if (a && typeof b === 'string') {
35
+ b = Sequence.tryParse(b)
36
+ }
37
+
29
38
  if (!a && !b) {
30
39
  return 0
31
40
  }
@@ -38,15 +47,7 @@ export class Sequence {
38
47
  return -1
39
48
  }
40
49
 
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)
50
+ return a.compare(b, strict)
50
51
  }
51
52
 
52
53
  /**
@@ -72,6 +73,7 @@ export class Sequence {
72
73
  this.#value = '0'
73
74
  } else if (Array.isArray(value)) {
74
75
  this.#identity = identity
76
+ this.#count = 0
75
77
  for (const part of value) {
76
78
  if (typeof part === 'string') {
77
79
  const [sequenceStr, id] = part.split(ID_SEP)
@@ -79,15 +81,18 @@ export class Sequence {
79
81
  assert(Number.isFinite(sequence) && sequence >= 0)
80
82
  this.#parts.push(id, sequence)
81
83
  this.#identity = null
84
+ this.#count += part
82
85
  } else if (typeof part === 'number') {
83
86
  assert(Number.isFinite(part) && part >= 0)
84
87
  assert(Number.isFinite(identity))
85
88
  this.#parts.push('', part)
89
+ this.#count += part
86
90
  } else if (part != null) {
87
91
  assert(Number.isFinite(part.sequence) && part.sequence >= 0)
88
92
  assert(typeof part.id === 'string' && part.id.length > 0)
89
93
  this.#parts.push(part.id, part.sequence)
90
94
  this.#identity = null
95
+ this.#count += part.sequence
91
96
  } else {
92
97
  assert(false, part)
93
98
  }
@@ -103,8 +108,10 @@ export class Sequence {
103
108
  assert(Number.isFinite(sequence) && sequence >= 0)
104
109
  this.#parts.push(id, sequence)
105
110
  }
111
+ this.#count = count
106
112
  this.#value = value
107
113
  } else if (value instanceof Sequence) {
114
+ this.#count = value.#count
108
115
  this.#value = value.#value
109
116
  this.#parts = value.#parts.slice()
110
117
  this.#identity = value.#identity
@@ -145,6 +152,17 @@ export class Sequence {
145
152
  return this.#identity
146
153
  }
147
154
 
155
+ get count() {
156
+ if (this.#count == null) {
157
+ let count = 0
158
+ for (let n = 0; n < this.#parts.length; n += 2) {
159
+ count += this.#parts[n + 1]
160
+ }
161
+ this.#count = count
162
+ }
163
+ return this.#count
164
+ }
165
+
148
166
  get length() {
149
167
  return this.#parts.length / 2
150
168
  }
@@ -160,6 +178,7 @@ export class Sequence {
160
178
  if (this.#parts[index * 2 + 1] !== sequence) {
161
179
  this.#parts[index * 2 + 1] = sequence
162
180
  this.#value = undefined
181
+ this.#count = undefined
163
182
  }
164
183
  }
165
184