Skip to content

Commit 23854fa

Browse files
author
Thibault
authored
fix(split): fix when multiples indexes (#18)
1 parent b15bba9 commit 23854fa

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

lib/lib.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,13 @@ test('will not split when no intersection', t => {
512512
testInterval(t, res[1], [8, 9], { test: 'bar' });
513513
testInterval(t, res[2], [9, 10], { test: 'bar' });
514514
});
515+
516+
test('will split with multiple indexes', t => {
517+
const r1 = [{ start: 0, end: 10, test: 'foo' }];
518+
const r2 = [2, 8];
519+
const res = split(r2, r1);
520+
t.is(res.length, 3);
521+
testInterval(t, res[0], [0, 2], { test: 'foo' });
522+
testInterval(t, res[1], [2, 8], { test: 'foo' });
523+
testInterval(t, res[2], [8, 10], { test: 'foo' });
524+
});

lib/lib.ts

+28-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
any,
33
aperture,
44
applySpec,
5-
chain,
65
concat,
76
converge,
87
dissoc,
@@ -203,6 +202,10 @@ const isOverlappingSimple = (a: IntervalSE, b: IntervalSE): boolean => {
203202
return b.start < a.end && b.end > a.start;
204203
};
205204

205+
const isOverlappingNum = (a: IntervalSE, b: number): boolean => {
206+
return a.start < b && b < a.end;
207+
};
208+
206209
const beforeOrAdjTo = (afterInt: IntervalSE) => (beforeInt: IntervalSE) =>
207210
beforeInt.end <= afterInt.start;
208211

@@ -689,24 +692,39 @@ export function substract<D extends interval, T extends interval>(
689692
}
690693
}
691694

692-
const numberToRange = (n: number): IntervalSE => ({ start: n, end: n });
695+
const splitIntervalWithIndex = (int: IntervalSE, index: number): IntervalSE[] => {
696+
if (!isOverlappingNum(int, index)) {
697+
return [int];
698+
}
699+
return [{ ...int, start: int.start, end: index }, { ...int, start: index, end: int.end }];
700+
};
693701

702+
/**
703+
* ||  |  |     |
704+
* [ ] [ ][ ]
705+
*
706+
*/
694707
const splitGen = (splits: roat<number>, intervals: IntervalSE[]): IntervalSE[] => {
695-
return chain((i => {
696-
return chain((int => isOverlappingSimple(int, numberToRange(i)) ? [
697-
{ ...int, start: int.start, end: i },
698-
{ ...int, start: i, end: int.end },
699-
] : [int]), intervals);
700-
}), splits);
708+
return unnest(
709+
intervals.map(int =>
710+
splits.reduce(
711+
(acc: IntervalSE[], i: number) => {
712+
const lastInt = acc.pop() as IntervalSE;
713+
return [...acc, ...splitIntervalWithIndex(lastInt, i)];
714+
},
715+
[int]
716+
)
717+
)
718+
);
701719
};
702720

703721
const splitCurry = <T extends interval>(splitIndexes: roat<number>, intervals: T | T[]): T[] => {
704722
const typeStr = getType(intervals);
705723
const intervalSE = prepareInput(typeStr, intervals);
706-
if (splitIndexes.length < 1 || Array.isArray(intervals) && intervals.length < 1) {
724+
if (splitIndexes.length < 1 || (Array.isArray(intervals) && intervals.length < 1)) {
707725
return intervalSE.map(convertTo<T>(typeStr));
708726
}
709-
return splitGen(splitIndexes, intervalSE).map(convertTo<T>(typeStr));
727+
return splitGen([...splitIndexes].sort(), intervalSE).map(convertTo<T>(typeStr));
710728
};
711729

712730
/**

0 commit comments

Comments
 (0)