1.0.1 • Published 5 years ago

databoi v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
5 years ago

Goal

Databoi strives to be a no-dependency, in-memory data wrangler with a small and intuitive API. It's useful if you have a small to medium (thousands or tens of thousands) records returned from a database, and you'd like to massage and wrangle and hustle with the data without having to perform additional database queries.

Concepts

You use Databoi to build a Frame object from any data source of tidy data; typically from a result set returned from a database query. The Frame object can then be manipulated and queried further using aggregate, selection, sorting, and filtering operations. Aggregate functions return one value. All other operations return a new Frame object. This allows operations to be chained.

Databoi works best with tidy data sets. Tidy data is data that is arranged such that each row represents one sample, and each column represents one variable. In Databoi, we call a column a field.

Install

npm install databoi

Building the Frame object

The Frame object, once built, is immutable. You build a Frame object with a Builder.

// build by adding one object at a time
const frame = new Builder()
  .addRow({ name: "foo", size: 10 })
  .addRow({ name: "bar", size: 30 })
  .build()  

// or build from a given array of objects
const data = [
    { name: "alice", age: 20, height: 170 },
    { name: "bob", age: 30, height: 180 },
    { name: "charlie", age: 40, height: 175 }    
]

const w = new Builder(data).build()

Aggregate functions

Aggregate functions return a single value calculated from applying an aggregate function to all rows that have a value for the given field.

const sum = f.sum("age") // sum = 90
const max = f.max("height") // max = 180
const avg = f.avg("age") // avg = 30
const median = f.median("height") // median = 175

Filtering

The where function is used to filter out rows based on a condition. The where function returns a new Frame object.

f.where("age", ">=", 30).print()

Grouping

The group function combines grouping and aggregation. It groups data by given field, and then it applies an aggregate function to every item in each group. The resulting Frame has one Row per group.

const gameData = [
    { player: "eva", points: 80 },
    { player: "eva", points: 10 },
    { player: "eva", points: 50 },
    { player: "bob", points: 90 },
    { player: "joe", points: 20 },
] 
new Builder(gameData)
            .build()
            .print("Player stats")
            .group("player", "sum", "points")
            .print("Total points per player")
     
Player stats
┌─────────┬────────┬────────┐
│ (index) │ player │ points │
├─────────┼────────┼────────┤
│    0    │ 'eva'  │   80   │
│    1    │ 'eva'  │   10   │
│    2    │ 'eva'  │   50   │
│    3    │ 'bob'  │   90   │
│    4    │ 'joe'  │   20   │
└─────────┴────────┴────────┘

Total points per player
┌─────────┬────────┬────────────┐
│ (index) │ player │ sum_points │
├─────────┼────────┼────────────┤
│    0    │ 'eva'  │    140     │
│    1    │ 'bob'  │     90     │
│    2    │ 'joe'  │     20     │
└─────────┴────────┴────────────┘

Running tests

npm run test

Author

August Flatby

Show your support

Give this project a ⭐️ if you like this kind of stuff!

1.0.1

5 years ago

1.0.0

5 years ago