UNPKG

10.9 kBPlain TextView Raw
1import { RowData } from '../src/util/helper';
2import { parse } from '../src/parse';
3
4const data1: RowData[] = [
5 { id: 1, name: 'nameI', type: 'A', gender: 'female', DOB: '1987-03-21', weight: 80, height: 160 },
6 { id: 2, name: 'nameII', type: 'B', gender: 'female', DOB: '1991-04-27', weight: 90, height: 180 },
7 { id: 3, name: 'nameIII', type: 'B', gender: 'female', DOB: '1990-05-15', weight: 60, height: 160 },
8 { id: 4, name: 'nameIV', type: 'A', gender: 'female', DOB: '1982-02-09', weight: 40, height: 150 },
9 { id: 5, name: 'nameV', type: 'B', gender: 'female', DOB: '1994-07-03', weight: 50, height: 155 },
10 { id: 6, name: 'nameVI', type: 'A', gender: 'male', DOB: '1982-10-23', weight: 70, height: 165 },
11 { id: 7, name: 'nameVII', type: 'A', gender: 'male', DOB: '1971-03-05', weight: 85, height: 185 },
12 { id: 8, name: 'nameVIII', type: 'C', gender: 'male', DOB: '1978-12-12', weight: 75, height: 160 },
13 { id: 9, name: 'nameIX', type: 'A', gender: 'male', DOB: '1995-08-01', weight: 65, height: 170 },
14 { id: 10, name: 'nameX', type: 'C', gender: 'male', DOB: '1980-03-12', weight: 70, height: 165 },
15];
16
17// classic
18test('parse', () => {
19 expect(
20 parse(data1, {
21 groupBy: ['gender'],
22 actions: [
23 {
24 type: 'sum',
25 field: 'weight',
26 as: 'sum_weight',
27 },
28 {
29 type: 'avg',
30 field: 'height',
31 as: 'avg_height',
32 },
33 ],
34 })
35 ).toEqual([
36 { gender: 'female', sum_weight: 320, avg_height: 161 },
37 { gender: 'male', sum_weight: 365, avg_height: 169 },
38 ]);
39});
40
41// multi groupBy
42test('parse', () => {
43 expect(
44 parse(data1, {
45 groupBy: ['gender', 'type'],
46 actions: [
47 {
48 type: 'max',
49 field: 'weight',
50 as: 'max_weight',
51 },
52 {
53 type: 'min',
54 field: 'height',
55 as: 'min_height',
56 },
57 ],
58 })
59 ).toEqual([
60 { gender: 'female', type: 'A', max_weight: 80, min_height: 150 },
61 { gender: 'female', type: 'B', max_weight: 90, min_height: 155 },
62 { gender: 'male', type: 'A', max_weight: 85, min_height: 165 },
63 { gender: 'male', type: 'C', max_weight: 75, min_height: 160 },
64 ]);
65});
66
67// no groupBy
68test('parse', () => {
69 expect(
70 parse(data1, {
71 groupBy: [],
72 actions: [
73 {
74 type: 'countd',
75 field: 'type',
76 as: 'countd_type',
77 },
78 ],
79 })
80 ).toEqual([{ countd_type: 3 }]);
81});
82
83// no as
84test('parse', () => {
85 expect(
86 parse(data1, {
87 groupBy: [],
88 actions: [
89 {
90 type: 'countd',
91 field: 'type',
92 as: '',
93 },
94 {
95 type: 'median',
96 field: 'weight',
97 as: null,
98 },
99 {
100 type: 'min',
101 field: 'height',
102 as: null,
103 },
104 ],
105 })
106 ).toEqual([{ type: 3, weight: 70, height: 150 }]);
107});
108
109// count
110test('parse', () => {
111 expect(
112 parse(data1, {
113 groupBy: ['type'],
114 actions: [
115 {
116 type: 'count',
117 field: 'id',
118 as: 'c1',
119 },
120 {
121 type: 'count',
122 field: 'name',
123 as: '',
124 },
125 {
126 type: 'count',
127 field: '',
128 as: null,
129 },
130 ],
131 })
132 ).toEqual([
133 { type: 'A', c1: 5, name: 5, new_field: 5 },
134 { type: 'B', c1: 3, name: 3, new_field: 3 },
135 { type: 'C', c1: 2, name: 2, new_field: 2 },
136 ]);
137});
138
139// Conversions: toString, toFloat, toInt
140test('parse', () => {
141 expect(
142 parse(
143 [
144 { id: 1, kpi: '3.25', height: '10.5' },
145 { id: 2, kpi: '3.5', height: '10' },
146 { id: 3, kpi: '3.75', height: 9.5 },
147 ],
148 {
149 actions: [
150 {
151 type: 'toString',
152 field: 'id',
153 as: null,
154 },
155 {
156 type: 'toFloat',
157 field: 'kpi',
158 as: null,
159 },
160 {
161 type: 'toInt',
162 field: 'height',
163 as: 'size',
164 },
165 ],
166 }
167 )
168 ).toEqual([
169 { id: '1', kpi: 3.25, height: '10.5', size: 10 },
170 { id: '2', kpi: 3.5, height: '10', size: 10 },
171 { id: '3', kpi: 3.75, height: 9.5, size: 9 },
172 ]);
173});
174
175// toString with groupBy
176test('parse', () => {
177 expect(
178 parse(data1, {
179 groupBy: [],
180 actions: [
181 {
182 type: 'toString',
183 field: 'id',
184 as: null,
185 },
186 ],
187 })
188 ).toEqual([{ id: '1' }]);
189});
190
191// multi transforms
192test('parse', () => {
193 expect(
194 parse(
195 [
196 { id: 1, kpi: '3.25', height: '10.5' },
197 { id: 2, kpi: '3.5', height: '10' },
198 { id: 3, kpi: '3.75', height: 9.5 },
199 ],
200 [
201 {
202 actions: [
203 {
204 type: 'toFloat',
205 field: 'kpi',
206 as: null,
207 },
208 {
209 type: 'toInt',
210 field: 'height',
211 as: 'size',
212 },
213 ],
214 },
215 {
216 groupBy: ['size'],
217 actions: [
218 {
219 type: 'max',
220 field: 'kpi',
221 as: null,
222 },
223 ],
224 },
225 ]
226 )
227 ).toEqual([
228 { kpi: 3.5, size: 10 },
229 { kpi: 3.75, size: 9 },
230 ]);
231});
232
233// removeNull
234test('parse', () => {
235 expect(
236 parse(
237 [
238 { id: '1', height: 10.5, weight: 60 },
239 { id: '2', height: null, weight: 40 },
240 { id: '3', height: 9.5, weight: null },
241 { id: '', height: 9.5, weight: 80 },
242 { id: '5', height: 9.5 },
243 ],
244 {
245 actions: [
246 {
247 type: 'removeNull',
248 field: 'id',
249 as: null,
250 },
251 ],
252 }
253 )
254 ).toEqual([
255 { id: '1', height: 10.5, weight: 60 },
256 { id: '2', height: null, weight: 40 },
257 { id: '3', height: 9.5, weight: null },
258 { id: '5', height: 9.5 },
259 ]);
260});
261
262test('parse', () => {
263 expect(
264 parse(
265 [
266 { id: '1', height: 10.5, weight: 60 },
267 { id: '2', height: null, weight: 40 },
268 { id: '3', height: 9.5, weight: null },
269 { id: '', height: 9.5, weight: 80 },
270 { id: '5', height: 9.5 },
271 ],
272 {
273 actions: [
274 {
275 type: 'removeNull',
276 field: 'height',
277 as: null,
278 },
279 ],
280 }
281 )
282 ).toEqual([
283 { id: '1', height: 10.5, weight: 60 },
284 { id: '3', height: 9.5, weight: null },
285 { id: '', height: 9.5, weight: 80 },
286 { id: '5', height: 9.5 },
287 ]);
288});
289test('parse', () => {
290 expect(
291 parse(
292 [
293 { id: '1', height: 10.5, weight: 60 },
294 { id: '2', height: null, weight: 40 },
295 { id: '3', height: 9.5, weight: null },
296 { id: '', height: 9.5, weight: 80 },
297 { id: '5', height: 9.5 },
298 ],
299 {
300 actions: [
301 {
302 type: 'removeNull',
303 field: 'weight',
304 as: null,
305 },
306 ],
307 }
308 )
309 ).toEqual([
310 { id: '1', height: 10.5, weight: 60 },
311 { id: '2', height: null, weight: 40 },
312 { id: '', height: 9.5, weight: 80 },
313 ]);
314});
315
316// fillNull byValue
317test('parse', () => {
318 expect(
319 parse(
320 [
321 { id: '1', height: 10.5, weight: 60 },
322 { id: '2', height: null, weight: 40 },
323 { id: '3', height: 9.5, weight: null },
324 { id: '', height: 9.5, weight: 80 },
325 { id: '5', height: 9.5 },
326 ],
327 {
328 actions: [
329 {
330 type: 'fillNull',
331 field: 'id',
332 as: null,
333 options: {
334 type: 'byValue',
335 cfg: {
336 value: '-1',
337 },
338 },
339 },
340 ],
341 }
342 )
343 ).toEqual([
344 { id: '1', height: 10.5, weight: 60 },
345 { id: '2', height: null, weight: 40 },
346 { id: '3', height: 9.5, weight: null },
347 { id: '-1', height: 9.5, weight: 80 },
348 { id: '5', height: 9.5 },
349 ]);
350});
351
352// fillNull byAgg
353test('parse', () => {
354 expect(
355 parse(
356 [
357 { id: 'a', height: 10.5, weight: 60 },
358 { id: 'b', height: null, weight: 40 },
359 { id: 'c', height: 9.5, weight: null },
360 { id: '', height: 9.5, weight: 80 },
361 { id: 'e', height: 9.5 },
362 ],
363 {
364 actions: [
365 {
366 type: 'fillNull',
367 field: 'id',
368 as: null,
369 options: {
370 type: 'byAgg',
371 cfg: {
372 agg: 'avg',
373 },
374 },
375 },
376 {
377 type: 'fillNull',
378 field: 'weight',
379 as: null,
380 options: {
381 type: 'byAgg',
382 cfg: {
383 agg: 'min',
384 },
385 },
386 },
387 ],
388 }
389 )
390 ).toEqual([
391 { id: 'a', height: 10.5, weight: 60 },
392 { id: 'b', height: null, weight: 40 },
393 { id: 'c', height: 9.5, weight: 40 },
394 { id: NaN, height: 9.5, weight: 80 },
395 { id: 'e', height: 9.5, weight: 40 },
396 ]);
397});
398
399// fillNull bySmart
400test('parse', () => {
401 console.log(
402 parse(
403 [
404 { id: 1, name: 'nameI', type: 'A', gender: 'female', DOB: '1987-03-21', weight: 80, height: 160 },
405 { id: 2, name: 'nameII', type: 'B', gender: 'female', DOB: '1991-04-27', weight: 90, height: 180 },
406 { id: 3, name: 'nameIII', type: 'B', gender: 'female', DOB: '1990-05-15', weight: 60, height: 160 },
407 { id: 4, name: 'nameIV', type: 'A', gender: 'male', DOB: '1982-02-09', weight: 40, height: 150 },
408 { id: null, name: '', type: '', gender: null, DOB: '' },
409 ],
410 {
411 actions: [
412 {
413 type: 'fillNull',
414 field: 'id',
415 as: null,
416 options: {
417 type: 'bySmart',
418 },
419 },
420 {
421 type: 'fillNull',
422 field: 'name',
423 as: null,
424 options: {
425 type: 'bySmart',
426 },
427 },
428 {
429 type: 'fillNull',
430 field: 'type',
431 as: null,
432 options: {
433 type: 'bySmart',
434 },
435 },
436 {
437 type: 'fillNull',
438 field: 'gender',
439 as: null,
440 options: {
441 type: 'bySmart',
442 },
443 },
444 {
445 type: 'fillNull',
446 field: 'DOB',
447 as: null,
448 options: {
449 type: 'bySmart',
450 },
451 },
452 {
453 type: 'fillNull',
454 field: 'weight',
455 as: null,
456 options: {
457 type: 'bySmart',
458 },
459 },
460 {
461 type: 'fillNull',
462 field: 'height',
463 as: null,
464 options: {
465 type: 'bySmart',
466 },
467 },
468 ],
469 }
470 )[4]
471 );
472});