@lumjs/core 1.26.0 → 1.31.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/README.md +15 -1
- package/jsdoc.js +13 -0
- package/lib/console.js +54 -0
- package/lib/enum.js +65 -15
- package/lib/events/event.js +85 -0
- package/lib/events/index.js +30 -0
- package/lib/events/listener.js +127 -0
- package/lib/events/registry.js +537 -0
- package/lib/index.js +34 -17
- package/lib/meta.js +71 -82
- package/lib/obj/clone.js +1 -1
- package/lib/obj/copyprops.js +2 -2
- package/lib/obj/cp.js +1375 -0
- package/lib/obj/flip.js +2 -0
- package/lib/obj/index.js +2 -1
- package/lib/obj/merge.js +1 -1
- package/lib/obj/ns.js +1 -1
- package/lib/objectid.js +41 -13
- package/lib/observable.js +1 -1
- package/lib/old/abstractclass.js +82 -0
- package/lib/opt/args.js +50 -0
- package/lib/opt/getpath.js +70 -0
- package/lib/opt/getval.js +61 -0
- package/lib/opt/index.js +21 -0
- package/lib/opt/val.js +63 -0
- package/lib/strings.js +1 -1
- package/lib/types/basics.js +35 -16
- package/lib/types/def.js +49 -36
- package/lib/types/dt.js +147 -18
- package/lib/types/index.js +32 -59
- package/lib/types/isa.js +4 -0
- package/lib/types/js.js +43 -7
- package/lib/types/lazy.js +3 -2
- package/lib/types/needs.js +0 -1
- package/lib/types/typelist.js +6 -4
- package/lum.build.js +40 -0
- package/package.json +24 -14
- package/TODO.md +0 -76
- package/jsdoc.json +0 -33
- package/lib/opt.js +0 -664
package/lib/obj/flip.js
CHANGED
|
@@ -59,6 +59,7 @@ function flip(obj, opts={})
|
|
|
59
59
|
* @throws {ReferenceError} If `fatalDup` is `true`
|
|
60
60
|
* and a duplicate value is found.
|
|
61
61
|
*
|
|
62
|
+
* @alias module:@lumjs/core/obj.flipKeyVal
|
|
62
63
|
*/
|
|
63
64
|
function flipKeyVal(inObj, opts={})
|
|
64
65
|
{
|
|
@@ -148,6 +149,7 @@ function flipKeyVal(inObj, opts={})
|
|
|
148
149
|
* - If both `opts.valid.map` and `opts.valid.fatal` are `true`,
|
|
149
150
|
* and the validation tests against the `inMap` object fail.
|
|
150
151
|
*
|
|
152
|
+
* @alias module:@lumjs/core/obj.flipMap
|
|
151
153
|
*/
|
|
152
154
|
function flipMap(inMap, opts={})
|
|
153
155
|
{
|
package/lib/obj/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const getProperty = require('./getproperty');
|
|
|
13
13
|
const {lock,addLock} = require('./lock');
|
|
14
14
|
const {mergeNested,syncNested} = require('./merge');
|
|
15
15
|
const ns = require('./ns');
|
|
16
|
+
const cp = require('./cp');
|
|
16
17
|
|
|
17
18
|
const
|
|
18
19
|
{
|
|
@@ -22,7 +23,7 @@ const
|
|
|
22
23
|
|
|
23
24
|
module.exports =
|
|
24
25
|
{
|
|
25
|
-
CLONE, clone, addClone, cloneIfLocked, lock, addLock,
|
|
26
|
+
cp, CLONE, clone, addClone, cloneIfLocked, lock, addLock,
|
|
26
27
|
mergeNested, syncNested, copyProps, copyAll, ns,
|
|
27
28
|
getObjectPath, setObjectPath, getNamespace, setNamespace,
|
|
28
29
|
getProperty, duplicateAll, duplicateOne, getMethods, signatureOf,
|
package/lib/obj/merge.js
CHANGED
package/lib/obj/ns.js
CHANGED
package/lib/objectid.js
CHANGED
|
@@ -1,26 +1,55 @@
|
|
|
1
1
|
|
|
2
2
|
const {notNil,def,isNil,F,N,S,SY} = require('./types');
|
|
3
|
+
const argOpts = require('./opt/args');
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
* Generate a
|
|
6
|
+
* Generate a random number.
|
|
6
7
|
*
|
|
7
|
-
* @param {number} [
|
|
8
|
+
* @param {(object|number)} [opts] Options
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
+
* @param {number} [opts.seed] A base number to use.
|
|
11
|
+
*
|
|
12
|
+
* If this is omitted, or an invalid value (including `0`),
|
|
13
|
+
* the default is to use `Date.now()` as the `seed`.
|
|
14
|
+
*
|
|
15
|
+
* @param {number} [mode=1] Determine how to handle return value
|
|
16
|
+
*
|
|
17
|
+
* | Mode | Description |
|
|
18
|
+
* | ------- | --------------------------------------------------------- |
|
|
19
|
+
* | `0` | Return unmodified floating point number |
|
|
20
|
+
* | `1` | `Math.round()` (default) |
|
|
21
|
+
* | `2` | `Math.floor()` |
|
|
22
|
+
* | `3` | `Math.ceil()` |
|
|
10
23
|
*
|
|
11
24
|
* @returns {number}
|
|
12
25
|
* @alias module:@lumjs/core.randomNumber
|
|
13
26
|
*/
|
|
14
|
-
function randomNumber(seed)
|
|
27
|
+
function randomNumber(seed=0, mode=1)
|
|
15
28
|
{
|
|
16
|
-
if (typeof seed !== N) seed = Date.now();
|
|
17
|
-
|
|
29
|
+
if (typeof seed !== N || seed === 0) seed = Date.now();
|
|
30
|
+
|
|
31
|
+
const rand = Math.random() * seed;
|
|
32
|
+
|
|
33
|
+
switch(mode)
|
|
34
|
+
{
|
|
35
|
+
case 1: return Math.round(rand);
|
|
36
|
+
case 2: return Math.floor(rand);
|
|
37
|
+
case 3: return Math.ceil(rand);
|
|
38
|
+
default: return rand;
|
|
39
|
+
}
|
|
18
40
|
}
|
|
19
41
|
|
|
20
42
|
exports.randomNumber = randomNumber;
|
|
21
43
|
|
|
22
44
|
const validBase = base => (typeof base === N && base > 1 && base < 37);
|
|
23
45
|
|
|
46
|
+
/**
|
|
47
|
+
* A class for generating unique ids for objects.
|
|
48
|
+
*
|
|
49
|
+
* TODO: document all the methods, etc.
|
|
50
|
+
*
|
|
51
|
+
* @alias module:@lumjs/core.UniqueObjectIds
|
|
52
|
+
*/
|
|
24
53
|
class UniqueObjectIds
|
|
25
54
|
{
|
|
26
55
|
constructor(opts={})
|
|
@@ -206,20 +235,20 @@ class InternalObjectId
|
|
|
206
235
|
* @param {object} opts - Named options to change default behaviours.
|
|
207
236
|
* @param {string} [opts.name] A friendly name for diagnostics.
|
|
208
237
|
* @param {(string|number)} [opts.id] An internal id value.
|
|
209
|
-
*
|
|
238
|
+
* A reasonable default will be generated if it's not specified.
|
|
210
239
|
* @param {(string|Symbol)} [opts.property] The property name or Symbol.
|
|
211
240
|
* This is the property that will be set on objects that are tagged
|
|
212
|
-
* with this InternalObjectId. Default is `Symbol
|
|
241
|
+
* with this InternalObjectId. Default is a unique (non-global) `Symbol`.
|
|
213
242
|
* @param {boolean} [opts.useInstance=true] Store object or id value?
|
|
214
243
|
* If this is `true` (now the default), we store the instance itself.
|
|
215
244
|
* If this is `false` (the old default), we store just the `id` value.
|
|
216
245
|
*/
|
|
217
246
|
constructor(opts={})
|
|
218
247
|
{
|
|
248
|
+
const ui = this.useInstance = opts.useInstance ?? true;
|
|
219
249
|
this.name = opts.name;
|
|
220
|
-
this.id = opts.id ?? randomNumber();
|
|
221
|
-
this.property = opts.property ?? Symbol(this.id);
|
|
222
|
-
this.useInstance = opts.useInstance ?? true;
|
|
250
|
+
this.id = opts.id ?? (ui ? Date.now() : randomNumber());
|
|
251
|
+
this.property = opts.property ?? Symbol(this.name ?? this.id);
|
|
223
252
|
}
|
|
224
253
|
|
|
225
254
|
/**
|
|
@@ -268,8 +297,7 @@ class InternalObjectId
|
|
|
268
297
|
*/
|
|
269
298
|
isFunction()
|
|
270
299
|
{
|
|
271
|
-
|
|
272
|
-
return function(obj) { return oid.is(obj); }
|
|
300
|
+
return obj => this.is(obj);
|
|
273
301
|
}
|
|
274
302
|
|
|
275
303
|
}
|
package/lib/observable.js
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {F,S,isArray,isa} = require('../types');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract classes for Javascript.
|
|
7
|
+
* @deprecated Just use `throw new AbstractError()` instead.
|
|
8
|
+
* @alias module:@lumjs/core/meta.AbstractClass
|
|
9
|
+
*/
|
|
10
|
+
class AbstractClass
|
|
11
|
+
{
|
|
12
|
+
/**
|
|
13
|
+
* If you want to mark a method as abstract use this.
|
|
14
|
+
*/
|
|
15
|
+
$abstract(name)
|
|
16
|
+
{
|
|
17
|
+
if (name.indexOf('(') === -1)
|
|
18
|
+
{ // Add empty method signature.
|
|
19
|
+
name += '()';
|
|
20
|
+
}
|
|
21
|
+
throw new Error(`Abstract method ${name} was not implemented`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Check for required properties
|
|
26
|
+
*
|
|
27
|
+
* @param {...(string|Array)} needs - What is needed
|
|
28
|
+
*
|
|
29
|
+
* If this is a `string` it should be in a format like:
|
|
30
|
+
*
|
|
31
|
+
* - `methodName(arg1,arg2,arg3)`
|
|
32
|
+
* - `anotherMethod(number, string, object) : boolean`
|
|
33
|
+
* - `yetAnother (className) : resultClass`
|
|
34
|
+
*
|
|
35
|
+
* The names are case sensitive, and we'll look for the method after
|
|
36
|
+
* stripping off anything from the first *non-word* character.
|
|
37
|
+
*
|
|
38
|
+
* If this is an `Array`, the first item must be the name of a property,
|
|
39
|
+
* and each other item should be a type checking value, or array of type
|
|
40
|
+
* checking values from the [TYPES]{@link module:@lumjs/core/types.TYPES}
|
|
41
|
+
* object, as used by [isa()]{@link module:@lumjs/core/types.isa}.
|
|
42
|
+
*
|
|
43
|
+
* If you are calling this in an abstract class constructor, likely only
|
|
44
|
+
* the method checks will be useful, as the `super()` call must be done
|
|
45
|
+
* *before* any instance property assignments.
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
$needs(...needs)
|
|
49
|
+
{
|
|
50
|
+
const className = this.constructor.name;
|
|
51
|
+
|
|
52
|
+
const getName = fullName => fullName.replace(/\W.*/, '');
|
|
53
|
+
const missing = propName =>
|
|
54
|
+
{
|
|
55
|
+
throw new Error(`${className} is missing ${propName}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const need of needs)
|
|
59
|
+
{
|
|
60
|
+
if (typeof need === S)
|
|
61
|
+
{ // A simple method
|
|
62
|
+
const meth = getName(need);
|
|
63
|
+
if (typeof this[meth] !== F)
|
|
64
|
+
{
|
|
65
|
+
missing(need);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (isArray(need))
|
|
69
|
+
{
|
|
70
|
+
const prop = getName(need[0]);
|
|
71
|
+
const types = need.slice(1);
|
|
72
|
+
if (!isa(this[prop], ...types))
|
|
73
|
+
{
|
|
74
|
+
missing(need);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = AbstractClass;
|
package/lib/opt/args.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {isObj} = require('../types/basics');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A helper function to support both positional arguments
|
|
7
|
+
* and named options in the same method signature.
|
|
8
|
+
*
|
|
9
|
+
* @param {object} opts - Options built from positional arguments
|
|
10
|
+
*
|
|
11
|
+
* Keep in mind that this object **WILL** be modified!
|
|
12
|
+
*
|
|
13
|
+
* @param {string} optArg - The option that may contain named options
|
|
14
|
+
*
|
|
15
|
+
* Generally the name of the first positional argument that may be
|
|
16
|
+
* an `object` full of options or a different positional argument value.
|
|
17
|
+
*
|
|
18
|
+
* The biggest limitation is that it cannot be an `object` value when used
|
|
19
|
+
* as a positional argument, as that will always be seen as the _options_.
|
|
20
|
+
*
|
|
21
|
+
* @param {*} optDef - A default value for `opts[optArg]`
|
|
22
|
+
*
|
|
23
|
+
* If `opts[optArg]` was an `object`, we'll compose its properties
|
|
24
|
+
* into `opts` directly. If after that `opts[optArg]` is still the
|
|
25
|
+
* options `object` then this value will be used instead.
|
|
26
|
+
*
|
|
27
|
+
* @example <caption>Example usage</caption>
|
|
28
|
+
*
|
|
29
|
+
* function example(first=true, second=null, third="test")
|
|
30
|
+
* {
|
|
31
|
+
* const opts = argOpts({first, second, third}, 'first', true);
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* @exports module:@lumjs/core/opt/args
|
|
35
|
+
*/
|
|
36
|
+
function argOpts(opts, optArg, optDef)
|
|
37
|
+
{
|
|
38
|
+
if (isObj(opts[optArg]))
|
|
39
|
+
{ // Merge the named options.
|
|
40
|
+
const specOpts = opts[optArg];
|
|
41
|
+
Object.assign(opts, specOpts);
|
|
42
|
+
if (opts[optArg] === specOpts)
|
|
43
|
+
{ // specOpts didn't override the real option.
|
|
44
|
+
opts[optArg] = optDef;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return opts;
|
|
48
|
+
} // argOpts()
|
|
49
|
+
|
|
50
|
+
module.exports = argOpts;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {isArrayOf,S} = require('../types/basics');
|
|
4
|
+
const {getObjectPath} = require('../obj/ns');
|
|
5
|
+
const {val} = require('./val');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An alternative to `get()` that uses `getObjectPath()`
|
|
9
|
+
* to look for a specific nested property value.
|
|
10
|
+
*
|
|
11
|
+
* While `get()` supports positional arguments like `val()`,
|
|
12
|
+
* this function _only_ supports named options.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} obj - Object we're looking for properties in
|
|
15
|
+
* @param {(string|Array)} path - Path for `getObjectPath()`
|
|
16
|
+
* @param {object} [opts] Options
|
|
17
|
+
*
|
|
18
|
+
* This supports all of the same options as `get()`, plus all of the
|
|
19
|
+
* options supported by `getObjectPath()`. See the docs for both those
|
|
20
|
+
* functions to see what all is supported. If the same option is supported
|
|
21
|
+
* by *both* functions (e.g. `allowFun`) then the default value
|
|
22
|
+
* will be the one from `getObjectPath()` rather than `get()`.
|
|
23
|
+
*
|
|
24
|
+
* @param {boolean} [opts.ro=false] Should `opts` be read-only?
|
|
25
|
+
*
|
|
26
|
+
* If `true`, a copy of the `opts` will be made before any changes
|
|
27
|
+
* are performed, ensuring the original options aren't modified.
|
|
28
|
+
*
|
|
29
|
+
* @returns {*} The property if found, or `opts.default` if not.
|
|
30
|
+
*
|
|
31
|
+
* @see module:@lumjs/core/opt.get
|
|
32
|
+
* @see module:@lumjs/core/obj.getObjectPath
|
|
33
|
+
* @alias module:@lumjs/core/opt.getPath
|
|
34
|
+
*/
|
|
35
|
+
function getPath(obj, path, opts={})
|
|
36
|
+
{
|
|
37
|
+
const defvalue = opts.default;
|
|
38
|
+
if (opts.ro)
|
|
39
|
+
{
|
|
40
|
+
opts = Object.assign({}, opts);
|
|
41
|
+
}
|
|
42
|
+
delete opts.default;
|
|
43
|
+
|
|
44
|
+
return val(getObjectPath(obj, path, opts), defvalue,
|
|
45
|
+
(opts.allowNull ?? true),
|
|
46
|
+
opts.isLazy,
|
|
47
|
+
(opts.lazyThis ?? obj),
|
|
48
|
+
opts.lazyArgs);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Does a value appear to be a path usable by the
|
|
53
|
+
* {@link module:@lumjs/core/opt.getPath} function?
|
|
54
|
+
*
|
|
55
|
+
* @param {*} value - Value to test;
|
|
56
|
+
* will be considered a path if it is:
|
|
57
|
+
*
|
|
58
|
+
* - An `Array` with only `string` values in it.
|
|
59
|
+
* - A string with a `.` character in it.
|
|
60
|
+
*
|
|
61
|
+
* @returns {boolean}
|
|
62
|
+
* @alias module:@lumjs/core/opt.isPath
|
|
63
|
+
*/
|
|
64
|
+
function isPath(value)
|
|
65
|
+
{
|
|
66
|
+
return (isArrayOf(value, S)
|
|
67
|
+
|| (typeof value === S && value.includes('.')));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = {getPath, isPath};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {S,needObj,needType} = require('../types');
|
|
4
|
+
const {_opts,val} = require('./val');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* See if a property in an object is set.
|
|
8
|
+
*
|
|
9
|
+
* If it is, return the property, otherwise return a default value.
|
|
10
|
+
* This uses the `val()` method, and as such supports the same arguments.
|
|
11
|
+
* However read the descriptions, as defaults may be quite different!
|
|
12
|
+
*
|
|
13
|
+
* @param {object} obj - An object to test for a property in.
|
|
14
|
+
* @param {string} optname - The property name we're checking for.
|
|
15
|
+
* @param {*} defvalue - The default value.
|
|
16
|
+
*
|
|
17
|
+
* @param {(object|boolean)} [opts] Options
|
|
18
|
+
*
|
|
19
|
+
* If this is a `boolean` it is used as the `allowNull` option.
|
|
20
|
+
*
|
|
21
|
+
* @param {boolean} [opts.allowNull=true] Passed to `val()`;
|
|
22
|
+
* default is `true`, which differs from `val()`.
|
|
23
|
+
* @param {boolean} [opts.isLazy=false] Passed to `val()`;
|
|
24
|
+
* default is `false`, the same as `val()`.
|
|
25
|
+
* @param {object} [opts.lazyThis=obj] Passed to `val()`;
|
|
26
|
+
* default is `obj`, which differs from `val()`.
|
|
27
|
+
* @param {Array} [opts.lazyArgs] Passed to `val()`
|
|
28
|
+
* @param {boolean} [opts.allowFun=false] Allow `obj` to be a `function` ?
|
|
29
|
+
*
|
|
30
|
+
* By default only `object` values are valid for `obj`; this can be set to
|
|
31
|
+
* `true` to allow `function` values to be used.
|
|
32
|
+
*
|
|
33
|
+
* @param {boolean} [isLazy=false] Same as `opts.isLazy`
|
|
34
|
+
* @param {object} [lazyThis=opts] Same as `opts.lazyThis`
|
|
35
|
+
* @param {Array} [lazyArgs] Same as `opts.lazyArgs`
|
|
36
|
+
* @param {boolean} [allowFun] Same as `opts.allowFun`
|
|
37
|
+
*
|
|
38
|
+
* @returns {*} Either the property value, or the default value.
|
|
39
|
+
* @see module:@lumjs/core/opt.val
|
|
40
|
+
* @alias module:@lumjs/core/opt.get
|
|
41
|
+
*/
|
|
42
|
+
function get(obj, optname, defvalue,
|
|
43
|
+
allowNull=true,
|
|
44
|
+
isLazy=false,
|
|
45
|
+
lazyThis=obj,
|
|
46
|
+
lazyArgs=[],
|
|
47
|
+
allowFun=false)
|
|
48
|
+
{
|
|
49
|
+
const opts = _opts({allowNull,isLazy,lazyThis,lazyArgs,allowFun}, true);
|
|
50
|
+
|
|
51
|
+
needObj(obj, opts.allowFun);
|
|
52
|
+
needType(S, optname);
|
|
53
|
+
|
|
54
|
+
return val(obj[optname], defvalue,
|
|
55
|
+
opts.allowNull,
|
|
56
|
+
opts.isLazy,
|
|
57
|
+
opts.lazyThis,
|
|
58
|
+
opts.lazyArgs);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = {val, get};
|
package/lib/opt/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Functions for working with options and default values.
|
|
3
|
+
* @module @lumjs/core/opt
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const argOpts = require('./args');
|
|
7
|
+
const {getPath,isPath} = require('./getpath');
|
|
8
|
+
const {val,get} = require('./getval');
|
|
9
|
+
|
|
10
|
+
exports = module.exports =
|
|
11
|
+
{
|
|
12
|
+
argOpts, get, getPath, isPath, val,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const {wrapDepr} = require('../meta');
|
|
16
|
+
wrapDepr(exports, 'Opts',
|
|
17
|
+
{
|
|
18
|
+
dep: '@lumjs/core.opt.Opts',
|
|
19
|
+
rep: '@lumjs/opts',
|
|
20
|
+
get: () => require('@lumjs/opts'),
|
|
21
|
+
});
|
package/lib/opt/val.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {U,F} = require('../types/js');
|
|
4
|
+
const argOpts = require('./args');
|
|
5
|
+
|
|
6
|
+
// Helper for `val()` and `get()` to support new-style options.
|
|
7
|
+
function _opts(opts, defNull)
|
|
8
|
+
{
|
|
9
|
+
return argOpts(opts, 'allowNull', defNull);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* See if a value is *set*, and if not, return a default value.
|
|
14
|
+
*
|
|
15
|
+
* This function used to use all positional arguments, but it now
|
|
16
|
+
* supports named options if an `object` is passed as the third argument.
|
|
17
|
+
* If both named options and the corresponding positional arguments are
|
|
18
|
+
* specified, the named options will take precedence.
|
|
19
|
+
*
|
|
20
|
+
* @param {*} optvalue - The value we are testing.
|
|
21
|
+
* @param {*} defvalue - The default value if opt was null or undefined.
|
|
22
|
+
*
|
|
23
|
+
* @param {(object|boolean)} opts - Options
|
|
24
|
+
*
|
|
25
|
+
* If this is a `boolean` it is used as the `allowNull` option.
|
|
26
|
+
*
|
|
27
|
+
* @param {boolean} [opts.allowNull=false] If true, allow null to count as *set*.
|
|
28
|
+
* @param {boolean} [opts.isLazy=false] If true, and `defvalue` is a function,
|
|
29
|
+
* use the value from the function as
|
|
30
|
+
* the default.
|
|
31
|
+
* @param {object} [opts.lazyThis=null] If `isLazy` is true, this object will
|
|
32
|
+
* be used as `this` for the function.
|
|
33
|
+
* @param {Array} [opts.lazyArgs] If `isLazy` is true, this may be used
|
|
34
|
+
* as a list of arguments to pass.
|
|
35
|
+
*
|
|
36
|
+
* @param {boolean} [isLazy=false] Same as `opts.isLazy`
|
|
37
|
+
* @param {object} [lazyThis=null] Same as `opts.lazyThis`
|
|
38
|
+
* @param {Array} [lazyArgs] Same as `opts.lazyArgs`
|
|
39
|
+
*
|
|
40
|
+
* @return {*} Either `optvalue` or `defvalue` depending on the test.
|
|
41
|
+
* @alias module:@lumjs/core/opt.val
|
|
42
|
+
*/
|
|
43
|
+
function val(optvalue, defvalue,
|
|
44
|
+
allowNull=false,
|
|
45
|
+
isLazy=false,
|
|
46
|
+
lazyThis=null,
|
|
47
|
+
lazyArgs=[])
|
|
48
|
+
{
|
|
49
|
+
const opts = _opts({allowNull,isLazy,lazyThis,lazyArgs}, false);
|
|
50
|
+
|
|
51
|
+
if (typeof optvalue === U || (!opts.allowNull && optvalue === null))
|
|
52
|
+
{ // The defined value was not "set" as per our rules.
|
|
53
|
+
if (opts.isLazy && typeof defvalue === F)
|
|
54
|
+
{ // Get the default value from a passed in function.
|
|
55
|
+
return defvalue.apply(opts.lazyThis, opts.lazyArgs);
|
|
56
|
+
}
|
|
57
|
+
return defvalue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return optvalue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = {_opts, val};
|
package/lib/strings.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* String and locale related functions.
|
|
3
3
|
* @module @lumjs/core/strings
|
|
4
4
|
*/
|
|
5
|
-
const {S,B,F,isObj,root,isArray,needType,needObj
|
|
5
|
+
const {S,B,F,isObj,root,isArray,needType,needObj} = require('./types');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Get the locale/language string.
|
package/lib/types/basics.js
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Basic types sub-module.
|
|
5
|
+
*
|
|
6
|
+
* Provides a whole assortment of simple type test functions,
|
|
7
|
+
* as well as all of the constants found in its `JS` property.
|
|
8
|
+
* Which means `basics.S === basics.JS.S`.
|
|
9
|
+
*
|
|
10
|
+
* Unlike the `JS` object, this sub-module is NOT frozen/locked,
|
|
11
|
+
* although the JS constants are assigned as _non-configurable_ so
|
|
12
|
+
* they cannot be overwritten by different values.
|
|
13
|
+
*
|
|
14
|
+
* @module @lumjs/core/types/basics
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const JS = require('./js');
|
|
18
|
+
const {O, F, S, SY} = JS;
|
|
2
19
|
|
|
3
20
|
/**
|
|
4
21
|
* See if a value is a non-null `object`.
|
|
5
22
|
* @param {*} v - The value we're testing.
|
|
6
23
|
* @returns {boolean}
|
|
7
|
-
* @alias module:@lumjs/core/types.isObj
|
|
24
|
+
* @alias module:@lumjs/core/types/basics.isObj
|
|
8
25
|
*/
|
|
9
26
|
function isObj(v) { return (typeof v === O && v !== null); }
|
|
10
27
|
|
|
@@ -13,7 +30,7 @@ function isObj(v) { return (typeof v === O && v !== null); }
|
|
|
13
30
|
* Like `isObj()`, `null` does not count as an `object`.
|
|
14
31
|
* @param {*} v - The value we're testing.
|
|
15
32
|
* @returns {boolean}
|
|
16
|
-
* @alias module:@lumjs/core/types.isComplex
|
|
33
|
+
* @alias module:@lumjs/core/types/basics.isComplex
|
|
17
34
|
*/
|
|
18
35
|
function isComplex(v) { return (typeof v === F || isObj(v)); }
|
|
19
36
|
|
|
@@ -21,7 +38,7 @@ function isComplex(v) { return (typeof v === F || isObj(v)); }
|
|
|
21
38
|
* See if a value is *nil* (i.e. either `null` or `undefined`).
|
|
22
39
|
* @param {*} v - The value we're testing.
|
|
23
40
|
* @returns {boolean}
|
|
24
|
-
* @alias module:@lumjs/core/types.isNil
|
|
41
|
+
* @alias module:@lumjs/core/types/basics.isNil
|
|
25
42
|
*/
|
|
26
43
|
function isNil(v) { return (v === undefined || v === null); }
|
|
27
44
|
|
|
@@ -29,7 +46,7 @@ function isNil(v) { return (v === undefined || v === null); }
|
|
|
29
46
|
* See if a value is not *nil* (i.e. neither `null` nor `undefined`).
|
|
30
47
|
* @param {*} v - The value we're testing.
|
|
31
48
|
* @returns {boolean}
|
|
32
|
-
* @alias module:@lumjs/core/types.notNil
|
|
49
|
+
* @alias module:@lumjs/core/types/basics.notNil
|
|
33
50
|
*/
|
|
34
51
|
function notNil(v) { return (v !== undefined && v !== null); }
|
|
35
52
|
|
|
@@ -39,7 +56,7 @@ function notNil(v) { return (v !== undefined && v !== null); }
|
|
|
39
56
|
* is neither *nil* nor *complex*.
|
|
40
57
|
* @param {*} v - The value we're testing.
|
|
41
58
|
* @returns {boolean}
|
|
42
|
-
* @alias module:@lumjs/core/types.isScalar
|
|
59
|
+
* @alias module:@lumjs/core/types/basics.isScalar
|
|
43
60
|
*/
|
|
44
61
|
function isScalar(v) { return (notNil(v) && !isComplex(v)); }
|
|
45
62
|
|
|
@@ -49,7 +66,7 @@ function isScalar(v) { return (notNil(v) && !isComplex(v)); }
|
|
|
49
66
|
* @function
|
|
50
67
|
* @param {*} v - The value we're testing.
|
|
51
68
|
* @returns {boolean}
|
|
52
|
-
* @alias module:@lumjs/core/types.isArray
|
|
69
|
+
* @alias module:@lumjs/core/types/basics.isArray
|
|
53
70
|
*/
|
|
54
71
|
const isArray = Array.isArray;
|
|
55
72
|
|
|
@@ -57,7 +74,7 @@ const isArray = Array.isArray;
|
|
|
57
74
|
* See if a value is a `TypedArray` object.
|
|
58
75
|
* @param {*} v - The value we're testing.
|
|
59
76
|
* @returns {boolean}
|
|
60
|
-
* @alias module:@lumjs/core/types.isTypedArray
|
|
77
|
+
* @alias module:@lumjs/core/types/basics.isTypedArray
|
|
61
78
|
*/
|
|
62
79
|
function isTypedArray(v)
|
|
63
80
|
{
|
|
@@ -70,7 +87,7 @@ function isTypedArray(v)
|
|
|
70
87
|
* @param {boolean} [typed=false] If `true` we want a `TypedArray`.
|
|
71
88
|
* If `false` (default) we want a regular `Array`.
|
|
72
89
|
* @returns {boolean}
|
|
73
|
-
* @alias module:@lumjs/core/types.nonEmptyArray
|
|
90
|
+
* @alias module:@lumjs/core/types/basics.nonEmptyArray
|
|
74
91
|
*/
|
|
75
92
|
function nonEmptyArray(v, typed=false)
|
|
76
93
|
{
|
|
@@ -84,7 +101,7 @@ function nonEmptyArray(v, typed=false)
|
|
|
84
101
|
* See if a value is an `arguments` object.
|
|
85
102
|
* @param {*} v - The value we're testing.
|
|
86
103
|
* @returns {boolean}
|
|
87
|
-
* @alias module:@lumjs/core/types.isArguments
|
|
104
|
+
* @alias module:@lumjs/core/types/basics.isArguments
|
|
88
105
|
*/
|
|
89
106
|
function isArguments(v)
|
|
90
107
|
{
|
|
@@ -95,7 +112,7 @@ function isArguments(v)
|
|
|
95
112
|
* See if a value is a Property name.
|
|
96
113
|
* @param {*} v - The value we're testing.
|
|
97
114
|
* @returns {boolean}
|
|
98
|
-
* @alias module:@lumjs/core/types.isProperty
|
|
115
|
+
* @alias module:@lumjs/core/types/basics.isProperty
|
|
99
116
|
*/
|
|
100
117
|
function isProperty(v)
|
|
101
118
|
{
|
|
@@ -117,7 +134,7 @@ function isProperty(v)
|
|
|
117
134
|
* function will pass the test.
|
|
118
135
|
*
|
|
119
136
|
* @returns {boolean}
|
|
120
|
-
* @alias module:@lumjs/core/types.isIterable
|
|
137
|
+
* @alias module:@lumjs/core/types/basics.isIterable
|
|
121
138
|
*/
|
|
122
139
|
function isIterable(v)
|
|
123
140
|
{
|
|
@@ -129,7 +146,7 @@ function isIterable(v)
|
|
|
129
146
|
*
|
|
130
147
|
* @param {*} v - Value to test
|
|
131
148
|
* @returns {boolean}
|
|
132
|
-
* @alias module:@lumjs/core/types.isConstructor
|
|
149
|
+
* @alias module:@lumjs/core/types/basics.isConstructor
|
|
133
150
|
*/
|
|
134
151
|
function isConstructor(v)
|
|
135
152
|
{
|
|
@@ -153,7 +170,7 @@ function isConstructor(v)
|
|
|
153
170
|
*
|
|
154
171
|
* @param {object} obj - The object we are testing.
|
|
155
172
|
* @returns {boolean} - Is the object a valid descriptor?
|
|
156
|
-
* @alias module:@lumjs/core/types.doesDescriptor
|
|
173
|
+
* @alias module:@lumjs/core/types/basics.doesDescriptor
|
|
157
174
|
*/
|
|
158
175
|
function doesDescriptor(obj)
|
|
159
176
|
{
|
|
@@ -193,7 +210,7 @@ function doesDescriptor(obj)
|
|
|
193
210
|
* If this is `false` then any unknown properties will be ignored.
|
|
194
211
|
*
|
|
195
212
|
* @returns {boolean} Is the object a valid descriptor template?
|
|
196
|
-
* @alias module:@lumjs/core/types.doesDescriptorTemplate
|
|
213
|
+
* @alias module:@lumjs/core/types/basics.doesDescriptorTemplate
|
|
197
214
|
*/
|
|
198
215
|
function doesDescriptorTemplate(obj, accessor=false, strict=true)
|
|
199
216
|
{
|
|
@@ -229,5 +246,7 @@ module.exports =
|
|
|
229
246
|
{
|
|
230
247
|
isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
|
|
231
248
|
isIterable, nonEmptyArray, isArguments, isProperty, isConstructor,
|
|
232
|
-
doesDescriptor, doesDescriptorTemplate,
|
|
249
|
+
doesDescriptor, doesDescriptorTemplate, JS,
|
|
233
250
|
}
|
|
251
|
+
|
|
252
|
+
JS.addTo(module.exports);
|