Skip to content

Commit

Permalink
fix(split): fix when multiples indexes (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibault authored May 18, 2018
1 parent b15bba9 commit 23854fa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
10 changes: 10 additions & 0 deletions lib/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,13 @@ test('will not split when no intersection', t => {
testInterval(t, res[1], [8, 9], { test: 'bar' });
testInterval(t, res[2], [9, 10], { test: 'bar' });
});

test('will split with multiple indexes', t => {
const r1 = [{ start: 0, end: 10, test: 'foo' }];
const r2 = [2, 8];
const res = split(r2, r1);
t.is(res.length, 3);
testInterval(t, res[0], [0, 2], { test: 'foo' });
testInterval(t, res[1], [2, 8], { test: 'foo' });
testInterval(t, res[2], [8, 10], { test: 'foo' });
});
38 changes: 28 additions & 10 deletions lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
any,
aperture,
applySpec,
chain,
concat,
converge,
dissoc,
Expand Down Expand Up @@ -203,6 +202,10 @@ const isOverlappingSimple = (a: IntervalSE, b: IntervalSE): boolean => {
return b.start < a.end && b.end > a.start;
};

const isOverlappingNum = (a: IntervalSE, b: number): boolean => {
return a.start < b && b < a.end;
};

const beforeOrAdjTo = (afterInt: IntervalSE) => (beforeInt: IntervalSE) =>
beforeInt.end <= afterInt.start;

Expand Down Expand Up @@ -689,24 +692,39 @@ export function substract<D extends interval, T extends interval>(
}
}

const numberToRange = (n: number): IntervalSE => ({ start: n, end: n });
const splitIntervalWithIndex = (int: IntervalSE, index: number): IntervalSE[] => {
if (!isOverlappingNum(int, index)) {
return [int];
}
return [{ ...int, start: int.start, end: index }, { ...int, start: index, end: int.end }];
};

/**
* ||  |  |     |
* [ ] [ ][ ]
*
*/
const splitGen = (splits: roat<number>, intervals: IntervalSE[]): IntervalSE[] => {
return chain((i => {
return chain((int => isOverlappingSimple(int, numberToRange(i)) ? [
{ ...int, start: int.start, end: i },
{ ...int, start: i, end: int.end },
] : [int]), intervals);
}), splits);
return unnest(
intervals.map(int =>
splits.reduce(
(acc: IntervalSE[], i: number) => {
const lastInt = acc.pop() as IntervalSE;
return [...acc, ...splitIntervalWithIndex(lastInt, i)];
},
[int]
)
)
);
};

const splitCurry = <T extends interval>(splitIndexes: roat<number>, intervals: T | T[]): T[] => {
const typeStr = getType(intervals);
const intervalSE = prepareInput(typeStr, intervals);
if (splitIndexes.length < 1 || Array.isArray(intervals) && intervals.length < 1) {
if (splitIndexes.length < 1 || (Array.isArray(intervals) && intervals.length < 1)) {
return intervalSE.map(convertTo<T>(typeStr));
}
return splitGen(splitIndexes, intervalSE).map(convertTo<T>(typeStr));
return splitGen([...splitIndexes].sort(), intervalSE).map(convertTo<T>(typeStr));
};

/**
Expand Down

0 comments on commit 23854fa

Please sign in to comment.