-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for interval notation (#7)
- Loading branch information
Showing
10 changed files
with
240 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const assert = require("assert"); | ||
const IntervalModule = require("../dist/node"); | ||
const Interval = IntervalModule.Interval; | ||
const Interval = require("../dist/node").Interval; | ||
|
||
const result = new Interval(1, 2); | ||
|
||
assert.equal(result.contains(1), true); | ||
assert.deepEqual(result, {start: 1, end: 2, includeStart: true, includeEnd: true}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Interval} from "../Interval"; | ||
|
||
export class IntervalFrom { | ||
public static from(intervalNotation: string): Interval { | ||
|
||
if (null === intervalNotation || !(typeof intervalNotation === "string")) { | ||
throw new Error(`Invalid interval definition`); | ||
} | ||
|
||
const regex = new RegExp(/^\{\}|\{\s*([^\s\t\n\r,]+)\s*\}|([\(\[\]])\s*([^\s\t\n\r,]+)\s*,\s*([^\s\t\n\r,]+)\s*([\)\]\[])$/); | ||
const matches = regex.exec(intervalNotation); | ||
|
||
if (matches && matches.length === 6) { | ||
if (matches[1] !== undefined) { | ||
const start = Number(matches[1]); | ||
const end = Number(matches[1]); | ||
const includeStart = true; | ||
const includeEnd = true; | ||
return new Interval(start, end, includeStart, includeEnd); | ||
} else { | ||
const start = Number(matches[3]); | ||
const end = Number(matches[4]); | ||
const includeStart = (matches[2] === "["); | ||
const includeEnd = (matches[5] === "]"); | ||
return new Interval(start, end, includeStart, includeEnd); | ||
} | ||
} | ||
|
||
throw new Error(`Invalid interval definition ${intervalNotation}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {Interval} from "../Interval"; | ||
|
||
export class IntervalMatch { | ||
|
||
public static isOverlapping(interval1: Interval, interval2: Interval): boolean { | ||
if (!this.isIntervalComparable(interval1, interval2)) { | ||
return false; | ||
} | ||
|
||
if (interval1.start === interval2.end) { | ||
return (interval1.includeStart || interval2.includeEnd); | ||
} | ||
|
||
if (interval1.end === interval2.start) { | ||
return interval1.includeEnd || interval2.includeStart; | ||
} | ||
|
||
return (interval1.start <= interval2.start && interval2.start <= interval1.end) || | ||
(interval1.start <= interval2.end && interval2.end <= interval1.end) || | ||
(interval2.start <= interval1.start && interval1.start <= interval2.end) || | ||
(interval2.start <= interval1.end && interval1.end <= interval2.end); | ||
} | ||
|
||
|
||
public static contains(interval: Interval, intervalToCheck: Interval): boolean { | ||
if (!this.isIntervalComparable(interval, intervalToCheck)) { | ||
return false; | ||
} | ||
|
||
const startIsEqual = (interval.start === intervalToCheck.start && (interval.includeStart || !intervalToCheck.includeStart)); | ||
const endIsEqual = (interval.end === intervalToCheck.end && (interval.includeEnd || !intervalToCheck.includeEnd)); | ||
|
||
return ((interval.start < intervalToCheck.start || startIsEqual) && | ||
(intervalToCheck.end < interval.end || endIsEqual)); | ||
} | ||
|
||
private static isIntervalComparable(interval1: Interval, interval2: Interval): boolean { | ||
if (!interval1 || !interval2 || interval1.isEmpty() || interval2.isEmpty()) { | ||
return false; | ||
} | ||
|
||
return true | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export {IntervalEmpty} from "./IntervalEmpty"; | ||
export {IntervalOverlap} from "./IntervalOverlap"; | ||
export {IntervalMatch} from "./IntervalMatch"; | ||
export {IntervalSort} from "./IntervalSort"; | ||
export {IntervalUnion} from "./IntervalUnion"; | ||
export {IntervalFrom} from "./IntervalFrom"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import {Interval} from "../src"; | ||
|
||
describe("IntervalFromTest", () => { | ||
const testee = Interval; | ||
|
||
test.each([ | ||
["[", "]", true, true], | ||
["]", "[", false, false], | ||
["[", "[", true, false], | ||
["]", "]", false, true], | ||
["(", ")", false, false], | ||
["(", "]", false, true], | ||
["[", ")", true, false], | ||
["(", "[", false, false], | ||
["]", ")", false, false], | ||
])("testFrom (%s, %s, %s, %s)", (includeStart: string, | ||
includeEnd: string, | ||
includeStartExpected: boolean, | ||
includeEndExpected: boolean) => { | ||
|
||
let interval = testee.from(includeStart + '1,3' + includeEnd) | ||
|
||
expect(interval).toEqual({ | ||
start: 1, | ||
end: 3, | ||
includeStart: includeStartExpected, | ||
includeEnd: includeEndExpected | ||
}); | ||
}); | ||
|
||
test("testFromDegenerated", () => { | ||
let interval = testee.from('{1}') | ||
|
||
expect(interval).toEqual({ | ||
start: 1, | ||
end: 1, | ||
includeStart: true, | ||
includeEnd: true | ||
}); | ||
}); | ||
|
||
test("testFromEmpty", () => { | ||
let interval = testee.from('{}') | ||
|
||
expect(interval).toEqual({ | ||
start: null, | ||
end: null, | ||
includeStart: null, | ||
includeEnd: null | ||
}); | ||
}); | ||
|
||
test.each([ | ||
"[1,2,3]", | ||
"1", | ||
"1,2", | ||
"a,b]", | ||
"[]", | ||
"()", | ||
213, | ||
{a: 1} | ||
])("testFromInvalidString", (value: string) => { | ||
let from = (): void => { | ||
testee.from(value); | ||
} | ||
|
||
expect(from).toThrowError(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters