@lumjs/core 1.16.0 → 1.18.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.
@@ -0,0 +1,233 @@
1
+
2
+ const {N,def} = require('../types');
3
+
4
+ /**
5
+ * Functions for adding values to arrays in different ways.
6
+ * @alias module:@lumjs/core/arrays.add
7
+ */
8
+ const ArrayAdd = module.exports = exports =
9
+ {
10
+ prepend, before, append, after, insert,
11
+ }
12
+
13
+ function _addWith(array, oldValue, newValue, ifMissing, fn)
14
+ {
15
+ let pos = array.indexOf(oldValue);
16
+
17
+ if (pos === -1)
18
+ {
19
+ if (typeof ifMissing === N)
20
+ {
21
+ pos = ifMissing;
22
+ }
23
+ else
24
+ {
25
+ return false;
26
+ }
27
+ }
28
+
29
+ return fn(array, newValue, pos);
30
+ }
31
+
32
+ /**
33
+ * Prepend a value to an array.
34
+ *
35
+ * @param {Array} array - Array to add the value to.
36
+ * @param {*} value - Value to add to the array.
37
+ * @param {number} [pos=0] Position to prepend at.
38
+ *
39
+ * If `pos` is `0` then `array.unshift(value)` is used.
40
+ * Otherwise `array.splice(pos, 0, value)` is used.
41
+ *
42
+ * @returns {Array} `array`
43
+ * @alias module:@lumjs/core/arrays.add.prepend
44
+ */
45
+ function prepend(array, value, pos=0)
46
+ {
47
+ if (pos === 0)
48
+ {
49
+ array.unshift(value);
50
+ }
51
+ else
52
+ {
53
+ array.splice(pos, 0, value);
54
+ }
55
+
56
+ return array;
57
+ }
58
+
59
+ /**
60
+ * Append a value to an array.
61
+ *
62
+ * @param {Array} array - Array to add the value to.
63
+ * @param {*} value - Value to add to the array.
64
+ * @param {number} [pos=-1] Position to append at.
65
+ *
66
+ * If `pos` is `-1` then `array.push(value)` is used.
67
+ * Otherwise `array.splice(pos+1, 0, value)` is used.
68
+ *
69
+ * @returns {Array} `array`
70
+ * @alias module:@lumjs/core/arrays.add.append
71
+ */
72
+ function append(array, value, pos=-1)
73
+ {
74
+ if (pos === -1)
75
+ {
76
+ array.push(value);
77
+ }
78
+ else
79
+ {
80
+ array.splice(pos+1, 0, value);
81
+ }
82
+
83
+ return array;
84
+ }
85
+
86
+ /**
87
+ * Add a value to an array _before_ an existing value.
88
+ *
89
+ * This uses `prepend()` to add the value.
90
+ *
91
+ * @param {Array} array - Array to add the value to.
92
+ * @param {*} oldValue - Existing value to find.
93
+ * @param {*} newValue - Value to add to the array.
94
+ * @param {(number|false)} [ifMissing=0] If `oldValue` is not found.
95
+ *
96
+ * If this is a `number` it will be used as the `pos` argument.
97
+ *
98
+ * If this is `false` the function will not add the value at all,
99
+ * but will return `false` instead.
100
+ *
101
+ * @returns {(Array|false)} Usually the input `array`; but will return
102
+ * `false` if `ifMissing` was `false` and the `oldValue` was not found.
103
+ *
104
+ * @alias module:@lumjs/core/arrays.add.before
105
+ */
106
+ function before(array, oldValue, newValue, ifMissing=0)
107
+ {
108
+ return _addWith(array, oldValue, newValue, ifMissing, prepend);
109
+ }
110
+
111
+ /**
112
+ * Add a value to an array _after_ an existing value.
113
+ *
114
+ * This uses `append()` to add the value.
115
+ *
116
+ * @param {Array} array - Array to add the value to.
117
+ * @param {*} oldValue - Existing value to find.
118
+ * @param {*} newValue - Value to add to the array.
119
+ * @param {(number|false)} [ifMissing=-1] If `oldValue` is not found.
120
+ *
121
+ * If this is a `number` it will be used as the `pos` argument.
122
+ *
123
+ * If this is `false` the function will not add the value at all,
124
+ * but will return `false` instead.
125
+ *
126
+ * @returns {(Array|false)} Usually the input `array`; but will return
127
+ * `false` if `ifMissing` was `false` and the `oldValue` was not found.
128
+ *
129
+ * @alias module:@lumjs/core/arrays.add.after
130
+ */
131
+ function after(array, oldValue, newValue, ifMissing=-1)
132
+ {
133
+ return _addWith(array, oldValue, newValue, ifMissing, append);
134
+ }
135
+
136
+ /**
137
+ * Insert a value to an array using special logic.
138
+ *
139
+ * See the `pos` argument for the positioning logic used.
140
+ *
141
+ * @param {Array} array - Array to add the value to.
142
+ * @param {*} value - Value to add to the array.
143
+ * @param {number} [pos=-1] Position to insert at.
144
+ *
145
+ * If `pos` is less than `0` this uses `append()`;
146
+ * Otherwise it uses `prepend()`.
147
+ *
148
+ * @returns {Array} `array`
149
+ * @alias module:@lumjs/core/arrays.add.insert
150
+ */
151
+ function insert(array, value, pos=-1)
152
+ {
153
+ if (pos < 0)
154
+ {
155
+ return append(array, value, pos);
156
+ }
157
+ else
158
+ {
159
+ return prepend(array, value, pos);
160
+ }
161
+ }
162
+
163
+ /**
164
+ * A wrapper class to provide helper methods to Arrays.
165
+ *
166
+ * Provides wrapped versions of `prepend()`, `append()`,
167
+ * `before()`, `after()`, and `insert()`.
168
+ *
169
+ * The methods obviously pass the array argument, so remove it from
170
+ * the argument signature when using the wrapper method. Other than that,
171
+ * the rest of the arguments are the same as the wrapped functions.
172
+ *
173
+ * @alias module:@lumjs/core/arrays.add.At
174
+ */
175
+ class ArrayAt
176
+ {
177
+ /**
178
+ * Build a wrapper around an array.
179
+ *
180
+ * @param {Array} array - Array to wrap.
181
+ */
182
+ constructor(array)
183
+ {
184
+ this.array = array;
185
+ }
186
+
187
+ /**
188
+ * Add wrappers for the helper functions to the Array directly.
189
+ *
190
+ * WARNING: This is monkey patching the array object instance.
191
+ * If you're okay with that, cool.
192
+ *
193
+ * Otherwise, use a new `ArrayAt` object instance instead.
194
+ *
195
+ * @param {Array} array - Array to add methods to.
196
+ *
197
+ * @returns {Array} The input `array`
198
+ */
199
+ static extend(array)
200
+ {
201
+ for (const methName in ArrayAdd)
202
+ {
203
+ const methFunc = ArrayAdd[methName];
204
+ def(array, methName, methFunc.bind(array, array));
205
+ }
206
+ return array;
207
+ }
208
+
209
+ /**
210
+ * A static method that calls `new ArrayAt(array)`;
211
+ *
212
+ * @param {Array} array - Array to wrap
213
+ * @returns {module:@lumjs/core/arrays.ArrayAt} A new instance.
214
+ */
215
+ static new(array)
216
+ {
217
+ return new this(array);
218
+ }
219
+
220
+ } // ArrayAt class
221
+
222
+ // Set up ArrayAt methods, and export the functions.
223
+ for (const methName in ArrayAdd)
224
+ {
225
+ const methFunc = ArrayAdd[methName];
226
+
227
+ def(ArrayAt.prototype, methName, function()
228
+ {
229
+ methFunc(this.array, ...arguments);
230
+ });
231
+ }
232
+
233
+ def(ArrayAdd, 'At', ArrayAt);
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Array helper libraries
3
+ *
4
+ * @module @lumjs/core/arrays
5
+ */
6
+
7
+ const {lazy} = require('../types');
8
+ const {powerset, random} = require('./util');
9
+
10
+ // Utils.
11
+ exports.powerset = powerset;
12
+ exports.random = random;
13
+
14
+ // List stuff.
15
+ lazy(exports, 'List', () => require('./list'));
16
+ lazy(exports, 'containsAny', () => exports.List.containsAny);
17
+ lazy(exports, 'containsAll', () => exports.List.containsAll);
18
+ lazy(exports, 'removeItems', () => exports.List.removeItems);
19
+
20
+ // Add functions and At class.
21
+ lazy(exports, 'add', () => require('./add'));
@@ -1,8 +1,5 @@
1
- /**
2
- * Array (and other list objects) helper functions.
3
- * @module @lumjs/core/arrays
4
- */
5
- const {F,S} = require('./types/js');
1
+
2
+ const {F,S} = require('../types');
6
3
 
