@lumjs/core 1.38.0 → 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.
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
 
3
- const def = require('../types/def');
3
+ const {dfa} = require('./df');
4
+ const {isObj,isNil} = require('../types');
4
5
 
5
6
  /**
6
7
  * Copy properties into a target object.
7
8
  *
8
- * Like Object.assign(), but it uses getOwnPropertyDescriptors()
9
- * to get the properties to copy and type.def() to assign them.
9
+ * Like Object.assign(), but it uses getOwnPropertyDescriptors() to get
10
+ * the properties to copy, and [dfa()]{@link module:@lumjs/core/obj.dfa}
11
+ * to assign them.
10
12
  *
11
13
  * Basically a super simple, non-recursive replacement for my copyProps()
12
14
  * and cp() functions that were overly complex and tried to do way too much.
@@ -21,8 +23,15 @@ function assignd (target, ...sources)
21
23
  {
22
24
  for (const src of sources)
23
25
  {
24
- const descs = Object.getOwnPropertyDescriptors(src);
25
- def(target, descs);
26
+ if (isObj(src))
27
+ {
28
+ const descs = Object.getOwnPropertyDescriptors(src);
29
+ dfa(target, descs);
30
+ }
31
+ else if (!isNil(src))
32
+ {
33
+ console.error("Invalid assignd source", {src, sources, target});
34
+ }
26
35
  }
27
36
  return target;
28
37
  }
package/lib/obj/df.js CHANGED
@@ -3,7 +3,6 @@
3
3
  const {doesDescriptor,needObj,needType,lazy,B,TYPES} = require('../types');
4
4
 
5
5
  const DEF_DESC = {configurable: true}
6
- const DEF_OPTS = {autoDesc: true}
7
6
 
8
7
  const cp = Object.assign;
9
8
  const clone = (...args) => cp({}, ...args);
@@ -66,29 +65,38 @@ function descriptor(desc, opts)
66
65
  * @param {(string|symbol)} name - Property identifier
67
66
  * @param {*} value - Value to assign
68
67
  *
69
- * If `opts.autoDesc` is `true` and this is an `object` that passes the
70
- * {@link module:@lumjs/core/types.doesDescriptor} test, it will be
71
- * used as the template descriptor to assign the property.
72
- *
73
- * In any other case, this will be used as the `value` property of
74
- * a generated descriptor to be assigned.
68
+ * See `opts.useDesc` for how this value is handled in regards to
69
+ * whether it is treated as a property descriptor or a plain data value.
75
70
  *
76
71
  * @param {object} [opts] Options
77
72
  *
73
+ * See {@link module:@lumjs/core/obj~descriptor} for options that
74
+ * are used as descriptor defaults when assigning the property.
75
+ *
76
+ * @param {?boolean} [opts.useDesc=null] Treat `value` is a descriptor?
77
+ *
78
+ * If this is set to `true` then the `value` will be assumed to be a
79
+ * valid descriptor object; no tests will be done to confirm, so be sure!
80
+ *
81
+ * If this is set to `false` then a descriptor of `{value}` will be created.
82
+ * Again, no tests will be done, so this is a way to assign properties that
83
+ * have `value`, `get`, or `set` properties in them.
84
+ *
85
+ * If this is `null` (the default), then the `value` will be tested with
86
+ * [doesDescriptor()]{@link module:@lumjs/core/types.doesDescriptor},
87
+ * and that will determine if the value is treated as a descriptor or not.
88
+ *
78
89
  * @returns {object} `obj`
79
90
  * @alias module:@lumjs/core/obj.df
80
91
  */
81
- function df(obj, name, value, opts)
92
+ function df(obj, name, value, opts={})
82
93
  {
83
94
  const log = {obj,name,value,opts};
84
95
  needObj(obj, {log, fun: true});
85
96
  needType(TYPES.PROP, name, {log});
86
97
 
87
- opts = clone(DEF_OPTS, opts);
88
-
89
- const desc = descriptor(((opts.autoDesc && doesDescriptor(value))
90
- ? value
91
- : {value}), opts);
98
+ const useDesc = opts.useDesc ?? doesDescriptor(value);
99
+ const desc = descriptor((useDesc ? value : {value}), opts);
92
100
 
93
101
  return Object.defineProperty(obj, name, desc);
94
102
  }
@@ -121,11 +129,35 @@ function dfa(obj, values, opts)
121
129
  return obj;
122
130
  }
123
131
 
