Skip to content

Commit

Permalink
Merge b50f136 into f2c2345
Browse files Browse the repository at this point in the history
  • Loading branch information
elhay-av committed Jul 27, 2016
2 parents f2c2345 + b50f136 commit fb3e65b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -60,6 +60,16 @@ The search function has 2 overloads:
```javascript
new BS(arrObj).search('20380a36-8777-43f7-a79e-65bdb53f4621', 'guid');
```
3. Value and key in case of the object with custom condition as following:
```javascript
function searchCustomIterator(item, target) {
return (target >= item.min && target <= item.max)? 0: target - item.min; // res>0 || res<0
}
var arrObj = [{id: '1',min:1,max:6},{id:'2',min:5,max:6},{id:'3',min:7,max:9},{id:'4',min:10,max: 12}];

new BS(arrObj).search(5, searchCustomIterator); // {id:'2',min:5,max:6}
```

## Sorting
Now you can sort the array in chaining mechanism.
```javascript
Expand Down Expand Up @@ -92,4 +102,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
25 changes: 24 additions & 1 deletion dist/js-binarysearch.js
@@ -1,5 +1,5 @@
/*! js-binarysearch - v1.0.2
* Release on: 2016-05-29
* Release on: 2016-07-27
* Copyright (c) 2016 Amgad Fahmi
* Licensed MIT */
(function(root, factory) {
Expand Down Expand Up @@ -31,6 +31,8 @@ var BS = function(array) {
BS.prototype.search = function(target, key) {
if (key && typeof key === 'string') {
return this.searchObj(target, key);
} else if (typeof key === 'function') {
return this.searchCustom(target, key);
} else if (typeof target === 'number') {
return this.searchNum(target);
} else if (typeof target === 'string') {
Expand Down Expand Up @@ -87,6 +89,26 @@ BS.prototype.searchObj = function(target, key) {
}
};

/*
* @param iterator(item, target) => (1 || 0 || -1)
*/
BS.prototype.searchCustom = function(target, iterator) {
var min = 0,
max = this.internalArray.length - 1,
temp, mid;
while (min <= max) {
mid = Math.round(min + (max - min) / 2);
temp = iterator(this.internalArray[mid], target);
if (temp === 0) { // temp match to target
return this.internalArray[mid];
} else if (temp > 0) {
min = mid + 1;
} else {
max = mid - 1;
}
}
};

BS.prototype.sort = function(key) {
if (this.internalArray.length <= 1) {
return;
Expand Down Expand Up @@ -123,5 +145,6 @@ BS.prototype.sortNum = function() {
return a - b;
});
};

return BS;
}));
4 changes: 2 additions & 2 deletions dist/js-binarysearch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion lib/js-binarysearch.js
Expand Up @@ -13,6 +13,8 @@ var BS = function(array) {
BS.prototype.search = function(target, key) {
if (key && typeof key === 'string') {
return this.searchObj(target, key);
} else if (typeof key === 'function') {
return this.searchCustom(target, key);
} else if (typeof target === 'number') {
return this.searchNum(target);
} else if (typeof target === 'string') {
Expand Down Expand Up @@ -69,6 +71,29 @@ BS.prototype.searchObj = function(target, key) {
}
};

/*
* @param iterator(item, target) => (1 || 0 || -1)
* iterator(item, target) => 1 => target is bigger
* iterator(item, target) => 0 => target found
* iterator(item, target) => -1 => target is smaller
*/
BS.prototype.searchCustom = function(target, iterator) {
var min = 0,
max = this.internalArray.length - 1,
temp, mid;
while (min <= max) {
mid = Math.round(min + (max - min) / 2);
temp = iterator(this.internalArray[mid], target);
if (temp === 0) { // temp match to target
return this.internalArray[mid];
} else if (temp > 0) {
min = mid + 1;
} else {
max = mid - 1;
}
}
};

BS.prototype.sort = function(key) {
if (this.internalArray.length <= 1) {
return;
Expand Down Expand Up @@ -104,4 +129,4 @@ BS.prototype.sortNum = function() {
this.internalArray.sort(function(a, b) {
return a - b;
});
};
};
20 changes: 20 additions & 0 deletions spec/mainSpec.js
Expand Up @@ -81,5 +81,25 @@ define(['BS'], function(BS) {
});
});

describe("Search", function() {
it("search by custom-iterator", function() {
function searchCustomIterator(item, target) {
if (target >= item.min && target <= item.max) {
return 0; // This is the item we want to find
}

return target - item.min; // res>0 || res<0
}
var arr = [
{id: 33, name: 'Jasmin', min: 1, max: 6},
{id: 12, name: 'David', min: 5, max: 6},
{id: 7, name: 'Amgad', min: 7, max: 9},
{id: 56, name: 'Katya', min: 10, max: 12}
];
var bs = new BS(arr).sort('min').search(5, searchCustomIterator);
expect(bs).toEqual({id: 12, name: 'David', min: 5, max: 6});
});
});

});
console.log('Test ended ...');

0 comments on commit fb3e65b

Please sign in to comment.