Comparing version 0.2.4 to 0.2.5
@@ -131,3 +131,3 @@ /** | ||
/** | ||
Merge each item in items that shares the same identifier. | ||
Merge each subset of items that share the same identifier. | ||
@@ -145,2 +145,23 @@ mergeBy([ | ||
/** | ||
Concatenate each subset of items that share the same identifier. | ||
groupBy([ | ||
{id: 1, key: 'firstname', value: 'Chris'}, | ||
{id: 1, key: 'lastname', value: 'Brown'}, | ||
{id: 2, key: 'firstname', value: 'Lionel'}, | ||
]) => [ | ||
[ | ||
{id: 1, key: 'firstname', value: 'Chris'}, | ||
{id: 1, key: 'lastname', value: 'Brown'}, | ||
], | ||
[ | ||
{id: 2, key: 'firstname', value: 'Lionel'}, | ||
] | ||
] | ||
This is very similar to mergeBy, except that instead of using {} as a base | ||
and combining with assign(), groupBy uses [] as a base and combines with push(). | ||
*/ | ||
export declare function groupBy<T>(items: T[], idKey?: string): T[][]; | ||
/** | ||
Convert an Array of objects with fixed keys to an object with variable keys. | ||
@@ -147,0 +168,0 @@ |
38
index.js
@@ -237,3 +237,3 @@ /** | ||
/** | ||
Merge each item in items that shares the same identifier. | ||
Merge each subset of items that share the same identifier. | ||
@@ -268,2 +268,38 @@ mergeBy([ | ||
/** | ||
Concatenate each subset of items that share the same identifier. | ||
groupBy([ | ||
{id: 1, key: 'firstname', value: 'Chris'}, | ||
{id: 1, key: 'lastname', value: 'Brown'}, | ||
{id: 2, key: 'firstname', value: 'Lionel'}, | ||
]) => [ | ||
[ | ||
{id: 1, key: 'firstname', value: 'Chris'}, | ||
{id: 1, key: 'lastname', value: 'Brown'}, | ||
], | ||
[ | ||
{id: 2, key: 'firstname', value: 'Lionel'}, | ||
] | ||
] | ||
This is very similar to mergeBy, except that instead of using {} as a base | ||
and combining with assign(), groupBy uses [] as a base and combines with push(). | ||
*/ | ||
function groupBy(items, idKey) { | ||
if (idKey === void 0) { idKey = 'id'; } | ||
var groupedItems = []; | ||
var groupedItemsMapping = {}; | ||
items.forEach(function (item) { | ||
var id = item[idKey]; | ||
var groupedItem = groupedItemsMapping[id]; | ||
if (groupedItem === undefined) { | ||
groupedItem = groupedItemsMapping[id] = []; | ||
groupedItems.push(groupedItem); | ||
} | ||
groupedItem.push(item); | ||
}); | ||
return groupedItems; | ||
} | ||
exports.groupBy = groupBy; | ||
/** | ||
Convert an Array of objects with fixed keys to an object with variable keys. | ||
@@ -270,0 +306,0 @@ |
{ | ||
"name": "tarry", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Utility library for manipulating JavaScript Arrays", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
15441
436