132
+ df(df, 'all', dfa);
133
+
134
+ /**
135
+ * A wrapper around df() that defines an *accessor property* with
136
+ * BOTH a getter and a setter specified explicitly.
137
+ *
138
+ * @param {(object|function)} obj - Target to define property in
139
+ * @param {(string|symbol)} name - Property identifier
140
+ * @param {function} getter - Getter function
141
+ * @param {function} setter - Setter function
142
+ * @param {object} [opts] Options
143
+ *
144
+ * The actual options passed to df() will be a clone, with the
145
+ * `useDesc` option explicitly forced to `true`.
146
+ *
147
+ */
148
+ function dfb(obj, name, getter, setter, opts)
149
+ {
150
+ const log = {obj,name,getter,setter,opts};
151
+ opts = clone(opts, {useDesc: true});
152
+ return df(obj, name, {get: getter, set: setter}, opts);
153
+ }
154
+
155
+ df(df, 'both', dfb);
156
+
124
157
  /**
125
158
  * Build magic closures to define properties using a chained syntax.
126
159
  *
127
- * This actually creates three closures, one around df(), another
128
- * that wraps dfa(), and one that wraps the lazy() function.
160
+ * This actually creates closures for df(), dfa(), dfb(), and lazy().
129
161
  * You can switch between them using magic properties.
130
162
  *
131
163
  * The closures when called will pass their arguments to the function
@@ -136,13 +168,15 @@ function dfa(obj, values, opts)
136
168
  *
137
169
  * - `one(name, value)` → A closure that wraps df()
138
170
  * - `all(values)` → A closure that wraps dfa()
171
+ * - `both(name, g, s)` → A closure that wraps dfb()
172
+ * - `lazy(name, func)` → A closure that wraps lazy()
139
173
  * - `update(opts)` → Update the options used by the closures.
140
174
  * Uses Object.assign() to copy options from the specied object
141
175
  * to the internal options object from the original dfc() call.
142
176
  * This is a method call, NOT a closure function.
143
177
  * - `opts` → The actual options object used by the closures.
144
178
  * - `obj` → The target object used by the closures.
145
- * - `next(obj)` → A wrapper method that will call dfc().
179
+ * - `for(obj)` → A wrapper method that will call dfc().
146
180
  * When using this method, a *new set of closures* will be created,
147
181
  * with the current options, but a new target object.
148
182
  * If you use this in a chained statement, any calls after this
@@ -170,6 +204,12 @@ function dfc(obj, opts={})
170
204
  return fnAll;
171
205
  }
172
206
 
207
+ const fnBoth = function (name, getter, setter)
208
+ {
209
+ dfb(obj, name, getter, setter, opts);
210
+ return fnBoth;
211
+ }
212
+
173
213
  const fnLazy = function (name, initfunc)
174
214
  {
175
215
  lazy(obj, name, initfunc, opts);
@@ -182,6 +222,8 @@ function dfc(obj, opts={})
182
222
  opts: {value: opts},
183
223
  one: {value: fnOne},
184
224
  all: {value: fnAll},
225
+ both: {value: fnBoth},
226
+ lazy: {value: fnLazy},
185
227
  update:
186
228
  {
187
229
  value()
@@ -190,7 +232,7 @@ function dfc(obj, opts={})
190
232
  return this;
191
233
  },
192
234
  },
193
- next:
235
+ for:
194
236
  {
195
237
  value(newobj)
196
238
  {
@@ -199,15 +241,20 @@ function dfc(obj, opts={})
199
241
  },
200
242
  }
201
243
 
202
- dps(fnOne, ctx);
203
- dps(fnAll, ctx);
244
+ // Quick alias
245
+ ctx.next = ctx.for;
246
+
247
+ dps(fnOne, ctx);
248
+ dps(fnAll, ctx);
249
+ dps(fnBoth, ctx);
250
+ dps(fnLazy, ctx);
204
251
 
205
252
  return fnOne;
206
253
  }
207
254
 
208
-
255
+ df(df, 'for', dfc);
209
256
 
210
257
  module.exports =
211
258
  {
212
- df, dfa, dfc, lazy,
259
+ df, dfa, dfb, dfc, lazy,
213
260
  }
package/lib/obj/index.js CHANGED
@@ -8,7 +8,7 @@ const assignd = require('./assignd');
8
8
  const {copyAll,duplicateOne,duplicateAll} = require('./copyall');
9
9
  const copyProps = require('./copyprops');
10
10
  const {CLONE,clone,addClone,cloneIfLocked} = require('./clone');
11
- const {df,dfa,dfc,lazy} = require('./df');
11
+ const {df,dfa,dfb,dfc,lazy} = require('./df');
12
12
  const {flip, flipKeyVal, flipMap} = require('./flip');
13
13
  const {getMethods,signatureOf,MethodFilter} = require('./getmethods');
14
14
  const getProperty = require('./getproperty');
@@ -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
- df, dfa, dfc, lazy,
31
- mergeNested, syncNested, copyProps, copyAll, ns,
32
- getObjectPath, setObjectPath, getNamespace, setNamespace,
30
+ df, dfa, dfb, dfc, lazy,
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, notNil,
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
- * Generally a string of dot (`.`) separated nested property names.
85
+ *
86
+ * Generally a string of dot (`.`) separated nested property names.
87
+ *
83
88
  * @param {(object|boolean)} [opts] Options changing the behaviours.
84
- * If this is a `boolean` it's assumed to be the `opts.log` option.
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
- * Only applicable if `opts.value` was also specified.
145
+ *
146
+ * Only applicable if `opts.value` was also specified.
147
+ *
136
148
  * @param {object} [opts.desc] Descriptor rules for defining the properties.
137
- * Must NOT contain `value`, `get`, or `set` properties.
138
- * Only, `configurable`, `enumerable`, and `writable` are supported.
139
- * Will be ignored if `opts.assign` is `true`.
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
- needObj(obj, true, 'obj parameter must be an object or function');
150
- needObj(opts, 'opts parameter must be an object');
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 = (o,p,v={}) => o[p] = v;
189
+ assign = (t,p,v) => t[p] = v;
158
190
  }
159
191
  else if (doesDescriptorTemplate(opts.desc))
160
192
  { // An explicit descriptor.
161
- assign = (o,p,v={}) =>
193
+ assign = (t,p,v) =>
162
194
  {
163
- const desc = Object.assign({}, opts.desc);
195
+ const desc = cp({}, opts.desc);
164
196
  desc.value = v;
165
- def(o,p,desc);
197
+ setprop(t,p,desc,propopts);
166
198
  }
167
199
  }
168
200
  else
169
201
  { // Use def with default descriptor.
170
- assign = (o,p,v={}) => def(o,p,v);
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 && notNil(opts.value))
187
- { // We're at the end and have a value to assign.
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 && notNil(opts.value))
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/def.js CHANGED
@@ -7,13 +7,13 @@ const clone = (...args) => Object.assign({}, ...args);
7
7
  /**
8
8
  * A wrapper around `Object.defineProperty()` with added flair!
9
9
  *
10
- * FUTURE DEPRECATION: When I release @lumjs/core 2.0, this function
11
- * will be deprecated! It's gotten almost as convoluted as the old
12
- * prop() method it replaced from my original Nano.js libraries.
10
+ * **FUTURE DEPRECATION**: this function will be deprecated in v1.19.0!
11
+ * It's gotten almost as convoluted as the old prop() method it replaced
12
+ * from my original Nano.js libraries.
13
+ *
13
14
  * I have written new `obj.{df,dfa,dfc}` functions that will replace this
14
15
  * function going forward. The related `types.lazy()` function will also
15
- * be refactored to use the new functions, and moved to the `obj` module,
16
- * although for the `2.x` lifetime an alias will remain in the `type` module.
16
+ * be refactored to use the new functions, and moved to the `obj` module.
17
17
  *
18
18
  * @param {(object|function)} obj - The object to add a property to.
19
19
  *
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} rules - Object to set as the `this.rules` property.
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} rules.value - The container being tested.
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} rules - Rules for this test function.
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} rules.value - The actual list object for the tests.
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
  *
package/lib/types/lazy.js CHANGED
@@ -101,11 +101,11 @@ const {doesDescriptor} = require('./basics');
101
101
  * This is an extension of the [def()]{@link module:@lumjs/core/types.def}
102
102
  * method, and indeed an alias called `def.lazy()` is also available.
103
103
  *
104
- * IMPORTANT: In version 2.x, this will be refactored to use the new
104
+ * IMPORTANT: In a later v1.18.x release, this will be refactored to use the
105
105
  * [obj.df()]{@link module:@lumjs/core/obj.df} function that will replace
106
106
  * def(). There's already an alias called `df.lazy()` available now.
107
- * This will also be moved to the `obj` module, with an alias left
108
- * in the `types` module for the duration of the 2.x lifecycle.
107
+ * This will also be moved to the `obj` module, with a deprecated alias left
108
+ * in the `types` module for the duration of the 1.x lifecycle.
109
109
  *
110
110
  * @param {(object|function)} target - The object to add the property to.
111
111
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumjs/core",
3
- "version": "1.38.0",
3
+ "version": "1.38.2",
4
4
  "main": "lib/index.js",
5
5
  "exports":
6
6
  {