Code coverage report for src/countBy.js

Statements: 100% (26 / 26)      Branches: 100% (12 / 12)      Functions: 100% (2 / 2)      Lines: 100% (13 / 13)      Ignored: 4 statements, 3 branches     

All files » src/ » countBy.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 30 31 32 33                  1 7 7 7   7 1   6     7 39   39   39     7        
/**
 * Creates an object mapping the iterable's values to counts.
 * @param {*} xs - The iterable to count.
 * @param {Function|String} iteratee -
 * The function or property for generating count keys.
 * @param {Function|String} thisArg -
 * The this binding for the iteratee function.
 * @returns {Object} - An object containing values and counts.
 */
function countBy(xs, iteratee, thisArg) {
    let i = 0;
    let counts = {};
    let fn;
 
    if (typeof iteratee === 'string') {
        fn = x => x[iteratee];
    } else {
        fn = thisArg ? iteratee.bind(thisArg) : iteratee;
    }
 
    for (let x of xs) {
        let key = fn(x, i, xs);
 
        counts[key] = counts[key] ? counts[key] + 1 : 1;
 
        i += 1;
    }
 
    return counts;
}
 
export default countBy;