|
2 | 2 | any,
|
3 | 3 | aperture,
|
4 | 4 | applySpec,
|
5 |
| - chain, |
6 | 5 | concat,
|
7 | 6 | converge,
|
8 | 7 | dissoc,
|
@@ -203,6 +202,10 @@ const isOverlappingSimple = (a: IntervalSE, b: IntervalSE): boolean => {
|
203 | 202 | return b.start < a.end && b.end > a.start;
|
204 | 203 | };
|
205 | 204 |
|
| 205 | +const isOverlappingNum = (a: IntervalSE, b: number): boolean => { |
| 206 | + return a.start < b && b < a.end; |
| 207 | +}; |
| 208 | + |
206 | 209 | const beforeOrAdjTo = (afterInt: IntervalSE) => (beforeInt: IntervalSE) =>
|
207 | 210 | beforeInt.end <= afterInt.start;
|
208 | 211 |
|
@@ -689,24 +692,39 @@ export function substract<D extends interval, T extends interval>(
|
689 | 692 | }
|
690 | 693 | }
|
691 | 694 |
|
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 | +}; |
693 | 701 |
|
| 702 | +/** |
| 703 | + * || | | | |
| 704 | + * [ ] [ ][ ] |
| 705 | + * |
| 706 | + */ |
694 | 707 | 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 | + ); |
701 | 719 | };
|
702 | 720 |
|
703 | 721 | const splitCurry = <T extends interval>(splitIndexes: roat<number>, intervals: T | T[]): T[] => {
|
704 | 722 | const typeStr = getType(intervals);
|
705 | 723 | 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)) { |
707 | 725 | return intervalSE.map(convertTo<T>(typeStr));
|
708 | 726 | }
|
709 |
| - return splitGen(splitIndexes, intervalSE).map(convertTo<T>(typeStr)); |
| 727 | + return splitGen([...splitIndexes].sort(), intervalSE).map(convertTo<T>(typeStr)); |
710 | 728 | };
|
711 | 729 |
|
712 | 730 | /**
|
|
0 commit comments