type TRange = [number, number];
export const getSpaceRanges = (bounds: TRange, intersections: TRange[]) => {
const clonedBounds = clone(bounds);
let correctBounds: TRange[] = [clonedBounds];
if (isEmpty(intersections)) {
return correctBounds;
}
forEach(intersections, (intersection) => {
const [intersectionStart, intersectionEnd] = intersection;
const lastBoundsBlockIndex = correctBounds.length - 1;
const [lastBoundsBlockStart, lastBoundsBlockEnd] = correctBounds[lastBoundsBlockIndex];
if (intersectionStart <= lastBoundsBlockStart && intersectionEnd >= lastBoundsBlockEnd) {
correctBounds = [];
return false;
}
if (intersectionStart <= lastBoundsBlockStart && intersectionEnd <= lastBoundsBlockStart) {
return;
}
if (intersectionStart <= lastBoundsBlockStart && intersectionEnd > lastBoundsBlockStart) {
correctBounds[lastBoundsBlockIndex][0] = intersectionEnd;
return;
}
if (intersectionStart > lastBoundsBlockStart && intersectionEnd < lastBoundsBlockEnd) {
const prevLastBoundsBlockEnd = lastBoundsBlockEnd;
correctBounds[lastBoundsBlockIndex][1] = intersectionStart;
correctBounds.push([intersectionEnd, prevLastBoundsBlockEnd]);
return;
}
if (
(intersectionStart < lastBoundsBlockEnd && intersectionEnd > lastBoundsBlockEnd) ||
(intersectionStart > lastBoundsBlockStart && intersectionEnd === lastBoundsBlockEnd)
) {
correctBounds[lastBoundsBlockIndex][1] = intersectionStart;
return;
}
if (intersectionStart >= lastBoundsBlockEnd) {
return;
}
});
return correctBounds;
};
it("Обрезание трека диапазонами", () => {
const bounds: TRange = [32400, 54000];
expect(getSpaceRanges(bounds, [])).toEqual([bounds]);
expect(getSpaceRanges(bounds, [[5400, 10500]])).toEqual([bounds]);
expect(getSpaceRanges(bounds, [[5400, 32400]])).toEqual([bounds]);
expect(getSpaceRanges(bounds, [[5400, 52000]])).toEqual([[52000, 54000]]);
expect(getSpaceRanges(bounds, [[5400, 54000]])).toEqual([]);
expect(getSpaceRanges(bounds, [[5400, 56000]])).toEqual([]);
expect(getSpaceRanges(bounds, [[32400, 32400]])).toEqual([bounds]); // Скорее всего, это невозможно
expect(getSpaceRanges(bounds, [[32400, 52000]])).toEqual([[52000, 54000]]);
expect(getSpaceRanges(bounds, [[32400, 54000]])).toEqual([]);
expect(getSpaceRanges(bounds, [[32400, 56000]])).toEqual([]);
expect(getSpaceRanges(bounds, [[38000, 38000]])).toEqual([
[32400, 38000],
[38000, 54000],
]); // Скорее всего, это невозможно, но если возможно, продумать
expect(getSpaceRanges(bounds, [[38000, 52000]])).toEqual([
[32400, 38000],
[52000, 54000],
]);
expect(getSpaceRanges(bounds, [[38000, 54000]])).toEqual([[32400, 38000]]);
expect(getSpaceRanges(bounds, [[38000, 56000]])).toEqual([[32400, 38000]]);
expect(getSpaceRanges(bounds, [[54000, 54000]])).toEqual([bounds]);
expect(getSpaceRanges(bounds, [[54000, 56000]])).toEqual([bounds]);
expect(getSpaceRanges(bounds, [[56000, 58000]])).toEqual([bounds]);
/** Более сложные случаи */
expect(
getSpaceRanges(bounds, [
[38000, 40000],
[45000, 52000],
])
).toEqual([
[32400, 38000],
[40000, 45000],
[52000, 54000],
]);
expect(
getSpaceRanges(bounds, [
[33000, 45000],
[35000, 48000],
[46000, 52000],
])
).toEqual([
[32400, 33000],
[52000, 54000],
]);
// expect(
// getSpaceRanges(bounds, [
// [35000, 37000],
// [36000, 54000],
// ])
// ).toEqual([[32400, 35000]]);
expect(
getSpaceRanges(bounds, [
[5400, 10500],
[35000, 37000],
[56000, 58000],
])
).toEqual([
[32400, 35000],
[37000, 54000],
]);
expect(
getSpaceRanges(bounds, [
[5400, 33000],
[35000, 37000],
[53000, 58000],
])
).toEqual([
[33000, 35000],
[37000, 53000],
]);
});
const bounds: TRange = [32400, 54000];
// getSpaceRanges(bounds, [
// [35000, 37000],
// [36000, 54000],
// ])