Skip to content

Commit

Permalink
fix(split): keep ranges that doesn't intersect with split index
Browse files Browse the repository at this point in the history
  • Loading branch information
Draeken committed Apr 26, 2018
1 parent 6d2c1b9 commit ee36634
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
28 changes: 19 additions & 9 deletions lib/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ const testFnToIntervals = (
t.throws(fn.bind(null, [{ test: 1 }], { test: 1 }), 'Unrecognized interval format');
};

const testInterval = (t: TestContext, i: IntervalSE, vals: [number, number], extra: object) => {
const testInterval = (t: TestContext, i: IntervalSE, vals: [number, number], extra?: object) => {
t.is(i.start, vals[0], `test interval: [${i.start}, ${i.end}]`);
t.is(i.end, vals[1], `test interval: ${i}`);
if (!extra) {
return;
}
Object.entries(extra).forEach(([key, val]) => {
t.true(
i.hasOwnProperty(key) && Reflect.get(i, key) === val,
Expand Down Expand Up @@ -481,13 +484,20 @@ test('will split empty interval', t => {
});


test('will split intervals', t => {
const r1 = [{ start: 0, end: 7, test: 'foo' }, { start: 3, end: 8, test: 'bar' }];
const r2 = [5];
test('will not split when no indexes', t => {
const r1 = [{ start: 0, end: 7, test: 'foo' }];
const r2: number[] = [];
const res = split(r2, r1);
t.is(res.length, 1);
testInterval(t, res[0], [0, 7], { test: 'foo' });
});

test('will not split when no intersection', t => {
const r1 = [{ start: 0, end: 7, test: 'foo' }, { start: 8, end: 10 }];
const r2 = [9];
const res = split(r2, r1);
t.is(res.length, 4);
testInterval(t, res[0], [0, 5], { test: 'foo' });
testInterval(t, res[1], [5, 7], { test: 'foo' });
testInterval(t, res[2], [3, 5], { test: 'bar' });
testInterval(t, res[3], [5, 8], { test: 'bar' });
t.is(res.length, 3);
testInterval(t, res[0], [0, 7], { test: 'foo' });
testInterval(t, res[1], [8, 9]);
testInterval(t, res[2], [9, 10]);
});
10 changes: 5 additions & 5 deletions lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,19 +693,19 @@ const numberToRange = (n: number): IntervalSE => ({ start: n, end: n });

const splitGen = (splits: roat<number>, intervals: IntervalSE[]): IntervalSE[] => {
return chain((i => {
return chain((int => [
return chain((int => isOverlappingSimple(int, numberToRange(i)) ? [
{ ...int, start: int.start, end: i },
{ ...int, start: i, end: int.end },
]), intervals.filter(int => isOverlappingSimple(int, numberToRange(i))));
] : [int]), intervals);
}), splits);
};

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

Expand Down

0 comments on commit ee36634

Please sign in to comment.