-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlens-data-zip.html
254 lines (212 loc) · 7.45 KB
/
lens-data-zip.html
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../core-selector/core-selector.html">
<!-- <link rel="import" href="../core-input/core-input.html">
--><link rel="import" href="../core-icon-button/core-icon-button.html">
<!--
A transform component that zips two arrays of objects together based on a common key.
##### Example
<lens-data-zip></lens-data-zip>
@element lens-data-zip
@blurb A transform component that zips two arrays of objects together based on a common key.
@status alpha
@homepage http://lenses.github.io/lens-data-zip
-->
<polymer-element name="lens-data-zip" attributes="input output input_aux smart zipColKey0 zipColKey1 joinType">
<template>
<div class="lens-container lens-data">
<link rel="stylesheet" href="lens-data-zip.css">
<p class="info">Zips two datasets together based on a common key</p>
<label class="label" for="func_selector">Key1:</label>
<core-selector class="selector" id="value_selector0" selected="{{zipColKey0}}" valueattr="label">
<template repeat="{{attr in _dataAttributes0}}">
<div class="col" label="{{attr}}">{{attr}}</div>
</template>
</core-selector>
<label class="label" for="func_selector">Key2:</label>
<core-selector class="selector" id="value_selector1" selected="{{zipColKey1}}" valueattr="label">
<template repeat="{{attr in _dataAttributes1}}">
<div class="col" label="{{attr}}">{{attr}}</div>
</template>
</core-selector>
<label class="label">Zip (Join) Type:</label>
<core-selector class="selector" id="join_type" selected="{{joinType}}" valueattr="label">
<div class="col" label="inner">Strict/Inner</div>
<div class="col" label="outer">Full/Outer</div>
</core-selector>
<label class="label">Remove duplicate column:</label>
<input type="checkbox" checked="{{removeDuplicates}}">
</div> <!-- end container-->
</template>
<script src="../lodash/lodash.js"></script>
<script>
Polymer({
/**
* Input data
*
* @property input
* @type object
* @default undefined
*/
/**
* Auxilary Input data for second input
*
* @property input_aux
* @type object
* @default undefined
*/
/**
* Output data
*
* @property output
* @type object
* @default undefined
*/
/**
* Name of the column from input which will be used as zip key.
*
* @property zipColKey0
* @type String
* @default undefined
*/
zipColKey0: null,
/**
* Name of the column from input_aux which will be used as zip key.
*
* @property zipColKey1
* @type String
* @default undefined
*/
zipColKey1: null,
/**
* Be smarter and find non-exact matches (Slows down performance).
*
* @property smart
* @type Boolean
* @default false
*/
smart: false,
/**
* Join Type values: (inner/outer). Options: <br>
* - <code>'inner'</code> - Output will only include data whose columnKey appears
* in both input sets and can be joined.<br/>
* - <code>'outer'</code> - Output will also include data whose columnKey is unique
* to one of the inputs, while joining data whose columnKeys appear in both.
*
* @property joinType
* @type String
* @default 'inner'
*/
joinType: 'inner',
/**
* If true, zipped columns will be merged into one column to remove duplicates.
*
* @property removeDuplicates
* @type Boolean
* @default false
*/
removeDuplicates: false,
// Observe changes on below attributes and trigger _calculateOutput() when changed
observe: {
beSmart: "_calculateOutput",
zipColKey0: "_calculateOutput",
zipColKey1: "_calculateOutput",
joinType: "_calculateOutput",
removeDuplicates: "_calculateOutput"
},
// Called when <th-data-split> element is created.
ready: function () {
},
// Change watcher for input attribute. Triggered wheneve input is changed
inputChanged: function() {
var self = this;
if(!self.input || !self.input.length>0) {
return;
}
var firstItem = self.input[0];
if(!firstItem) {
return;
}
var attributes = Object.keys(firstItem);
if(JSON.stringify(self._dataAttributes0) === JSON.stringify(attributes) ) {
//self._mapChartData();
}
else {
self._dataAttributes0 = attributes;
}
self._calculateOutput();
},
input_auxChanged: function() {
var self = this;
if(!self.input_aux || !self.input_aux.length>0) {
return;
}
var firstItem = self.input_aux[0];
if(!firstItem) {
return;
}
var attributes = Object.keys(firstItem);
if(JSON.stringify(self._dataAttributes1) === JSON.stringify(attributes) ) {
//self._mapChartData();
}
else {
self._dataAttributes1 = attributes;
}
self._calculateOutput();
},
// Calculates output from input based on settings
_calculateOutput: function() {
var self = this;
if(self.zipColKey1 && self.zipColKey1)
{
//var intersect = _.intersection(_.pluck(self.input, self.zipColKey1), _.pluck(self.input, self.zipColKey1));
//console.log(intersect);
// console.log('key',self.zipColKey0);
var input0 = _.map(self.input, function(item) {
item['__toZip'] = item[self.zipColKey0];
return item;
});
var input1 = _.map(self.input_aux, function(item) {
item['__toZip'] = item[self.zipColKey1];
return item;
});
var grouped = _.groupBy(input0.concat(input1), '__toZip');
// console.log(grouped);
/*
grouped.map(grouped, function(item) {
var key = Object.keys(item)[0];
var ret = {};
//ret[self.zipColKey0] = key;
console.log(key);
if(item[key].length>1) {
_.assign(ret, item[key][0], item[key][1]);
console.log(ret);
}
return ret;
})
*/
self.output = [];
// console.log(self.joinType);
_.forIn(grouped, function(value, key) {
var ret = {};
console.log(key,value);
//when there is nothing to groupby lodash groups them under undefined
if(key!=='undefined') {
//if inner join, only merge when there are at least two values grouped
if(self.joinType==='outer' || value.length>1) {
_.assign(ret, value[0], value[1]);
delete ret['__toZip'];
// console.log('remove dupes', self.removeDuplicates);
if(self.removeDuplicates && self.zipColKey0!==self.zipColKey1 && ret[self.zipColKey0]) {
delete ret[self.zipColKey1];
}
// console.log(ret);
self.output.push(ret);
}
}
});
// console.log(self.output);
}
}
});
</script>
</polymer-element>