@supercharge/arrays
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -155,4 +155,12 @@ declare type Values<T> = Array<T | Iterable<T> | undefined | null>; | ||
*/ | ||
groupBy<R = any>(key: keyof T): R; | ||
groupBy<R = any>(key: keyof T | ((item: T) => any)): R; | ||
/** | ||
* Create a value receiving callback. | ||
* | ||
* @param {*} value | ||
* | ||
* @returns {Function} | ||
*/ | ||
private valueRetriever; | ||
/** | ||
* Determine whether the array contains the given `value`. | ||
@@ -159,0 +167,0 @@ * |
@@ -182,7 +182,8 @@ 'use strict'; | ||
groupBy(key) { | ||
if (String(key).includes('.')) { | ||
if (typeof key === 'string' && key.includes('.')) { | ||
throw new Error('We do not support nested grouping yet. Please send a PR for that feature.'); | ||
} | ||
const selector = this.valueRetriever(key); | ||
return this.reduce((carry, item) => { | ||
const group = item[key] || ''; | ||
const group = selector(item) || ''; | ||
if (carry[group] === undefined) { | ||
@@ -196,2 +197,16 @@ carry[group] = []; | ||
/** | ||
* Create a value receiving callback. | ||
* | ||
* @param {*} value | ||
* | ||
* @returns {Function} | ||
*/ | ||
valueRetriever(value) { | ||
return typeof value === 'function' | ||
? value | ||
: function (item) { | ||
return item[value]; | ||
}; | ||
} | ||
/** | ||
* Determine whether the array contains the given `value`. | ||
@@ -198,0 +213,0 @@ * |
{ | ||
"name": "@supercharge/arrays", | ||
"description": "Array utilities for Node.js and JavaScript", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"author": "Marcus Pöhls <marcus@superchargejs.com>", | ||
@@ -12,3 +12,3 @@ "bugs": { | ||
"@supercharge/tsconfig": "~1.0.0", | ||
"c8": "~7.11.0", | ||
"c8": "~7.11.2", | ||
"eslint": "~7.32.0", | ||
@@ -15,0 +15,0 @@ "expect": "~27.5.1", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30417
920