proto

makeFindMethod

function
 makeFindMethod() 

Option name Type Description
eachMethod Function
  • method to use for iteration (forEach for array or eachKey for object)
findWhat String

'value' - returns find method of Array (implemented in ES6) or findValue method of Object, anything else = returns findIndex/findKey methods.

return Function

Returns find or findIndex method, depending on parameter

function makeFindMethod(someMethod, findWhat) {
    var argIndex = findWhat == 'value' ? 0 : 1;

    return function findValueOrIndex(callback, thisArg, onlyEnumerable) {
        var foundValueOrIndex;
        var found = someMethod.call(this, testItem, thisArg, onlyEnumerable);
        if (found)
            return foundValueOrIndex;
        // if looking for index and not found, return -1
        else if (argIndex && someMethod == Array.prototype.some)
            return -1;

        function testItem(value, index, self) {
            var test = callback.call(this, value, index, self);
            if (test) {
                foundValueOrIndex = arguments[argIndex]
                return test;
            }
        }
    }
}