@lumjs/core 1.12.0 → 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/clone.js CHANGED
@@ -272,7 +272,7 @@ function addClone(obj, defOpts=null)
272
272
  {
273
273
  if (!isObj(defOpts))
274
274
  { // Assign a default set of defaults.
275
- defOpts = {mode: CLONE.DEF, addClone: true, addLock: false};
275
+ defOpts = {addClone: true};
276
276
  }
277
277
 
278
278
  const defDesc = defOpts.cloneDesc ?? {};
@@ -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/lock.js CHANGED
@@ -22,7 +22,7 @@ const {B, isObj, def} = require('../types');
22
22
  { // Add the clone method before freezing.
23
23
  if (!isObj(cloneOpts))
24
24
  {
25
- cloneOpts = {mode: CLONE.DEF, addClone: true, addLock: true};
25
+ cloneOpts = {addClone: true, addLock: true};
26
26
  }
27
27
  addClone(obj, cloneOpts);
28
28
  }
@@ -64,4 +64,5 @@ const {B, isObj, def} = require('../types');
64
64
  exports.addLock = addLock;
65
65
 
66
66
  // Importing addLock at the end, just because.
67
- const {addClone} = require('./clone');
67
+ const {addClone} = require('./clone');
68
+
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.
package/lib/types/lazy.js CHANGED
@@ -206,7 +206,7 @@ function lazy(target, name, initfunc, opts={})
206
206
  }
207
207
 
208
208
  // Assign the lazy accessor property.
209
- def(target, name, desc);
209
+ return def(target, name, desc);
210
210
  }
211
211
 
212
212
  // Gotta be one of the greatest lines...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumjs/core",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "main": "lib/index.js",
5
5
  "exports":
6
6
  {