-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjustTwo.js
87 lines (76 loc) · 2.03 KB
/
justTwo.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
const JustTwo = (function() {
const hide = (object, property) => {
Object.defineProperty(object, property, {
// writable: false,
enumerable: false
});
};
function get(array) {
array.by = by;
array.and = array.by;
array.or = function(input, value) {
get(this.parent).by.existing = this;
return value
? get(this.parent).by(input)(value)
: get(this.parent).by(input);
};
hide(array, "or");
hide(array, "by");
hide(array, "and");
return array;
}
function by(property, value) {
if (this === undefined) {
return;
}
const array = Object.values(this);
let equals = value => compare(value, (a, b) => a === b);
equals.equals = equals;
const extras = {
equals,
greaterThan: test((a, b) => a > b),
lessThan: test((a, b) => a < b),
notEquals: test((a, b) => a !== b)
};
function test(fn) {
return function(value) {
return compare(value, fn);
};
}
equals = Object.assign(equals, extras);
Object.keys(extras).forEach(key => hide(equals, key));
return value ? equals(value) : equals;
function compare(value, fn) {
const result = get(
array.filter(
//comparator ||
arrayItem => {
try {
let result = arrayItem;
const props = property.split(".");
while (props.length) {
const curr = props.shift();
result = result[curr];
}
return fn(result, value);
} catch (error) {
//console.error(error);
return false;
}
}
)
);
while (by.existing && by.existing.length) {
const current = by.existing.shift();
if (!result.includes(current)) {
result[result.length] = current;
}
}
result.parent = array;
hide(result, "parent");
return result;
}
}
return { get, by };
})();
module && (module.exports = JustTwo);