-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtransformations.ts
146 lines (135 loc) · 3.59 KB
/
transformations.ts
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
import every from 'lodash-es/every';
import some from 'lodash-es/some';
import startsWith from 'lodash-es/startsWith';
import endsWith from 'lodash-es/endsWith';
import lt from 'lodash-es/lt';
import lte from 'lodash-es/lte';
import gt from 'lodash-es/gt';
import gte from 'lodash-es/gte';
import eq from 'lodash-es/eq';
import map from 'lodash-es/map';
import keyBy from 'lodash-es/keyBy';
import chunk from 'lodash-es/chunk';
import drop from 'lodash-es/drop';
import dropRight from 'lodash-es/dropRight';
import take from 'lodash-es/take';
import takeRight from 'lodash-es/takeRight';
import flattenDepth from 'lodash-es/flattenDepth';
import fromPairs from 'lodash-es/fromPairs';
import nth from 'lodash-es/nth';
import reverse from 'lodash-es/reverse';
import uniq from 'lodash-es/uniq';
import uniqBy from 'lodash-es/uniqBy';
import countBy from 'lodash-es/countBy';
import filter from 'lodash-es/filter';
import reject from 'lodash-es/reject';
import groupBy from 'lodash-es/groupBy';
import sortBy from 'lodash-es/sortBy';
import minBy from 'lodash-es/minBy';
import maxBy from 'lodash-es/maxBy';
import meanBy from 'lodash-es/meanBy';
import sumBy from 'lodash-es/sumBy';
import join from 'lodash-es/join';
import expression from 'lodash-es/join';
import get from 'lodash-es/get';
import mapValues from 'lodash-es/mapValues';
import at from 'lodash-es/at';
import toPairs from 'lodash-es/toPairs';
import invert from 'lodash-es/invert';
import invertBy from 'lodash-es/invertBy';
import keys from 'lodash-es/keys';
import values from 'lodash-es/values';
const compute = function(scope, expression) {
return _.template("<%= " + expression + " %>")(scope);
}
const transformations = {
Array: {
each: (array, arg) => {
return map(array, item => applyTransformations(item, arg));
},
mapCompute: (array, arg) => {
return compute(array, item => applyTransformations(item, arg));
},
map,
keyBy,
chunk,
drop,
dropRight,
take,
takeRight,
flattenDepth,
fromPairs,
nth,
reverse,
uniq,
uniqBy,
countBy,
filter,
reject,
filterIf: (array, arg) => {
return filter(array, item => applyTransformations(item, arg));
},
rejectIf: (array, arg) => {
return reject(array, item => applyTransformations(item, arg));
},
groupBy,
sortBy,
minBy,
maxBy,
meanBy,
sumBy,
join,
},
Object: {
get,
mapValues,
at,
toPairs,
invert,
invertBy,
keys,
values,
compute,
},
Number: {
lt,
lte,
gt,
gte,
eq,
},
String: {
startsWith,
endsWith,
},
};
const opToExpectedType = {};
for (const type in transformations)
for (const name in transformations[type])
opToExpectedType[name] = type;
export function applyTransformations(object, args) {
if (!args)
return object;
for (const op in args) {
if (object === null)
break;
const arg = args[op];
if (op === 'and') {
object = every(arg, predicateArgs => applyTransformations(object, predicateArgs));
continue;
}
if (op === 'or') {
object = some(arg, predicateArgs => applyTransformations(object, predicateArgs));
continue;
}
const expectedType = opToExpectedType[op];
let type = object.constructor && object.constructor.name;
// handle objects created with Object.create(null)
if (!type && (typeof object === 'object'))
type = 'Object';
if (expectedType !== type)
throw Error(`"${op}" transformation expect "${expectedType}" but got "${type}"`);
object = transformations[type][op](object, arg);
}
return object;
}