Skip to content

Strongly-typed data structure and utilities for tabular data

License

Notifications You must be signed in to change notification settings

mshafir/typed-data-table

 
 

Repository files navigation

Typed Data Table

GitHub license PRs Welcome Build

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.

Live example

Documentation

GitHub

Installation

npm install typed-data-table

Usage

// 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 }
]);

Add/Remove Column

you can define new columns and rename or remove columns with chaining the value functions are typesafe, knowing about previous transformations and preserving the type information of the objects in each row

const updatedInventory = inventory
  .withColumn("totalValue", (r) => r.price * r.inventory)
  .renameColumn("inventory", "amountInStock");

Joining

You can join tables together in memory, again preserving type-assist for the resulting data structure

// 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);

Grouping

you can group the data as well, specifying the columns and how they're calculated over the groups of rows

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");

Rolling

you can compute rolling windows over the data

purchases
  .sortValues(['timestamp'], true)
  .rolling(3)
  .aggregate(window => ({
      timestamp: window.last().timestamp,
      purchases: window.size(),
      amountPurchased: window.sum('amount')
  }))

About

Strongly-typed data structure and utilities for tabular data

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.7%
  • JavaScript 1.3%