@lumjs/core 1.38.1 → 1.38.2
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/index.js +4 -4
- package/lib/obj/ns.js +96 -20
- package/lib/objectid.js +1 -1
- package/lib/types/isa.js +4 -4
- package/package.json +1 -1
package/lib/obj/index.js
CHANGED
|
@@ -20,16 +20,16 @@ const unlocked = require('./unlocked');
|
|
|
20
20
|
|
|
21
21
|
const
|
|
22
22
|
{
|
|
23
|
-
getObjectPath,setObjectPath,
|
|
24
|
-
getNamespace,setNamespace,
|
|
23
|
+
getObjectPath,setObjectPath,delObjectPath,
|
|
24
|
+
getNamespace,setNamespace,nsFactory,
|
|
25
25
|
} = ns;
|
|
26
26
|
|
|
27
27
|
module.exports =
|
|
28
28
|
{
|
|
29
29
|
assignd, cp, CLONE, clone, addClone, cloneIfLocked, lock, addLock,
|
|
30
30
|
df, dfa, dfb, dfc, lazy,
|
|
31
|
-
mergeNested, syncNested, copyProps, copyAll, ns,
|
|
32
|
-
getObjectPath, setObjectPath, getNamespace, setNamespace,
|
|
31
|
+
mergeNested, syncNested, copyProps, copyAll, ns, nsFactory,
|
|
32
|
+
getObjectPath, setObjectPath, delObjectPath, getNamespace, setNamespace,
|
|
33
33
|
getProperty, duplicateAll, duplicateOne, getMethods, signatureOf,
|
|
34
34
|
MethodFilter, apply, flip, flipKeyVal, flipMap, unlocked,
|
|
35
35
|
}
|
package/lib/obj/ns.js
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
const
|
|
3
3
|
{
|
|
4
4
|
B, S,
|
|
5
|
-
root, isObj, needObj, def, nonEmptyArray, isNil,
|
|
5
|
+
root, isObj, needObj, def, nonEmptyArray, isNil,
|
|
6
6
|
doesDescriptorTemplate,
|
|
7
7
|
} = require('../types');
|
|
8
8
|
|
|
9
|
+
const {df} = require('./df');
|
|
10
|
+
const cp = Object.assign;
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* Need a String or Array
|
|
11
14
|
* @alias module:@lumjs/core/obj.SOA
|
|
@@ -79,9 +82,13 @@ exports.nsArray = nsArray;
|
|
|
79
82
|
*
|
|
80
83
|
* @param {(object|function)} obj - Object we're looking in.
|
|
81
84
|
* @param {(string|Array)} proppath - Property path we're looking for.
|
|
82
|
-
*
|
|
85
|
+
*
|
|
86
|
+
* Generally a string of dot (`.`) separated nested property names.
|
|
87
|
+
*
|
|
83
88
|
* @param {(object|boolean)} [opts] Options changing the behaviours.
|
|
84
|
-
*
|
|
89
|
+
*
|
|
90
|
+
* If this is a `boolean` it's assumed to be the `opts.log` option.
|
|
91
|
+
*
|
|
85
92
|
* @param {boolean} [opts.log=false] Log errors for missing namespaces?
|
|
86
93
|
* @param {boolean} [opts.allowFun=true] Allow `obj` to be a `function` ?
|
|
87
94
|
*
|
|
@@ -129,15 +136,33 @@ exports.getObjectPath = getObjectPath;
|
|
|
129
136
|
*
|
|
130
137
|
* @param {(object|function)} obj - Object the property path is for.
|
|
131
138
|
* @param {(string|Array)} proppath - Property path to create.
|
|
132
|
-
* @param {object} [opts] Options changing the behaviours.
|
|
139
|
+
* @param {(object|boolean)} [opts] Options changing the behaviours.
|
|
140
|
+
*
|
|
141
|
+
* If this is a `boolean` it's assumed to be the `opts.overwrite` option.
|
|
142
|
+
*
|
|
133
143
|
* @param {*} [opts.value] A value to assign to the last property path.
|
|
134
144
|
* @param {boolean} [opts.overwrite=false] Allow overwriting property paths.
|
|
135
|
-
*
|
|
145
|
+
*
|
|
146
|
+
* Only applicable if `opts.value` was also specified.
|
|
147
|
+
*
|
|
136
148
|
* @param {object} [opts.desc] Descriptor rules for defining the properties.
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
149
|
+
*
|
|
150
|
+
* Must NOT contain `value`, `get`, or `set` properties.
|
|
151
|
+
* Only, `configurable`, `enumerable`, and `writable` are supported.
|
|
152
|
+
* Will be ignored if `opts.assign` is `true`.
|
|
153
|
+
*
|
|
154
|
+
* NOTE: This option will likely be removed in version 2.x, as you can
|
|
155
|
+
* use `opts.df` to set descriptor rules instead.
|
|
156
|
+
*
|
|
140
157
|
* @param {boolean} [opts.assign=false] Use direct assignment instead of `def()`.
|
|
158
|
+
* @param {(boolean|object)} [opts.df] Use `df()` instead of `def()`?
|
|
159
|
+
*
|
|
160
|
+
* If this is an object, it will be used as options for the df() function.
|
|
161
|
+
*
|
|
162
|
+
* NOTE: In version 2.x, the boolean use of this option will be removed,
|
|
163
|
+
* as df() will replace def() entirely in that release.
|
|
164
|
+
*
|
|
165
|
+
* @param {boolean} [opts.allowFun=true] See getObjectPath() for details.
|
|
141
166
|
* @param {boolean} [opts.returnThis=false] Return `this` variable.
|
|
142
167
|
* @param {boolean} [opts.returnObj=false] Return the `obj` parameter.
|
|
143
168
|
* @return {*} Generally the last object in the nested property paths.
|
|
@@ -146,28 +171,35 @@ exports.getObjectPath = getObjectPath;
|
|
|
146
171
|
*/
|
|
147
172
|
function setObjectPath(obj, proppath, opts={})
|
|
148
173
|
{
|
|
149
|
-
|
|
150
|
-
|
|
174
|
+
if (typeof opts === B)
|
|
175
|
+
opts = {overwrite: opts};
|
|
176
|
+
else if (!isObj(opts))
|
|
177
|
+
opts = {};
|
|
178
|
+
|
|
179
|
+
needObj(obj, (opts.allowFun ?? true));
|
|
180
|
+
|
|
181
|
+
const setprop = opts.df ? df : def;
|
|
182
|
+
const propopts = cp({}, opts.def, opts.df);
|
|
151
183
|
|
|
152
184
|
proppath = nsArray(proppath);
|
|
153
185
|
|
|
154
186
|
let assign;
|
|
155
187
|
if (opts.assign)
|
|
156
188
|
{ // Use direct property assignment.
|
|
157
|
-
assign = (
|
|
189
|
+
assign = (t,p,v) => t[p] = v;
|
|
158
190
|
}
|
|
159
191
|
else if (doesDescriptorTemplate(opts.desc))
|
|
160
192
|
{ // An explicit descriptor.
|
|
161
|
-
assign = (
|
|
193
|
+
assign = (t,p,v) =>
|
|
162
194
|
{
|
|
163
|
-
const desc =
|
|
195
|
+
const desc = cp({}, opts.desc);
|
|
164
196
|
desc.value = v;
|
|
165
|
-
|
|
197
|
+
setprop(t,p,desc,propopts);
|
|
166
198
|
}
|
|
167
199
|
}
|
|
168
200
|
else
|
|
169
201
|
{ // Use def with default descriptor.
|
|
170
|
-
assign = (
|
|
202
|
+
assign = (t,p,v) => setprop(t,p,v,propopts);
|
|
171
203
|
}
|
|
172
204
|
|
|
173
205
|
let cns = obj;
|
|
@@ -183,17 +215,17 @@ function setObjectPath(obj, proppath, opts={})
|
|
|
183
215
|
|
|
184
216
|
if (cns[ns] === undefined)
|
|
185
217
|
{ // Nothing currently here. Let's fix that.
|
|
186
|
-
if (n == lastns &&
|
|
187
|
-
{ //
|
|
218
|
+
if (n == lastns && opts.value !== undefined)
|
|
219
|
+
{ // It's the end of the world as we know it.
|
|
188
220
|
assign(cns, ns, opts.value);
|
|
189
221
|
}
|
|
190
222
|
else
|
|
191
223
|
{ // Create a new empty object.
|
|
192
|
-
assign(cns, ns);
|
|
224
|
+
assign(cns, ns, {});
|
|
193
225
|
}
|
|
194
226
|
}
|
|
195
|
-
else if (opts.overwrite && n == lastns &&
|
|
196
|
-
{ // We have a value, and overwrite mode is on.
|
|
227
|
+
else if (opts.overwrite && n == lastns && opts.value !== undefined)
|
|
228
|
+
{ // We have a defined value, and overwrite mode is on.
|
|
197
229
|
assign(cns, ns, opts.value);
|
|
198
230
|
}
|
|
199
231
|
|
|
@@ -251,11 +283,54 @@ function delObjectPath(obj, proppath, opts={})
|
|
|
251
283
|
|
|
252
284
|
exports.delObjectPath = delObjectPath;
|
|
253
285
|
|
|
286
|
+
/**
|
|
287
|
+
* Build a simple factory object for working with property paths.
|
|
288
|
+
*
|
|
289
|
+
* @param {(object|function)} value - The target object
|
|
290
|
+
* @param {object} [opts] Default options for the *ObjectPath() functions
|
|
291
|
+
* @returns {object} A frozen factory object
|
|
292
|
+
*
|
|
293
|
+
* Has `get(path,opts)`, `set(path,opts)`, and `del(path,opts)` methods,
|
|
294
|
+
* which are wrappers for getObjectPath(), setObjectPath(),
|
|
295
|
+
* and delObjectPath() respectively.
|
|
296
|
+
*
|
|
297
|
+
* Also has `value` and `opts` properties for the arguments.
|
|
298
|
+
*/
|
|
299
|
+
function nsFactory(value, opts={})
|
|
300
|
+
{
|
|
301
|
+
const getOpts = (po) => cp({}, opts, po);
|
|
302
|
+
|
|
303
|
+
const factory =
|
|
304
|
+
{
|
|
305
|
+
value,
|
|
306
|
+
opts,
|
|
307
|
+
get(pp,po)
|
|
308
|
+
{
|
|
309
|
+
return getObjectPath(value, pp, getOpts(po));
|
|
310
|
+
},
|
|
311
|
+
set(pp,po)
|
|
312
|
+
{
|
|
313
|
+
setObjectPath(value, pp, getOpts(po));
|
|
314
|
+
return this;
|
|
315
|
+
},
|
|
316
|
+
del(pp,po)
|
|
317
|
+
{
|
|
318
|
+
delObjectPath(value, pp, getOpts(po));
|
|
319
|
+
return this;
|
|
320
|
+
},
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return Object.freeze(factory);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
exports.nsFactory = nsFactory;
|
|
327
|
+
|
|
254
328
|
/**
|
|
255
329
|
* Get a global namespace path if it exists.
|
|
256
330
|
*
|
|
257
331
|
* This literally just calls `getObjectPath()` on the `root` object.
|
|
258
332
|
*
|
|
333
|
+
* @deprecated
|
|
259
334
|
* @param {(string|Array)} proppath - Property path we're looking for.
|
|
260
335
|
* Generally a string of dot (`.`) separated nested property names.
|
|
261
336
|
* @param {object} [opts] See `getObjectPath()` for details.
|
|
@@ -275,6 +350,7 @@ exports.getNamespace = getNamespace;
|
|
|
275
350
|
*
|
|
276
351
|
* This literally just calls `setObjectPath()` on the `root` object.
|
|
277
352
|
*
|
|
353
|
+
* @deprecated
|
|
278
354
|
* @param {(string|Array)} proppath - Property path to create.
|
|
279
355
|
* @param {object} [opts] See `setObjectPath()` for details.
|
|
280
356
|
* @return {*} See `setObjectPath()` for details.
|
package/lib/objectid.js
CHANGED
|
@@ -116,7 +116,7 @@ class UniqueObjectIds
|
|
|
116
116
|
id += this.$options.prefix;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
let className = obj.constructor.name;
|
|
119
|
+
let className = (typeof obj === F ? obj : obj.constructor).name;
|
|
120
120
|
|
|
121
121
|
if (typeof cno.setup === F)
|
|
122
122
|
{ // Perform a transformation before any other changes.
|
package/lib/types/isa.js
CHANGED
|
@@ -398,7 +398,7 @@ class OfTest
|
|
|
398
398
|
*
|
|
399
399
|
* This will be used to set the value of the `this.valid` property.
|
|
400
400
|
*
|
|
401
|
-
* @param {object}
|
|
401
|
+
* @param {object} rulesets - Object to set as the `this.rules` property.
|
|
402
402
|
*
|
|
403
403
|
* If `valid(rules)` returns `true` then the `rules` will be
|
|
404
404
|
* changed to `{value: rules}`. This is a short-cut so that
|
|
@@ -409,7 +409,7 @@ class OfTest
|
|
|
409
409
|
*
|
|
410
410
|
* See the class property description for the supported optional values.
|
|
411
411
|
*
|
|
412
|
-
* @param {object}
|
|
412
|
+
* @param {object} rulesets.value - The container being tested.
|
|
413
413
|
*
|
|
414
414
|
* @param {?Array} valueTypes - An array of type tests for the values.
|
|
415
415
|
*
|
|
@@ -839,7 +839,7 @@ const isntRules = v => (isObj(v) && !(v instanceof OfTestRules));
|
|
|
839
839
|
* test function that will return an instance of the `OfTest.Rules` class,
|
|
840
840
|
* passing all parameters along to the constructor.
|
|
841
841
|
*
|
|
842
|
-
* @param {object}
|
|
842
|
+
* @param {object} rulesets - Rules for this test function.
|
|
843
843
|
*
|
|
844
844
|
* If this argument is anything other than a `OfTest.Rules` instance,
|
|
845
845
|
* it is assumed to be the `rules.value` named option.
|
|
@@ -852,7 +852,7 @@ const isntRules = v => (isObj(v) && !(v instanceof OfTestRules));
|
|
|
852
852
|
* const isValid = isObjOf(isObjOf.rules(value, opts), type1, type2, ...);
|
|
853
853
|
* ```
|
|
854
854
|
*
|
|
855
|
-
* @param {Iterable}
|
|
855
|
+
* @param {Iterable} rulesets.value - The actual list object for the tests.
|
|
856
856
|
*
|
|
857
857
|
* @param {...any} types - See {@link module:@lumjs/core/types.isa}.
|
|
858
858
|
*
|