forked from graphql-kit/graphql-lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformations.ts
157 lines (145 loc) · 3.84 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
147
148
149
150
151
152
153
154
155
156
157
import every from 'lodash/every.js';
import some from 'lodash/some.js';
import startsWith from 'lodash/startsWith.js';
import endsWith from 'lodash/endsWith.js';
import lt from 'lodash/lt.js';
import lte from 'lodash/lte.js';
import gt from 'lodash/gt.js';
import gte from 'lodash/gte.js';
import eq from 'lodash/eq.js';
import map from 'lodash/map.js';
import keyBy from 'lodash/keyBy.js';
import chunk from 'lodash/chunk.js';
import drop from 'lodash/drop.js';
import dropRight from 'lodash/dropRight.js';
import take from 'lodash/take.js';
import takeRight from 'lodash/takeRight.js';
import flattenDepth from 'lodash/flattenDepth.js';
import fromPairs from 'lodash/fromPairs.js';
import nth from 'lodash/nth.js';
import reverse from 'lodash/reverse.js';
import uniq from 'lodash/uniq.js';
import uniqBy from 'lodash/uniqBy.js';
import countBy from 'lodash/countBy.js';
import filter from 'lodash/filter.js';
import reject from 'lodash/reject.js';
import groupBy from 'lodash/groupBy.js';
import sortBy from 'lodash/sortBy.js';
import minBy from 'lodash/minBy.js';
import maxBy from 'lodash/maxBy.js';
import meanBy from 'lodash/meanBy.js';
import sumBy from 'lodash/sumBy.js';
import join from 'lodash/join.js';
import get from 'lodash/get.js';
import mapValues from 'lodash/mapValues.js';
import at from 'lodash/at.js';
import toPairs from 'lodash/toPairs.js';
import invert from 'lodash/invert.js';
import invertBy from 'lodash/invertBy.js';
import keys from 'lodash/keys.js';
import values from 'lodash/values.js';
const typeCasts = {
'Date': val => new Date(val),
'Number': val => new Number(val),
'String': val => new String(val),
'Boolean': val => new Boolean(val),
}
// this code is not working yey, it somehow doesn't receive the arg argument
const as = (value, arg) => {
if(!value) {
return value
} else if(arg in typeCasts) {
return typeCasts[arg](value)
} else {
throw Error(`can't cast to "${arg}" only the following types are supported: ${Object.keys(typeCasts).join(', ')}.`);
}
},
const transformations = {
Array: {
each: (array, arg) => {
return map(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,
},
Number: {
lt,
lte,
gt,
gte,
eq,
as
},
String: {
startsWith,
endsWith,
as
},
};
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;
}