-
Notifications
You must be signed in to change notification settings - Fork 0
/
operationsByAttribute.js
41 lines (32 loc) · 1.33 KB
/
operationsByAttribute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function makeOperationByAttribute(operation, candidatesStratgey){
var recur = function(){
if(arguments.length === 2)
return arguments[1];
var attribute = arguments[0];
var arr1 = arguments[1];
var arr2 = arguments[2];
var rest = Array.prototype.slice.call(arguments, 3);
var attributeValuesOfOperation = _.uniq(operation(_(arr1).pluck(attribute),
_(arr2).pluck(attribute)));
var candidates = candidatesStratgey(arr1, arr2);
var getFirstObjectWithAttributeValue = (attributeValue) => _(candidates).find( (candidate)=> attributeValue === candidate[attribute] );
var mem = _(attributeValuesOfOperation).map(getFirstObjectWithAttributeValue);
rest.unshift(mem);
rest.unshift(attribute);
return recur(rest);
};
return function(){
var length = arguments.length;
if(length < 2)
return [];
var args = Array.prototype.slice.call(arguments, 0)
if(length === 2)
args.push([]);
return recur.apply(null, args);
};
}
var first = (arr1, arr2) => arr1;
var both = (arr1, arr2) => _.flattern([arr1, arr2]);
var unionByAttribute = makeOperationByAttribute(_.union, both);
var intersectionByAttribute = makeOperationByAttribute(_.intersection, first);
var differenceByAttribute = makeOperationByAttribute(_.difference, first);