7
4
  /**
8
5
  * A wrapper class to abstract functionality of various kinds of lists.
@@ -278,6 +275,8 @@ function containsAny(list, ...items)
278
275
  return List.for(list, CONTAINS_OPTS).containsAny(...items);
279
276
  }
280
277
 
278
+ List.containsAny = containsAny;
279
+
281
280
  /**
282
281
  * See if a list contains *all* of the specified items.
283
282
  *
@@ -291,6 +290,8 @@ function containsAll(list, ...items)
291
290
  return List.for(list, CONTAINS_OPTS).containsAll(...items);
292
291
  }
293
292
 
293
+ List.containsAll = containsAll;
294
+
294
295
  const REMOVE_OPTS = {closures: ['remove']};
295
296
 
296
297
  /**
@@ -309,42 +310,6 @@ function removeItems(list, ...items)
309
310
  return List.for(list, REMOVE_OPTS).removeAll(...items);
310
311
  }
311
312
 
312
- /**
313
- * Return a Powerset of values in the array.
314
- * @param {Array} array The array to make the powerset from.
315
- * @returns {Array} The powerset.
316
- * @alias module:@lumjs/core/arrays.powerset
317
- */
318
- function powerset(array)
319
- {
320
- var ps = [[]];
321
- for (var i=0; i < array.length; i++)
322
- {
323
- // we modify the ps array in the next loop,
324
- // so can't use the ps.length property directly in the loop condition.
325
- var current_length = ps.length;
326
- for (var j = 0; j < current_length; j++)
327
- {
328
- ps.push(ps[j].concat(array[i]));
329
- }
330
- }
331
- return ps;
332
- }
333
-
334
- /**
335
- * Get a random element from an array.
336
- * @param {Array} array The array to get an item from.
337
- * @returns {mixed} The randomly selected item.
338
- * @alias module:@lumjs/core/arrays.random
339
- */
340
- function random(array)
341
- {
342
- return array[Math.floor(Math.random()*array.length)];
343
- }
344
-
345
- module.exports = exports =
346
- {
347
- List, containsAny, containsAll,
348
- removeItems, powerset, random,
349
- }
313
+ List.removeItems = removeItems;
350
314
 
