ts-flex-query
Version:
Flexible and type-safe data queries
31 lines • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.aggregation = void 0;
const lodash_1 = require("lodash");
const utils_1 = require("../helpers/utils");
function applyToDefinedValues(values, func, defaultResult) {
const definedValues = values.filter(utils_1.isDefined);
return definedValues.length ? func(definedValues) : defaultResult;
}
exports.aggregation = {
count(values) {
var _a;
return (_a = values === null || values === void 0 ? void 0 : values.length) !== null && _a !== void 0 ? _a : 0;
},
countDistinct(values) {
return values ? (0, lodash_1.uniqWith)(values, lodash_1.isEqual).length : 0;
},
maximum(values) {
return applyToDefinedValues(values, (v) => Math.max(...v), undefined);
},
minimum(values) {
return applyToDefinedValues(values, (v) => Math.min(...v), undefined);
},
sum(values) {
return values.filter(utils_1.isDefined).reduce((acc, v) => acc + v, 0);
},
average(values) {
return applyToDefinedValues(values, (vs) => exports.aggregation.sum(vs) / vs.length, undefined);
}
};
//# sourceMappingURL=aggregation.js.map