Code coverage report for array/toLookup.js

Statements: 100% (13 / 13)      Branches: 100% (2 / 2)      Functions: 100% (3 / 3)      Lines: 100% (12 / 12)     

All files » array/ » toLookup.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301                   1 2     2 1 2 2     1 2 2     2   1    
define(['../lang/isFunction'], function (isFunction) {
 
    /**
     * Creates an object that holds a lookup for the objects in the array.
     * The key for each value in `arr` is specified by the `key` parameter.
     * If `key` is a function, the function will be called with the value as
     * the parameter and the result will be used for the key. If `key` is a
     * string it will use the property specified by `key` as the key for each
     * value.
     */
    function toLookup(arr, key) {
        var result = {},
            value,
            i = -1, n = arr.length;
        if (isFunction(key)) {
            while (++i < n) {
                value = arr[i];
                result[key(value)] = value;
            }
        } else {
            while (++i < n) {
                value = arr[i];
                result[value[key]] = value;
            }
        }
        return result;
    }
    return toLookup;
});