315
+ module.exports = exports = List;
@@ -0,0 +1,38 @@
1
+
2
+ /**
3
+ * Return a Powerset of values in the array.
4
+ * @param {Array} array The array to make the powerset from.
5
+ * @returns {Array} The powerset.
6
+ * @alias module:@lumjs/core/arrays.powerset
7
+ */
8
+ function powerset(array)
9
+ {
10
+ var ps = [[]];
11
+ for (var i=0; i < array.length; i++)
12
+ {
13
+ // we modify the ps array in the next loop,
14
+ // so can't use the ps.length property directly in the loop condition.
15
+ var current_length = ps.length;
16
+ for (var j = 0; j < current_length; j++)
17
+ {
18
+ ps.push(ps[j].concat(array[i]));
19
+ }
20
+ }
21
+ return ps;
22
+ }
23
+
24
+ /**
25
+ * Get a random element from an array.
26
+ * @param {Array} array The array to get an item from.
27
+ * @returns {mixed} The randomly selected item.
28
+ * @alias module:@lumjs/core/arrays.random
29
+ */
30
+ function random(array)
31
+ {
32
+ return array[Math.floor(Math.random()*array.length)];
33
+ }
34
+
35
+ module.exports = exports =
36
+ {
37
+ powerset, random,
38
+ }
package/lib/index.js CHANGED
@@ -44,6 +44,13 @@ def(exports, 'lazy', lazy);
44
44
  */
45
45
  lazy(exports, 'arrays', () => require('./arrays'));
46
46
 
47
+ /**
48
+ * Map utility functions «Lazy»
49
+ * @name module:@lumjs/core.maps
50
+ * @type {module:@lumjs/core/maps}
51
+ */
52
+ lazy(exports, 'maps', () => require('./maps'));
53
+
47
54
  /**
48
55
  * Information about the JS context we're running in
49
56
  * @name module:@lumjs/core.context
package/lib/maps.js ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Map object helper functions.
3
+ * @module @lumjs/core/maps
4
+ */
5
+ const {F,isObj} = require('./types/js');
6
+
7
+ /**
8
+ * Build a `Map` instance out of various types of objects.
9
+ *
10
+ * @param {object} input - An object to be converted into a `Map`.
11
+ *
12
+ * Supported object types:
13
+ *
14
+ * - `Array`, `Map`, or any other object with an `entries()` method.
15
+ * - Implements `Iterable` interface (`[Symbol.iterator]`).
16
+ * - Implements `Iterator` interface (`next()`).
17
+ * - Anything other object we'll use all enumerable properties.
18
+ *
19
+ * @returns {Map}
20
+ * @alias module:@lumjs/core/maps.mapOf
21
+ */
22
+ function mapOf(input)
23
+ {
24
+ if (!isObj(input))
25
+ {
26
+ throw new TypeError('Non-object passed to mapOf');
27
+ }
28
+
29
+ if (typeof input.entries === F)
30
+ { // Short-cut for Arrays, other Maps, etc.
31
+ return new Map(input.entries());
32
+ }
33
+
34
+ const map = new Map();
35
+
36
+ if (typeof input[Symbol.iterator] === F)
37
+ { // Using the Iterable interface.
38
+ let i = 0;
39
+ for (const value of input)
40
+ {
41
+ map.set(i++, value);
42
+ }
43
+ }
44
+ else if (typeof thing.next === F)
45
+ { // Using the Iterator interface.
46
+ let i = 0, next;
47
+
48
+ while ((next = input.next()) && !next.done)
49
+ {
50
+ map.set(i++, next.value);
51
+ }
52
+ }
53
+ else
54
+ { // A plain old object. Use enumerable properties.
55
+ for (const key in input)
56
+ {
57
+ map.set(key, input[key]);
58
+ }
59
+ }
60
+
61
+ return map;
62
+ }
63
+
64
+ module.exports =
65
+ {
66
+ mapOf,
67
+ }
package/lib/observable.js CHANGED
@@ -11,31 +11,46 @@ const lock = Object.freeze;
11
11
  * @param {object} el - The object we are making observable.
12
12
  * @param {object} [opts] Options that define behaviours.
13
13
  * @param {string} [opts.wildcard='*'] The event name used as a wildcard.
14
- * @param {boolean} [opts.wrapthis=false] If `true`, `this` will be a wrapper.
15
14
  *
