1.2.0 • Published 2 years ago

groupjs_by v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

groupjs_by 🚀

Publish

groupjs_by is a javascript library for grouping array of objects using attribute name , it support basic operations such as sum, avg, max, min and countDistinct. it support chaning more than one operation.

Installation

Use the package manager to install groupjs_by.

Usage

sum(alias,columnName)

typeParametersdescription
stringaliasalias for the agreggation sum
stringcolumnNamename of the column to perform the aggregation

example using data :

const data = [
  { category: 'mammals', name: 'lion', weight: 80 },
  { category: 'mammals', name : 'panter', weight: 100 },
  { category : 'reptiles', name : 'cocodrile', weight: 100}
]
const group = require('groupjs_by');
group
.groupBy(data,'category')
.sum('animalsWeight','weight')
.data;

// will return
{
    mammals:{
        items :  [
            { category: 'mammals', name: 'lion', weight: 80 },
            { category: 'mammals', name : 'panter', weight: 100 }
        ],
        animalsWeight: 180
    },
    reptiles: {
        items : [
            { category : 'reptiles', name : cocodrile, weight: 100}
        ],
        animalsWeight: 100
    }
}

min(alias,columnName)

OperationParametersdescription
stringaliasalias for the agreggation min
stringcolumnNamename of the column to get the min value

example :

const group = require('groupjs_by');
group.groupBy(data,'category')
.min('minWeight','weight')
.data;

// will return

{
    mammals:{
        items :  [
            { category: 'mammals', name: 'lion', weight: 80 },
            { category: 'mammals', name : 'panter', weight: 100 }
        ]
        minWeight: 80
    },
    reptiles: {
        items : [
            { category : 'reptiles', name : cocodrile, weight: 100}
        ],
        minWeight: 100
    }
}

max(alias,ColumnName)

OperationParametersdescription
stringaliasalias for the agreggation max
stringcolumnNamename of the column to get the max value

example :

const group = require('groupjs_by');
group.groupBy(data,'category')
.max('maxWeight','weight')
.data;

// will return

{
    mammals:{
        items :  [
            { category: 'mammals', name: 'lion', weight: 80 },
            { category: 'mammals', name : 'panter', weight: 100 }
        ]
        maxWeight: 100
    },
    reptiles: {
        items : [
            { category : 'reptiles', name : cocodrile, weight: 100}
        ],
        maxWeight: 100
    }
}

avg(alias,columnName,decimals)

OperationParametersdescription
stringaliasalias for the agreggation avg
stringcolumnNamename of the column to get to perform the avg
intdecimalsThe number of decimals to format the float avg value, by default 2

example :

const group = require('groupjs_by');
group
.groupBy(data,'category')
.avg('avgWeight','weight').data;

// will return

{
    mammals:{
        items :  [
            { category: 'mammals', name: 'lion', weight: 80 },
            { category: 'mammals', name : 'panter', weight: 100 }
        ]
        avgWeight: '90.00'
    },
    reptiles: {
        items : [
            { category : 'reptiles', name : cocodrile, weight: 100}
        ],
        avgWeight: '100.00'
    }
}

distinctCount(alias,columnName)

OperationParametersdescription
stringaliasalias for the agreggation distinctCount
stringcolumnNamename of the column to get to perform counting

example :

const group = require('groupjs_by');
group
.groupBy(data,'category')
.distinctCount('distinctAnimals','name')
.data

// will return

{
    mammals:{
        items :  [
            { category: 'mammals', name: 'lion', weight: 80 },
            { category: 'mammals', name : 'panter', weight: 100 }
        ]
        distinctAnimals: 2
    },
    reptiles: {
        items : [
            { category : 'reptiles', name : cocodrile, weight: 100}
        ],
        distinctAnimals: 1
    }
}

Chaining Operations

Chaining operations are supported , it´s possible to chain more than one operation

example :

const group = require('groupjs_by');
const chainedResult =
group
.groupBy(data,'category')
.distinctCount('distinctAnimals','name')
.avg('avgWeight','weight')
.sum('animalsWeight','weight').data

// will return
{
    mammals:{
        items :  [
            { category: 'mammals', name: 'lion', weight: 80 },
            { category: 'mammals', name : 'panter', weight: 100 }
        ]
        distinctAnimals: 2,
        avgWeight: '90.00',
        animalsWeight: 180
    },
    reptiles: {
        items : [
            { category : 'reptiles', name : cocodrile, weight: 100}
        ],
        distinctAnimals: 1,
        avgWeight: '100.00',
        animalsWeight: 100
    }
}

keys

To retrieve the list of keys of which the object is grouped

const group = require('groupjs_by');
group
.groupBy(data,'category')
.keys

// will return an array of keys of grouped data.
 ex:
 ['mammals','reptiles']

firstGroup

Returns the first group of aggregate data , the order its determinaded by the order of data was provided.

const group = require('groupjs_by');
group
.groupBy(data,'category')
.firstGroup

will return

 ex:
 [
     { category: 'mammals', name: 'lion', weight: 80 },
     { category: 'mammals', name : 'panter', weight: 100 }
 ]

lastGroup

Returns the last group of aggregate data , the order its determinaded by the order of data was provided.

const group = require('groupjs_by');
group
.groupBy(data,'category')
.lastGroup

will return

 ex:
 [
     { category : 'reptiles', name : cocodrile, weight: 100}
 ]
1.2.0

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago