@nxtedition/lib 15.0.52 → 15.0.53
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.
- package/merge-ranges.js +23 -8
- package/package.json +1 -1
package/merge-ranges.js
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
+
const EMPTY_ARR = Object.freeze([])
|
|
2
|
+
|
|
1
3
|
module.exports = function mergeRanges(ranges) {
|
|
2
4
|
if (!Array.isArray(ranges)) {
|
|
3
|
-
return
|
|
5
|
+
return EMPTY_ARR
|
|
4
6
|
}
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
if (ranges.length === 1) {
|
|
9
|
+
const range = ranges[0]
|
|
10
|
+
return Array.isArray(range) && range.length === 2 && range[1] > range[0] ? ranges : EMPTY_ARR
|
|
11
|
+
}
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
{
|
|
14
|
+
// Make sure ranges are valid and copied for mutation.
|
|
15
|
+
const tmp = []
|
|
16
|
+
for (let n = 0, len = ranges.length; n < len; n++) {
|
|
17
|
+
const range = ranges[n]
|
|
18
|
+
if (Array.isArray(range) && range.length === 2 && range[1] > range[0]) {
|
|
19
|
+
tmp.push([range[0], range[1]])
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
ranges = tmp
|
|
23
|
+
}
|
|
11
24
|
|
|
12
|
-
if (
|
|
13
|
-
return
|
|
25
|
+
if (ranges.length <= 1) {
|
|
26
|
+
return ranges
|
|
14
27
|
}
|
|
15
28
|
|
|
16
|
-
ranges
|
|
29
|
+
ranges.sort((a, b) => a[0] - b[0])
|
|
30
|
+
|
|
31
|
+
const stack = []
|
|
17
32
|
|
|
18
33
|
// Add first range to stack
|
|
19
34
|
stack.push(ranges[0])
|