binarysearch
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -40,2 +40,4 @@ | ||
// this is inconsistent because it gives values. | ||
// i should breaking change this soon. | ||
module.exports.range = function(arr,from,to,comparitor) { | ||
@@ -42,0 +44,0 @@ if(!comparitor) comparitor = module.exports._defaultComparitor(); |
{ | ||
"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.3", | ||
"version": "0.1.4", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "url": "git://github.com/soldair/node-binarysearch.git" |
@@ -52,7 +52,7 @@ | ||
find closest key to search value | ||
- if the value is in the array it returns the first index with that value in the array | ||
- if the value is not the index in the array of the closest item that sorts immediately before this item will be returned. | ||
- +1 is the index where you should set this item into the array if you want to add it. | ||
- if index 0 is the closest match -i will be returmed. | ||
find closest key to where or key of searched value in the array | ||
- if the key is in the array the key will point to | ||
- the first key that has that value by default | ||
- the last key that has that value if {end:true} option is specified | ||
- only returns -1 if array is empty | ||
@@ -62,8 +62,12 @@ ```js | ||
bs.closest([1,2,4,5,6],3) === 1 | ||
bs.closest([1,2,4,5,6],0) === -1 | ||
bs.closest([1,2,4,5,6],0) === 0 | ||
bs.closest([1,2,4,5,6],200) === 6 | ||
// non unique matching/matching at end of series | ||
bs.closest([1,2,4,5,5,5,6],5) === 3 | ||
bs.closest([1,2,4,5,5,5,6],5,{end:true}) === 5 | ||
``` | ||
query for range (inclusive) | ||
query for range (inclusive). returns sliced values. | ||
@@ -75,3 +79,3 @@ ```js | ||
search with object index | ||
create an object index | ||
@@ -83,3 +87,10 @@ ```js | ||
``` | ||
search an object index | ||
```js | ||
var obj = {a:{id:22,name:'bob'},b:{id:11,name:'joe'}}; | ||
// [{k:'b',v:11},{k:'a',v:22}]; | ||
index = bs.indexObject(obj,function(o1,o2){ | ||
@@ -90,5 +101,3 @@ if(o1.id > o2.id) return 1 | ||
}); | ||
// [{k:'b',v:11},{k:a,v:22}]; | ||
obj[bs(index,'bob').k] === {id:22,name:'bob'}; | ||
@@ -95,0 +104,0 @@ |
@@ -12,3 +12,3 @@ var test = require('tap').test; | ||
var key = bs.closest([3],1); | ||
t.equals(key,0,key+' should have got -1 as key because there is only 1 and it is higher'); | ||
t.equals(key,0,key+' should have got 0 as key even though there is only 1 and it is higher'); | ||
t.end(); | ||
@@ -23,2 +23,8 @@ }); | ||
test("gets lowest number closest",function(t){ | ||
var key = bs.closest([2,3,4,100],99); | ||
t.equals(key,2,key+' should have got key 2 because that is the last number before a number greater'); | ||
t.end(); | ||
}); | ||
// end sorted | ||
@@ -43,1 +49,8 @@ | ||
}); | ||
test("gets highest number closest with end",function(t){ | ||
var key = bs.closest([2,3,4,100,100],99,{end:true}); | ||
t.equals(key,3,key+' should have got key 3 because that is the closest number greater than the search'); | ||
t.end(); | ||
}); | ||
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
232
110
10305
10