import { exec, map, aggregatesCombinator, expandAggregates, AggregateResult } from '../transducers'; import { Combinator, Transformer, Preprocessors } from '../common.interfaces'; /** * The aggregate operation. * * For more information, refer to the [`aggregateBy`]({% slug api_kendo-data-query_aggregateby %}) method. */ export interface AggregateDescriptor { /** * The name of the record field on which the function will be executed. */ field: string; /** * The aggregate function that will be calculated. */ aggregate: 'count' | 'sum' | 'average' | 'min' | 'max' | 'count_distinct'; } const identity = map(x => x); // tslint:disable:max-line-length /** * Applies the specified [`AggregateDescriptors`]({% slug api_kendo-data-query_aggregatedescriptor %}) to the data. Returns an [`AggregateResult`]({% slug api_kendo-data-query_aggregateresult %}) instance. * * @example * ```ts-no-run * const data = [ * { unitPrice: 23, unitsInStock: 21 }, * { unitPrice: 10, unitsInStock: 12 }, * { unitPrice: 20, unitsInStock: 33 } * ]; * * const result = aggregateBy(data, [ * { aggregate: "sum", field: "unitPrice" }, * { aggregate: "sum", field: "unitsInStock" } * ]); * * //output: * // { * // "unitPrice": { "sum": 53 }, * // "unitsInStock": { "sum": 66 } * // } * ``` * @param {T[]} data - The data on which the calculation will be executed. * @param {AggregateDescriptor[]} descriptors - The aggregate operations that will be executed. * @param {any} transformers - For internal use. * @returns {AggregateResult} - The aggregated result. * For more information, refer to the [`aggregateresult`]({% slug api_kendo-data-query_aggregateresult %}) configuration. */ // tslint:enable:max-line-length export const aggregateBy = (data: T[], descriptors: AggregateDescriptor[] = [], preprocessors: Preprocessors = {}, transformers: (reduce: Combinator) => Transformer = identity): AggregateResult => { const initialValue = {}; if (!descriptors.length) { return initialValue; } const result = exec(transformers(aggregatesCombinator(descriptors, preprocessors)), initialValue, data); return expandAggregates(result); };