@nxtedition/ranges 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) nxtedition
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # @nxtedition/ranges
2
+
3
+ Merge and subtract numeric ranges. Useful for byte ranges, time ranges, and similar interval arithmetic.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @nxtedition/ranges
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Merge overlapping ranges
14
+
15
+ ```js
16
+ import { merge } from '@nxtedition/ranges'
17
+
18
+ merge([
19
+ [10, 20],
20
+ [15, 30],
21
+ [50, 60],
22
+ ])
23
+ // [[10, 30], [50, 60]]
24
+ ```
25
+
26
+ ### Subtract ranges
27
+
28
+ ```js
29
+ import { subtract } from '@nxtedition/ranges'
30
+
31
+ subtract(
32
+ [[0, 100]],
33
+ [
34
+ [10, 20],
35
+ [30, 40],
36
+ ],
37
+ )
38
+ // [[0, 10], [20, 30], [40, 100]]
39
+ ```
40
+
41
+ ## API
42
+
43
+ ### `merge(ranges: Range[]): readonly Range[]`
44
+
45
+ Sorts and merges overlapping or adjacent ranges. Returns a new array of non-overlapping ranges. Invalid ranges (empty, reversed, non-finite) are filtered out.
46
+
47
+ ### `subtract(a: readonly Range[], b: readonly Range[]): readonly Range[]`
48
+
49
+ Subtracts ranges `b` from ranges `a`. Returns the remaining intervals. Does not modify the input arrays.
50
+
51
+ ### `Range`
52
+
53
+ ```ts
54
+ type Range = readonly [start: number, end: number]
55
+ ```
56
+
57
+ Ranges are half-open intervals: `start` is inclusive, `end` is exclusive.
58
+
59
+ ## License
60
+
61
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export type Range = readonly [start: number, end: number];
2
+ export declare function merge(ranges: Range[]): readonly Range[];
3
+ export declare function subtract(a: readonly Range[], b: readonly Range[]): readonly Range[];
package/lib/index.js ADDED
@@ -0,0 +1,75 @@
1
+
2
+
3
+ const EMPTY_ARR = Object.freeze([])
4
+
5
+ export function merge(ranges ) {
6
+ if (!Array.isArray(ranges) || ranges.length === 0) {
7
+ return EMPTY_ARR
8
+ }
9
+
10
+ if (ranges.length === 1) {
11
+ const range = ranges[0]
12
+ return Array.isArray(range) && range.length === 2 && range[1] > range[0] ? ranges : EMPTY_ARR
13
+ }
14
+
15
+ ranges = ranges.filter(
16
+ (range) => Array.isArray(range) && range.length === 2 && range[1] > range[0],
17
+ )
18
+
19
+ if (ranges.length <= 1) {
20
+ return ranges
21
+ }
22
+
23
+ ranges.sort((a, b) => a[0] - b[0])
24
+
25
+ const stack = []
26
+
27
+ stack.push([ranges[0][0], ranges[0][1]])
28
+
29
+ for (let n = 1, len = ranges.length; n < len; ++n) {
30
+ const range = ranges[n]
31
+ const top = stack[stack.length - 1]
32
+
33
+ if (
34
+ !Array.isArray(range) ||
35
+ range.length !== 2 ||
36
+ !Number.isFinite(range[0]) ||
37
+ !Number.isFinite(range[1]) ||
38
+ range[0] > range[1]
39
+ ) {
40
+ continue
41
+ }
42
+
43
+ if (top[1] < range[0]) {
44
+ stack.push([range[0], range[1]])
45
+ } else if (top[1] < range[1]) {
46
+ top[1] = range[1]
47
+ }
48
+ }
49
+
50
+ return stack
51
+ }
52
+
53
+ export function subtract(a , b ) {
54
+ const pending = a.map((r) => [r[0], r[1]])
55
+
56
+ const c = []
57
+
58
+ while (pending.length > 0) {
59
+ const ar = pending.shift()
60
+
61
+ if (ar[1] <= ar[0]) {
62
+ continue
63
+ }
64
+
65
+ const br = b.find((br) => ar[0] < br[1] && br[0] < ar[1])
66
+ if (!br) {
67
+ c.push(ar)
68
+ } else {
69
+ pending.unshift([br[1], ar[1]])
70
+ pending.unshift([ar[0], br[0]])
71
+ }
72
+ }
73
+
74
+ return c
75
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@nxtedition/ranges",
3
+ "version": "1.0.1",
4
+ "type": "module",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "README.md",
10
+ "LICENSE"
11
+ ],
12
+ "license": "MIT",
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "scripts": {
17
+ "build": "rimraf lib && tsc && amaroc ./src/index.ts && mv src/index.js lib/",
18
+ "prepublishOnly": "yarn build",
19
+ "typecheck": "tsc --noEmit",
20
+ "test": "node --test",
21
+ "test:ci": "node --test",
22
+ "test:coverage": "node --test --experimental-test-coverage --test-coverage-include=src/index.ts --test-coverage-lines=90 --test-coverage-branches=90 --test-coverage-functions=100"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "^25.2.3",
26
+ "amaroc": "^1.0.1",
27
+ "oxlint-tsgolint": "^0.13.0",
28
+ "rimraf": "^6.1.3",
29
+ "typescript": "^5.9.3"
30
+ },
31
+ "gitHead": "a54f10182ade3ca92e198dce10ff06d059d296b8"
32
+ }