Comparing version 0.2.0 to 0.2.1
@@ -130,1 +130,14 @@ /** | ||
export declare function assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W; | ||
/** | ||
Merge each item in items that shares the same identifier. | ||
mergeBy([ | ||
{id: 1, firstname: 'Chris'}, | ||
{id: 1, lastname: 'Brown'}, | ||
{id: 2, firstname: 'Lionel'}, | ||
]) => [ | ||
{id: 1, firstname: 'Chris', lastname: 'Brown'}, | ||
{id: 2, firstname: 'Lionel'}, | ||
] | ||
*/ | ||
export declare function mergeBy<T>(items: T[], idKey?: string): T[]; |
30
index.js
@@ -236,1 +236,31 @@ /** | ||
exports.assign = assign; | ||
/** | ||
Merge each item in items that shares the same identifier. | ||
mergeBy([ | ||
{id: 1, firstname: 'Chris'}, | ||
{id: 1, lastname: 'Brown'}, | ||
{id: 2, firstname: 'Lionel'}, | ||
]) => [ | ||
{id: 1, firstname: 'Chris', lastname: 'Brown'}, | ||
{id: 2, firstname: 'Lionel'}, | ||
] | ||
*/ | ||
function mergeBy(items, idKey) { | ||
if (idKey === void 0) { idKey = 'id'; } | ||
var mergedItems = []; | ||
// mergedItemsMapping is a helper that maps from ids to the matching object, | ||
// which is also stored in the mergedItems array | ||
var mergedItemsMapping = {}; | ||
items.forEach(function (item) { | ||
var id = item[idKey]; | ||
var mergedItem = mergedItemsMapping[id]; | ||
if (mergedItem === undefined) { | ||
mergedItem = mergedItemsMapping[id] = {}; | ||
mergedItems.push(mergedItem); | ||
} | ||
assign(mergedItem, item); | ||
}); | ||
return mergedItems; | ||
} | ||
exports.mergeBy = mergeBy; |
{ | ||
"name": "tarry", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"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
12814
352