0.8.9 • Published 2 days ago
@starbeamx/store v0.8.9
Aggregating and Grouping
Grouping turns a list of rows into a map of lists of rows based on some grouping criteria.
const people = Table.define<Person>({ columns: ["name", "location"] });
people.append({ name: "John", location: "Berlin" });
people.append({ name: "Jane", location: "London" });
people.append({ name: "Jill", location: "Berlin" });
const byLocation = people.groupBy({
group: (row) => row.location,
});
byLocation.get("Berlin").length; // 2
byLocation.get("London").length; // 1
byLocation.get("Berlin").rows;
// [
// { name: "John", location: "Berlin" },
// { name: "Jill", location: "Berlin" }
// ]
Aggregating turns a list of rows into a smaller list of rows where some of the columns have been aggregated.
const transactions = Table.define<Transaction>({
columns: ["amount", "type", "category", "date"],
});
transactions.append({
amount: 100,
type: "expense",
category: "food",
date: "2022-01-01",
});
transactions.append({
amount: 200,
type: "expense",
category: "food",
date: "2022-02-12",
});
transactions.append({
amount: 50,
type: "expense",
category: "gas",
date: "2022-02-07",
});
transactions.append({
amount: -100,
type: "refund",
category: "food",
date: "2022-02-10",
});
transactions.append({
amount: 400,
type: "income",
category: "paycheck",
date: "2022-02-14",
});
const byMonth = transactions.groupBy({
group: (row) => row.date.substring(0, 7),
});
byMonth.groups;
// Map<{
// "2022-01": [
// { amount: 100, type: "expense", category: "food", date: "2022-01-01" },
// ],
// "2022-02": [
// { amount: 200, type: "expense", category: "food", date: "2022-02-12" },
// { amount: 50, type: "expense", category: "gas", date: "2022-02-07" },
// { amount: -100, type: "refund", category: "food", date: "2022-02-10" },
// { amount: 200, type: "expense", category: "food", date: "2022-02-12" },
// { amount: 400, type: "income", category: "paycheck", date: "2022-02-14" },
// ],
// }>
byMonth.rows({ group: "month" });
const byCategory = byMonth.groupBy({
group: (row) => row.category,
});
byCategory.groups;
// Map<{
// "2022-01": Map<{
// "food": [
// { amount: 100, type: "expense", category: "food", date: "2022-01-01" },
// ],
// }>,
// "2022-02": Map<{
// "food": [
// { amount: 200, type: "expense", category: "food", date: "2022-02-12" },
// { amount: -100, type: "refund", category: "food", date: "2022-02-10" },
// ],
// "gas": [
// { amount: 50, type: "expense", category: "gas", date: "2022-02-07" },
// ],
// "paycheck": [
// { amount: 400, type: "income", category: "paycheck", date: "2022-02-14" },
// ],
// }>,
// }>
const SumAggregator = {
aggregate: (rows) => rows.reduce((acc, row) => acc + row.amount, 0),
description: (column) => `${column} (sum)`,
};
const byMonthExpenses = byMonth
.groupBy({ group: (row) => row.category })
.omit("type")
.aggregate({
amount: SumAggregator,
});
byMonthExpenses.rows;
// Map<{
// "2022-01": [
// { category: "food", amount: 100 },
// ],
// "2022-02": [
// { category: "food", amount: 100 },
// { category: "gas", amount: 50 },
// { category: "paycheck", amount: 400 },
// ],
// }>
0.8.10-unstable.9b17989
2 days ago
0.8.10-unstable.229fe05
2 days ago
0.8.10-unstable.3b6efdd
14 days ago
0.8.10-unstable.bca55c9
3 months ago
0.8.10-unstable.9b5a07d
4 months ago
0.8.10-unstable.d342d90
5 months ago
0.8.10-unstable.2739f1d
5 months ago
0.8.10-unstable.3e1af4d
5 months ago
0.8.10-unstable.0b31af0
5 months ago
0.8.10-unstable.0ad7df5
5 months ago
0.8.10-unstable.4247abc
5 months ago
0.8.10-unstable.398c1bd
9 months ago
0.8.10-unstable.9203102
9 months ago
0.8.10-unstable.aeb2ad9
8 months ago
0.8.10-unstable.5912ae7
8 months ago
0.8.10-unstable.2e56437
7 months ago
0.8.10-unstable.c41f68b
8 months ago
0.8.10-unstable.84e5619
7 months ago
0.8.10-unstable.6ff9a7b
8 months ago
0.8.10-unstable.d5a7b81
8 months ago
0.8.10-unstable.4198c97
7 months ago
0.8.10-unstable.7ee43a9
8 months ago
0.8.10-unstable.3ead76a
8 months ago
0.8.10-unstable.35f0b99
8 months ago
0.8.9
1 year ago
0.8.8
1 year ago
0.8.7
1 year ago
0.8.5
2 years ago
0.8.4
2 years ago
0.8.6
2 years ago
0.8.1
2 years ago
0.8.0
2 years ago
0.7.2
2 years ago
0.7.1
2 years ago
0.7.4
2 years ago
0.7.3
2 years ago
0.7.0
2 years ago
0.6.0
2 years ago
0.5.8
2 years ago
0.5.7
2 years ago
0.5.6
2 years ago
0.5.5
2 years ago
0.5.4
2 years ago
0.5.3
2 years ago
0.5.2
2 years ago
0.5.1
2 years ago
0.5.0
2 years ago