binarysearch
Advanced tools
Comparing version 0.1.5 to 0.2.0
29
index.js
@@ -33,3 +33,2 @@ | ||
if(closest > arr.length-1) closest = arr.length-1; | ||
@@ -79,4 +78,3 @@ else if(closest < 0) closest = 0; | ||
// this is inconsistent because it gives values. | ||
// i should breaking change this soon. | ||
// this method returns the start and end indicies of a range. [start,end] | ||
module.exports.range = function(arr,from,to,comparitor) { | ||
@@ -91,17 +89,24 @@ if(!comparitor) comparitor = module.exports._defaultComparitor(); | ||
// i should be able to fix the algorithm and generate a correct range. | ||
var range = arr.slice(fromi,toi+1); | ||
while(range.length){ | ||
if(comparitor(range[0],from) > -1) break; | ||
range.shift(); | ||
while(fromi <= toi){ | ||
if(comparitor(arr[fromi],from) > -1) break; | ||
fromi++ | ||
} | ||
while(range.length){ | ||
if(comparitor(range[range.length-1],to) < 1) break; | ||
range.pop(); | ||
while(toi >= fromi){ | ||
if(comparitor(arr[toi],to) < 1) break; | ||
toi--; | ||
} | ||
return range; | ||
return [fromi,toi]; | ||
} | ||
// this method returns the values of a range; | ||
module.exports.rangeValue = function(arr,from,to,comparitor){ | ||
var range = module.exports.range(arr,from,to,comparitor); | ||
return arr.slice(range[0],range[1]+1); | ||
} | ||
// | ||
module.exports.indexObject = function(o,extractor) { | ||
@@ -108,0 +113,0 @@ var index = []; |
{ | ||
"name": "binarysearch", | ||
"description": "pure js binary search for sorted javascript arrays||array like objects. returns any || last || first || closest matched key for value, or slice between 2 values where values need not exist.", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "url": "git://github.com/soldair/node-binarysearch.git" |
@@ -70,9 +70,16 @@ | ||
query for range (inclusive). returns sliced values. | ||
query for rangeValue (inclusive). returns sliced values. | ||
```js | ||
bs.range([1,2,3,3,3,4,4,6],3,5) === [3,3,3,4,4] | ||
bs.rangeValue([1,2,3,3,3,4,4,6],3,5) === [3,3,3,4,4] | ||
``` | ||
or simply access the array offsets directly as [start,end] | ||
```js | ||
bs.range([1,2,3,3,3,4,4,6],3,5) === [2,6] | ||
``` | ||
insert a value into a sorted array. | ||
@@ -79,0 +86,0 @@ |
@@ -6,3 +6,3 @@ var test = require('tap').test; | ||
var range = bs.range([1,2,3,3,3,4,4,6,6],3,5); | ||
var range = bs.rangeValue([1,2,3,3,3,4,4,6,6],3,5); | ||
@@ -16,3 +16,3 @@ t.equals(range.join(','),[3,3,3,4,4].join(','),' should have correct range'); | ||
var range = bs.range([2,3,3,3,6,6],0,3); | ||
var range = bs.rangeValue([2,3,3,3,6,6],0,3); | ||
@@ -26,3 +26,3 @@ t.equals(range.join(','),[2,3,3,3].join(','),' should have correct range'); | ||
var range = bs.range([6,6,6],7,9); | ||
var range = bs.rangeValue([6,6,6],7,9); | ||
@@ -36,3 +36,3 @@ t.equals(range.join(','),'',' should have correct range'); | ||
var range = bs.range([6,6,8],7,9); | ||
var range = bs.rangeValue([6,6,8],7,9); | ||
@@ -39,0 +39,0 @@ t.equals(range.join(','),'8',' should have correct range'); |
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
13542
312
142