A strongly typed data structure for tabular data
npm install typed-data-table
Typed data table is a typescript node project aiming to provide a type-safe way to interact with tabular data and a basic set of useful primitives.
npm install typed-data-table
``ts
// Let's set up some tables to work with
let inventory = new Table([
{ item: "Cheese", price: 4.25, inventory: 10, category: "Dairy" },
{ item: "Milk", price: 2.25, inventory: 10, category: "Dairy" },
{ item: "Tomato", price: 2.0, inventory: 20, category: "Produce" },
{ item: "Cucumber", price: 1.0, inventory: 15, category: "Produce" },
{ item: "Apple", price: 1.5, inventory: 40, category: "Produce" }
]);
let purchases = new Table([
{ item: "Cheese", customer: 1, amount: 2 },
{ item: "Tomato", customer: 1, amount: 1 },
{ item: "Apple", customer: 2, amount: 3 },
{ item: "Milk", customer: 3, amount: 1 }
]);
`
ts
const updatedInventory = inventory
.withColumn("totalValue", (r) => r.price * r.inventory)
.renameColumn("inventory", "amountInStock");
`$3
You can join tables together in memory, again preserving type-assist for the resulting data structure
`ts
// you can do type-safe joins
const joinedPurchases = purchases
.innerJoin(
inventory,
(r) => r.item,
(r) => r.item,
(left, right) => ({
...left,
...right
})
)
.withColumn("cost", (r) => r.amount * r.price);
`$3
you can group the data as well, specifying the columns and how they're calculated over the groups of rows`ts
const groupedByCustomer = joinedPurchases
.groupBy((r) => r.customer)
.aggregateByColumn({
// makes a column 'total' that is the sum of the cost column for each group
total: (rows) => sum(rows.map((r) => r.cost)),
// makes a column 'items' that is the sum of the amount column for each group
items: (rows) => sum(rows.map((r) => r.amount))
})
// even the strings in this func will auto-complete, aggregating a group returns a table with the group key
// as the 'id' column, you can rename this back to customer if desired.
.renameColumn("id", "customer");
`$3
you can compute rolling windows over the data`ts
purchases
.sortValues(['timestamp'], true)
.rolling(3)
.aggregate(window => ({
timestamp: window.last().timestamp,
purchases: window.size(),
amountPurchased: window.sum('amount')
}))
``