Files
geekgeekrun/packages/utils/number.mjs
2026-02-22 13:14:01 +08:00

16 lines
617 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function hasIntersection(interval1, interval2) {
// 通用函数将区间标准化null转换为对应的无穷大
const normalizeInterval = (interval) => {
const [start, end] = interval;
return [
[null, undefined].includes(start) ? -Infinity : start,
[null, undefined].includes(end) ? Infinity : end
];
};
const [norm1Start, norm1End] = normalizeInterval(interval1);
const [norm2Start, norm2End] = normalizeInterval(interval2);
// 判断交集
return Math.max(norm1Start, norm2Start) <= Math.min(norm1End, norm2End);
}