array-prototype-functions
Advanced tools
Comparing version 2.6.1 to 2.7.0-beta.0
@@ -13,3 +13,12 @@ import './blank-module'; | ||
}; | ||
/** | ||
* Returns an object with keys having lists of objects | ||
* containing the given field value | ||
* | ||
* @param field field to group by | ||
*/ | ||
groupBy(mapper: (item: T) => string): { | ||
[key: string]: T[]; | ||
}; | ||
} | ||
} |
import './blank-module'; | ||
if (Array.prototype.groupBy === undefined) { | ||
Array.prototype.groupBy = function (field) { | ||
if (!field) { | ||
Array.prototype.groupBy = function (fieldOrMapper) { | ||
if (!fieldOrMapper) { | ||
throw new Error('Need a field to group by'); | ||
} | ||
return this.reduce((acc, item) => { | ||
if (acc[item[field]]) | ||
acc[item[field]].push(item); | ||
else | ||
acc[item[field]] = [item]; | ||
return acc; | ||
}, {}); | ||
const result = {}; | ||
this.forEach(item => { | ||
const value = typeof fieldOrMapper === 'function' ? fieldOrMapper(item) : item[fieldOrMapper]; | ||
if (value !== undefined) { | ||
if (result[value] === undefined) | ||
result[value] = []; | ||
result[value].push(item); | ||
} | ||
}); | ||
return result; | ||
}; | ||
} |
{ | ||
"name": "array-prototype-functions", | ||
"version": "2.6.1", | ||
"version": "2.7.0-beta.0", | ||
"description": "Array prototype augmentation for easier arithmetics and overall pleasure!", | ||
@@ -9,3 +9,4 @@ "scripts": { | ||
"test:unit:watch": "jest --watch", | ||
"build": "tsc" | ||
"build": "tsc", | ||
"prepublish": "npm install && npm run build && npm run test" | ||
}, | ||
@@ -12,0 +13,0 @@ "author": "Matthias Hryniszak <padcom@gmail.com>", |
@@ -40,2 +40,3 @@ <p align="center"> | ||
import 'array-prototype-functions/group-by' | ||
import 'array-prototype-functions/group-by-as-map' | ||
import 'array-prototype-functions/head' | ||
@@ -55,2 +56,3 @@ import 'array-prototype-functions/last' | ||
require('array-prototype-functions/group-by') | ||
require('array-prototype-functions/group-by-as-map') | ||
require('array-prototype-functions/head') | ||
@@ -180,2 +182,13 @@ require('array-prototype-functions/last') | ||
Objects that don't have the field in them will be skipped. | ||
### `Array.prototype.groupByAsMap(field)` | ||
Dependencies: none | ||
Returns a Map of lists of objects from the array grouped by a field name. The field name is required. | ||
The difference to `groupBy(field)` is that the return value is an instance of `Map<any, T[]>` which means that the original type of the values of the field is also preserved and not converted to a string. This also means that if throughout the objects in the source array field values have different types they will be treated differently and you'll end up having more than one item in the result. | ||
Objects that don't have the field in them will be skipped. | ||
### `Array.prototype.uniq(fieldOrMapper = null)` | ||
@@ -182,0 +195,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
32954
31
368
221
2