2.1.3 • Published 5 years ago
aggregate-by-date v2.1.3
aggregate-by-date
Aggregate collections by date and supply a custom aggregation function.
Installation
$ npm install aggregate-by-date --save
API
aggregateByDate(data: [], options: {}, properties: []): [{}]
Main function to aggregate a collection.
Parameter | Type | Description |
---|---|---|
data | [{}] | Collection of objects |
options: {property: String, time: String, name: String}
Options object for the aggregation.
Parameter | Type | Description |
---|---|---|
property | String | Name of the property inside data to aggregate |
time | String | Format of the property inside data, see possibilities |
name | String | Name of the aggregated property |
properties: [{property: String, fn: Function}]
Array collection of objects for properties inside data.
Parameter | Type | Description |
---|---|---|
property | String | Name of the property inside data |
fn | Function | Aggregation function to apply, e. g. max from lodash |
Usage
There are three steps necessary. The first is to import the library and setup the data.
import aggregateByDate from 'aggregate-by-date'
const data = [{
date: '2000-01-01',
firstValue: 50,
secondValue: 55,
thirdValue: true
}, {
date: '2000-01-02',
firstValue: 23,
secondValue: 57,
thirdValue: true
}, {
date: '2000-01-03',
firstValue: 11,
secondValue: 51,
thirdValue: false
}, {
date: '2000-02-01',
firstValue: 62,
secondValue: 68,
thirdValue: false
}, {
date: '2001-01-01',
firstValue: 40,
secondValue: 63,
thirdValue: true
}, {
date: '2001-01-01',
firstValue: 44,
secondValue: 54,
thirdValue: true
}, {
date: '2001-02-01',
firstValue: 41,
secondValue: 57,
thirdValue: true
}]
Now define the property the library should take to do the aggregation. Also define the schema and the name of the resulting property.
const options = {
property: 'date',
time: 'YYYY/MM',
name: 'month'
}
Finally, choose the properties to apply a function you supply and get the result.
const properties = [{
property: 'firstValue',
fn: _.max // By lodash
}, {
property: 'secondValue',
fn: _.min // By lodash
}]
const result = aggregateByDate(data, options, properties)
Author
Julian Claus and contributors.
License
MIT
2.1.3
5 years ago
2.1.2
5 years ago
2.1.1
5 years ago
2.1.0
5 years ago
2.0.10
5 years ago
2.0.9
6 years ago
2.0.8
6 years ago
2.0.7
6 years ago
2.0.6
6 years ago
2.0.5
6 years ago
2.0.4
6 years ago
2.0.3
7 years ago
2.0.2-n
7 years ago
2.0.1
7 years ago
2.0.0
7 years ago
1.0.4
7 years ago
1.0.3
7 years ago
1.0.1
7 years ago
1.0.0
7 years ago