-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpopulate-by-convention.js
276 lines (252 loc) · 6.77 KB
/
populate-by-convention.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Dependencies
*/
const { plural } = require('pluralize');
const { getByIdFromCollection } = require('./utils');
/**
* Common object population interface
*
* Function that can populate an object
* according to a set of collections
*
* NOTE: See unit tests for usage and purpose
*/
module.exports = function populateByConvention() {
return typeof Proxy !== 'undefined' ?
populateByProxy.apply(null, arguments) :
populateByAssign.apply(null, arguments);
}
/**
* Object population / linking implemented with ES6 Proxies
*
* Creates a proxied object which will resolve
* to the collections recursively by convention.
*
* NOTE: Lazy evaluation (faster) implemented using ES6 Proxies and getter traps
*/
const populateByProxy = module.exports.populateByProxy = function populateByProxy(
depth = 0,
collections = {},
object
) {
/**
* Handle invalid input
*/
if (!object || typeof object !== 'object') {
return object;
}
/**
* List collection keys
*/
const collectionKeys = Reflect.ownKeys(collections);
/**
* Create proxy trap handler for all get calls
*/
const handler = {
get(target, key, receiver) {
/**
* Get the accessed value
*/
const value = Reflect.get(target, key, receiver);
/**
* Handle symbol access
*/
if (typeof key === 'symbol' || depth <= 0) {
return value;
}
/**
* Handle null
*/
if (typeof value === 'object' && !value) {
return value;
}
/**
* Handle dates (Date methods are not supported by proxies)
*/
if (typeof value === 'object' && !!value.getTime) {
return value;
}
/**
* For collection references (list of ids) (Array)
*/
if (Array.isArray(value) && collectionKeys.includes(key)) {
return value
.map(getByIdFromCollection(collections[key]))
.filter(function(x) { return x })
.map(populateByProxy.bind(null, depth - 1, collections));
}
/**
* For collection references (list of ids) (Map)
*/
if (typeof value === 'object' && collectionKeys.includes(key)) {
return Reflect.ownKeys(value)
.map(getByIdFromCollection(collections[key]))
.filter(function(x) { return x })
.map(populateByProxy.bind(null, depth - 1, collections))
.reduce(function(res, item) {
return Object.assign({}, res, {
[item.id]: item
});
}, {});
}
/**
* For single value references (single id)
*/
if (collectionKeys.includes(plural(key))) {
return populateByProxy(
depth - 1,
collections,
getByIdFromCollection(collections[plural(key)])(value)
);
}
/**
* Handle nested objects
*/
if (value && typeof value === 'object') {
return populateByProxy(depth, collections, value);
}
/**
* Handle primitive values
*/
return value;
},
};
/**
* Return a proxied object or a list of proxied object
*/
return Array.isArray(object) ? (
object.map(function(item) { return populateByProxy(depth, collections, item) })
) : (
new Proxy(object, handler)
);
}
/**
* Object population / linking implemented with recursive Object.assign
*
* Creates a new object/array which has been populated
* against the collections recursively by convention.
*
* NOTE: Eager evaluation (slow)
* NOTE: Only for compatibility (very memory inefficient)
*/
const populateByAssign = module.exports.populateByAssign = function populateByAssign(
depth = 0,
collections = {},
object
) {
/**
* Handle null/undefined/primitive values
*/
if (!object || typeof object !== 'object') {
return object;
}
/**
* List collection keys
*/
const collectionKeys = Object.keys(collections);
/**
* Recursively populate sub-items
*/
function populateRecursively(depthLeft, subItem) {
/**
* Handle null/undefined/primitive values
*/
if (!subItem || typeof subItem !== 'object') {
return subItem;
}
const objectKeys = Object.keys(subItem);
/**
* Extend / populate object
*/
return objectKeys.reduce(function(result, key) {
/**
* For collection references (list of ids) (Array)
*/
if (
collectionKeys.includes(key) &&
Array.isArray(subItem[key])
) {
return Object.assign({}, result, {
[key]: !depthLeft ? (
subItem[key]
) : (
subItem[key]
.map(getByIdFromCollection(collections[key]))
.filter(function(x) { return x })
.map(function(item) { return populateRecursively(depthLeft - 1, item) })
)
});
}
/**
* For collection references (list of ids) (Map)
*/
if (
collectionKeys.includes(key) &&
subItem[key] &&
typeof subItem[key] === 'object'
) {
return Object.assign({}, result, {
[key]: !depthLeft ? (
subItem[key]
) : (
Object.keys(subItem[key])
.map(getByIdFromCollection(collections[key]))
.filter(function(x) { return x })
.reduce(function(result, item) {
return Object.assign({}, result, {
[item.id]: populateRecursively(depthLeft - 1, item)
});
}, {})
)
});
}
/**
* For single value references (single id)
*/
if (collectionKeys.includes(plural(key))) {
return Object.assign({}, result, {
[key]: !depthLeft ? (
subItem[key]
) : (
populateRecursively(depthLeft - 1, getByIdFromCollection(collections[plural(key)])(subItem[key]))
)
});
}
/**
* Handle arrays objects
*/
if (Array.isArray(subItem[key])) {
return Object.assign({}, result, {
[key]: !depthLeft ? (
subItem[key]
) : (
subItem[key]
.filter(function(x) { return x })
.map(function(item) { return populateRecursively(depthLeft, item) })
)
});
}
/**
* Handle nested objects
*/
if (subItem[key] && typeof subItem[key] === 'object') {
return Object.assign({}, result, {
[key]: !depthLeft ? (
subItem[key]
) : (
populateRecursively(depthLeft, subItem[key])
)
})
}
return result;
}, Object.assign({}, subItem));
}
/**
* Handle both objects and array inputs
*/
return Array.isArray(object) ? (
object.map(function(item) { return populateRecursively(depth, item) })
) : (
populateRecursively(depth, object)
);
}