-
Notifications
You must be signed in to change notification settings - Fork 24
/
is-where.js
60 lines (56 loc) · 1.41 KB
/
is-where.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
const entries = require('object.entries');
const _isWhere = (where, target) => {
for (const [key, value] of entries(where)) {
if (typeof value === 'object') {
if (!(Boolean(target[key]) && _isWhere(value, target[key]))) {
return false;
}
}
else if (key === 'nodeName') {
if (typeof target.nodeName === 'function') {
if (target.nodeName.name !== value && target.nodeName.displayName !== value) {
return false;
}
}
else if (/[a-z]/.test(value[0])) {
if (target.nodeName !== value) {
return false;
}
}
else {
return false;
}
}
else if (key === 'class') {
let attr = target.class || target.className;
if (typeof attr === 'object') {
attr = Object.keys(attr).filter(key => attr[key]);
}
else if (typeof attr === 'string') {
attr = attr.split(/\s+/);
}
if (!attr || attr.indexOf(value) === -1) {
return false;
}
}
else if (value === null) {
if (!(key in target)) {
return false;
}
}
else if (Array.isArray(value)) {
if (!value.some(test => target[key] === test)) {
return false;
}
}
else if (!target || target[key] !== value) {
return false;
}
}
return true;
};
const isWhere = where => value => _isWhere(where, value);
module.exports = {
_isWhere,
isWhere,
};