Comparing version 0.0.6 to 0.1.0
{ | ||
"name": "rangerjs", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"description": "A lightweight JS library for defining an array of ranges.", | ||
@@ -5,0 +5,0 @@ "main": "ranger.js", |
@@ -6,2 +6,9 @@ | ||
ranger.Range = function(Type) { | ||
return { | ||
start: Type, | ||
end: Type | ||
} | ||
} | ||
var endBeforeStart = function(start, end) { | ||
@@ -59,27 +66,28 @@ if (start - end > 0) | ||
count = 0; | ||
for (var i=0; i < this.ranges.length; i+=2) { | ||
if (this.ranges[i] > end) { | ||
for (var i=0; i < this.ranges.length; i++) { | ||
var range = this.ranges[i]; | ||
if (range.start > end) { | ||
break; | ||
} | ||
else if (this.ranges[i+1] >= start) { | ||
if (this.ranges[i] <= start && | ||
this.ranges[i+1] >= end) { | ||
else if (range.end >= start) { | ||
if (range.start <= start && | ||
range.end >= end) { | ||
return; | ||
} | ||
else if (this.ranges[i] >= start && | ||
this.ranges[i+1] < end) { | ||
else if (range.start >= start && | ||
range.end < end) { | ||
if (index === null) index = i; | ||
count++; | ||
} | ||
else if (this.ranges[i] < start && | ||
this.ranges[i+1] <= end) { | ||
else if (range.start < start && | ||
range.end <= end) { | ||
if (index === null) index = i; | ||
count++; | ||
start = this.ranges[i]; | ||
start = range.start; | ||
} | ||
else if (this.ranges[i] >= start && | ||
this.ranges[i+1] >= end) { | ||
else if (range.start >= start && | ||
range.end >= end) { | ||
if (index === null) index = i; | ||
count++; | ||
end = this.ranges[i+1]; | ||
end = range.end; | ||
break;; | ||
@@ -90,9 +98,10 @@ } | ||
index = index === null ? this.ranges.length : index; | ||
this.ranges.splice(index, 2 * count, start, end); | ||
this.ranges.splice(index, count, {start: start, end: end}); | ||
}, | ||
check: function(val) { | ||
for (var i = 0; i < this.ranges.length; i+=2) { | ||
var min = this.ranges[i]; | ||
var max = this.ranges[i+1]; | ||
for (var i = 0; i < this.ranges.length; i++) { | ||
var range = this.ranges[i]; | ||
var min = range.start; | ||
var max = range.end; | ||
if (min <= val && val < max) | ||
@@ -109,5 +118,6 @@ return true; | ||
return new Error('Start must be before end for range'); | ||
for (var i=0; i < this.ranges.length; i+=2) { | ||
if (this.ranges[i] <= start && | ||
this.ranges[i+1] >= end) | ||
for (var i=0; i < this.ranges.length; i++) { | ||
var range = this.ranges[i]; | ||
if (range.start <= start && | ||
range.end >= end) | ||
return true; | ||
@@ -121,23 +131,27 @@ } | ||
return new Error('Start must be before end for range'); | ||
for (var i=0; i < this.ranges.length; i+=2) { | ||
if (this.ranges[i+1] >= start) { | ||
if (this.ranges[i] >= end) | ||
for (var i=0; i < this.ranges.length; i++) { | ||
var range = this.ranges[i]; | ||
if (range.end >= start) { | ||
if (range.start >= end) | ||
return; | ||
else if (this.ranges[i] < start && | ||
this.ranges[i+1] > end) { | ||
this.ranges.splice(i + 1, 0, start, end); | ||
else if (range.start < start && | ||
range.end > end) { | ||
this.ranges.splice(i+1, 0, { | ||
start: end, | ||
end: range.end | ||
}) | ||
range.end = start; | ||
return; | ||
} | ||
else if (this.ranges[i] >= start && | ||
this.ranges[i+1] <= end) { | ||
this.ranges.splice(i, 2); | ||
i -= 2; | ||
else if (range.start >= start && | ||
range.end <= end) { | ||
this.ranges.splice(i--, 1); | ||
} | ||
else if (this.ranges[i] < start && | ||
this.ranges[i+1] <= end) { | ||
this.ranges[i+1] = start; | ||
else if (range.start < start && | ||
range.end <= end) { | ||
range.end = start; | ||
} | ||
else if (this.ranges[i] >= start && | ||
this.ranges[i+1] > end) { | ||
this.ranges[i] = end; | ||
else if (range.start >= start && | ||
range.end > end) { | ||
range.start = end; | ||
} | ||
@@ -144,0 +158,0 @@ } |
ranger.js | ||
======== | ||
A lightweight JS library for defining an array of ranges. | ||
A lightweight JS library for defining an array of ranges. Stores the ranges in an array of range objects. | ||
@@ -11,3 +11,27 @@ *note: upper limit is always non-inclusive* | ||
var existingRanger = new ranger(array); | ||
####ranger.Range(Type) | ||
//Returns: | ||
{ | ||
start: Type, | ||
end: Type | ||
} | ||
In order to save the array to db (in this case MongoDB), use the following Schema type: | ||
{ | ||
... | ||
ranges: { | ||
type: ranger.Range(Type), //Type can be Number, Date, etc. | ||
get: function(data) { | ||
return new ranger(data); | ||
}, | ||
set: function(data) { | ||
return data.ranges || data; | ||
} | ||
} | ||
} | ||
Keep in mind that MongoDB does not listen for changes in individual array elements so after performing an operation (such as `addRange` or `removeRange`) make sure to `markModified('ranges')` | ||
####newRanger.addRange(from, to); | ||
@@ -63,2 +87,2 @@ newRanger.addRange(1, 4); | ||
newRanger.check(6); //true | ||
newRanger.check(7); //true | ||
newRanger.check(7); //true |
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
9988
189
87