16
- * If `wrapthis` is `true`, the function will be called with a wrapper object
17
- * as the `this` variable instead of the target object. The wrapper will be:
18
- *
15
+ * @param {boolean} [opts.wrapargs=false] If `true`, the event handlers will
16
+ * be passed a wrapper object as the sole argument:
17
+ *
19
18
  * ```js
20
- * {
19
+ * {
21
20
  * isObservable:
22
- * {
21
+ * { // Immutable;
23
22
  * event: true, // This is an event data object.
24
23
  * target: false, // This is not the target object.
25
24
  * },
26
- * self: el, // The target object.
27
- * name: event, // The event name that was triggered.
28
25
  * wildcard: bool, // Will be true if this was a wildcard event handler.
29
26
  * func: function, // The function being called.
30
27
  * args: array, // The arguments passed to trigger.
28
+ * self: el, // The target object.
29
+ * name: event, // The event name that was triggered.
30
+ * target: el, // An alias to `self`.
31
+ * type: event, // An alias to `name`.
32
+ * // ...
31
33
  * }
32
34
  * ```
33
35
  *
34
- * The object will be frozen so its values cannot be modified.
36
+ * @param {boolean} [opts.wrapthis=false] If `true`, `this` in event
37
+ * handlers will be the same object as `opts.wrapargs`.
38
+ *
39
+ * @param {?function} [opts.wrapsetup=null] Setup function for wrapper data.
40
+ *
41
+ * This function will be called with the target `el` as `this`,
42
+ * and will be passed the wrapper object (before it is locked),
43
+ * so it can examine the event name and arguments passed to
44
+ * `trigger()` and adjust the object accordingly.
35
45
  *
36
- * @param {boolean} [opts.wrapargs=false] If `true`, the functions will be
37
- * passed a single object as the sole argument. The object will be the same
38
- * as the one from `opts.wrapthis`.
46
+ * This allows the wrapper objects to have a lot more custom properties,
47
+ * and feel more like the standard Event objects used by the `DOM`.
48
+ *
49
+ * If this is specified, but neither `opts.wrapargs` or `opts.wrapthis`
50
+ * is `true`, then `opts.wrapargs` will be changed to `true` implicitly.
51
+ *
52
+ * @param {boolean} [opts.wraplock=true] If `true`, the wrapper object
53
+ * will be made immutable using the `Object.freeze()` method.
39
54
  *
40
55
  * @param {boolean} [opts.addname] If `true` callbacks with
41
56
  * multiple events will have the name of the triggered event added as
@@ -99,16 +114,24 @@ function observable (el={}, opts={})
99
114
  ? opts.wildcard
100
115
  : '*';
101
116
 
117
+ const wrapsetup = (typeof opts.wrapsetup === F)
118
+ ? opts.wrapsetup
119
+ : null;
120
+
102
121
  const wrapthis = (typeof opts.wrapthis === B)
103
122
  ? opts.wrapthis
104
123
  : false;
105
124
 
106
125
  const wrapargs = (typeof opts.wrapargs === B)
107
126
  ? opts.wrapargs
108
- : false;
127
+ : (wrapsetup ? !wrapthis : false);
109
128
 
110
129
  const wrapped = (wrapthis || wrapargs);
111
130
 
131
+ const wraplock = (typeof opts.wraplock === B)
132
+ ? opts.wraplock
133
+ : true;
134
+
112
135
  const addname = (typeof opts.addname === B)
113
136
  ? opts.addname
114
137
  : !wrapped;
@@ -154,15 +177,28 @@ function observable (el={}, opts={})
154
177
  { // Something is going to use our wrapper object.
155
178
  const isWild = (name === wildcard);
156
179
  const fname = isWild ? (addname ? args[0] : args.shift()) : name;
180
+
157
181
  fobj =
158
- lock({
182
+ {
159
183
  isObservable: lock({event: true, target: false}),
160
184
  self: el,
161
- name: fname,
185
+ target: el,
186
+ name: fname,
187
+ type: fname,
162
188
  func: fn,
163
189
  wildcard: isWild,
164
190
  args,
165
- });
191
+ };
192
+
193
+ if (wrapsetup)
194
+ {
195
+ wrapsetup.call(el, fobj);
196
+ }
197
+
198
+ if (wraplock)
199
+ {
200
+ lock(fobj);
201
+ }
166
202
  }
167
203
 
168
204
  const fthis = wrapthis ? fobj : el;
package/lib/opt.js CHANGED
@@ -3,12 +3,13 @@
3
3
  * @module @lumjs/core/opt
4
4
  */
5
5
 
6
- const {U,F,S,needObj,needType} = require('./types');
6
+ const {U,F,S,N,B,isObj,isComplex,needObj,needType} = require('./types');
7
+ const {insert} = require('./arrays/add');
7
8
 
8
9
  /**
9
10
  * See if a value is *set*, and if not, return a default value.
10
11
  *
11
- * @param {*} opt - The value we are testing.
12
+ * @param {*} optvalue - The value we are testing.
12
13
  * @param {*} defvalue - The default value if opt was null or undefined.
13
14
  *
14
15
  * @param {boolean} [allowNull=false] If true, allow null to count as *set*.
@@ -17,22 +18,28 @@ const {U,F,S,needObj,needType} = require('./types');
17
18
  * the default.
18
19
  * @param {object} [lazyThis=null] If `isLazy` is true, this object will
19
20
  * be used as `this` for the function.
21
+ * @param {Array} [lazyArgs] If `isLazy` is true, this may be used
22
+ * as a list of arguments to pass.
20
23
  *
21
24
  * @return {*} Either the specified `opt` value or the default value.
22
25
  * @alias module:@lumjs/core/opt.val
23
26
  */
