Comparing version 1.2.3 to 1.2.4
@@ -206,4 +206,11 @@ /*! | ||
if (a.point > b.point) { return 1; } | ||
if (a.isStart && b.isEnd) { return -1; } | ||
if (a.isEnd && b.isStart) { return 1; } | ||
if (!a.isOpen && !b.isOpen) { | ||
if (a.isStart && b.isEnd) { return -1; } | ||
if (a.isEnd && b.isStart) { return 1; } | ||
} else { | ||
if (a.isStart && b.isEnd) { return 1; } | ||
if (a.isEnd && b.isStart) { return -1; } | ||
} | ||
if (a.isStart) { | ||
@@ -296,2 +303,3 @@ if (a.isOpen && !b.isOpen) { return 1; } | ||
var result = []; | ||
points.forEach(function (endPoint) { | ||
@@ -386,2 +394,25 @@ if (endPoint.isStart) { | ||
function endPointToInt(text) { | ||
if (text === 'Infinity') { | ||
return Infinity; | ||
} else if (text === '-Infinity') { | ||
return -Infinity; | ||
} else if (text.match(/^-?\d+$/)) { | ||
return parseInt(text, 10); | ||
} else { | ||
return text; | ||
} | ||
} | ||
Interval.fromString = function (text) { | ||
var m = text.match(/^([\[\(])([^;]+);\s?([^\]\)]+)([\]\)])$/); | ||
if (m) { | ||
var start = endPointToInt(m[2]); | ||
var end = endPointToInt(m[3]); | ||
return Interval.create(m[1], start, end, m[4]); | ||
} else { | ||
throw new Error('Could not parse "' + text + '" as an interval'); | ||
} | ||
}; | ||
Interval.prototype.toString = function (detailed) { | ||
@@ -388,0 +419,0 @@ return detailed ? "Insection.interval('" + this.startString + "'," + this.start + "," + this.end + ",'" + this.endString + "')" : |
@@ -185,4 +185,11 @@ var InvalidIntervalError = require('./InvalidIntervalError'); | ||
if (a.point > b.point) { return 1; } | ||
if (a.isStart && b.isEnd) { return -1; } | ||
if (a.isEnd && b.isStart) { return 1; } | ||
if (!a.isOpen && !b.isOpen) { | ||
if (a.isStart && b.isEnd) { return -1; } | ||
if (a.isEnd && b.isStart) { return 1; } | ||
} else { | ||
if (a.isStart && b.isEnd) { return 1; } | ||
if (a.isEnd && b.isStart) { return -1; } | ||
} | ||
if (a.isStart) { | ||
@@ -275,2 +282,3 @@ if (a.isOpen && !b.isOpen) { return 1; } | ||
var result = []; | ||
points.forEach(function (endPoint) { | ||
@@ -277,0 +285,0 @@ if (endPoint.isStart) { |
@@ -63,2 +63,25 @@ var InvalidIntervalError = require('./InvalidIntervalError'); | ||
function endPointToInt(text) { | ||
if (text === 'Infinity') { | ||
return Infinity; | ||
} else if (text === '-Infinity') { | ||
return -Infinity; | ||
} else if (text.match(/^-?\d+$/)) { | ||
return parseInt(text, 10); | ||
} else { | ||
return text; | ||
} | ||
} | ||
Interval.fromString = function (text) { | ||
var m = text.match(/^([\[\(])([^;]+);\s?([^\]\)]+)([\]\)])$/); | ||
if (m) { | ||
var start = endPointToInt(m[2]); | ||
var end = endPointToInt(m[3]); | ||
return Interval.create(m[1], start, end, m[4]); | ||
} else { | ||
throw new Error('Could not parse "' + text + '" as an interval'); | ||
} | ||
}; | ||
Interval.prototype.toString = function (detailed) { | ||
@@ -65,0 +88,0 @@ return detailed ? "Insection.interval('" + this.startString + "'," + this.start + "," + this.end + ",'" + this.endString + "')" : |
{ | ||
"name": "insection", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "A data structure for storing number intervals", | ||
@@ -40,5 +40,5 @@ "main": "lib/insection.js", | ||
"mocha": "2.1.0", | ||
"unexpected": "^9.6.0", | ||
"unexpected": "9.12.0", | ||
"unexpected-documentation-site-generator": "2.2.3" | ||
} | ||
} |
@@ -510,7 +510,18 @@ var Insection = require('../lib/Insection.js'); | ||
insection.add(9, 10, '4'); | ||
expect(insection.getGaps(0, 10).map(function (interval) { | ||
return interval.toString(); | ||
}).sort(), 'to equal', [ '(2;3)', '(8;9)', '[7;7]' ]); | ||
expect(insection, 'when getting gaps between', '[0;10]', | ||
'to equal', | ||
['(2;3)', '(8;9)', '[7;7]']); | ||
}); | ||
it('find gaps between negative intervals', function () { | ||
var insection = new Insection(); | ||
insection.add(-1, 'a'); | ||
insection.add(-10, -8, 'b'); | ||
expect(insection, 'when getting gaps between', '[-11;2]', | ||
'to equal', | ||
['(-1;2]', '(-8;-1)', '[-11;-10)']); | ||
}); | ||
}); | ||
}); |
var Insection = require('../lib/Insection'); | ||
var Interval = require('../lib/Interval'); | ||
var expect = require('./unexpected-configured'); | ||
@@ -33,2 +34,43 @@ | ||
}); | ||
describe('toString', function () { | ||
[ | ||
{ interval: Insection.interval('(', -Infinity, 5, ']'), toString: '(-Infinity;5]' }, | ||
{ interval: Insection.interval(-3454, 5), toString: '[-3454;5]' }, | ||
{ interval: Insection.interval(4, 5), toString: '[4;5]' }, | ||
{ interval: Insection.interval(4, 3345), toString: '[4;3345]' }, | ||
{ interval: Insection.interval(4, 4), toString: '[4;4]' }, | ||
{ interval: Insection.interval("a", "b"), toString: '[a;b]' }, | ||
{ interval: Insection.interval('[', 4, 4, ')'), toString: '[4;4)' }, | ||
{ interval: Insection.interval('[', 4, Infinity, ')'), toString: '[4;Infinity)' }, | ||
{ interval: Insection.interval('(', -Infinity, Infinity, ')'), toString: '(-Infinity;Infinity)' } | ||
].forEach(function (example) { | ||
it('on ' + example.interval.toString(true) + " returns " + example.toString, function () { | ||
expect(example.interval.toString(), 'to equal', example.toString); | ||
}); | ||
}); | ||
}); | ||
describe('fromString', function () { | ||
[ | ||
{ interval: Insection.interval('(', -Infinity, 5, ']'), text: '(-Infinity;5]' }, | ||
{ interval: Insection.interval(-3454, 5), text: '[-3454;5]' }, | ||
{ interval: Insection.interval(4, 5), text: '[4;5]' }, | ||
{ interval: Insection.interval(4, 3345), text: '[4;3345]' }, | ||
{ interval: Insection.interval(4, 4), text: '[4;4]' }, | ||
{ interval: Insection.interval("a", "b"), text: '[a;b]' }, | ||
{ interval: Insection.interval('[', 4, 4, ')'), text: '[4;4)' }, | ||
{ interval: Insection.interval('[', 4, Infinity, ')'), text: '[4;Infinity)' }, | ||
{ interval: Insection.interval('(', -Infinity, Infinity, ')'), text: '(-Infinity;Infinity)' } | ||
].forEach(function (example) { | ||
it('on ' + example.text + " returns " + example.interval.toString(true), function () { | ||
expect(Interval.fromString(example.text), 'to equal', example.interval); | ||
}); | ||
}); | ||
it('fails when given a string that cannot be parsed as an interval', function () { | ||
expect(function () { | ||
Interval.fromString('foobar'); | ||
}, 'to throw', 'Could not parse "foobar" as an interval'); | ||
}); | ||
}); | ||
}); |
var Insection = require('../lib/Insection.js'); | ||
var Interval = require('../lib/Interval.js'); | ||
@@ -66,5 +67,5 @@ module.exports = require('unexpected').clone() | ||
output.text(interval.startString || '[') | ||
.number(interval.start) | ||
.jsNumber(interval.start) | ||
.text(';') | ||
.number(interval.end) | ||
.jsNumber(interval.end) | ||
.text(interval.endString || ']'); | ||
@@ -133,2 +134,8 @@ return output; | ||
})(root, 0, -1); | ||
}) | ||
.addAssertion('Insection', 'when getting gaps between', function (expect, subject, interval) { | ||
var gaps = subject.getGaps(Interval.fromString(interval)).map(function (interval) { | ||
return interval.toString(); | ||
}).sort(); | ||
this.shift(gaps, 1); | ||
}); |
294472
3418