@tanstack/table-core 9.0.0-beta.46 → 9.0.0-beta.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/features/column-filtering/columnFilteringFeature.types.d.cts +43 -3
- package/dist/features/column-filtering/columnFilteringFeature.types.d.ts +43 -3
- package/dist/features/column-filtering/columnFilteringFeature.utils.cjs +18 -9
- package/dist/features/column-filtering/columnFilteringFeature.utils.cjs.map +1 -1
- package/dist/features/column-filtering/columnFilteringFeature.utils.d.cts +6 -2
- package/dist/features/column-filtering/columnFilteringFeature.utils.d.ts +6 -2
- package/dist/features/column-filtering/columnFilteringFeature.utils.js +18 -9
- package/dist/features/column-filtering/columnFilteringFeature.utils.js.map +1 -1
- package/dist/features/column-filtering/createFilteredRowModel.cjs +7 -2
- package/dist/features/column-filtering/createFilteredRowModel.cjs.map +1 -1
- package/dist/features/column-filtering/createFilteredRowModel.d.cts +6 -2
- package/dist/features/column-filtering/createFilteredRowModel.d.ts +6 -2
- package/dist/features/column-filtering/createFilteredRowModel.js +7 -2
- package/dist/features/column-filtering/createFilteredRowModel.js.map +1 -1
- package/dist/features/column-grouping/columnGroupingFeature.types.d.cts +41 -3
- package/dist/features/column-grouping/columnGroupingFeature.types.d.ts +41 -3
- package/dist/features/column-grouping/columnGroupingFeature.utils.cjs +17 -3
- package/dist/features/column-grouping/columnGroupingFeature.utils.cjs.map +1 -1
- package/dist/features/column-grouping/columnGroupingFeature.utils.d.cts +5 -0
- package/dist/features/column-grouping/columnGroupingFeature.utils.d.ts +5 -0
- package/dist/features/column-grouping/columnGroupingFeature.utils.js +17 -3
- package/dist/features/column-grouping/columnGroupingFeature.utils.js.map +1 -1
- package/dist/features/column-grouping/createGroupedRowModel.cjs +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.cjs.map +1 -1
- package/dist/features/column-grouping/createGroupedRowModel.d.cts +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.d.ts +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.js +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.js.map +1 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.cjs +3 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.cjs.map +1 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.d.cts +2 -6
- package/dist/features/global-filtering/globalFilteringFeature.utils.d.ts +2 -6
- package/dist/features/global-filtering/globalFilteringFeature.utils.js +3 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.js.map +1 -1
- package/dist/features/row-sorting/createSortedRowModel.cjs +6 -2
- package/dist/features/row-sorting/createSortedRowModel.cjs.map +1 -1
- package/dist/features/row-sorting/createSortedRowModel.d.cts +6 -2
- package/dist/features/row-sorting/createSortedRowModel.d.ts +6 -2
- package/dist/features/row-sorting/createSortedRowModel.js +6 -2
- package/dist/features/row-sorting/createSortedRowModel.js.map +1 -1
- package/dist/features/row-sorting/rowSortingFeature.types.d.cts +31 -2
- package/dist/features/row-sorting/rowSortingFeature.types.d.ts +31 -2
- package/dist/features/row-sorting/rowSortingFeature.utils.cjs +19 -4
- package/dist/features/row-sorting/rowSortingFeature.utils.cjs.map +1 -1
- package/dist/features/row-sorting/rowSortingFeature.utils.js +19 -4
- package/dist/features/row-sorting/rowSortingFeature.utils.js.map +1 -1
- package/dist/fns/aggregationFns.cjs +128 -57
- package/dist/fns/aggregationFns.cjs.map +1 -1
- package/dist/fns/aggregationFns.d.cts +72 -18
- package/dist/fns/aggregationFns.d.ts +72 -18
- package/dist/fns/aggregationFns.js +126 -58
- package/dist/fns/aggregationFns.js.map +1 -1
- package/dist/fns/filterFns.cjs +253 -96
- package/dist/fns/filterFns.cjs.map +1 -1
- package/dist/fns/filterFns.d.cts +126 -94
- package/dist/fns/filterFns.d.ts +126 -94
- package/dist/fns/filterFns.js +246 -97
- package/dist/fns/filterFns.js.map +1 -1
- package/dist/fns/sortFns.cjs +68 -21
- package/dist/fns/sortFns.cjs.map +1 -1
- package/dist/fns/sortFns.d.cts +48 -15
- package/dist/fns/sortFns.d.ts +48 -15
- package/dist/fns/sortFns.js +68 -22
- package/dist/fns/sortFns.js.map +1 -1
- package/dist/helpers/tableFeatures.cjs +5 -4
- package/dist/helpers/tableFeatures.cjs.map +1 -1
- package/dist/helpers/tableFeatures.d.cts +5 -4
- package/dist/helpers/tableFeatures.d.ts +5 -4
- package/dist/helpers/tableFeatures.js +5 -4
- package/dist/helpers/tableFeatures.js.map +1 -1
- package/dist/index.cjs +12 -0
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +4 -4
- package/dist/types/TableFeatures.d.cts +15 -7
- package/dist/types/TableFeatures.d.ts +15 -7
- package/dist/types/type-utils.d.cts +8 -1
- package/dist/types/type-utils.d.ts +8 -1
- package/package.json +1 -1
- package/skills/api-not-found/SKILL.md +1 -1
- package/skills/client-vs-server/SKILL.md +1 -1
- package/skills/column-faceting/SKILL.md +11 -5
- package/skills/column-filtering/SKILL.md +8 -3
- package/skills/column-ordering/SKILL.md +1 -1
- package/skills/column-pinning/SKILL.md +1 -1
- package/skills/column-resizing/SKILL.md +1 -1
- package/skills/column-sizing/SKILL.md +1 -1
- package/skills/column-visibility/SKILL.md +1 -1
- package/skills/core/SKILL.md +1 -1
- package/skills/custom-features/SKILL.md +1 -1
- package/skills/expanding/SKILL.md +1 -1
- package/skills/global-filtering/SKILL.md +3 -3
- package/skills/grouping/SKILL.md +8 -3
- package/skills/migrate-v8-to-v9/SKILL.md +7 -7
- package/skills/pagination/SKILL.md +1 -1
- package/skills/row-pinning/SKILL.md +1 -1
- package/skills/row-selection/SKILL.md +1 -1
- package/skills/sorting/SKILL.md +9 -3
- package/skills/table-features/SKILL.md +16 -13
- package/skills/typescript/SKILL.md +1 -1
- package/src/features/column-filtering/columnFilteringFeature.types.ts +60 -0
- package/src/features/column-filtering/columnFilteringFeature.utils.ts +33 -19
- package/src/features/column-filtering/createFilteredRowModel.ts +13 -3
- package/src/features/column-grouping/columnGroupingFeature.types.ts +63 -8
- package/src/features/column-grouping/columnGroupingFeature.utils.ts +43 -9
- package/src/features/column-grouping/createGroupedRowModel.ts +6 -3
- package/src/features/global-filtering/globalFilteringFeature.utils.ts +8 -0
- package/src/features/row-sorting/createSortedRowModel.ts +6 -2
- package/src/features/row-sorting/rowSortingFeature.types.ts +53 -1
- package/src/features/row-sorting/rowSortingFeature.utils.ts +45 -13
- package/src/fns/aggregationFns.ts +204 -146
- package/src/fns/filterFns.ts +361 -283
- package/src/fns/sortFns.ts +95 -78
- package/src/helpers/tableFeatures.ts +5 -4
- package/src/types/TableFeatures.ts +15 -7
- package/src/types/type-utils.ts +8 -0
|
@@ -1,77 +1,127 @@
|
|
|
1
1
|
|
|
2
2
|
//#region src/fns/aggregationFns.ts
|
|
3
3
|
/**
|
|
4
|
+
* Builds an `AggregationFn` from a value-level reducer plus optional
|
|
5
|
+
* `resolveDataValue` and `fromRows` settings.
|
|
6
|
+
*
|
|
7
|
+
* The `aggregate` reducer receives an array of the group rows' values, each
|
|
8
|
+
* already passed through `resolveDataValue` when one is defined. Keeping
|
|
9
|
+
* normalization in the resolver means a variant of an existing aggregation
|
|
10
|
+
* function only has to swap the resolver, not re-implement the computation.
|
|
11
|
+
*
|
|
12
|
+
* The definition is attached to the returned function, so a variant can be
|
|
13
|
+
* created by spreading a built-in aggregation function and overriding what
|
|
14
|
+
* differs. For example, a `min` that works on date columns:
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const earliest = constructAggregationFn({
|
|
18
|
+
* ...aggregationFn_min,
|
|
19
|
+
* resolveDataValue: (value) =>
|
|
20
|
+
* value instanceof Date ? value.getTime() : value,
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* The built-in `count`, `first`, and `last` aggregation functions are plain
|
|
25
|
+
* row-level functions instead: they read group position or size and never
|
|
26
|
+
* look at every value, so materializing a values array would be wasted work.
|
|
27
|
+
*/
|
|
28
|
+
function constructAggregationFn(def) {
|
|
29
|
+
const aggregationFn = Object.assign((columnId, leafRows, childRows) => {
|
|
30
|
+
const rows = aggregationFn.fromRows === "childRows" ? childRows : leafRows;
|
|
31
|
+
const resolveDataValue = aggregationFn.resolveDataValue;
|
|
32
|
+
const values = new Array(rows.length);
|
|
33
|
+
for (let i = 0; i < rows.length; i++) {
|
|
34
|
+
const value = rows[i].getValue(columnId);
|
|
35
|
+
values[i] = resolveDataValue ? resolveDataValue(value) : value;
|
|
36
|
+
}
|
|
37
|
+
return aggregationFn.aggregate(values, rows, columnId);
|
|
38
|
+
}, def);
|
|
39
|
+
return aggregationFn;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
4
42
|
* Sums numeric child-row values for a grouped column.
|
|
5
43
|
*
|
|
6
44
|
* Non-number values contribute `0`. Child rows are used so nested group totals
|
|
7
45
|
* can reuse already aggregated values.
|
|
8
46
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
47
|
+
const aggregationFn_sum = constructAggregationFn({
|
|
48
|
+
fromRows: "childRows",
|
|
49
|
+
aggregate: (values) => {
|
|
50
|
+
let sumValue = 0;
|
|
51
|
+
for (let i = 0; i < values.length; i++) {
|
|
52
|
+
const nextValue = values[i];
|
|
53
|
+
sumValue += typeof nextValue === "number" ? nextValue : 0;
|
|
54
|
+
}
|
|
55
|
+
return sumValue;
|
|
14
56
|
}
|
|
15
|
-
|
|
16
|
-
}
|
|
57
|
+
});
|
|
17
58
|
/**
|
|
18
59
|
* Finds the minimum numeric child-row value for a grouped column.
|
|
19
60
|
*
|
|
20
61
|
* Nullish and non-number values are ignored. Returns `undefined` when no
|
|
21
62
|
* numeric value is found.
|
|
22
63
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
64
|
+
const aggregationFn_min = constructAggregationFn({
|
|
65
|
+
fromRows: "childRows",
|
|
66
|
+
aggregate: (values) => {
|
|
67
|
+
let minValue;
|
|
68
|
+
for (let i = 0; i < values.length; i++) {
|
|
69
|
+
const value = values[i];
|
|
70
|
+
if (typeof value === "number" && (minValue === void 0 || value < minValue)) minValue = value;
|
|
71
|
+
}
|
|
72
|
+
return minValue;
|
|
28
73
|
}
|
|
29
|
-
|
|
30
|
-
}
|
|
74
|
+
});
|
|
31
75
|
/**
|
|
32
76
|
* Finds the maximum numeric child-row value for a grouped column.
|
|
33
77
|
*
|
|
34
78
|
* Nullish and non-number values are ignored. Returns `undefined` when no
|
|
35
79
|
* numeric value is found.
|
|
36
80
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
81
|
+
const aggregationFn_max = constructAggregationFn({
|
|
82
|
+
fromRows: "childRows",
|
|
83
|
+
aggregate: (values) => {
|
|
84
|
+
let maxValue;
|
|
85
|
+
for (let i = 0; i < values.length; i++) {
|
|
86
|
+
const value = values[i];
|
|
87
|
+
if (typeof value === "number" && (maxValue === void 0 || value > maxValue)) maxValue = value;
|
|
88
|
+
}
|
|
89
|
+
return maxValue;
|
|
42
90
|
}
|
|
43
|
-
|
|
44
|
-
}
|
|
91
|
+
});
|
|
45
92
|
/**
|
|
46
93
|
* Finds the numeric extent for a grouped column.
|
|
47
94
|
*
|
|
48
95
|
* Returns `[min, max]`, where each entry is `undefined` when no numeric value is
|
|
49
96
|
* present.
|
|
50
97
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (minValue
|
|
59
|
-
|
|
98
|
+
const aggregationFn_extent = constructAggregationFn({
|
|
99
|
+
fromRows: "childRows",
|
|
100
|
+
aggregate: (values) => {
|
|
101
|
+
let minValue;
|
|
102
|
+
let maxValue;
|
|
103
|
+
for (let i = 0; i < values.length; i++) {
|
|
104
|
+
const value = values[i];
|
|
105
|
+
if (typeof value === "number") if (minValue === void 0) minValue = maxValue = value;
|
|
106
|
+
else {
|
|
107
|
+
if (minValue > value) minValue = value;
|
|
108
|
+
if (maxValue < value) maxValue = value;
|
|
109
|
+
}
|
|
60
110
|
}
|
|
111
|
+
return [minValue, maxValue];
|
|
61
112
|
}
|
|
62
|
-
|
|
63
|
-
}
|
|
113
|
+
});
|
|
64
114
|
/**
|
|
65
115
|
* Averages numeric leaf-row values for a grouped column.
|
|
66
116
|
*
|
|
67
117
|
* Number-like values are coerced with unary `+`; nullish and non-numeric values
|
|
68
118
|
* are ignored.
|
|
69
119
|
*/
|
|
70
|
-
|
|
120
|
+
const aggregationFn_mean = constructAggregationFn({ aggregate: (values) => {
|
|
71
121
|
let count = 0;
|
|
72
122
|
let sumValue = 0;
|
|
73
|
-
for (let i = 0; i <
|
|
74
|
-
const value =
|
|
123
|
+
for (let i = 0; i < values.length; i++) {
|
|
124
|
+
const value = values[i];
|
|
75
125
|
if (value != null && typeof value === "number") {
|
|
76
126
|
++count;
|
|
77
127
|
sumValue += value;
|
|
@@ -84,58 +134,74 @@ function aggregationFn_mean(columnId, leafRows) {
|
|
|
84
134
|
}
|
|
85
135
|
}
|
|
86
136
|
if (count) return sumValue / count;
|
|
87
|
-
}
|
|
137
|
+
} });
|
|
88
138
|
/**
|
|
89
139
|
* Computes the median of numeric leaf-row values for a grouped column.
|
|
90
140
|
*
|
|
91
141
|
* All values must be numbers. If any value is non-numeric, or no leaf rows are
|
|
92
142
|
* present, the result is `undefined`.
|
|
93
143
|
*/
|
|
94
|
-
|
|
95
|
-
if (!
|
|
96
|
-
|
|
97
|
-
for (let i = 0; i < leafRows.length; i++) {
|
|
98
|
-
const v = leafRows[i].getValue(columnId);
|
|
99
|
-
if (typeof v !== "number") return;
|
|
100
|
-
values[i] = v;
|
|
101
|
-
}
|
|
144
|
+
const aggregationFn_median = constructAggregationFn({ aggregate: (values) => {
|
|
145
|
+
if (!values.length) return;
|
|
146
|
+
for (let i = 0; i < values.length; i++) if (typeof values[i] !== "number") return;
|
|
102
147
|
if (values.length === 1) return values[0];
|
|
103
148
|
const mid = Math.floor(values.length / 2);
|
|
104
149
|
values.sort((a, b) => a - b);
|
|
105
150
|
return values.length % 2 !== 0 ? values[mid] : (values[mid - 1] + values[mid]) / 2;
|
|
106
|
-
}
|
|
151
|
+
} });
|
|
107
152
|
/**
|
|
108
153
|
* Collects unique leaf-row values for a grouped column.
|
|
109
154
|
*
|
|
110
155
|
* Values are compared with JavaScript `Set` semantics.
|
|
111
156
|
*/
|
|
112
|
-
|
|
113
|
-
const set = /* @__PURE__ */ new Set();
|
|
114
|
-
for (let i = 0; i < leafRows.length; i++) set.add(leafRows[i].getValue(columnId));
|
|
115
|
-
return Array.from(set.values());
|
|
116
|
-
}
|
|
157
|
+
const aggregationFn_unique = constructAggregationFn({ aggregate: (values) => Array.from(new Set(values).values()) });
|
|
117
158
|
/**
|
|
118
159
|
* Counts unique leaf-row values for a grouped column.
|
|
119
160
|
*
|
|
120
161
|
* Values are compared with JavaScript `Set` semantics.
|
|
121
162
|
*/
|
|
122
|
-
|
|
123
|
-
const set = /* @__PURE__ */ new Set();
|
|
124
|
-
for (let i = 0; i < leafRows.length; i++) set.add(leafRows[i].getValue(columnId));
|
|
125
|
-
return set.size;
|
|
126
|
-
}
|
|
163
|
+
const aggregationFn_uniqueCount = constructAggregationFn({ aggregate: (values) => new Set(values).size });
|
|
127
164
|
/**
|
|
128
165
|
* Counts the number of leaf rows in the group.
|
|
129
166
|
*
|
|
130
167
|
* The column id is ignored because the result is based only on group size.
|
|
168
|
+
* This is a plain row-level function (not built with
|
|
169
|
+
* `constructAggregationFn`) because it never reads row values.
|
|
131
170
|
*/
|
|
132
171
|
function aggregationFn_count(_columnId, leafRows) {
|
|
133
172
|
return leafRows.length;
|
|
134
173
|
}
|
|
135
174
|
/**
|
|
175
|
+
* Returns the first leaf-row value for a grouped column.
|
|
176
|
+
*
|
|
177
|
+
* This is a plain row-level function (not built with
|
|
178
|
+
* `constructAggregationFn`) because it only reads one positional value.
|
|
179
|
+
*/
|
|
180
|
+
function aggregationFn_first(columnId, leafRows) {
|
|
181
|
+
return leafRows[0]?.getValue(columnId);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Returns the last leaf-row value for a grouped column.
|
|
185
|
+
*
|
|
186
|
+
* This is a plain row-level function (not built with
|
|
187
|
+
* `constructAggregationFn`) because it only reads one positional value.
|
|
188
|
+
*/
|
|
189
|
+
function aggregationFn_last(columnId, leafRows) {
|
|
190
|
+
return leafRows[leafRows.length - 1]?.getValue(columnId);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
136
193
|
* The built-in aggregation function registry.
|
|
137
194
|
*
|
|
138
|
-
*
|
|
195
|
+
* Registering this full object opts out of tree-shaking: every built-in
|
|
196
|
+
* aggregation function ends up in your bundle. Prefer importing the
|
|
197
|
+
* `aggregationFn_*` functions you actually use and registering just those in
|
|
198
|
+
* the `aggregationFns` slot, or passing them directly to the `aggregationFn`
|
|
199
|
+
* column option.
|
|
200
|
+
*
|
|
201
|
+
* @deprecated Import individual `aggregationFn_*` functions instead for a
|
|
202
|
+
* smaller bundle. This export still works and is not going away in v9, but
|
|
203
|
+
* built-in name resolution (including `aggregationFn: 'auto'`) only finds
|
|
204
|
+
* functions you register yourself.
|
|
139
205
|
*/
|
|
140
206
|
const aggregationFns = {
|
|
141
207
|
sum: aggregationFn_sum,
|
|
@@ -146,12 +212,16 @@ const aggregationFns = {
|
|
|
146
212
|
median: aggregationFn_median,
|
|
147
213
|
unique: aggregationFn_unique,
|
|
148
214
|
uniqueCount: aggregationFn_uniqueCount,
|
|
149
|
-
count: aggregationFn_count
|
|
215
|
+
count: aggregationFn_count,
|
|
216
|
+
first: aggregationFn_first,
|
|
217
|
+
last: aggregationFn_last
|
|
150
218
|
};
|
|
151
219
|
|
|
152
220
|
//#endregion
|
|
153
221
|
exports.aggregationFn_count = aggregationFn_count;
|
|
154
222
|
exports.aggregationFn_extent = aggregationFn_extent;
|
|
223
|
+
exports.aggregationFn_first = aggregationFn_first;
|
|
224
|
+
exports.aggregationFn_last = aggregationFn_last;
|
|
155
225
|
exports.aggregationFn_max = aggregationFn_max;
|
|
156
226
|
exports.aggregationFn_mean = aggregationFn_mean;
|
|
157
227
|
exports.aggregationFn_median = aggregationFn_median;
|
|
@@ -160,4 +230,5 @@ exports.aggregationFn_sum = aggregationFn_sum;
|
|
|
160
230
|
exports.aggregationFn_unique = aggregationFn_unique;
|
|
161
231
|
exports.aggregationFn_uniqueCount = aggregationFn_uniqueCount;
|
|
162
232
|
exports.aggregationFns = aggregationFns;
|
|
233
|
+
exports.constructAggregationFn = constructAggregationFn;
|
|
163
234
|
//# sourceMappingURL=aggregationFns.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregationFns.cjs","names":[],"sources":["../../src/fns/aggregationFns.ts"],"sourcesContent":["import type { RowData } from '../types/type-utils'\nimport type { TableFeatures } from '../types/TableFeatures'\nimport type { Row } from '../types/Row'\n\n/**\n * Sums numeric child-row values for a grouped column.\n *\n * Non-number values contribute `0`. Child rows are used so nested group totals\n * can reuse already aggregated values.\n */\nexport function aggregationFn_sum<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n columnId: string,\n _leafRows: Array<Row<TFeatures, TData>>,\n childRows: Array<Row<TFeatures, TData>>,\n) {\n // It's faster to just add the aggregations together instead of\n // process leaf nodes individually\n let sumValue = 0\n for (let i = 0; i < childRows.length; i++) {\n const nextValue = childRows[i]!.getValue(columnId)\n sumValue += typeof nextValue === 'number' ? nextValue : 0\n }\n return sumValue\n}\n\n/**\n * Finds the minimum numeric child-row value for a grouped column.\n *\n * Nullish and non-number values are ignored. Returns `undefined` when no\n * numeric value is found.\n */\nexport function aggregationFn_min<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n columnId: string,\n _leafRows: Array<Row<TFeatures, TData>>,\n childRows: Array<Row<TFeatures, TData>>,\n) {\n let minValue: number | undefined\n\n for (let i = 0; i < childRows.length; i++) {\n const value = childRows[i]!.getValue(columnId)\n\n if (\n typeof value === 'number' &&\n (minValue === undefined || value < minValue)\n ) {\n minValue = value\n }\n }\n\n return minValue\n}\n\n/**\n * Finds the maximum numeric child-row value for a grouped column.\n *\n * Nullish and non-number values are ignored. Returns `undefined` when no\n * numeric value is found.\n */\nexport function aggregationFn_max<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n columnId: string,\n _leafRows: Array<Row<TFeatures, TData>>,\n childRows: Array<Row<TFeatures, TData>>,\n) {\n let maxValue: number | undefined\n\n for (let i = 0; i < childRows.length; i++) {\n const value = childRows[i]!.getValue(columnId)\n if (\n typeof value === 'number' &&\n (maxValue === undefined || value > maxValue)\n ) {\n maxValue = value\n }\n }\n\n return maxValue\n}\n\n/**\n * Finds the numeric extent for a grouped column.\n *\n * Returns `[min, max]`, where each entry is `undefined` when no numeric value is\n * present.\n */\nexport function aggregationFn_extent<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n columnId: string,\n _leafRows: Array<Row<TFeatures, TData>>,\n childRows: Array<Row<TFeatures, TData>>,\n) {\n let minValue: number | undefined\n let maxValue: number | undefined\n\n for (let i = 0; i < childRows.length; i++) {\n const value = childRows[i]!.getValue(columnId)\n if (typeof value === 'number') {\n if (minValue === undefined) {\n minValue = maxValue = value\n } else {\n if (minValue > value) minValue = value\n if (maxValue! < value) maxValue = value\n }\n }\n }\n\n return [minValue, maxValue]\n}\n\n/**\n * Averages numeric leaf-row values for a grouped column.\n *\n * Number-like values are coerced with unary `+`; nullish and non-numeric values\n * are ignored.\n */\nexport function aggregationFn_mean<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n let count = 0\n let sumValue = 0\n\n for (let i = 0; i < leafRows.length; i++) {\n const value = leafRows[i]!.getValue(columnId)\n if (value != null && typeof value === 'number') {\n ++count\n sumValue += value\n } else if (value != null) {\n const numValue = +value\n if (!Number.isNaN(numValue)) {\n ++count\n sumValue += numValue\n }\n }\n }\n\n if (count) return sumValue / count\n\n return\n}\n\n/**\n * Computes the median of numeric leaf-row values for a grouped column.\n *\n * All values must be numbers. If any value is non-numeric, or no leaf rows are\n * present, the result is `undefined`.\n */\nexport function aggregationFn_median<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n if (!leafRows.length) {\n return\n }\n\n const values: Array<number> = new Array(leafRows.length)\n for (let i = 0; i < leafRows.length; i++) {\n const v = leafRows[i]!.getValue(columnId)\n if (typeof v !== 'number') return\n values[i] = v\n }\n\n if (values.length === 1) {\n return values[0]\n }\n\n const mid = Math.floor(values.length / 2)\n values.sort((a, b) => a - b)\n return values.length % 2 !== 0\n ? values[mid]\n : (values[mid - 1]! + values[mid]!) / 2\n}\n\n/**\n * Collects unique leaf-row values for a grouped column.\n *\n * Values are compared with JavaScript `Set` semantics.\n */\nexport function aggregationFn_unique<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n const set = new Set<unknown>()\n for (let i = 0; i < leafRows.length; i++) {\n set.add(leafRows[i]!.getValue(columnId))\n }\n return Array.from(set.values())\n}\n\n/**\n * Counts unique leaf-row values for a grouped column.\n *\n * Values are compared with JavaScript `Set` semantics.\n */\nexport function aggregationFn_uniqueCount<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n const set = new Set<unknown>()\n for (let i = 0; i < leafRows.length; i++) {\n set.add(leafRows[i]!.getValue(columnId))\n }\n return set.size\n}\n\n/**\n * Counts the number of leaf rows in the group.\n *\n * The column id is ignored because the result is based only on group size.\n */\nexport function aggregationFn_count<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(_columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n return leafRows.length\n}\n\n/**\n * The built-in aggregation function registry.\n *\n * Pass this object to grouped row model creation or extend it with custom aggregation functions for grouped columns.\n */\nexport const aggregationFns = {\n sum: aggregationFn_sum,\n min: aggregationFn_min,\n max: aggregationFn_max,\n extent: aggregationFn_extent,\n mean: aggregationFn_mean,\n median: aggregationFn_median,\n unique: aggregationFn_unique,\n uniqueCount: aggregationFn_uniqueCount,\n count: aggregationFn_count,\n}\n\nexport type BuiltInAggregationFn = keyof typeof aggregationFns\n"],"mappings":";;;;;;;;AAUA,SAAgB,kBAId,UACA,WACA,WACA;CAGA,IAAI,WAAW;CACf,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,YAAY,UAAU,EAAE,CAAE,SAAS,QAAQ;EACjD,YAAY,OAAO,cAAc,WAAW,YAAY;CAC1D;CACA,OAAO;AACT;;;;;;;AAQA,SAAgB,kBAId,UACA,WACA,WACA;CACA,IAAI;CAEJ,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,QAAQ,UAAU,EAAE,CAAE,SAAS,QAAQ;EAE7C,IACE,OAAO,UAAU,aAChB,aAAa,UAAa,QAAQ,WAEnC,WAAW;CAEf;CAEA,OAAO;AACT;;;;;;;AAQA,SAAgB,kBAId,UACA,WACA,WACA;CACA,IAAI;CAEJ,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,QAAQ,UAAU,EAAE,CAAE,SAAS,QAAQ;EAC7C,IACE,OAAO,UAAU,aAChB,aAAa,UAAa,QAAQ,WAEnC,WAAW;CAEf;CAEA,OAAO;AACT;;;;;;;AAQA,SAAgB,qBAId,UACA,WACA,WACA;CACA,IAAI;CACJ,IAAI;CAEJ,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,QAAQ,UAAU,EAAE,CAAE,SAAS,QAAQ;EAC7C,IAAI,OAAO,UAAU,UACnB,IAAI,aAAa,QACf,WAAW,WAAW;OACjB;GACL,IAAI,WAAW,OAAO,WAAW;GACjC,IAAI,WAAY,OAAO,WAAW;EACpC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AAwC;CAC1D,MAAM,sBAAM,IAAI,IAAa;CAC7B,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KACnC,IAAI,IAAI,SAAS,EAAE,CAAE,SAAS,QAAQ,CAAC;CAEzC,OAAO,IAAI;AACb;;;;;;AAOA,SAAgB,oBAGd,WAAmB,UAAwC;CAC3D,OAAO,SAAS;AAClB;;;;;;AAOA,MAAa,iBAAiB;CAC5B,KAAK;CACL,KAAK;CACL,KAAK;CACL,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,aAAa;CACb,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"aggregationFns.cjs","names":[],"sources":["../../src/fns/aggregationFns.ts"],"sourcesContent":["import type {\n AggregationFnDef,\n CreatedAggregationFn,\n} from '../features/column-grouping/columnGroupingFeature.types'\nimport type { RowData } from '../types/type-utils'\nimport type { TableFeatures } from '../types/TableFeatures'\nimport type { Row } from '../types/Row'\n\n/**\n * Builds an `AggregationFn` from a value-level reducer plus optional\n * `resolveDataValue` and `fromRows` settings.\n *\n * The `aggregate` reducer receives an array of the group rows' values, each\n * already passed through `resolveDataValue` when one is defined. Keeping\n * normalization in the resolver means a variant of an existing aggregation\n * function only has to swap the resolver, not re-implement the computation.\n *\n * The definition is attached to the returned function, so a variant can be\n * created by spreading a built-in aggregation function and overriding what\n * differs. For example, a `min` that works on date columns:\n *\n * ```ts\n * const earliest = constructAggregationFn({\n * ...aggregationFn_min,\n * resolveDataValue: (value) =>\n * value instanceof Date ? value.getTime() : value,\n * })\n * ```\n *\n * The built-in `count`, `first`, and `last` aggregation functions are plain\n * row-level functions instead: they read group position or size and never\n * look at every value, so materializing a values array would be wasted work.\n */\nexport function constructAggregationFn<\n TFeatures extends TableFeatures = any,\n TData extends RowData = any,\n>(\n def: AggregationFnDef<TFeatures, TData>,\n): CreatedAggregationFn<TFeatures, TData> {\n const aggregationFn: CreatedAggregationFn<TFeatures, TData> = Object.assign(\n <TRowFeatures extends TableFeatures, TRowData extends RowData>(\n columnId: string,\n leafRows: Array<Row<TRowFeatures, TRowData>>,\n childRows: Array<Row<TRowFeatures, TRowData>>,\n ): any => {\n const rows = aggregationFn.fromRows === 'childRows' ? childRows : leafRows\n const resolveDataValue = aggregationFn.resolveDataValue\n const values: Array<any> = new Array(rows.length)\n for (let i = 0; i < rows.length; i++) {\n const value = rows[i]!.getValue(columnId)\n values[i] = resolveDataValue ? resolveDataValue(value) : value\n }\n // The def's reducer is typed against the factory's TFeatures/TData;\n // the caller's row generics are erased at this boundary.\n return aggregationFn.aggregate(values, rows as any, columnId)\n },\n def,\n )\n return aggregationFn\n}\n\n/**\n * Sums numeric child-row values for a grouped column.\n *\n * Non-number values contribute `0`. Child rows are used so nested group totals\n * can reuse already aggregated values.\n */\nexport const aggregationFn_sum = constructAggregationFn({\n fromRows: 'childRows',\n aggregate: (values) => {\n let sumValue = 0\n for (let i = 0; i < values.length; i++) {\n const nextValue = values[i]\n sumValue += typeof nextValue === 'number' ? nextValue : 0\n }\n return sumValue\n },\n})\n\n/**\n * Finds the minimum numeric child-row value for a grouped column.\n *\n * Nullish and non-number values are ignored. Returns `undefined` when no\n * numeric value is found.\n */\nexport const aggregationFn_min = constructAggregationFn({\n fromRows: 'childRows',\n aggregate: (values) => {\n let minValue: number | undefined\n\n for (let i = 0; i < values.length; i++) {\n const value = values[i]\n if (\n typeof value === 'number' &&\n (minValue === undefined || value < minValue)\n ) {\n minValue = value\n }\n }\n\n return minValue\n },\n})\n\n/**\n * Finds the maximum numeric child-row value for a grouped column.\n *\n * Nullish and non-number values are ignored. Returns `undefined` when no\n * numeric value is found.\n */\nexport const aggregationFn_max = constructAggregationFn({\n fromRows: 'childRows',\n aggregate: (values) => {\n let maxValue: number | undefined\n\n for (let i = 0; i < values.length; i++) {\n const value = values[i]\n if (\n typeof value === 'number' &&\n (maxValue === undefined || value > maxValue)\n ) {\n maxValue = value\n }\n }\n\n return maxValue\n },\n})\n\n/**\n * Finds the numeric extent for a grouped column.\n *\n * Returns `[min, max]`, where each entry is `undefined` when no numeric value is\n * present.\n */\nexport const aggregationFn_extent = constructAggregationFn({\n fromRows: 'childRows',\n aggregate: (values) => {\n let minValue: number | undefined\n let maxValue: number | undefined\n\n for (let i = 0; i < values.length; i++) {\n const value = values[i]\n if (typeof value === 'number') {\n if (minValue === undefined) {\n minValue = maxValue = value\n } else {\n if (minValue > value) minValue = value\n if (maxValue! < value) maxValue = value\n }\n }\n }\n\n return [minValue, maxValue]\n },\n})\n\n/**\n * Averages numeric leaf-row values for a grouped column.\n *\n * Number-like values are coerced with unary `+`; nullish and non-numeric values\n * are ignored.\n */\nexport const aggregationFn_mean = constructAggregationFn({\n aggregate: (values) => {\n let count = 0\n let sumValue = 0\n\n for (let i = 0; i < values.length; i++) {\n const value = values[i]\n if (value != null && typeof value === 'number') {\n ++count\n sumValue += value\n } else if (value != null) {\n const numValue = +value\n if (!Number.isNaN(numValue)) {\n ++count\n sumValue += numValue\n }\n }\n }\n\n if (count) return sumValue / count\n\n return\n },\n})\n\n/**\n * Computes the median of numeric leaf-row values for a grouped column.\n *\n * All values must be numbers. If any value is non-numeric, or no leaf rows are\n * present, the result is `undefined`.\n */\nexport const aggregationFn_median = constructAggregationFn({\n aggregate: (values: Array<number>) => {\n if (!values.length) {\n return\n }\n\n for (let i = 0; i < values.length; i++) {\n if (typeof values[i] !== 'number') return\n }\n\n if (values.length === 1) {\n return values[0]\n }\n\n const mid = Math.floor(values.length / 2)\n values.sort((a, b) => a - b)\n return values.length % 2 !== 0\n ? values[mid]\n : (values[mid - 1]! + values[mid]!) / 2\n },\n})\n\n/**\n * Collects unique leaf-row values for a grouped column.\n *\n * Values are compared with JavaScript `Set` semantics.\n */\nexport const aggregationFn_unique = constructAggregationFn({\n aggregate: (values) => Array.from(new Set(values).values()),\n})\n\n/**\n * Counts unique leaf-row values for a grouped column.\n *\n * Values are compared with JavaScript `Set` semantics.\n */\nexport const aggregationFn_uniqueCount = constructAggregationFn({\n aggregate: (values) => new Set(values).size,\n})\n\n/**\n * Counts the number of leaf rows in the group.\n *\n * The column id is ignored because the result is based only on group size.\n * This is a plain row-level function (not built with\n * `constructAggregationFn`) because it never reads row values.\n */\nexport function aggregationFn_count<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(_columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n return leafRows.length\n}\n\n/**\n * Returns the first leaf-row value for a grouped column.\n *\n * This is a plain row-level function (not built with\n * `constructAggregationFn`) because it only reads one positional value.\n */\nexport function aggregationFn_first<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n return leafRows[0]?.getValue(columnId)\n}\n\n/**\n * Returns the last leaf-row value for a grouped column.\n *\n * This is a plain row-level function (not built with\n * `constructAggregationFn`) because it only reads one positional value.\n */\nexport function aggregationFn_last<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(columnId: string, leafRows: Array<Row<TFeatures, TData>>) {\n return leafRows[leafRows.length - 1]?.getValue(columnId)\n}\n\n/**\n * The built-in aggregation function registry.\n *\n * Registering this full object opts out of tree-shaking: every built-in\n * aggregation function ends up in your bundle. Prefer importing the\n * `aggregationFn_*` functions you actually use and registering just those in\n * the `aggregationFns` slot, or passing them directly to the `aggregationFn`\n * column option.\n *\n * @deprecated Import individual `aggregationFn_*` functions instead for a\n * smaller bundle. This export still works and is not going away in v9, but\n * built-in name resolution (including `aggregationFn: 'auto'`) only finds\n * functions you register yourself.\n */\nexport const aggregationFns = {\n sum: aggregationFn_sum,\n min: aggregationFn_min,\n max: aggregationFn_max,\n extent: aggregationFn_extent,\n mean: aggregationFn_mean,\n median: aggregationFn_median,\n unique: aggregationFn_unique,\n uniqueCount: aggregationFn_uniqueCount,\n count: aggregationFn_count,\n first: aggregationFn_first,\n last: aggregationFn_last,\n}\n\nexport type BuiltInAggregationFn = keyof typeof aggregationFns\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,SAAgB,uBA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BAA4B,uBAAuB,EAC9D,YAAY,WAAW,IAAI,IAAI,MAAM,CAAC,CAAC,KACzC,CAAC;;;;;;;;AASD,SAAgB,oBAGd,WAAmB,UAAwC;CAC3D,OAAO,SAAS;AAClB;;;;;;;AAQA,SAAgB,oBAGd,UAAkB,UAAwC;CAC1D,OAAO,SAAS,EAAE,EAAE,SAAS,QAAQ;AACvC;;;;;;;AAQA,SAAgB,mBAGd,UAAkB,UAAwC;CAC1D,OAAO,SAAS,SAAS,SAAS,EAAE,EAAE,SAAS,QAAQ;AACzD;;;;;;;;;;;;;;;AAgBA,MAAa,iBAAiB;CAC5B,KAAK;CACL,KAAK;CACL,KAAK;CACL,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,aAAa;CACb,OAAO;CACP,OAAO;CACP,MAAM;AACR"}
|
|
@@ -1,85 +1,139 @@
|
|
|
1
1
|
import { RowData } from "../types/type-utils.cjs";
|
|
2
|
+
import { AggregationFnDef, CreatedAggregationFn } from "../features/column-grouping/columnGroupingFeature.types.cjs";
|
|
2
3
|
import { Row } from "../types/Row.cjs";
|
|
3
4
|
import { TableFeatures } from "../types/TableFeatures.cjs";
|
|
4
5
|
|
|
5
6
|
//#region src/fns/aggregationFns.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Builds an `AggregationFn` from a value-level reducer plus optional
|
|
9
|
+
* `resolveDataValue` and `fromRows` settings.
|
|
10
|
+
*
|
|
11
|
+
* The `aggregate` reducer receives an array of the group rows' values, each
|
|
12
|
+
* already passed through `resolveDataValue` when one is defined. Keeping
|
|
13
|
+
* normalization in the resolver means a variant of an existing aggregation
|
|
14
|
+
* function only has to swap the resolver, not re-implement the computation.
|
|
15
|
+
*
|
|
16
|
+
* The definition is attached to the returned function, so a variant can be
|
|
17
|
+
* created by spreading a built-in aggregation function and overriding what
|
|
18
|
+
* differs. For example, a `min` that works on date columns:
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* const earliest = constructAggregationFn({
|
|
22
|
+
* ...aggregationFn_min,
|
|
23
|
+
* resolveDataValue: (value) =>
|
|
24
|
+
* value instanceof Date ? value.getTime() : value,
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* The built-in `count`, `first`, and `last` aggregation functions are plain
|
|
29
|
+
* row-level functions instead: they read group position or size and never
|
|
30
|
+
* look at every value, so materializing a values array would be wasted work.
|
|
31
|
+
*/
|
|
32
|
+
declare function constructAggregationFn<TFeatures extends TableFeatures = any, TData extends RowData = any>(def: AggregationFnDef<TFeatures, TData>): CreatedAggregationFn<TFeatures, TData>;
|
|
6
33
|
/**
|
|
7
34
|
* Sums numeric child-row values for a grouped column.
|
|
8
35
|
*
|
|
9
36
|
* Non-number values contribute `0`. Child rows are used so nested group totals
|
|
10
37
|
* can reuse already aggregated values.
|
|
11
38
|
*/
|
|
12
|
-
declare
|
|
39
|
+
declare const aggregationFn_sum: CreatedAggregationFn<any, any>;
|
|
13
40
|
/**
|
|
14
41
|
* Finds the minimum numeric child-row value for a grouped column.
|
|
15
42
|
*
|
|
16
43
|
* Nullish and non-number values are ignored. Returns `undefined` when no
|
|
17
44
|
* numeric value is found.
|
|
18
45
|
*/
|
|
19
|
-
declare
|
|
46
|
+
declare const aggregationFn_min: CreatedAggregationFn<any, any>;
|
|
20
47
|
/**
|
|
21
48
|
* Finds the maximum numeric child-row value for a grouped column.
|
|
22
49
|
*
|
|
23
50
|
* Nullish and non-number values are ignored. Returns `undefined` when no
|
|
24
51
|
* numeric value is found.
|
|
25
52
|
*/
|
|
26
|
-
declare
|
|
53
|
+
declare const aggregationFn_max: CreatedAggregationFn<any, any>;
|
|
27
54
|
/**
|
|
28
55
|
* Finds the numeric extent for a grouped column.
|
|
29
56
|
*
|
|
30
57
|
* Returns `[min, max]`, where each entry is `undefined` when no numeric value is
|
|
31
58
|
* present.
|
|
32
59
|
*/
|
|
33
|
-
declare
|
|
60
|
+
declare const aggregationFn_extent: CreatedAggregationFn<any, any>;
|
|
34
61
|
/**
|
|
35
62
|
* Averages numeric leaf-row values for a grouped column.
|
|
36
63
|
*
|
|
37
64
|
* Number-like values are coerced with unary `+`; nullish and non-numeric values
|
|
38
65
|
* are ignored.
|
|
39
66
|
*/
|
|
40
|
-
declare
|
|
67
|
+
declare const aggregationFn_mean: CreatedAggregationFn<any, any>;
|
|
41
68
|
/**
|
|
42
69
|
* Computes the median of numeric leaf-row values for a grouped column.
|
|
43
70
|
*
|
|
44
71
|
* All values must be numbers. If any value is non-numeric, or no leaf rows are
|
|
45
72
|
* present, the result is `undefined`.
|
|
46
73
|
*/
|
|
47
|
-
declare
|
|
74
|
+
declare const aggregationFn_median: CreatedAggregationFn<any, any>;
|
|
48
75
|
/**
|
|
49
76
|
* Collects unique leaf-row values for a grouped column.
|
|
50
77
|
*
|
|
51
78
|
* Values are compared with JavaScript `Set` semantics.
|
|
52
79
|
*/
|
|
53
|
-
declare
|
|
80
|
+
declare const aggregationFn_unique: CreatedAggregationFn<any, any>;
|
|
54
81
|
/**
|
|
55
82
|
* Counts unique leaf-row values for a grouped column.
|
|
56
83
|
*
|
|
57
84
|
* Values are compared with JavaScript `Set` semantics.
|
|
58
85
|
*/
|
|
59
|
-
declare
|
|
86
|
+
declare const aggregationFn_uniqueCount: CreatedAggregationFn<any, any>;
|
|
60
87
|
/**
|
|
61
88
|
* Counts the number of leaf rows in the group.
|
|
62
89
|
*
|
|
63
90
|
* The column id is ignored because the result is based only on group size.
|
|
91
|
+
* This is a plain row-level function (not built with
|
|
92
|
+
* `constructAggregationFn`) because it never reads row values.
|
|
64
93
|
*/
|
|
65
94
|
declare function aggregationFn_count<TFeatures extends TableFeatures, TData extends RowData>(_columnId: string, leafRows: Array<Row<TFeatures, TData>>): number;
|
|
95
|
+
/**
|
|
96
|
+
* Returns the first leaf-row value for a grouped column.
|
|
97
|
+
*
|
|
98
|
+
* This is a plain row-level function (not built with
|
|
99
|
+
* `constructAggregationFn`) because it only reads one positional value.
|
|
100
|
+
*/
|
|
101
|
+
declare function aggregationFn_first<TFeatures extends TableFeatures, TData extends RowData>(columnId: string, leafRows: Array<Row<TFeatures, TData>>): unknown;
|
|
102
|
+
/**
|
|
103
|
+
* Returns the last leaf-row value for a grouped column.
|
|
104
|
+
*
|
|
105
|
+
* This is a plain row-level function (not built with
|
|
106
|
+
* `constructAggregationFn`) because it only reads one positional value.
|
|
107
|
+
*/
|
|
108
|
+
declare function aggregationFn_last<TFeatures extends TableFeatures, TData extends RowData>(columnId: string, leafRows: Array<Row<TFeatures, TData>>): unknown;
|
|
66
109
|
/**
|
|
67
110
|
* The built-in aggregation function registry.
|
|
68
111
|
*
|
|
69
|
-
*
|
|
112
|
+
* Registering this full object opts out of tree-shaking: every built-in
|
|
113
|
+
* aggregation function ends up in your bundle. Prefer importing the
|
|
114
|
+
* `aggregationFn_*` functions you actually use and registering just those in
|
|
115
|
+
* the `aggregationFns` slot, or passing them directly to the `aggregationFn`
|
|
116
|
+
* column option.
|
|
117
|
+
*
|
|
118
|
+
* @deprecated Import individual `aggregationFn_*` functions instead for a
|
|
119
|
+
* smaller bundle. This export still works and is not going away in v9, but
|
|
120
|
+
* built-in name resolution (including `aggregationFn: 'auto'`) only finds
|
|
121
|
+
* functions you register yourself.
|
|
70
122
|
*/
|
|
71
123
|
declare const aggregationFns: {
|
|
72
|
-
sum:
|
|
73
|
-
min:
|
|
74
|
-
max:
|
|
75
|
-
extent:
|
|
76
|
-
mean:
|
|
77
|
-
median:
|
|
78
|
-
unique:
|
|
79
|
-
uniqueCount:
|
|
124
|
+
sum: CreatedAggregationFn<any, any>;
|
|
125
|
+
min: CreatedAggregationFn<any, any>;
|
|
126
|
+
max: CreatedAggregationFn<any, any>;
|
|
127
|
+
extent: CreatedAggregationFn<any, any>;
|
|
128
|
+
mean: CreatedAggregationFn<any, any>;
|
|
129
|
+
median: CreatedAggregationFn<any, any>;
|
|
130
|
+
unique: CreatedAggregationFn<any, any>;
|
|
131
|
+
uniqueCount: CreatedAggregationFn<any, any>;
|
|
80
132
|
count: typeof aggregationFn_count;
|
|
133
|
+
first: typeof aggregationFn_first;
|
|
134
|
+
last: typeof aggregationFn_last;
|
|
81
135
|
};
|
|
82
136
|
type BuiltInAggregationFn = keyof typeof aggregationFns;
|
|
83
137
|
//#endregion
|
|
84
|
-
export { BuiltInAggregationFn, aggregationFn_count, aggregationFn_extent, aggregationFn_max, aggregationFn_mean, aggregationFn_median, aggregationFn_min, aggregationFn_sum, aggregationFn_unique, aggregationFn_uniqueCount, aggregationFns };
|
|
138
|
+
export { BuiltInAggregationFn, aggregationFn_count, aggregationFn_extent, aggregationFn_first, aggregationFn_last, aggregationFn_max, aggregationFn_mean, aggregationFn_median, aggregationFn_min, aggregationFn_sum, aggregationFn_unique, aggregationFn_uniqueCount, aggregationFns, constructAggregationFn };
|
|
85
139
|
//# sourceMappingURL=aggregationFns.d.cts.map
|
|
@@ -1,85 +1,139 @@
|
|
|
1
1
|
import { RowData } from "../types/type-utils.js";
|
|
2
|
+
import { AggregationFnDef, CreatedAggregationFn } from "../features/column-grouping/columnGroupingFeature.types.js";
|
|
2
3
|
import { Row } from "../types/Row.js";
|
|
3
4
|
import { TableFeatures } from "../types/TableFeatures.js";
|
|
4
5
|
|
|
5
6
|
//#region src/fns/aggregationFns.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Builds an `AggregationFn` from a value-level reducer plus optional
|
|
9
|
+
* `resolveDataValue` and `fromRows` settings.
|
|
10
|
+
*
|
|
11
|
+
* The `aggregate` reducer receives an array of the group rows' values, each
|
|
12
|
+
* already passed through `resolveDataValue` when one is defined. Keeping
|
|
13
|
+
* normalization in the resolver means a variant of an existing aggregation
|
|
14
|
+
* function only has to swap the resolver, not re-implement the computation.
|
|
15
|
+
*
|
|
16
|
+
* The definition is attached to the returned function, so a variant can be
|
|
17
|
+
* created by spreading a built-in aggregation function and overriding what
|
|
18
|
+
* differs. For example, a `min` that works on date columns:
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* const earliest = constructAggregationFn({
|
|
22
|
+
* ...aggregationFn_min,
|
|
23
|
+
* resolveDataValue: (value) =>
|
|
24
|
+
* value instanceof Date ? value.getTime() : value,
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* The built-in `count`, `first`, and `last` aggregation functions are plain
|
|
29
|
+
* row-level functions instead: they read group position or size and never
|
|
30
|
+
* look at every value, so materializing a values array would be wasted work.
|
|
31
|
+
*/
|
|
32
|
+
declare function constructAggregationFn<TFeatures extends TableFeatures = any, TData extends RowData = any>(def: AggregationFnDef<TFeatures, TData>): CreatedAggregationFn<TFeatures, TData>;
|
|
6
33
|
/**
|
|
7
34
|
* Sums numeric child-row values for a grouped column.
|
|
8
35
|
*
|
|
9
36
|
* Non-number values contribute `0`. Child rows are used so nested group totals
|
|
10
37
|
* can reuse already aggregated values.
|
|
11
38
|
*/
|
|
12
|
-
declare
|
|
39
|
+
declare const aggregationFn_sum: CreatedAggregationFn<any, any>;
|
|
13
40
|
/**
|
|
14
41
|
* Finds the minimum numeric child-row value for a grouped column.
|
|
15
42
|
*
|
|
16
43
|
* Nullish and non-number values are ignored. Returns `undefined` when no
|
|
17
44
|
* numeric value is found.
|
|
18
45
|
*/
|
|
19
|
-
declare
|
|
46
|
+
declare const aggregationFn_min: CreatedAggregationFn<any, any>;
|
|
20
47
|
/**
|
|
21
48
|
* Finds the maximum numeric child-row value for a grouped column.
|
|
22
49
|
*
|
|
23
50
|
* Nullish and non-number values are ignored. Returns `undefined` when no
|
|
24
51
|
* numeric value is found.
|
|
25
52
|
*/
|
|
26
|
-
declare
|
|
53
|
+
declare const aggregationFn_max: CreatedAggregationFn<any, any>;
|
|
27
54
|
/**
|
|
28
55
|
* Finds the numeric extent for a grouped column.
|
|
29
56
|
*
|
|
30
57
|
* Returns `[min, max]`, where each entry is `undefined` when no numeric value is
|
|
31
58
|
* present.
|
|
32
59
|
*/
|
|
33
|
-
declare
|
|
60
|
+
declare const aggregationFn_extent: CreatedAggregationFn<any, any>;
|
|
34
61
|
/**
|
|
35
62
|
* Averages numeric leaf-row values for a grouped column.
|
|
36
63
|
*
|
|
37
64
|
* Number-like values are coerced with unary `+`; nullish and non-numeric values
|
|
38
65
|
* are ignored.
|
|
39
66
|
*/
|
|
40
|
-
declare
|
|
67
|
+
declare const aggregationFn_mean: CreatedAggregationFn<any, any>;
|
|
41
68
|
/**
|
|
42
69
|
* Computes the median of numeric leaf-row values for a grouped column.
|
|
43
70
|
*
|
|
44
71
|
* All values must be numbers. If any value is non-numeric, or no leaf rows are
|
|
45
72
|
* present, the result is `undefined`.
|
|
46
73
|
*/
|
|
47
|
-
declare
|
|
74
|
+
declare const aggregationFn_median: CreatedAggregationFn<any, any>;
|
|
48
75
|
/**
|
|
49
76
|
* Collects unique leaf-row values for a grouped column.
|
|
50
77
|
*
|
|
51
78
|
* Values are compared with JavaScript `Set` semantics.
|
|
52
79
|
*/
|
|
53
|
-
declare
|
|
80
|
+
declare const aggregationFn_unique: CreatedAggregationFn<any, any>;
|
|
54
81
|
/**
|
|
55
82
|
* Counts unique leaf-row values for a grouped column.
|
|
56
83
|
*
|
|
57
84
|
* Values are compared with JavaScript `Set` semantics.
|
|
58
85
|
*/
|
|
59
|
-
declare
|
|
86
|
+
declare const aggregationFn_uniqueCount: CreatedAggregationFn<any, any>;
|
|
60
87
|
/**
|
|
61
88
|
* Counts the number of leaf rows in the group.
|
|
62
89
|
*
|
|
63
90
|
* The column id is ignored because the result is based only on group size.
|
|
91
|
+
* This is a plain row-level function (not built with
|
|
92
|
+
* `constructAggregationFn`) because it never reads row values.
|
|
64
93
|
*/
|
|
65
94
|
declare function aggregationFn_count<TFeatures extends TableFeatures, TData extends RowData>(_columnId: string, leafRows: Array<Row<TFeatures, TData>>): number;
|
|
95
|
+
/**
|
|
96
|
+
* Returns the first leaf-row value for a grouped column.
|
|
97
|
+
*
|
|
98
|
+
* This is a plain row-level function (not built with
|
|
99
|
+
* `constructAggregationFn`) because it only reads one positional value.
|
|
100
|
+
*/
|
|
101
|
+
declare function aggregationFn_first<TFeatures extends TableFeatures, TData extends RowData>(columnId: string, leafRows: Array<Row<TFeatures, TData>>): unknown;
|
|
102
|
+
/**
|
|
103
|
+
* Returns the last leaf-row value for a grouped column.
|
|
104
|
+
*
|
|
105
|
+
* This is a plain row-level function (not built with
|
|
106
|
+
* `constructAggregationFn`) because it only reads one positional value.
|
|
107
|
+
*/
|
|
108
|
+
declare function aggregationFn_last<TFeatures extends TableFeatures, TData extends RowData>(columnId: string, leafRows: Array<Row<TFeatures, TData>>): unknown;
|
|
66
109
|
/**
|
|
67
110
|
* The built-in aggregation function registry.
|
|
68
111
|
*
|
|
69
|
-
*
|
|
112
|
+
* Registering this full object opts out of tree-shaking: every built-in
|
|
113
|
+
* aggregation function ends up in your bundle. Prefer importing the
|
|
114
|
+
* `aggregationFn_*` functions you actually use and registering just those in
|
|
115
|
+
* the `aggregationFns` slot, or passing them directly to the `aggregationFn`
|
|
116
|
+
* column option.
|
|
117
|
+
*
|
|
118
|
+
* @deprecated Import individual `aggregationFn_*` functions instead for a
|
|
119
|
+
* smaller bundle. This export still works and is not going away in v9, but
|
|
120
|
+
* built-in name resolution (including `aggregationFn: 'auto'`) only finds
|
|
121
|
+
* functions you register yourself.
|
|
70
122
|
*/
|
|
71
123
|
declare const aggregationFns: {
|
|
72
|
-
sum:
|
|
73
|
-
min:
|
|
74
|
-
max:
|
|
75
|
-
extent:
|
|
76
|
-
mean:
|
|
77
|
-
median:
|
|
78
|
-
unique:
|
|
79
|
-
uniqueCount:
|
|
124
|
+
sum: CreatedAggregationFn<any, any>;
|
|
125
|
+
min: CreatedAggregationFn<any, any>;
|
|
126
|
+
max: CreatedAggregationFn<any, any>;
|
|
127
|
+
extent: CreatedAggregationFn<any, any>;
|
|
128
|
+
mean: CreatedAggregationFn<any, any>;
|
|
129
|
+
median: CreatedAggregationFn<any, any>;
|
|
130
|
+
unique: CreatedAggregationFn<any, any>;
|
|
131
|
+
uniqueCount: CreatedAggregationFn<any, any>;
|
|
80
132
|
count: typeof aggregationFn_count;
|
|
133
|
+
first: typeof aggregationFn_first;
|
|
134
|
+
last: typeof aggregationFn_last;
|
|
81
135
|
};
|
|
82
136
|
type BuiltInAggregationFn = keyof typeof aggregationFns;
|
|
83
137
|
//#endregion
|
|
84
|
-
export { BuiltInAggregationFn, aggregationFn_count, aggregationFn_extent, aggregationFn_max, aggregationFn_mean, aggregationFn_median, aggregationFn_min, aggregationFn_sum, aggregationFn_unique, aggregationFn_uniqueCount, aggregationFns };
|
|
138
|
+
export { BuiltInAggregationFn, aggregationFn_count, aggregationFn_extent, aggregationFn_first, aggregationFn_last, aggregationFn_max, aggregationFn_mean, aggregationFn_median, aggregationFn_min, aggregationFn_sum, aggregationFn_unique, aggregationFn_uniqueCount, aggregationFns, constructAggregationFn };
|
|
85
139
|
//# sourceMappingURL=aggregationFns.d.ts.map
|