24
- function val(opt, defvalue, allowNull=false, isLazy=false, lazyThis=null)
27
+ function val(optvalue, defvalue,
28
+ allowNull=false,
29
+ isLazy=false,
30
+ lazyThis=null,
31
+ lazyArgs=[])
25
32
  {
26
- if (typeof opt === U || (!allowNull && opt === null))
33
+ if (typeof optvalue === U || (!allowNull && optvalue === null))
27
34
  { // The defined value was not "set" as per our rules.
28
35
  if (isLazy && typeof defvalue === F)
29
36
  { // Get the default value from a passed in function.
30
- return defvalue.call(lazyThis);
37
+ return defvalue.apply(lazyThis, lazyArgs);
31
38
  }
32
39
  return defvalue;
33
40
  }
34
41
 
35
- return opt;
42
+ return optvalue;
36
43
  }
37
44
 
38
45
  exports.val = val;
@@ -48,18 +55,350 @@ exports.val = val;
48
55
  * @param {string} optname - The property name we're checking for.
49
56
  * @param {*} defvalue - The default value.
50
57
  *
51
- * @param {bool} [allowNull=true] Same as `val()`, but the default is `true`.
58
+ * @param {bool} [allowNull=true] Same as `val()`, but default is `true`.
52
59
  * @param {bool} [isLazy=false] Same as `val()`.
53
- * @param {object} [lazyThis=opts] Same as `val()`.
60
+ * @param {object} [lazyThis=opts] Same as `val()`, but default is `obj`.
61
+ * @param {Array} [lazyArgs] Same as `val()`.
54
62
  *
55
63
  * @return {*} Either the property value, or the default value.
56
- * module:@lumjs/core/opt.get
64
+ * @alias module:@lumjs/core/opt.get
57
65
  */
58
- function get(obj, optname, defvalue, allowNull=true, isLazy=false, lazyThis=obj)
66
+ function get(obj, optname, defvalue,
67
+ allowNull=true,
68
+ isLazy=false,
69
+ lazyThis=obj,
70
+ lazyArgs)
59
71
  {
60
72
  needObj(obj);
61
73
  needType(S, optname);
62
- return val(obj[optname], defvalue, allowNull, isLazy, lazyThis);
74
+ return val(obj[optname], defvalue, allowNull, isLazy, lazyThis, lazyArgs);
63
75
  }
64
76
 
65
77
  exports.get = get;
