Skip to content

Commit

Permalink
feat(data.structures): add [number, number] as input
Browse files Browse the repository at this point in the history
New input accepted: [start, end]: [number, number]
  • Loading branch information
Draeken authored and Thibault committed Nov 23, 2017
1 parent 821e263 commit 5e87d3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/data.structures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type interval = IntervalSE | IntervalFT;
export type interval = IntervalSE | IntervalFT | IntervalAR;

export interface IntervalSE {
start: number;
Expand All @@ -9,3 +9,5 @@ export interface IntervalFT {
from: number;
to: number;
}

export type IntervalAR = [number, number];
9 changes: 8 additions & 1 deletion lib/lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import { IntervalFT, IntervalSE } from './data.structures';
import { IntervalAR, IntervalFT, IntervalSE } from './data.structures';
import { complement, intersect, unify } from './lib';

const prepareInput = (i1: IntervalSE | IntervalSE[], convertFn: (i: IntervalSE) => any) => {
Expand All @@ -15,7 +15,9 @@ const prepareOutput = (i1: IntervalSE[], convertFn: (i: any) => any) => {
};

const convertFTtoSE = (r: IntervalFT): IntervalSE => ({ start: r.from, end: r.to });
const convertARtoSE = ([start, end]: IntervalAR): IntervalSE => ({ start, end });
const convertSEtoFT = (r: IntervalSE): IntervalFT => ({ from: r.start, to: r.end });
const convertSEtoAR = (r: IntervalSE): IntervalAR => [r.start, r.end];

const testFn = (
i1: IntervalSE | IntervalSE[],
Expand All @@ -29,8 +31,13 @@ const testFn = (
fn(prepareInput(i1, convertSEtoFT), prepareInput(i2, convertSEtoFT)),
convertFTtoSE
);
const res3 = prepareOutput(
fn(prepareInput(i1, convertSEtoAR), prepareInput(i2, convertSEtoAR)),
convertARtoSE
);
testOutputFn(res);
testOutputFn(res2);
testOutputFn(res3);
t.throws(fn.bind(null, [{ test: 1 }], { test: 1 }), 'Unrecognized interval format');
};

Expand Down
17 changes: 13 additions & 4 deletions lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ import {
unfold,
} from 'ramda';

import { interval, IntervalFT, IntervalSE } from './data.structures';
import { interval, IntervalAR, IntervalFT, IntervalSE } from './data.structures';

const sortByStart = sortBy<IntervalSE>(prop('start'));

const convertFrom = (typeStr: string) => (r: interval): IntervalSE => {
switch (typeStr) {
case 'IntervalFT':
return convertFTtoSE(r as IntervalFT);
case 'IntervalAR':
return convertARtoSE(r as IntervalAR);
default:
return r as IntervalSE;
}
};

const convertFTtoSE = (r: IntervalFT): IntervalSE => ({ start: r.from, end: r.to });
const convertARtoSE = ([start, end]: IntervalAR): IntervalSE => ({ start, end });
const convertSEtoFT = (r: IntervalSE): IntervalFT => ({ from: r.start, to: r.end });
const convertSEtoAR = (r: IntervalSE): IntervalAR => [r.start, r.end];

const getType = (r: interval | interval[]): string => {
const inter = Array.isArray(r) ? r[0] : r;
if (typeof inter === 'number' || Array.isArray(inter)) {
return 'IntervalAR';
}
if (inter.hasOwnProperty('start')) {
return 'IntervalSE';
}
Expand All @@ -54,16 +61,18 @@ const convertTo = <T extends interval>(typeStr: string) => (r: IntervalSE): T =>
switch (typeStr) {
case 'IntervalFT':
return convertSEtoFT(r) as T;
case 'IntervalAR':
return convertSEtoAR(r) as T;
default:
return r as T;
}
};

const prepareInput = (typeStr: string, i: interval | interval[]): IntervalSE[] => {
if (Array.isArray(i)) {
return i.map(convertFrom(typeStr));
if (Array.isArray(i) && typeof i[0] !== 'number') {
return (i as interval[]).map(convertFrom(typeStr));
}
return [convertFrom(typeStr)(i)];
return [convertFrom(typeStr)(i as interval)];
};

const complementGen = (boundaries: IntervalSE, intervals: IntervalSE[]): IntervalSE[] => {
Expand Down

0 comments on commit 5e87d3a

Please sign in to comment.