Comparing version 1.2.0 to 1.3.0
# Changelog | ||
## 1.3.0 | ||
- Adds `groupByMany` | ||
## 1.2.0 | ||
@@ -4,0 +8,0 @@ |
@@ -375,2 +375,14 @@ /** Use with: `filter` | ||
* | ||
* Given a function `func` that returns a list of keys, returns an object of lists of elements. Works like `groupBy` except that `func` should return a list of keys. Good for grouping objects by properties that are arrays. An empty object must be passed as the second argument to `reduce` | ||
```ts | ||
const b1: B = { items: ["a", "b"] }; | ||
const b2: B = { items: ["a"] }; | ||
[b1, b2].reduce(groupByMany(b => b.items), {}); | ||
// Returns { a: [{ items: ["a", "b"] }, { items: ["a"] }], b: [{ items: ["b"] }] } | ||
``` | ||
*/ | ||
export declare const groupByMany: <K extends string, V>(func: (el: V) => K[] | undefined) => (acc: Record<K, V[]>, el: V) => Record<K, V[]>; | ||
/** Use with: `reduce` | ||
* | ||
* Given a property name, returns an object of lists of elements, grouped by the values for that property. A second argument must be passed to `reduce`. For javascript an empty object is enough. For typescript an object with properties or a type cast is required. | ||
@@ -377,0 +389,0 @@ ```ts |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.countBy = exports.partition = exports.groupByProperty = exports.groupBy = exports.minByProperty = exports.minBy = exports.min = exports.maxByProperty = exports.maxBy = exports.max = exports.sumByProperty = exports.sumBy = exports.sum = exports.get = exports.byProperty = exports.byValue = exports.by = exports.propertyIsntOneOf = exports.isntOneOfBy = exports.isntOneOf = exports.excludeByProperty = exports.excludeBy = exports.exclude = exports.propertyIsOneOf = exports.isOneOfBy = exports.isOneOf = exports.intersectionByProperty = exports.intersectionBy = exports.intersection = exports.has = exports.propertyIsnt = exports.isntBy = exports.isnt = exports.propertyIs = exports.isBy = exports.is = exports.uniqueByProperty = exports.unique = exports.uniqueBy = exports.duplicatesByProperty = exports.duplicates = exports.duplicatesBy = exports.or = exports.isDefined = void 0; | ||
exports.countBy = exports.partition = exports.groupByProperty = exports.groupByMany = exports.groupBy = exports.minByProperty = exports.minBy = exports.min = exports.maxByProperty = exports.maxBy = exports.max = exports.sumByProperty = exports.sumBy = exports.sum = exports.get = exports.byProperty = exports.byValue = exports.by = exports.propertyIsntOneOf = exports.isntOneOfBy = exports.isntOneOf = exports.excludeByProperty = exports.excludeBy = exports.exclude = exports.propertyIsOneOf = exports.isOneOfBy = exports.isOneOf = exports.intersectionByProperty = exports.intersectionBy = exports.intersection = exports.has = exports.propertyIsnt = exports.isntBy = exports.isnt = exports.propertyIs = exports.isBy = exports.is = exports.uniqueByProperty = exports.unique = exports.uniqueBy = exports.duplicatesByProperty = exports.duplicates = exports.duplicatesBy = exports.or = exports.isDefined = void 0; | ||
exports.isDefined = function (x) { | ||
@@ -116,2 +116,9 @@ return typeof x !== "undefined"; | ||
}; }; | ||
exports.groupByMany = function (func) { return function (acc, el) { | ||
var groupNames = func(el) || []; | ||
groupNames.forEach(function (key) { | ||
acc[key] = (acc[key] || []).concat(el); | ||
}); | ||
return acc; | ||
}; }; | ||
exports.groupByProperty = function (key) { return function (acc, el) { | ||
@@ -118,0 +125,0 @@ var _a; |
{ | ||
"name": "list-fns", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "A collection of utility functions to be used with .map, .filter, .sort and .reduce", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -66,2 +66,3 @@ # list-fns | ||
<li><a href="#groupBy">groupBy</a></li> | ||
<li><a href="#groupByMany">groupByMany</a></li> | ||
<li><a href="#groupByProperty">groupByProperty</a></li> | ||
@@ -509,2 +510,43 @@ <li><a href="#has">has</a></li> | ||
### <div id="groupByMany"></div> groupByMany | ||
```ts | ||
groupByMany: <K extends string, V>(func: (el: V) => K[] | undefined) => (acc: Record<K, V[]>, el: V) => Record<K, V[]> | ||
``` | ||
Use with: `reduce` | ||
Given a function `func` that returns a list of keys, returns an object of lists of elements. Works like `groupBy` except that `func` should return a list of keys. Good for grouping objects by properties that are arrays. An empty object must be passed as the second argument to `reduce` | ||
```ts | ||
const b1: B = { items: ["a", "b"] }; | ||
const b2: B = { items: ["a"] }; | ||
[b1, b2].reduce(groupByMany(b => b.items), {}); | ||
// Returns { a: [{ items: ["a", "b"] }, { items: ["a"] }], b: [{ items: ["b"] }] } | ||
``` | ||
<details> | ||
<summary>Implementation</summary> | ||
<p> | ||
```ts | ||
const groupByMany = <K extends string, V>( | ||
func: (el: V) => K[] | undefined | ||
) => (acc: Record<K, V[]>, el: V): Record<K, V[]> => { | ||
const groupNames = func(el) || []; | ||
groupNames.forEach(key => { | ||
acc[key] = (acc[key] || []).concat(el); | ||
}); | ||
return acc; | ||
} | ||
``` | ||
<p> | ||
</details> | ||
### <div id="groupByProperty"></div> groupByProperty | ||
@@ -511,0 +553,0 @@ |
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
56572
554
1596