UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

161 lines (160 loc) • 5.36 kB
/** * DevExtreme (esm/__internal/grids/data_grid/m_aggregate_calculator.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { errors } from "../../../common/data/errors"; import { aggregators } from "../../../common/data/utils"; import { compileGetter } from "../../../core/utils/data"; import { isFunction } from "../../../core/utils/type"; const depthFirstSearch = (i, depth, root, callback) => { let j = 0; if (i < depth) { for (; j < root.items.length; j += 1) { depthFirstSearch(i + 1, depth, root.items[j], callback) } } if (i === depth) { callback(root) } }; const isEmpty = x => Number.isNaN(x) || "" === x || null === x || void 0 === x; const isCount = aggregator => aggregator === aggregators.count; const normalizeAggregate = aggregate => { const selector = compileGetter(aggregate.selector); const skipEmptyValues = "skipEmptyValues" in aggregate ? aggregate.skipEmptyValues : true; let { aggregator: aggregator } = aggregate; if ("string" === typeof aggregator) { aggregator = aggregators[aggregator]; if (!aggregator) { throw errors.Error("E4001", aggregate.aggregator) } } return { selector: selector, aggregator: aggregator, skipEmptyValues: skipEmptyValues } }; export default class AggregateCalculator { constructor(options) { this._data = options.data; this._groupLevel = options.groupLevel; this._totalAggregates = options.totalAggregates.map(normalizeAggregate); this._groupAggregates = options.groupAggregates.map(normalizeAggregate); this._totals = [] } calculate() { if (this._totalAggregates.length) { this._calculateTotals(0, { items: this._data }) } if (this._groupAggregates.length && this._groupLevel > 0) { this._calculateGroups({ items: this._data }) } } totalAggregates() { return this._totals } _aggregate(aggregates, data, container) { const length = data.items ? data.items.length : 0; for (let i = 0; i < aggregates.length; i += 1) { if (isCount(aggregates[i].aggregator)) { container[i] = (container[i] ?? 0) + length; continue } for (let j = 0; j < length; j += 1) { this._accumulate(i, aggregates[i], container, data.items[j]) } } } _calculateTotals(level, root) { if (0 === level) { this._totals = this._seed(this._totalAggregates) } if (level === this._groupLevel) { this._aggregate(this._totalAggregates, root, this._totals) } else { for (const item of root.items) { this._calculateTotals(level + 1, item) } } if (0 === level) { this._totals = this._finalize(this._totalAggregates, this._totals) } } _calculateGroups(root) { const maxLevel = this._groupLevel; let currentLevel = maxLevel + 1; const seedFn = this._seed.bind(this, this._groupAggregates); const stepFn = this._aggregate.bind(this, this._groupAggregates); const finalizeFn = this._finalize.bind(this, this._groupAggregates); const aggregator = node => { const aggregates = seedFn(currentLevel - 1); node.aggregates = aggregates; if (currentLevel === maxLevel) { stepFn(node, aggregates) } else { depthFirstSearch(currentLevel, maxLevel, node, innerNode => { stepFn(innerNode, aggregates) }) } node.aggregates = finalizeFn(aggregates) }; currentLevel -= 1; while (currentLevel > 0) { depthFirstSearch(0, currentLevel, root, aggregator); currentLevel -= 1 } } _seed(aggregates, groupIndex) { return aggregates.map(aggregate => { const { aggregator: aggregator } = aggregate; if (!("seed" in aggregator)) { return NaN } return isFunction(aggregator.seed) ? aggregator.seed(groupIndex) : aggregator.seed }) } _accumulate(aggregateIndex, aggregate, results, item) { const value = aggregate.selector(item); const { aggregator: aggregator } = aggregate; const { skipEmptyValues: skipEmptyValues } = aggregate; if (skipEmptyValues && isEmpty(value)) { return } if (Number.isNaN(results[aggregateIndex])) { results[aggregateIndex] = value } else { results[aggregateIndex] = aggregator.step(results[aggregateIndex], value) } } _finalize(aggregates, results) { return aggregates.map((aggregate, index) => { const fin = aggregate.aggregator.finalize; return fin ? fin(results[index]) : results[index] }) } }