All files / datamodel/src/operator group-by-function.js

68.09% Statements 32/47
54.17% Branches 13/24
56.52% Functions 13/23
78.38% Lines 29/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134            66x 66x 66x 126x     126x 126x   66x                 22x 22x 22x 22x     22x                 2x 2x       2x                 2x 2x       2x                 2x                 2x                 2x 2x 2x     2x                   2x 6x                   2x       1x                     1x              
/**
 * Reducer function that takes care about the sum aggregation
 * @param  {Array} arr array of values
 * @return {number}     sum of the array
 */
function sum (arr) {
    let allNulls = true;
    const isNestedArray = arr[0] instanceof Array;
    const sumVal = arr.reduce((carry, a) => {
        Iif (isNestedArray) {
            return carry.map((x, i) => x + a[i]);
        }
        allNulls = allNulls && (a === null);
        return carry + a;
    }, isNestedArray ? Array(...Array(arr[0].length)).map(() => 0) : 0);
    return allNulls ? null : sumVal;
}
 
/**
 * reducer function that takes care about the mean aggregation
 * @param  {Array} arr array of values
 * @return {number}     mean of the array
 */
function avg (arr) {
    const isNestedArray = arr[0] instanceof Array;
    const len = arr.length || 1;
    const arrSum = sum(arr);
    Iif (isNestedArray) {
        return arrSum.map(x => x / len);
    }
    return arrSum === null ? null : arrSum / len;
}
 
/**
 * reducer function that gives the min value
 * @param  {Array} arr array of values
 * @return {number}     min of the array
 */
function min (arr) {
    const isNestedArray = arr[0] instanceof Array;
    Iif (isNestedArray) {
        return arr.reduce((carry, a) => carry.map((x, i) => Math.min(x, a[i])),
        Array(...Array(arr[0].length)).map(() => Infinity));
    }
    return arr.every(d => d === null) ? null : Math.min(...arr);
}
 
/**
 * reducer function that gives the max value
 * @param  {Array} arr array of values
 * @return {number}     max of the array
 */
function max (arr) {
    const isNestedArray = arr[0] instanceof Array;
    Iif (isNestedArray) {
        return arr.reduce((carry, a) => carry.map((x, i) => Math.max(x, a[i])),
        Array(...Array(arr[0].length)).map(() => -Infinity));
    }
    return arr.every(d => d === null) ? null : Math.max(...arr);
}
 
/**
 * reducer function that gives the first value
 * @param  {Array} arr array of values
 * @return {number}     first value of the array
 */
function first (arr) {
    return arr[0];
}
 
/**
 * reducer function that gives the last value
 * @param  {Array} arr array of values
 * @return {number}     last value of the array
 */
function last (arr) {
    return arr[arr.length - 1];
}
 
/**
 * reducer function that gives the count value
 * @param  {Array} arr array of values
 * @return {number}     count of the array
 */
function count (arr) {
    const isNestedArray = arr[0] instanceof Array;
    const len = arr.length;
    Iif (isNestedArray) {
        return Array(...Array(arr[0].length)).map(() => len);
    }
    return len;
}
 
/**
 * Calculates the variance of the input array.
 *
 * @param {Array.<number>} arr - The input array.
 * @return {number} Returns the variance of the input array.
 */
function variance (arr) {
    let mean = avg(arr);
    return avg(arr.map(num => (num - mean) ** 2));
}
 
/**
 * Calculates the square root of the variance of the input array.
 *
 * @param {Array.<number>} arr - The input array.
 * @return {number} Returns the square root of the variance.
 */
function std (arr) {
    return Math.sqrt(variance(arr));
}
 
 
const fnList = {
    sum,
    avg,
    min,
    max,
    first,
    last,
    count,
    std
};
 
const defaultReducerName = 'sum';
 
export {
    defaultReducerName,
    sum as defReducer,
    fnList,
};