78
+
79
+ /**
80
+ * A class for handling options with multiple sources.
81
+ * @alias module:@lumjs/core/opt.Opts
82
+ */
83
+ class Opts
84
+ {
85
+ /**
86
+ * Build an Opts instance.
87
+ *
88
+ * @param {...object} sources - Initial sources of options.
89
+ *
90
+ * The order of sources matters, as the ones added later will override
91
+ * the ones added earlier. Keep that in mind when adding sources.
92
+ */
93
+ constructor(...sources)
94
+ {
95
+ this.$sources = [];
96
+ this.$curPos = -1;
97
+ this.$curSrc = null;
98
+
99
+ this.$fatalErrors = false;
100
+ this.$strictProps = false;
101
+
102
+ this.add(...sources);
103
+ this._compile();
104
+ }
105
+
106
+ /**
107
+ * Compile current sources into a data object.
108
+ *
109
+ * @returns {object} `this`
110
+ * @private
111
+ */
112
+ _compile()
113
+ {
114
+ this.$data = Object.assign({}, ...this.$sources);
115
+ return this;
116
+ }
117
+
118
+ /**
119
+ * Handle an error
120
+ *
121
+ * @param {string} msg - A summary of the error.
122
+ * @param {object} info - Debugging information for the logs.
123
+ * @param {function} [errClass=TypeError] Constructor for an `Error` class.
124
+ * Used if fatal errors are enabled.
125
+ *
126
+ * @returns {object} `this`
127
+ * @throws {Error} An error of `errClass` class, if fatal mode is enabled.
128
+ */
129
+ _err(msg, info={}, errClass=TypeError)
130
+ {
131
+ const args = this.$fatalErrors ? [info] : [msg, info];
132
+
133
+ info.instance = this;
134
+ console.error(...args);
135
+
136
+ if (this.$fatalErrors)
137
+ {
138
+ throw new errClass(msg);
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Set the fatal error handling setting.
144
+ *
145
+ * Default is `false`, so errors will be logged, but not thrown.
146
+ *
147
+ * @param {boolean} val - Should errors be fatal?
148
+ * @returns {object} `this`
149
+ */
150
+ fatal(val)
151
+ {
152
+ if (typeof val === B)
153
+ {
154
+ this.$fatalErrors = val;
155
+ }
156
+ else
157
+ {
158
+ this._err('invalid fatal value', {val});
159
+ }
160
+
161
+ return this;
162
+ }
163
+
164
+ /**
165
+ * Set the strict property check setting.
166
+ *
167
+ * Default is `false`, we don't care about non-existent properties.
168
+ *
169
+ * @param {boolean} val - Should non-existant properties be an error?
170
+ * @returns {object} `this`
171
+ */
172
+ strict(val)
173
+ {
174
+ if (typeof val === B)
175
+ {
176
+ this.$strictProps = val;
177
+ }
178
+ else
179
+ {
180
+ this._err('invalid strict value', {val});
181
+ }
182
+
183
+ return this;
184
+ }
185
+
186
+ /**
187
+ * Set the position/offset to add new sources at.
188
+ *
189
+ * This will affect subsequent calls to the `add()` method.
190
+ *
191
+ * @param {number} pos - The position/offset value.
192
+ *
193
+ * - A value of `-1` uses `Array#push(src))`; end of array.
194
+ * - A value of `0` uses `Array#unshift(src)`; start of array.
195
+ * - Any value `> 0` uses `Array#splice(pos, 0, src)`; offset from start.
196
+ * - Any value `< -1` uses `Array#splice(pos+1, 0, src)`; offset from end.
197
+ *
198
+ * The default value if none is specified is `-1`.
199
+ *
200
+ * @returns {object} `this`
201
+ * @throws {TypeError} An invalid value was passed while `fatal` was true.
202
+ */
203
+ at(pos)
204
+ {
205
+ if (typeof pos === N)
206
+ {
207
+ this.$curPos = pos;
208
+ }
209
+ else
210
+ {
211
+ this._err("Invalid pos value", {pos});
212
+ }
213
+
214
+ return this;
215
+ }
216
+
217
+ /**
218
+ * Set the object to look for nested properties in.
219
+ *
220
+ * This will affect subsequent calls to the `add()` method.
221
+ *
222
+ * @param {(object|number|boolean)} source - Source definition
223
+ *
224
+ * - If this is an `object` it will be used as the object directly.
225
+ * - If this is a `number` it is the position of one of our data sources.
226
+ * Negative numbers count from the end of the list of sources.
227
+ * - If this is `true` then the compiled options data at the time of the
228
+ * call to this method will be used.
229
+ * - If this is `false` then the next time a `string` value is passed to
230
+ * `add()` the options will be compiled on demand, and that object will
231
+ * be used until the next call to `from()`.
232
+ *
233
+ * If this is not specified, then it defaults to `false`.
234
+ *
235
+ * @returns {object} `this`
236
+ * @throws {TypeError} An invalid value was passed while `fatal` was true.
237
+ */
238
+ from(source)
239
+ {
240
+ if (source === true)
241
+ { // Use existing data as the source.
242
+ this.$curSrc = this.$data;
243
+ }
244
+ else if (source === false)
245
+ { // Auto-generate the source the next time.
246
+ this.$curSrc = null;
247
+ }
248
+ else if (typeof source === N)
249
+ { // A number will be the position of an existing source.
250
+ const offset
251
+ = (source < 0)
252
+ ? this.$sources.length + source
253
+ : source;
254
+
255
+ if (isObj(this.$sources[offset]))
256
+ {
257
+ this.$curSrc = this.$sources[offset];
258
+ }
259
+ else
260
+ {
261
+ this._err("Invalid source offset", {offset, source});
262
+ }
263
+ }
264
+ else if (isObj(source))
265
+ { // An object or function will be used as the source.
266
+ this.$curSrc = source;
267
+ }
268
+ else
269
+ {
270
+ this._err("Invalid source", {source});
271
+ }
272
+
273
+ return this;
274
+ }
275
+
276
+ /**
277
+ * Add new sources of options.
278
+ *
279
+ * @param {...(object|string)} sources - Sources and positions.
280
+ *
281
+ * If this is an `object` then it's a source of options to add.
282
+ * This is the most common way of using this.
283
+ *
284
+ * If this is a `string` then it's assumed to be nested property
285
+ * of the current `from()` source, and if that property exists and
286
+ * is an object, it will be used as the source to add. If it does
287
+ * not exist, then the behaviour will depend on the values of the
288
+ * `strict()` and `fatal()` modifiers.
289
+ *
290
+ * @returns {object} `this`
291
+ */
292
+ add(...sources)
293
+ {
294
+ let pos=-1;
295
+
296
+ for (let source of sources)
297
+ {
298
+ if (source === undefined || source === null)
299
+ { // Skip undefined or null values.
300
+ continue;
301
+ }
302
+
303
+ if (typeof source === S)
304
+ { // Try to find a nested property to include.
305
+ if (this.$curSrc === null)
306
+ { // Has not been initialized, let's do that now.
307
+ this._compile();
308
+ this.$curSrc = this.$data;
309
+ }
310
+
311
+ if (isObj(this.$curSrc[source]))
312
+ { // Found a property, use it.
313
+ source = this.$curSrc[source];
314
+ }
315
+ else
316
+ { // No such property.
317
+ if (this.$strictProps)
318
+ {
319
+ this._err('Property not found', {source});
320
+ }
321
+ continue;
322
+ }
323
+ }
324
+
325
+ if (isObj(source))
326
+ { // It's a source to add.
327
+ insert(this.$sources, source, pos);
328
+ }
329
+ else
330
+ { // That's not valid.
331
+ this._err('invalid source value', {source, sources});
332
+ }
333
+ }
334
+
335
+ return this._compile();
336
+ }
337
+
338
+ /**
339
+ * Remove existing sources of options.
340
+ *
341
+ * @param {...object} sources - Sources to remove.
342
+ *
343
+ * @returns {object} `this`
344
+ */
345
+ remove(...sources)
346
+ {
347
+ for (const source of sources)
348
+ {
349
+ const index = this.$sources.indexOf(source);
350
+ if (index !== -1)
351
+ {
352
+ this.$sources.splice(index, 1);
353
+ }
354
+ }
355
+
356
+ return this._compile();
357
+ }
358
+
359
+ /**
360
+ * Remove all current sources. Resets compiled options data.
361
+ *
362
+ * @returns {object} `this`
363
+ */
364
+ clear()
365
+ {
366
+ this.$sources = [];
367
+ return this._compile();
368
+ }
369
+
370
+ /**
371
+ * Get an option value from our compiled data sources.
372
+ *
373
+ * This uses the `get()` function, but instead of using positional
374
+ * arguments, it supports an object of named options instead.
375
+ *
376
+ * @param {string} opt - The name of the option to get.
377
+ * @param {object} [args] Optional arguments for `get()`.
378
+ * @param {*} [args.default] `defvalue` argument.
379
+ * @param {boolean} [args.null=true] `allowNull` argument.
380
+ * @param {boolean} [args.lazy=false] `isLazy` argument.
381
+ * @param {(object|function)} [args.lazyThis] `lazyThis` argument.
382
+ * If this is defined, `args.lazy` will be set to `true`.
383
+ * @param {Array} [args.lazyArgs] `lazyArgs` argument.
384
+ * If this is defined, `args.lazy` will be set to `true`.
385
+ *
386
+ * @returns {*} The output of the `get()` function.
387
+ */
388
+ get(opt, args={})
389
+ {
390
+ if (isComplex(args.lazyThis) || Array.isArray(args.lazyArgs))
391
+ {
392
+ args.lazy = true;
393
+ }
394
+
395
+ return get(this.$data, opt,
396
+ args.default,
397
+ args.null,
398
+ args.lazy,
399
+ args.lazyThis,
400
+ args.lazyArgs);
401
+ }
402
+ }
403
+
404
+ exports.Opts = Opts;
package/lib/strings.js CHANGED
@@ -189,3 +189,31 @@ function replaceItems(string, replacements, useAll)
189
189
  }
190
190
 
191
191
  exports.replaceItems = replaceItems;
192
+
193
+ /**
194
+ * Replace values in a string via an async function.
195
+ *
196
+ * @param {string} string - The input string.
197
+ *
198
+ * @param {(RegExp|string)} regexp - The pattern to find.
199
+ *
200
+ * If this is a `RegExp`, it *must* have the `g` flag set.
201
+ * If this is a `string` it will be converted into a `RegExp`,
202
+ * see the `String#matchAll` method for details.
203
+ *
204
+ * @param {function} replacerFunction - An `async` function.
205
+ *
206
+ * See `String#replace` for details on replacer functions.
207
+ *
208
+ * @returns {string}
209
+ */
210
+ async function replaceAsync(string, regexp, replacerFunction)
211
+ {
212
+ const replacements = await Promise.all(
213
+ Array.from(string.matchAll(regexp),
214
+ match => replacerFunction(...match)));
215
+ let i = 0;
216
+ return string.replace(regexp, () => replacements[i++]);
217
+ }
218
+
219
+ exports.replaceAsync = replaceAsync;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@lumjs/core",
3
- "version": "1.16.0",
3
+ "version": "1.18.0",
4
4
  "main": "lib/index.js",
5
5
  "exports":
6
6
  {
7
7
  ".": "./lib/index.js",
8
8
 
9
- "./arrays": "./lib/arrays.js",
9
+ "./arrays": "./lib/arrays/index.js",
10
+ "./maps": "./lib/maps.js",
10
11
  "./context": "./lib/context.js",
11
12
  "./enum": "./lib/enum.js",
12
13
  "./flags": "./lib/flags.js",
@@ -1,107 +0,0 @@
1
- 0 verbose cli /usr/bin/node /usr/bin/npm
2
- 1 info using npm@9.7.1
3
- 2 info using node@v18.16.0
4
- 3 timing npm:load:whichnode Completed in 1ms
5
- 4 timing config:load:defaults Completed in 1ms
6
- 5 timing config:load:file:/usr/lib/node_modules/npm/npmrc Completed in 1ms
7
- 6 timing config:load:builtin Completed in 1ms
8
- 7 timing config:load:cli Completed in 1ms
9
- 8 timing config:load:env Completed in 0ms
10
- 9 timing config:load:file:/system/docs/dev/js/lumjs/core/.npmrc Completed in 0ms
11
- 10 timing config:load:project Completed in 1ms
12
- 11 timing config:load:file:/home/novus/.npmrc Completed in 1ms
13
- 12 timing config:load:user Completed in 1ms
14
- 13 timing config:load:file:/usr/etc/npmrc Completed in 0ms
15
- 14 timing config:load:global Completed in 0ms
16
- 15 timing config:load:setEnvs Completed in 1ms
17
- 16 timing config:load Completed in 6ms
18
- 17 timing npm:load:configload Completed in 6ms
19
- 18 timing config:load:flatten Completed in 1ms
20
- 19 timing npm:load:mkdirpcache Completed in 0ms
21
- 20 timing npm:load:mkdirplogs Completed in 0ms
22
- 21 verbose title npm publish
23
- 22 verbose argv "publish"
24
- 23 timing npm:load:setTitle Completed in 0ms
25
- 24 timing npm:load:display Completed in 1ms
26
- 25 verbose logfile logs-max:10 dir:/home/novus/.npm/_logs/2023-06-08T19_55_05_247Z-
27
- 26 verbose logfile /home/novus/.npm/_logs/2023-06-08T19_55_05_247Z-debug-0.log
28
- 27 timing npm:load:logFile Completed in 5ms
29
- 28 timing npm:load:timers Completed in 0ms
30
- 29 timing npm:load:configScope Completed in 0ms
31
- 30 timing npm:load Completed in 22ms
32
- 31 verbose publish [ '.' ]
33
- 32 silly logfile start cleaning logs, removing 1 files
34
- 33 silly logfile done cleaning log files
35
- 34 timing arborist:ctor Completed in 0ms
36
- 35 notice
37
- 36 notice 📦 @lumjs/core@1.16.0
38
- 37 notice === Tarball Contents ===
39
- 38 notice 928B README.md
40
- 38 notice 627B TODO.md
41
- 38 notice 501B jsdoc.json
42
- 38 notice 8.6kB lib/arrays.js
43
- 38 notice 6.4kB lib/context.js
44
- 38 notice 5.8kB lib/enum.js
45
- 38 notice 1.3kB lib/flags.js
46
- 38 notice 3.6kB lib/index.js
47
- 38 notice 3.8kB lib/meta.js
48
- 38 notice 3.6kB lib/modules.js
49
- 38 notice 1.2kB lib/obj/apply.js
50
- 38 notice 12.1kB lib/obj/clone.js
51
- 38 notice 1.2kB lib/obj/copyall.js
52
- 38 notice 12.5kB lib/obj/copyprops.js
53
- 38 notice 3.2kB lib/obj/getmethods.js
54
- 38 notice 1.2kB lib/obj/getproperty.js
55
- 38 notice 870B lib/obj/index.js
56
- 38 notice 2.2kB lib/obj/lock.js
57
- 38 notice 2.8kB lib/obj/merge.js
58
- 38 notice 6.8kB lib/obj/ns.js
59
- 38 notice 2.5kB lib/objectid.js
60
- 38 notice 8.7kB lib/observable.js
61
- 38 notice 2.2kB lib/opt.js
62
- 38 notice 5.3kB lib/strings.js
63
- 38 notice 6.0kB lib/types/basics.js
64
- 38 notice 4.7kB lib/types/console.js
65
- 38 notice 7.2kB lib/types/def.js
66
- 38 notice 1.1kB lib/types/dt.js
67
- 38 notice 1.8kB lib/types/index.js
68
- 38 notice 4.9kB lib/types/isa.js
69
- 38 notice 457B lib/types/js.js
70
- 38 notice 7.2kB lib/types/lazy.js
71
- 38 notice 3.4kB lib/types/needs.js
72
- 38 notice 2.7kB lib/types/root.js
73
- 38 notice 3.8kB lib/types/stringify.js
74
- 38 notice 5.5kB lib/types/typelist.js
75
- 38 notice 826B package.json
76
- 39 notice === Tarball Details ===
77
- 40 notice name: @lumjs/core
78
- 40 notice version: 1.16.0
79
- 40 notice filename: lumjs-core-1.16.0.tgz
80
- 40 notice package size: 42.7 kB
81
- 40 notice unpacked size: 147.6 kB
82
- 40 notice shasum: 424727206c94f94fbf8e18c5f62eab4aa64e52af
83
- 40 notice integrity: sha512-iPLVQTAe/Bf8P[...]TV8PNxn6/ji5A==
84
- 40 notice total files: 37
85
- 41 notice
86
- 42 notice Publishing to https://registry.npmjs.org/ with tag latest and default access
87
- 43 http fetch PUT 401 https://registry.npmjs.org/@lumjs%2fcore 277ms
88
- 44 timing command:publish Completed in 367ms
89
- 45 verbose stack HttpErrorGeneral: 401 Unauthorized - PUT https://registry.npmjs.org/@lumjs%2fcore - unexpected status code from "get-packument" = 401
90
- 45 verbose stack at /usr/lib/node_modules/npm/node_modules/npm-registry-fetch/lib/check-response.js:95:15
91
- 45 verbose stack at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
92
- 45 verbose stack at async publish (/usr/lib/node_modules/npm/node_modules/libnpmpublish/lib/publish.js:54:17)
93
- 45 verbose stack at async otplease (/usr/lib/node_modules/npm/lib/utils/otplease.js:4:12)
94
- 45 verbose stack at async Publish.exec (/usr/lib/node_modules/npm/lib/commands/publish.js:123:7)
95
- 45 verbose stack at async module.exports (/usr/lib/node_modules/npm/lib/cli-entry.js:61:5)
96
- 46 verbose statusCode 401
97
- 47 verbose pkgid @lumjs/core@1.16.0
98
- 48 verbose cwd /system/docs/dev/js/lumjs/core
99
- 49 verbose Linux 5.10.0-22-amd64
100
- 50 verbose node v18.16.0
101
- 51 verbose npm v9.7.1
102
- 52 error code E401
103
- 53 error 401 Unauthorized - PUT https://registry.npmjs.org/@lumjs%2fcore - unexpected status code from "get-packument" = 401
104
- 54 verbose exit 1
105
- 55 timing npm Completed in 495ms
106
- 56 verbose code 1
107
- 57 error A complete log of this run can be found in: /home/novus/.npm/_logs/2023-06-08T19_55_05_247Z-debug-0.log