4.0.0 • Published 3 years ago

simple-pivot-data v4.0.0

Weekly downloads
17
License
MIT
Repository
github
Last release
3 years ago

Simple Pivot

This module performs simple pivot operations over an array of objects, to aggregate the data from one field using the values from another field and applying the aggregation function desired.

You always need to provide:

  • An array of objects
  • A configuration object including: groupField, valueField and pivotFunction (optional)

Let me give you an example. Lets say you have a transaction list with the following items:

const transactions = [
    {
      date: '2020-01-01',
      amount: 10,
      category: 'Food'
    },
    {
      date: '2020-01-02',
      amount: 20,
      category: 'Food'
    },
    {
      date: '2020-01-03',
      amount: 20,
      category: 'Movies'
    }
]

You can use the module to aggregate the transactions by a field (ie. amount by category):

const simplePivot = require('simple-pivot-data')
simplePivot(transactions, {groupField: 'category', valueField: 'amount'})
[
  { category: 'Food', amount: [ 10, 20 ], pivotFunction: undefined },
  { category: 'Movies', amount: [ 20 ], pivotFunction: undefined }
]

or sum them:

simplePivot(transactions, {groupField: 'category', valueField: 'amount', pivotFunction: 'sum'})
[
  { category: 'Food', amount: 30, pivotFunction: 'sum' },
  { category: 'Movies', amount: 20, pivotFunction: 'sum' }

or find the average:

simplePivot(transactions, {groupField: 'category', valueField: 'amount', pivotFunction: 'average'})
[
  { category: 'Food', amount: 15, pivotFunction: 'avg' },
  { category: 'Movies', amount: 20, pivotFunction: 'avg' }
]

You can check in the pivot.js file all the aggregation functions supported.

Running tests

You can run the tests and check the functionality of this module using:

$ npm install simple-pivot-data && npm test

License

Copyright © 2020, Juan Convers. Released under the MIT License.

4.0.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago