-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
109 lines (88 loc) · 2.37 KB
/
lib.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/******/ (() => { // webpackBootstrap
/******/ "use strict";
;// CONCATENATED MODULE: ./node_modules/object-grep/src/index.js
function grep({target, searchExpr, depth, path, result}) {
if (depth < 1) {
return result;
}
Object.keys(target).forEach((key) => {
const keyPath = !path ? key : `${path}.${key}`;
const value = target[key];
if (key.match(searchExpr)) {
result.inKeys[keyPath] = value;
}
if (['string', 'number', 'boolean', 'undefined'].includes(typeof value) || value === null) {
if (String(value).match(searchExpr)) {
result.inValues[keyPath] = value;
}
return;
}
grep({
searchExpr,
result,
path: keyPath,
target: value,
depth: depth - 1,
});
});
return result;
}
function objectGrep(target, searchExpr, depth = 20) {
const result = Object.create({
short: function () {
return {
inKeys: Object.keys(this.inKeys),
inValues: Object.keys(this.inValues),
};
}
},
{
inValues: {writable: true, configurable: true, enumerable: true, value: {}},
inKeys: {writable: true, configurable: true, enumerable: true, value: {}}
}
);
const path = '';
if (!searchExpr) {
searchExpr = String(searchExpr);
}
return grep({
target,
searchExpr,
depth,
path,
result
});
}
const symbol = Symbol('grep');
objectGrep.inject = function (propertyName = 'grep') {
if (propertyName in Object.prototype) {
throw new Error(`Object.prototype already has ${propertyName}. Choose another name`);
}
const fn = function (regex, depth) {
return objectGrep(this, regex, depth);
};
fn.symbol = symbol;
Object.defineProperty(Object.prototype, propertyName, {
configurable: true,
enumerable: false,
value: fn
});
};
objectGrep.revoke = function () {
const prototype = Object.prototype;
Object.getOwnPropertyNames(Object.prototype).forEach(propertyName => {
if (prototype[propertyName] && prototype[propertyName].symbol && prototype[propertyName].symbol === symbol) {
delete prototype[propertyName];
}
});
};
;// CONCATENATED MODULE: ./src/lib.js
window.addEventListener("load", () => {
try {
objectGrep.inject('__grep__');
} catch(e) {
console.error('[Object grep error]: Object.prototype already has method grep. Injection failed.')
}
});
/******/ })()
;