@lumjs/core 1.12.1 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/obj/getmethods.js +151 -0
- package/lib/obj/index.js +3 -1
- package/lib/obj/ns.js +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
const types = require('../types');
|
|
2
|
+
const {F, S, B, needType, needObj, isObj, notNil, def} = types;
|
|
3
|
+
|
|
4
|
+
// Default method names we don't want to include.
|
|
5
|
+
const DEFAULT_FILTER_NAMES =
|
|
6
|
+
[
|
|
7
|
+
'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
|
|
8
|
+
'toString', 'toLocaleString', 'valueOf',
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
// Default method prefixes we don't want to include.
|
|
12
|
+
const DEFAULT_FILTER_PREFIXES = ['_', '$'];
|
|
13
|
+
|
|
14
|
+
class MethodFilter
|
|
15
|
+
{
|
|
16
|
+
constructor(opts, ...args)
|
|
17
|
+
{
|
|
18
|
+
if (typeof opts === B)
|
|
19
|
+
{ // A simple way of specifying defaults mode.
|
|
20
|
+
opts = {defaults: opts};
|
|
21
|
+
}
|
|
22
|
+
else if (!isObj(opts) || opts instanceof RegExp)
|
|
23
|
+
{ // Something other than options was passed.
|
|
24
|
+
if (notNil(opts))
|
|
25
|
+
args.unshift(opts);
|
|
26
|
+
opts = {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const defaults = opts.defaults ?? true;
|
|
30
|
+
this.returns = opts.returns ?? !defaults;
|
|
31
|
+
|
|
32
|
+
this.names = defaults ? Array.from(DEFAULT_FILTER_NAMES) : [];
|
|
33
|
+
this.prefixes = defaults ? Array.from(DEFAULT_FILTER_PREFIXES) : [];
|
|
34
|
+
this.tests = [];
|
|
35
|
+
this.rules = [];
|
|
36
|
+
|
|
37
|
+
for (const arg of args)
|
|
38
|
+
{
|
|
39
|
+
this.add(arg);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
add(arg)
|
|
44
|
+
{
|
|
45
|
+
if (typeof arg === S)
|
|
46
|
+
{
|
|
47
|
+
if (arg.length === 1)
|
|
48
|
+
{
|
|
49
|
+
this.prefixes.push(arg);
|
|
50
|
+
}
|
|
51
|
+
else
|
|
52
|
+
{
|
|
53
|
+
this.names.push(arg);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else if (typeof arg === F)
|
|
57
|
+
{
|
|
58
|
+
this.tests.push(arg);
|
|
59
|
+
}
|
|
60
|
+
else if (arg instanceof RegExp)
|
|
61
|
+
{
|
|
62
|
+
this.rules.push(arg);
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
{
|
|
66
|
+
console.error("Unsupported argument", arg, this);
|
|
67
|
+
}
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
test(elem)
|
|
72
|
+
{
|
|
73
|
+
const rt = this.returns;
|
|
74
|
+
const rf = !rt;
|
|
75
|
+
|
|
76
|
+
if (this.prefixes.includes(elem[0])) return rt;
|
|
77
|
+
if (this.names.includes(elem)) return rt;
|
|
78
|
+
|
|
79
|
+
for (const rule of this.rules)
|
|
80
|
+
{
|
|
81
|
+
if (rule.test(elem)) return rt;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (const test of this.tests)
|
|
85
|
+
{
|
|
86
|
+
if (test.call(this, arguments)) return rt;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return rf;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for(obj)
|
|
93
|
+
{
|
|
94
|
+
return getMethods(obj, this);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
def(MethodFilter)
|
|
99
|
+
('DEFAULT_FILTER_NAMES', DEFAULT_FILTER_NAMES)
|
|
100
|
+
('DEFAULT_FILTER_PREFIXES', DEFAULT_FILTER_PREFIXES)
|
|
101
|
+
|
|
102
|
+
function getMethods(obj, filter)
|
|
103
|
+
{
|
|
104
|
+
needObj(obj, true);
|
|
105
|
+
|
|
106
|
+
if (!(filter instanceof MethodFilter))
|
|
107
|
+
{
|
|
108
|
+
if (Array.isArray(filter))
|
|
109
|
+
{
|
|
110
|
+
filter = new MethodFilter(...filter);
|
|
111
|
+
}
|
|
112
|
+
else
|
|
113
|
+
{
|
|
114
|
+
filter = new MethodFilter(filter);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const props = new Set();
|
|
119
|
+
let cur = obj;
|
|
120
|
+
do
|
|
121
|
+
{
|
|
122
|
+
Object.getOwnPropertyNames(cur)
|
|
123
|
+
.filter(i => filter.test(i))
|
|
124
|
+
.map(i => props.add(i));
|
|
125
|
+
}
|
|
126
|
+
while ((cur = Object.getPrototypeOf(cur)));
|
|
127
|
+
return [...props.keys()].filter(i => typeof obj[i] === F);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
def(getMethods, 'exclude', function()
|
|
131
|
+
{ // Get a MethodFilter using defaults (exclude mode).
|
|
132
|
+
return new MethodFilter(true, ...arguments);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
def(getMethods, 'include', function()
|
|
136
|
+
{ // Get a MethodFilter using include mode (no defaults).
|
|
137
|
+
return new MethodFilter(false, ...arguments);
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
function signatureOf(fn, name)
|
|
141
|
+
{
|
|
142
|
+
needType(F, fn);
|
|
143
|
+
const fns = fn.toString();
|
|
144
|
+
const fnt = fns.slice(0, fns.indexOf(')')+1);
|
|
145
|
+
return (typeof name === S) ? fnt.replace('function', name) : fnt;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports =
|
|
149
|
+
{
|
|
150
|
+
getMethods, signatureOf, MethodFilter,
|
|
151
|
+
}
|
package/lib/obj/index.js
CHANGED
|
@@ -10,6 +10,7 @@ const {lock,addLock} = require('./lock');
|
|
|
10
10
|
const {mergeNested,syncNested} = require('./merge');
|
|
11
11
|
const ns = require('./ns');
|
|
12
12
|
const getProperty = require('./getproperty');
|
|
13
|
+
const {getMethods,signatureOf,MethodFilter} = require('./getmethods');
|
|
13
14
|
const
|
|
14
15
|
{
|
|
15
16
|
getObjectPath,setObjectPath,
|
|
@@ -21,5 +22,6 @@ module.exports =
|
|
|
21
22
|
CLONE, clone, addClone, cloneIfLocked, lock, addLock,
|
|
22
23
|
mergeNested, syncNested, copyProps, copyAll, ns,
|
|
23
24
|
getObjectPath, setObjectPath, getNamespace, setNamespace,
|
|
24
|
-
getProperty, duplicateAll, duplicateOne,
|
|
25
|
+
getProperty, duplicateAll, duplicateOne, getMethods, signatureOf,
|
|
26
|
+
MethodFilter,
|
|
25
27
|
}
|
package/lib/obj/ns.js
CHANGED
|
@@ -169,11 +169,12 @@ function setObjectPath(obj, proppath, opts={})
|
|
|
169
169
|
const nsc = proppath.length;
|
|
170
170
|
const lastns = nsc - 1;
|
|
171
171
|
|
|
172
|
-
//console.debug("setObjectPath", obj, proppath, opts, nsc, arguments);
|
|
172
|
+
//console.debug("setObjectPath", {obj, proppath, opts, nsc, arguments});
|
|
173
173
|
|
|
174
174
|
for (let n = 0; n < nsc; n++)
|
|
175
175
|
{
|
|
176
176
|
const ns = proppath[n];
|
|
177
|
+
//console.debug("setObjectPath:loop", {n, ns, cns});
|
|
177
178
|
|
|
178
179
|
if (cns[ns] === undefined)
|
|
179
180
|
{ // Nothing currently here. Let's fix that.
|