-
Notifications
You must be signed in to change notification settings - Fork 0
/
populateObjForElement.js
42 lines (35 loc) · 1.23 KB
/
populateObjForElement.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
var pick = require('lodash.pick');
function populateObjForElement(element, optionsToInsert, allowedOptions) {
if (typeof element === 'string') {
if (element.includes(' ')) {
// If there's a space, we need to break it up and treat it like an array.
return populateObjForElement(
element.split(' ').filter(function (elm) { return !!elm; }),
optionsToInsert
);
}
return {
path: element,
options: optionsToInsert,
};
}
// Is it's an array recurse on each element
if (Array.isArray(element)) {
return element.map(
function (elm) { return populateObjForElement(elm, optionsToInsert); }
);
}
// Should be an object at this point with a path property.
if (element && element.path) {
var existingAllowedOptions = allowedOptions
? pick(element.options || {}, allowedOptions)
: element.options || {};
var optionsCopy = Object.assign({}, existingAllowedOptions, optionsToInsert);
var newElement = Object.assign({}, element, { options: optionsCopy });
if (newElement.populate) {
newElement.populate = populateObjForElement(newElement.populate, optionsToInsert);
}
return newElement;
}
}
module.exports = populateObjForElement;