discontinuous-range
Advanced tools
Comparing version 1.0.0 to 1.0.1
31
index.js
@@ -125,2 +125,33 @@ //protected helper class | ||
DiscontinuousRange.prototype.intersect = function (a, b) { | ||
var self = this; | ||
var new_ranges = []; | ||
function _intersect(subrange) { | ||
var i = 0; | ||
while (i < self.ranges.length && !subrange.overlaps(self.ranges[i])) { | ||
i++; | ||
} | ||
while (i < self.ranges.length && subrange.overlaps(self.ranges[i])) { | ||
var low = Math.max(self.ranges[i].low, subrange.low); | ||
var high = Math.min(self.ranges[i].high, subrange.high); | ||
new_ranges.push(new _SubRange(low, high)); | ||
i++; | ||
} | ||
} | ||
if (a instanceof DiscontinuousRange) { | ||
a.ranges.forEach(_intersect); | ||
} else { | ||
if (a instanceof _SubRange) { | ||
_intersect(a); | ||
} else { | ||
if (b === undefined) b = a; | ||
_intersect(new _SubRange(a, b)); | ||
} | ||
} | ||
self.ranges = new_ranges; | ||
_update_length(self); | ||
return this; | ||
}; | ||
DiscontinuousRange.prototype.index = function (index) { | ||
@@ -127,0 +158,0 @@ var i = 0; |
{ | ||
"name": "discontinuous-range", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "for adding, subtracting, and indexing discontinuous ranges of numbers", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha -R spec test/*-test.js" | ||
"test": "mocha ./test/main-test.js" | ||
}, | ||
@@ -25,4 +26,4 @@ "repository": { | ||
"devDependencies": { | ||
"mocha": "^1.21.4" | ||
"mocha": "^10.2.0" | ||
} | ||
} |
@@ -18,4 +18,4 @@ discontinuous-range | ||
``` | ||
var all_numbers = new DiscontinuousRange(1, 100); | ||
var bad_numbers = DiscontinuousRange(13).add(8).add(60,80); | ||
var all_numbers = new DiscontinuousRange(1, 100); //[ 1-100 ] | ||
var bad_numbers = DiscontinuousRange(13).add(8).add(60,80); //[8, 13, 60-80] | ||
var good_numbers = all_numbers.clone().subtract(bad_numbers); | ||
@@ -22,0 +22,0 @@ console.log(good_numbers.toString()); //[ 1-7, 9-12, 14-59, 81-100 ] |
@@ -1,8 +0,7 @@ | ||
var assert = require('assert'); | ||
const assert = require('assert'); | ||
const DRange = require('../index.js'); | ||
var DRange = require('../index.js'); | ||
describe('add sets', function() { | ||
it('should allow adding numbers', function () { | ||
var drange = DRange(5); | ||
const drange = new DRange(5); | ||
assert.equal('[ 5 ]', drange.toString()); | ||
@@ -18,3 +17,3 @@ drange.add(6); | ||
it('should allow adding ranges of numbers', function () { | ||
var drange = DRange(1,5); | ||
const drange = new DRange(1,5); | ||
assert.equal('[ 1-5 ]', drange.toString()); | ||
@@ -30,5 +29,5 @@ drange.add(6,10); | ||
it('should allow adding another DiscontinuousRange', function () { | ||
var drange = DRange(1,5); | ||
const drange = new DRange(1,5); | ||
drange.add(15,20); | ||
var erange = DRange(6); | ||
const erange = new DRange(6); | ||
erange.add(17, 30); | ||
@@ -43,3 +42,3 @@ drange.add(erange); | ||
it('should allow subtracting numbers', function () { | ||
var drange = DRange(1, 10); | ||
const drange = new DRange(1, 10); | ||
drange.subtract(5); | ||
@@ -54,3 +53,3 @@ assert.equal('[ 1-4, 6-10 ]', drange.toString()); | ||
it('should allow subtracting ranges of numbers', function () { | ||
var drange = DRange(1, 100); | ||
const drange = new DRange(1, 100); | ||
drange.subtract(5, 15); | ||
@@ -63,4 +62,4 @@ assert.equal('[ 1-4, 16-100 ]', drange.toString()); | ||
it('should allow subtracting another DiscontinuousRange', function () { | ||
var drange = DRange(0,100); | ||
var erange = DRange(6); | ||
const drange = new DRange(0,100); | ||
const erange = new DRange(6); | ||
erange.add(17, 30); | ||
@@ -74,5 +73,35 @@ drange.subtract(erange); | ||
describe('intersect sets', function() { | ||
it('should allow intersecting numbers', function () { | ||
const drange = new DRange(5,20); | ||
assert.equal('[ 5-20 ]', drange.toString()); | ||
drange.intersect(7); | ||
assert.equal('[ 7 ]', drange.toString()); | ||
}); | ||
it('should allow intersecting ranges of numbers', function () { | ||
const drange = new DRange(1,5); | ||
assert.equal('[ 1-5 ]', drange.toString()); | ||
drange.intersect(6,10); | ||
assert.equal('[ ]', drange.toString()); | ||
drange.add(15,20); | ||
assert.equal('[ 15-20 ]', drange.toString()); | ||
drange.intersect(0,18); | ||
assert.equal('[ 15-18 ]', drange.toString()); | ||
assert.equal(drange.length, 4); | ||
}); | ||
it('should allow intersecting another DiscontinuousRange', function () { | ||
const drange = new DRange(1,5); | ||
drange.add(15,20); | ||
const erange = new DRange(3,6); | ||
erange.add(17, 30); | ||
drange.intersect(erange); | ||
assert.equal('[ 3-5, 17-20 ]', drange.toString()); | ||
assert.equal(drange.length, 7); | ||
}); | ||
}); | ||
describe('index sets', function() { | ||
it('should appropriately retrieve numbers in range by index', function () { | ||
var drange = DRange(0, 9); | ||
const drange = new DRange(0, 9); | ||
drange.add(20, 29); | ||
@@ -89,4 +118,4 @@ drange.add(40, 49); | ||
it('should be able to clone a DiscontinuousRange that doesn\'t affect the original', function () { | ||
var drange = DRange(0, 9); | ||
var erange = drange.clone(); | ||
const drange = new DRange(0, 9); | ||
const erange = drange.clone(); | ||
erange.subtract(5); | ||
@@ -98,7 +127,7 @@ assert.equal('[ 0-9 ]', drange.toString()); | ||
var all_numbers = new DRange(1, 100); | ||
var bad_numbers = DRange(13).add(8).add(60,80); | ||
var good_numbers = all_numbers.clone().subtract(bad_numbers); | ||
const all_numbers = new DRange(1, 100); | ||
const bad_numbers = new DRange(13).add(8).add(60,80); | ||
const good_numbers = all_numbers.clone().subtract(bad_numbers); | ||
console.log(good_numbers.toString()); | ||
var random_good_number = good_numbers.index(Math.floor(Math.random() * good_numbers.length)); | ||
const random_good_number = good_numbers.index(Math.floor(Math.random() * good_numbers.length)); | ||
console.log(random_good_number); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13485
300