-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeclaraoids.js
More file actions
78 lines (61 loc) · 1.79 KB
/
declaraoids.js
File metadata and controls
78 lines (61 loc) · 1.79 KB
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
var parser = require('./parser');
module.exports = new Proxy({}, {
get (target, property) {
return finder(property);
}
});
function finder(query) {
var parsed = parser(query);
var mapFunc = generateMapFunction(parsed);
var filterFunc = generateFilterFunction(parsed);
return (array, args) => {
return array
.filter(filterFunc(args))
.map(mapFunc);
}
}
function generateMapFunction(parsed) {
if (parsed.find.length == 0) {
return e => e;
}
if (parsed.find.length == 1) {
return e => findNested(e, parsed.find[0].prop);
}
return e => {
var obj = {};
parsed.find.forEach(find => {
obj[find.name] = findNested(e, find.prop);
});
return obj;
};
}
var functions = {
equals: (value, compareWith) => value === compareWith,
noteequals: (value, compareWith) => value !== compareWith,
lessthan: (value, compareWith) => value < compareWith,
greaterthan: (value, compareWith) => value > compareWith,
includes: (value, compareWith) => value.includes(compareWith)
};
function generateFilterFunction(parsed) {
var filters = [];
parsed.where.forEach(where => {
var func = functions[where.comparison];
filters.push((e, args) => {
var value = findNested(e, where.property);
var compareWith = args[where.input];
return func(value, compareWith);
})
});
return args => e => filters.every(f => f(e, args));
}
function findNested(start, path) {
if (!path.includes('_')) {
return start[path];
}
var properties = path.split('_');
var current = start;
for (var i = 0; i < properties.length; i++) {
current = current[properties[i]];
}
return current;
}