-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.js
46 lines (38 loc) · 907 Bytes
/
helpers.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
/**
* Lookup dotted path in an object
*/
export function lookupPath(obj, path) {
const keys = path.split('.')
for (let i = 0; i < keys.length && obj !== undefined; i++) {
const key = keys[i]
obj = obj !== null ? obj[key] : undefined
}
return obj
}
/**
* Helper to extract match criteria
*/
export function matchCriteria(item, fields) {
const match = {}
for (const field of fields) {
match[field] = lookupPath(item, field)
}
return match
}
/**
* Parse item
*/
export function parseItem(item, Model, config) {
//Get config
const {ensureModel, toObjectConfig} = config
//Ensure item is a model, to allow inclusion of default values
if (ensureModel && !(item instanceof Model)) {
item = new Model(item)
}
//Convert to plain object now if model given
if (item instanceof Model) {
item = item.toObject(toObjectConfig)
}
//Return
return item
}