@nxtedition/lib 21.5.0 → 21.5.2

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 +42 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "21.5.0",
3
+ "version": "21.5.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/sequence.js CHANGED
@@ -49,26 +49,47 @@ export class Sequence {
49
49
  return a.compare(b)
50
50
  }
51
51
 
52
+ /**
53
+ *
54
+ * @param {string} seq
55
+ * @returns {Sequence|null}
56
+ */
57
+ static tryParse(seq) {
58
+ if (typeof seq !== 'string') {
59
+ return null
60
+ }
61
+
62
+ try {
63
+ return new Sequence(seq)
64
+ } catch {
65
+ return null
66
+ }
67
+ }
68
+
52
69
  constructor(value, identity) {
53
70
  try {
54
71
  if (!value) {
55
72
  this.#value = '0'
56
73
  } else if (Array.isArray(value)) {
74
+ this.#identity = identity
57
75
  for (const part of value) {
58
76
  if (typeof part === 'string') {
59
77
  const [sequenceStr, id] = part.split(ID_SEP)
60
78
  const sequence = parseInt(sequenceStr)
61
79
  assert(Number.isFinite(sequence) && sequence >= 0)
62
80
  this.#parts.push(id, sequence)
81
+ this.#identity = null
63
82
  } else if (typeof part === 'number') {
64
83
  assert(Number.isFinite(part) && part >= 0)
65
84
  assert(Number.isFinite(identity))
66
85
  this.#parts.push('', part)
67
- this.#identity = identity
68
- } else {
86
+ } else if (part != null) {
69
87
  assert(Number.isFinite(part.sequence) && part.sequence >= 0)
70
88
  assert(typeof part.id === 'string' && part.id.length > 0)
71
89
  this.#parts.push(part.id, part.sequence)
90
+ this.#identity = null
91
+ } else {
92
+ assert(false, part)
72
93
  }
73
94
  }
74
95
  } else if (typeof value === 'string') {
@@ -82,6 +103,10 @@ export class Sequence {
82
103
  this.#parts.push(id, sequence)
83
104
  }
84
105
  this.#value = value
106
+ } else if (value instanceof Sequence) {
107
+ this.#value = value.#value
108
+ this.#parts = value.#parts.slice()
109
+ this.#identity = value.#identity
85
110
  } else {
86
111
  assert(false)
87
112
  }
@@ -105,11 +130,15 @@ export class Sequence {
105
130
 
106
131
  get identity() {
107
132
  if (this.#identity == null) {
108
- let str
109
- for (let n = 0; n < this.#parts.length; n += 2) {
110
- str += this.#parts[n]
133
+ if (this.#parts.length === 0) {
134
+ this.#identity = 0
135
+ } else {
136
+ let str
137
+ for (let n = 0; n < this.#parts.length; n += 2) {
138
+ str += this.#parts[n]
139
+ }
140
+ this.#identity = cyrb53(str, this.#parts.length)
111
141
  }
112
- this.#identity = cyrb53(str, this.#parts.length)
113
142
  }
114
143
 
115
144
  return this.#identity
@@ -140,15 +169,18 @@ export class Sequence {
140
169
 
141
170
  assert(other instanceof Sequence)
142
171
  assert(typeof strict === 'boolean')
143
- assert(!strict && other.identity === this.identity)
172
+
173
+ if (strict && other.identity && this.identity) {
174
+ assert.strictEqual(other.identity, this.identity)
175
+ }
144
176
 
145
177
  if (this.#parts.length !== other.#parts.length) {
146
178
  return this.#parts.length - other.#parts.length
147
179
  }
148
180
 
149
- for (let n = 0; n < this.#parts.length; n++) {
150
- if (this.#parts[n] !== other.#parts[n]) {
151
- return this.#parts[n] < other.#parts[n] ? -1 : 1
181
+ for (let n = 0; n < this.#parts.length; n += 2) {
182
+ if (this.#parts[n + 1] !== other.#parts[n + 1]) {
183
+ return this.#parts[n + 1] < other.#parts[n + 1] ? -1 : 1
152
184
  }
153
185
  }
154
186