Last active
December 15, 2015 17:30
-
-
Save zkat/5296480 to your computer and use it in GitHub Desktop.
find/find_if search utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Array.prototype.find_if = function(test, opts) { | |
| opts = opts || {}; | |
| var key = opts.key || identity; | |
| var start = opts.start || 0; | |
| var end = opts.end || this.length; | |
| for (var i = 0; i < this.length; i++) { | |
| if (test(key(this[i]))) { | |
| return this[i]; | |
| } | |
| } | |
| return null; | |
| }; | |
| Array.prototype.find = function(match, opts) { | |
| opts = opts || {}; | |
| var test = opts.test || function(x,y) { return x === y; }; | |
| return this.find_if(function(x) { return test(match, x); }, opts); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment