interval-operations
Advanced tools
Comparing version 1.1.0 to 2.0.0
type IntervalPoint = number | Date; | ||
type Interval<T extends IntervalPoint = IntervalPoint> = [T, T]; | ||
declare function union(...intervals: Interval[]): Interval[]; | ||
declare function arrayUnion(...arrays: Interval[][]): Interval[]; | ||
declare function contains<T extends IntervalPoint>(outerInterval: Interval<T>, innerInterval: Interval<T>): boolean; | ||
declare function arrayContains<T extends IntervalPoint>(outerIntervals: Interval<T>[], innerIntervals: Interval<T>[]): boolean; | ||
declare function difference(a: Interval, b: Interval): Interval[] | null; | ||
declare function arrayDifference(inputIntervals: Interval[], inputDiffIntervals: Interval[]): Interval[]; | ||
declare function difference<T extends IntervalPoint>(a: Interval<T>, b: Interval<T>): Interval<T>[] | null; | ||
declare function arrayDifference<T extends IntervalPoint>(inputIntervals: Interval<T>[], inputDiffIntervals: Interval<T>[]): Interval<T>[]; | ||
declare function intersection(...intervals: Interval[]): Interval | null; | ||
declare function arrayIntersection(...arrays: Interval[][]): Interval[]; | ||
declare function intersection<T extends IntervalPoint>(...intervals: Interval<T>[]): Interval<T> | null; | ||
declare function arrayIntersection<T extends IntervalPoint>(...arrays: Interval<T>[][]): Interval<T>[]; | ||
declare function contains(outerInterval: Interval, innerInterval: Interval): boolean; | ||
declare function arrayContains(outerIntervals: Interval[], innerIntervals: Interval[]): boolean; | ||
declare function union<T extends IntervalPoint>(...intervals: Interval<T>[]): Interval<T>[]; | ||
declare function arrayUnion<T extends IntervalPoint>(...arrays: Interval<T>[][]): Interval<T>[]; | ||
export { arrayContains, arrayDifference, arrayIntersection, arrayUnion, contains, difference, intersection, union }; |
@@ -34,3 +34,3 @@ "use strict"; | ||
// src/helpers.ts | ||
// src/utils.ts | ||
function sortByStart(a, b) { | ||
@@ -48,10 +48,14 @@ return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; | ||
function union(...intervals) { | ||
if (!intervals.length) { | ||
intervals = intervals.sort(sortByStart); | ||
const result = []; | ||
if (!intervals[0]) { | ||
return []; | ||
} | ||
intervals = intervals.sort(sortByStart); | ||
const result = []; | ||
let [start, end] = intervals[0]; | ||
for (let i = 1, n = intervals.length; i < n; i++) { | ||
const [nextStart, nextEnd] = intervals[i]; | ||
const curr = intervals[i]; | ||
if (!curr) { | ||
continue; | ||
} | ||
const [nextStart, nextEnd] = curr; | ||
if (end < nextStart) { | ||
@@ -73,3 +77,6 @@ result.push([start, end]); | ||
for (let i = 0, n = arrays.length; i < n; i++) { | ||
flattenedIntervals = flattenedIntervals.concat(arrays[i]); | ||
const curr = arrays[i]; | ||
if (curr) { | ||
flattenedIntervals = flattenedIntervals.concat(curr); | ||
} | ||
} | ||
@@ -79,2 +86,26 @@ return union(...flattenedIntervals); | ||
// src/contains.ts | ||
function contains(outerInterval, innerInterval) { | ||
return innerInterval[0] >= outerInterval[0] && innerInterval[1] <= outerInterval[1]; | ||
} | ||
function arrayContains(outerIntervals, innerIntervals) { | ||
outerIntervals = arrayUnion(outerIntervals); | ||
innerIntervals = arrayUnion(innerIntervals); | ||
for (let i = 0, n = innerIntervals.length; i < n; i++) { | ||
const inner = innerIntervals[i]; | ||
let isContained = false; | ||
for (let j = 0, m = outerIntervals.length; j < m; j++) { | ||
const outer = outerIntervals[j]; | ||
if (contains(outer, inner)) { | ||
isContained = true; | ||
break; | ||
} | ||
} | ||
if (!isContained) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
// src/difference.ts | ||
@@ -107,17 +138,26 @@ function difference(a, b) { | ||
for (let i = 0, n = intervals.length; i < n; i++) { | ||
let [start, end] = intervals[i]; | ||
const currentInterval = intervals[i]; | ||
if (!currentInterval) { | ||
continue; | ||
} | ||
let [start, end] = currentInterval; | ||
let addInterval = true; | ||
for (let j = 0, m = diffIntervals.length; j < m; j++) { | ||
const diffInterval = diffIntervals[j]; | ||
if (diffInterval[1] <= start) { | ||
const curr = diffIntervals[j]; | ||
if (!curr) { | ||
continue; | ||
} | ||
if (diffInterval[0] >= end) { | ||
if (curr[1] <= start) { | ||
continue; | ||
} | ||
if (curr[0] >= end) { | ||
break; | ||
} | ||
const diff = difference([start, end], diffInterval); | ||
if (diff) { | ||
[start, end] = diff[diff.length - 1]; | ||
const diff = difference([start, end], curr); | ||
const first = diff == null ? void 0 : diff[0]; | ||
const prev = diff ? diff[diff.length - 1] : null; | ||
if (diff && first && prev) { | ||
[start, end] = prev; | ||
if (diff.length > 1) { | ||
result.push(diff[0]); | ||
result.push(first); | ||
} | ||
@@ -138,10 +178,16 @@ } else { | ||
function intersection(...intervals) { | ||
var _a, _b; | ||
intervals.sort(sortByStart); | ||
const [start, end] = [ | ||
intervals[intervals.length - 1][0], | ||
intervals.sort(sortByEnd)[0][1] | ||
]; | ||
const start = (_a = intervals[intervals.length - 1]) == null ? void 0 : _a[0]; | ||
const end = (_b = intervals.sort(sortByEnd)[0]) == null ? void 0 : _b[1]; | ||
if (start === void 0 || end === void 0) { | ||
return null; | ||
} | ||
for (let i = 0, n = intervals.length; i < n - 1; i++) { | ||
const [nextStart, nextEnd] = intervals[i + 1]; | ||
if (nextStart > end || nextEnd < start) { | ||
const next = intervals[i + 1]; | ||
if (!next) { | ||
continue; | ||
} | ||
const [nextStart, nextEnd] = next; | ||
if (nextStart >= end || nextEnd <= start) { | ||
return null; | ||
@@ -153,18 +199,28 @@ } | ||
function arrayIntersection(...arrays) { | ||
var _a; | ||
let result = arrays[0]; | ||
for (let i = 1, n = arrays.length; i < n; i++) { | ||
const array = arrays[i]; | ||
if (!array) { | ||
continue; | ||
} | ||
array.sort(sortByStart); | ||
const tempResult = []; | ||
for (let j = 0, m = result.length; j < m; j++) { | ||
const resultInterval = result[j]; | ||
for (let j = 0, m = (_a = result == null ? void 0 : result.length) != null ? _a : 0; j < m; j++) { | ||
const curr = result == null ? void 0 : result[j]; | ||
if (!curr) { | ||
continue; | ||
} | ||
for (let k = 0, l = array.length; k < l; k++) { | ||
const interval = array[k]; | ||
if (interval[1] <= resultInterval[0]) { | ||
if (!interval) { | ||
continue; | ||
} | ||
if (interval[0] >= resultInterval[1]) { | ||
if (interval[1] <= curr[0]) { | ||
continue; | ||
} | ||
if (interval[0] >= curr[1]) { | ||
break; | ||
} | ||
const isect = intersection(resultInterval, interval); | ||
const isect = intersection(curr, interval); | ||
if (isect) { | ||
@@ -177,28 +233,4 @@ tempResult.push(isect); | ||
} | ||
return result.length ? result : []; | ||
return result && result.length > 0 ? result : []; | ||
} | ||
// src/contains.ts | ||
function contains(outerInterval, innerInterval) { | ||
return innerInterval[0] >= outerInterval[0] && innerInterval[1] <= outerInterval[1]; | ||
} | ||
function arrayContains(outerIntervals, innerIntervals) { | ||
outerIntervals = arrayUnion(outerIntervals); | ||
innerIntervals = arrayUnion(innerIntervals); | ||
for (let i = 0, n = innerIntervals.length; i < n; i++) { | ||
const inner = innerIntervals[i]; | ||
let isContained = false; | ||
for (let j = 0, m = outerIntervals.length; j < m; j++) { | ||
const outer = outerIntervals[j]; | ||
if (contains(outer, inner)) { | ||
isContained = true; | ||
break; | ||
} | ||
} | ||
if (!isContained) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -205,0 +237,0 @@ 0 && (module.exports = { |
{ | ||
"name": "interval-operations", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Utilities for performing mathematical set operations on intervals and arrays of intervals", | ||
@@ -25,3 +25,4 @@ "keywords": [ | ||
"lint": "tsc", | ||
"test": "vitest", | ||
"test": "vitest run", | ||
"test:watch": "vitest", | ||
"prepublishOnly": "pnpm lint && pnpm test && pnpm build" | ||
@@ -33,8 +34,7 @@ }, | ||
"devDependencies": { | ||
"@types/node": "^18.13.0", | ||
"jest": "^29.4.2", | ||
"tsup": "^6.6.2", | ||
"typescript": "^4.9.5", | ||
"vitest": "^0.28.5" | ||
"@types/node": "^20.3.1", | ||
"tsup": "^7.1.0", | ||
"typescript": "^5.1.3", | ||
"vitest": "^0.32.2" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # Interval operations |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16540
4
6
443
0