@lumjs/core 1.0.0-beta.4 → 1.0.0-beta.6
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 +6 -3
- package/TODO.md +6 -12
- package/docs/changelogs/1.0-beta.md +33 -3
- package/docs/changelogs/1.x.md +17 -0
- package/lib/arrays.js +19 -7
- package/lib/context.js +46 -9
- package/lib/enum.js +29 -15
- package/lib/flags.js +6 -0
- package/lib/index.js +77 -5
- package/lib/lazy.js +7 -5
- package/lib/meta.js +10 -0
- package/lib/modules.js +27 -3
- package/lib/obj/clone.js +11 -9
- package/lib/obj/copyall.js +3 -0
- package/lib/obj/copyprops.js +1 -0
- package/lib/obj/index.js +4 -4
- package/lib/obj/lock.js +13 -9
- package/lib/obj/merge.js +2 -0
- package/lib/obj/ns.js +45 -8
- package/lib/objectid.js +2 -3
- package/lib/observable.js +23 -9
- package/lib/opt.js +6 -1
- package/lib/strings.js +43 -4
- package/lib/types/basics.js +12 -0
- package/lib/types/def.js +21 -18
- package/lib/types/index.js +19 -2
- package/lib/types/isa.js +5 -12
- package/lib/types/needs.js +6 -1
- package/lib/types/root.js +6 -8
- package/lib/types/stringify.js +98 -0
- package/lib/types/typelist.js +66 -2
- package/package.json +21 -3
- package/test/types.js +102 -13
package/lib/obj/clone.js
CHANGED
|
@@ -19,6 +19,7 @@ const copyProps = require('./copyprops');
|
|
|
19
19
|
* | `CLONE.ENTIRE` | ✓ | × | ✓ | |
|
|
20
20
|
* | `CLONE.JSON` | × | × | ✓ | Uses JSON, so no `function` or `symbol` support. |
|
|
21
21
|
*
|
|
22
|
+
* @alias module:@lumjs/core/obj.CLONE
|
|
22
23
|
*/
|
|
23
24
|
const CLONE = Enum(['DEF','FULL','ALL','DEEP','ENTIRE','JSON']);
|
|
24
25
|
|
|
@@ -43,25 +44,26 @@ const copyProps = require('./copyprops');
|
|
|
43
44
|
*
|
|
44
45
|
* Note: The `CLONE` enum is also aliased as `clone.MODE` as an alternative.
|
|
45
46
|
*
|
|
46
|
-
* @param {boolean} [opts.addClone=false] - Call
|
|
47
|
+
* @param {boolean} [opts.addClone=false] - Call `addClone()` on the cloned object.
|
|
47
48
|
*
|
|
48
49
|
* The options sent to this function will be used as the defaults in
|
|
49
|
-
* the clone() method added to the object.
|
|
50
|
+
* the `clone()` method added to the object.
|
|
50
51
|
*
|
|
51
|
-
* @param {boolean} [opts.addLock=false] - Call
|
|
52
|
+
* @param {boolean} [opts.addLock=false] - Call `addLock()` on the cloned object.
|
|
52
53
|
*
|
|
53
|
-
* No further options for this, just add a lock() method to the clone.
|
|
54
|
+
* No further options for this, just add a `lock()` method to the clone.
|
|
54
55
|
*
|
|
55
|
-
* @param {?object} [opts.copy] Call
|
|
56
|
+
* @param {?object} [opts.copy] Call `copyProps()` on the cloned object.
|
|
56
57
|
*
|
|
57
58
|
* Will pass the original `obj` as the source to copy from.
|
|
58
59
|
* Will pass `opts.copy` as the options.
|
|
59
60
|
*
|
|
60
61
|
* @return {object} - The clone of the object.
|
|
62
|
+
* @alias module:@lumjs/core/obj.clone
|
|
61
63
|
*/
|
|
62
64
|
function clone(obj, opts={})
|
|
63
65
|
{
|
|
64
|
-
//console.debug("
|
|
66
|
+
//console.debug("clone()", obj, opts);
|
|
65
67
|
|
|
66
68
|
if (!isComplex(obj))
|
|
67
69
|
{ // Doesn't need cloning.
|
|
@@ -156,7 +158,7 @@ exports.clone = clone;
|
|
|
156
158
|
*
|
|
157
159
|
* ```{mode: CLONE.DEF, addClone: true, addLock: false}```
|
|
158
160
|
*
|
|
159
|
-
* @
|
|
161
|
+
* @alias module:@lumjs/core/obj.addClone
|
|
160
162
|
*/
|
|
161
163
|
function addClone(obj, defOpts=null)
|
|
162
164
|
{
|
|
@@ -185,14 +187,14 @@ exports.addClone = addClone;
|
|
|
185
187
|
* If the object is extensible, it's returned as is.
|
|
186
188
|
*
|
|
187
189
|
* If not, if the object has a `clone()` method it will be used.
|
|
188
|
-
* Otherwise use
|
|
190
|
+
* Otherwise use our `clone()` function.
|
|
189
191
|
*
|
|
190
192
|
* @param {object} obj - The object to clone if needed.
|
|
191
193
|
* @param {object} [opts] - Options to pass to `clone()` method.
|
|
192
194
|
*
|
|
193
195
|
* @return {object} - Either the original object, or an extensible clone.
|
|
194
196
|
*
|
|
195
|
-
* @
|
|
197
|
+
* @alias module:@lumjs/core/obj.cloneIfLocked
|
|
196
198
|
*/
|
|
197
199
|
function cloneIfLocked(obj, opts)
|
|
198
200
|
{
|
package/lib/obj/copyall.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* It does no type checking, and has no qualms about overwriting properties.
|
|
5
5
|
* You probably want something like `copyProps` instead.
|
|
6
|
+
*
|
|
7
|
+
* @alias module:@lumjs/core/obj.copyAll
|
|
6
8
|
*/
|
|
7
9
|
function copyAll(target, ...sources)
|
|
8
10
|
{
|
|
@@ -13,6 +15,7 @@ function copyAll(target, ...sources)
|
|
|
13
15
|
target[name] = source[name];
|
|
14
16
|
}
|
|
15
17
|
}
|
|
18
|
+
return target;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
module.exports = copyAll;
|
package/lib/obj/copyprops.js
CHANGED
|
@@ -23,6 +23,7 @@ const {B,isObj,isComplex,isArray,def: defProp} = require('../types');
|
|
|
23
23
|
* if that property can be overwritten or not.
|
|
24
24
|
*
|
|
25
25
|
* @returns {object} The `target` object.
|
|
26
|
+
* @alias module:@lumjs/core/obj.copyProps
|
|
26
27
|
*/
|
|
27
28
|
function copyProps(source, target, propOpts)
|
|
28
29
|
{
|
package/lib/obj/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Object helpers sub-module.
|
|
3
|
+
* @module @lumjs/core/obj
|
|
4
|
+
*/
|
|
5
5
|
|
|
6
6
|
const copyAll = require('./copyall');
|
|
7
7
|
const copyProps = require('./copyprops');
|
package/lib/obj/lock.js
CHANGED
|
@@ -5,8 +5,8 @@ const {B, isObj, def} = require('../types');
|
|
|
5
5
|
* Lock an object using Object.freeze()
|
|
6
6
|
*
|
|
7
7
|
* @param {object} obj - The object we want to lock.
|
|
8
|
-
* @param {boolean} [clonable=true] Pass to
|
|
9
|
-
* @param {object} [cloneOpts=null] Options for addClone
|
|
8
|
+
* @param {boolean} [clonable=true] Pass to `addClone()` first?
|
|
9
|
+
* @param {object} [cloneOpts=null] Options for `addClone()`.
|
|
10
10
|
* @param {boolean} [useSeal=false] Use Object.seal() instead of freeze.
|
|
11
11
|
*
|
|
12
12
|
* If cloneOpts is `null` then we will use the following:
|
|
@@ -14,8 +14,7 @@ const {B, isObj, def} = require('../types');
|
|
|
14
14
|
* ```{mode: CLONE.DEF, addClone: true, addLock: true}```
|
|
15
15
|
*
|
|
16
16
|
* @return {object} The locked object.
|
|
17
|
-
*
|
|
18
|
-
* @method Lum._.lock
|
|
17
|
+
* @alias module:@lumjs/core/obj.lock
|
|
19
18
|
*/
|
|
20
19
|
function lock(obj, clonable=true, cloneOpts=null, useSeal=false)
|
|
21
20
|
{
|
|
@@ -37,12 +36,17 @@ const {B, isObj, def} = require('../types');
|
|
|
37
36
|
/**
|
|
38
37
|
* Add a lock() method to an object.
|
|
39
38
|
*
|
|
40
|
-
* Adds a wrapper version of
|
|
41
|
-
*
|
|
42
|
-
* @param {object} obj - The object we're adding lock() to.
|
|
43
|
-
* @param {object} opts - Options (TODO: document this).
|
|
39
|
+
* Adds a wrapper version of `lock()` to the object as a method.
|
|
44
40
|
*
|
|
45
|
-
* @
|
|
41
|
+
* @param {object} obj - The object we're adding `lock()` to.
|
|
42
|
+
* @param {object} [opts] - Options and defaults.
|
|
43
|
+
* In addition to options specific to this function, any options
|
|
44
|
+
* supported by `addClone()` may be specified here as well.
|
|
45
|
+
* @param {object} [opts.lockDesc] Descriptor rules the `lock()` method.
|
|
46
|
+
* @param {boolean} [opts.addClone=true] Default for `clonable` parameter.
|
|
47
|
+
* @param {object} [opts.useSeal=false] Default for `useSeal` parameter.
|
|
48
|
+
* @returns {object} `obj`
|
|
49
|
+
* @alias module:@lumjs/core/obj.addLock
|
|
46
50
|
*/
|
|
47
51
|
function addLock(obj, opts)
|
|
48
52
|
{
|
package/lib/obj/merge.js
CHANGED
|
@@ -20,6 +20,7 @@ const {B, isObj} = require('../types');
|
|
|
20
20
|
* as the `opts` argument directly will set this option.
|
|
21
21
|
*
|
|
22
22
|
* @returns {object} The `target` object.
|
|
23
|
+
* @alias module:@lumjs/core/obj.mergeNested
|
|
23
24
|
*/
|
|
24
25
|
function mergeNested(source, target, opts={})
|
|
25
26
|
{
|
|
@@ -64,6 +65,7 @@ const {B, isObj} = require('../types');
|
|
|
64
65
|
* @param {object} [opts2=opts1] Options for the second merge operation.
|
|
65
66
|
* If this is not specified, `opts2` will be the same as `opts1`.
|
|
66
67
|
*
|
|
68
|
+
* @alias module:@lumjs/core/obj.syncNested
|
|
67
69
|
*/
|
|
68
70
|
function syncNested(obj1, obj2, opts1={}, opts2=opts1)
|
|
69
71
|
{
|
package/lib/obj/ns.js
CHANGED
|
@@ -5,13 +5,14 @@ const
|
|
|
5
5
|
} = require('../types');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Need a String or Array
|
|
9
|
+
* @alias module:@lumjs/core/obj.SOA
|
|
9
10
|
*/
|
|
10
11
|
function SOA(name, err=true)
|
|
11
12
|
{
|
|
12
13
|
const msg = (typeof name === S)
|
|
13
|
-
? name + ' ' +
|
|
14
|
-
:
|
|
14
|
+
? name + ' ' + SOA.message
|
|
15
|
+
: SOA.message;
|
|
15
16
|
return err ? (new TypeError(msg)) : msg;
|
|
16
17
|
}
|
|
17
18
|
SOA.message = "must be a string or non-empty array";
|
|
@@ -19,6 +20,17 @@ def(SOA, 'toString', function() { return this.message; });
|
|
|
19
20
|
|
|
20
21
|
exports.SOA = SOA;
|
|
21
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Get a namespace path string.
|
|
25
|
+
*
|
|
26
|
+
* If it's already a string, return it as is.
|
|
27
|
+
* If it's an array of strings, join the elements with a `.` character.
|
|
28
|
+
*
|
|
29
|
+
* @param {(string|string[])} ns - A dotted string, or array of paths.
|
|
30
|
+
* @param {*} name - Name to use in the SOA error
|
|
31
|
+
* @returns {string}
|
|
32
|
+
* @alias module:@lumjs/core/obj.nsString
|
|
33
|
+
*/
|
|
22
34
|
function nsString(ns, name='Namespace')
|
|
23
35
|
{
|
|
24
36
|
if (nonEmptyArray(ns))
|
|
@@ -34,6 +46,17 @@ function nsString(ns, name='Namespace')
|
|
|
34
46
|
|
|
35
47
|
exports.nsString = nsString;
|
|
36
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Get a namespace path array.
|
|
51
|
+
*
|
|
52
|
+
* If it's already an array, return it as is.
|
|
53
|
+
* If it's a string, split it into an array, with the `.` delimiter.
|
|
54
|
+
*
|
|
55
|
+
* @param {(string|string[])} ns - A dotted string, or array of paths.
|
|
56
|
+
* @param {*} name - Name to use in the SOA error
|
|
57
|
+
* @returns {string[]}
|
|
58
|
+
* @alias module:@lumjs/core/obj.nsArray
|
|
59
|
+
*/
|
|
37
60
|
function nsArray(ns, name='Namespace')
|
|
38
61
|
{
|
|
39
62
|
if (typeof ns === S)
|
|
@@ -57,6 +80,7 @@ exports.nsArray = nsArray;
|
|
|
57
80
|
* Generally a string of dot (`.`) separated nested property names.
|
|
58
81
|
* @param {object} [opts] TBD.
|
|
59
82
|
* @return {*} The property if found, or `opts.default` if not.
|
|
83
|
+
* @alias module:@lumjs/core/obj.getObjectPath
|
|
60
84
|
*/
|
|
61
85
|
function getObjectPath(obj, proppath, opts={})
|
|
62
86
|
{
|
|
@@ -97,6 +121,7 @@ exports.getObjectPath = getObjectPath;
|
|
|
97
121
|
* @param {object} [opts] TBD.
|
|
98
122
|
* @return {*} Generally the last object in the nested path.
|
|
99
123
|
* However the output may vary depending on the options.
|
|
124
|
+
* @alias module:@lumjs/core/obj.setObjectPath
|
|
100
125
|
*/
|
|
101
126
|
function setObjectPath(obj, proppath, opts={})
|
|
102
127
|
{
|
|
@@ -169,9 +194,15 @@ exports.setObjectPath = setObjectPath;
|
|
|
169
194
|
|
|
170
195
|
/**
|
|
171
196
|
* Get a global namespace path if it exists.
|
|
197
|
+
*
|
|
198
|
+
* This literally just calls `getObjectPath()` on the `root` object.
|
|
172
199
|
*
|
|
173
|
-
* -
|
|
174
|
-
*
|
|
200
|
+
* @param {(string|Array)} proppath - Property path we're looking for.
|
|
201
|
+
* Generally a string of dot (`.`) separated nested property names.
|
|
202
|
+
* @param {object} [opts] See `getObjectPath()` for details.
|
|
203
|
+
* @return {*} The property if found, or `opts.default` if not.
|
|
204
|
+
* @alias module:@lumjs/core/obj.getNamespace
|
|
205
|
+
* @see module:@lumjs/core/obj.getObjectPath
|
|
175
206
|
*/
|
|
176
207
|
function getNamespace(namespaces, opts={})
|
|
177
208
|
{
|
|
@@ -183,12 +214,18 @@ exports.getNamespace = getNamespace;
|
|
|
183
214
|
/**
|
|
184
215
|
* Create a global namespace path if it does not exist.
|
|
185
216
|
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
217
|
+
* This literally just calls `setObjectPath()` on the `root` object.
|
|
218
|
+
*
|
|
219
|
+
* @param {(string|Array)} proppath - Property path to create.
|
|
220
|
+
* @param {object} [opts] See `setObjectPath()` for details.
|
|
221
|
+
* @return {*} Generally the last object in the nested path.
|
|
222
|
+
* However the output may vary depending on the options.
|
|
223
|
+
* @alias module:@lumjs/core/obj.setNamespace
|
|
224
|
+
* @see module:@lumjs/core/obj.setObjectPath
|
|
188
225
|
*/
|
|
189
226
|
function setNamespace(namespaces, opts={})
|
|
190
227
|
{
|
|
191
228
|
return setObjectPath(root, namespaces, opts);
|
|
192
229
|
}
|
|
193
230
|
|
|
194
|
-
exports.setNamespace = setNamespace;
|
|
231
|
+
exports.setNamespace = setNamespace;
|
package/lib/objectid.js
CHANGED
|
@@ -4,9 +4,8 @@ const {notNil,def,isNil} = require('./types');
|
|
|
4
4
|
/**
|
|
5
5
|
* Generate a large random number.
|
|
6
6
|
*
|
|
7
|
-
* Takes advantage of
|
|
8
|
-
*
|
|
9
7
|
* @returns {number}
|
|
8
|
+
* @alias module:@lumjs/core.randomNumber
|
|
10
9
|
*/
|
|
11
10
|
function randomNumber()
|
|
12
11
|
{
|
|
@@ -18,6 +17,7 @@ exports.randomNumber = randomNumber;
|
|
|
18
17
|
/**
|
|
19
18
|
* A class for creating unique identifier objects.
|
|
20
19
|
* Generally only used by my own inernal libraries, thus the name.
|
|
20
|
+
* @alias module:@lumjs/core.InternalObjectId
|
|
21
21
|
*/
|
|
22
22
|
class InternalObjectId
|
|
23
23
|
{
|
|
@@ -34,7 +34,6 @@ class InternalObjectId
|
|
|
34
34
|
* @param {boolean} [opts.useInstance=true] Store object or id value?
|
|
35
35
|
* If this is `true` (now the default), we store the instance itself.
|
|
36
36
|
* If this is `false` (the old default), we store just the `id` value.
|
|
37
|
-
*
|
|
38
37
|
*/
|
|
39
38
|
constructor(opts={})
|
|
40
39
|
{
|
package/lib/observable.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
// This library was original based on the observable library from riot.js,
|
|
2
|
-
// but has been refactored and expanded a lot since then.
|
|
3
1
|
|
|
4
|
-
const
|
|
5
|
-
const {B,F,S,def,isObj,isComplex} = types;
|
|
2
|
+
const {B,F,S,def,isObj,isComplex,TYPES} = require('./types');;
|
|
6
3
|
|
|
7
4
|
/**
|
|
8
|
-
* Make an object support the
|
|
5
|
+
* Make an object support the *Observable* API.
|
|
9
6
|
*
|
|
10
7
|
* Adds `on()`, `off()`, `one()`, and `trigger()` methods.
|
|
11
8
|
*
|
|
@@ -39,6 +36,8 @@ const {B,F,S,def,isObj,isComplex} = types;
|
|
|
39
36
|
* In any other case it defaults to `null`.
|
|
40
37
|
*
|
|
41
38
|
* @returns {object} el
|
|
39
|
+
*
|
|
40
|
+
* @exports module:@lumjs/core/observable
|
|
42
41
|
*/
|
|
43
42
|
function observable (el={}, opts={})
|
|
44
43
|
{
|
|
@@ -274,9 +273,10 @@ function observable (el={}, opts={})
|
|
|
274
273
|
module.exports = observable;
|
|
275
274
|
|
|
276
275
|
/**
|
|
277
|
-
* See if
|
|
276
|
+
* See if a value implements the *Observable* interface.
|
|
278
277
|
*
|
|
279
|
-
* @
|
|
278
|
+
* @function module:@lumjs/core/observable.is
|
|
279
|
+
* @param {*} obj - The expected object/function to test.
|
|
280
280
|
* @returns {boolean}
|
|
281
281
|
*/
|
|
282
282
|
function isObservable(obj)
|
|
@@ -289,5 +289,19 @@ function isObservable(obj)
|
|
|
289
289
|
// Add an 'is()' method to `observable` itself.
|
|
290
290
|
def(observable, 'is', isObservable);
|
|
291
291
|
|
|
292
|
-
|
|
293
|
-
|
|
292
|
+
/**
|
|
293
|
+
* Does a value implement the Observable interface?
|
|
294
|
+
* @name module:@lumjs/core/types.doesObservable
|
|
295
|
+
* @function
|
|
296
|
+
* @param {*} v - The expected object/function to test.
|
|
297
|
+
* @returns {boolean}
|
|
298
|
+
* @see module:@lumjs/core/observable.is
|
|
299
|
+
*/
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Extension type for the {@link module:@lumjs/core/observable} interface.
|
|
303
|
+
* @memberof module:@lumjs/core/types.TYPES
|
|
304
|
+
* @member {string} OBSERV - Implements the *Observable* interface.
|
|
305
|
+
*/
|
|
306
|
+
|
|
307
|
+
TYPES.add('OBSERV', 'observable', isObservable, 'doesObservable');
|
package/lib/opt.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Functions for working with options and default values.
|
|
3
|
+
* @module @lumjs/core/opt
|
|
4
|
+
*/
|
|
2
5
|
|
|
3
6
|
const {U,needObj,needType} = require('./types');
|
|
4
7
|
|
|
@@ -16,6 +19,7 @@ const {U,needObj,needType} = require('./types');
|
|
|
16
19
|
* be used as `this` for the function.
|
|
17
20
|
*
|
|
18
21
|
* @return {*} Either the specified `opt` value or the default value.
|
|
22
|
+
* @alias module:@lumjs/core/opt.val
|
|
19
23
|
*/
|
|
20
24
|
function val(opt, defvalue, allowNull=false, isLazy=false, lazyThis=null)
|
|
21
25
|
{
|
|
@@ -49,6 +53,7 @@ exports.val = val;
|
|
|
49
53
|
* @param {object} [lazyThis=opts] Same as `val()`.
|
|
50
54
|
*
|
|
51
55
|
* @return {*} Either the property value, or the default value.
|
|
56
|
+
* module:@lumjs/core/opt.get
|
|
52
57
|
*/
|
|
53
58
|
function get(obj, optname, defvalue, allowNull=true, isLazy=false, lazyThis=obj)
|
|
54
59
|
{
|
package/lib/strings.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* String and locale related functions.
|
|
3
|
+
* @module @lumjs/core/strings
|
|
4
|
+
*/
|
|
2
5
|
const {S,B,F,isObj,root,isArray,needType,needObj} = require('./types')
|
|
3
6
|
|
|
4
7
|
/**
|
|
@@ -8,7 +11,8 @@ const {S,B,F,isObj,root,isArray,needType,needObj} = require('./types')
|
|
|
8
11
|
* 2. If `Intl` exists it will be used.
|
|
9
12
|
* 3. If neither of those exist, uses `'en-US'` as a default.
|
|
10
13
|
*
|
|
11
|
-
* @returns string - The locale/language string.
|
|
14
|
+
* @returns {string} - The locale/language string.
|
|
15
|
+
* @alias module:@lumjs/core/strings.getLocale
|
|
12
16
|
*/
|
|
13
17
|
function getLocale()
|
|
14
18
|
{
|
|
@@ -42,7 +46,8 @@ exports.getLocale = getLocale;
|
|
|
42
46
|
* @param {boolean} [lcrest=false] Make the rest of the string lowercase?
|
|
43
47
|
* @param {string} [locale=getLocale()] The locale/language of the string.
|
|
44
48
|
*
|
|
45
|
-
* @returns string - The output string.
|
|
49
|
+
* @returns {string} - The output string.
|
|
50
|
+
* @alias module:@lumjs/core/strings.ucfirst
|
|
46
51
|
*/
|
|
47
52
|
function ucfirst ([ first, ...rest ], lcrest = false, locale = getLocale())
|
|
48
53
|
{
|
|
@@ -61,11 +66,13 @@ exports.ucfirst = ucfirst;
|
|
|
61
66
|
* Make the first character of each *word* in a string uppercase.
|
|
62
67
|
*
|
|
63
68
|
* @param {string} string - The input string.
|
|
64
|
-
* @param {boolean} [unicode=false] Use Unicode words?
|
|
69
|
+
* @param {boolean} [unicode=false] Use *Unicode* words?
|
|
70
|
+
* Only uses simple *PCRE-style* words (`\w`) otherwise.
|
|
65
71
|
* @param {boolean} [lcrest=false] Make the rest of each word lowercase?
|
|
66
72
|
* @param {string} [locale=getLocale()] The locale/language of the string.
|
|
67
73
|
*
|
|
68
74
|
* @returns {string} - The output string.
|
|
75
|
+
* @alias module:@lumjs/core/strings.ucwords
|
|
69
76
|
*/
|
|
70
77
|
function ucwords(string, unicode = false, lcrest = false, locale = getLocale())
|
|
71
78
|
{
|
|
@@ -77,6 +84,12 @@ exports.ucwords = ucwords;
|
|
|
77
84
|
|
|
78
85
|
/**
|
|
79
86
|
* Is the passed in value a valid `String.replace` search value.
|
|
87
|
+
*
|
|
88
|
+
* Only strings and `RegExp` objects are valid.
|
|
89
|
+
*
|
|
90
|
+
* @param {*} v - Value to check.
|
|
91
|
+
* @returns {boolean}
|
|
92
|
+
* @alias module:@lumjs/core/strings.isSearch
|
|
80
93
|
*/
|
|
81
94
|
function isSearch(value)
|
|
82
95
|
{
|
|
@@ -87,6 +100,12 @@ exports.isSearch = isSearch;
|
|
|
87
100
|
|
|
88
101
|
/**
|
|
89
102
|
* Is the passed in value a valid `String.replace` replacement value.
|
|
103
|
+
*
|
|
104
|
+
* Only strings and functions are valid.
|
|
105
|
+
*
|
|
106
|
+
* @param {*} v - Value to check.
|
|
107
|
+
* @returns {boolean}
|
|
108
|
+
* @alias module:@lumjs/core/strings.isReplacement
|
|
90
109
|
*/
|
|
91
110
|
function isReplacement(value)
|
|
92
111
|
{
|
|
@@ -95,6 +114,26 @@ function isReplacement(value)
|
|
|
95
114
|
|
|
96
115
|
exports.isReplacement = isReplacement;
|
|
97
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Apply multiple replacement rules to a string.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} string - The input string.
|
|
121
|
+
* @param {object} replacements - Replacement rules.
|
|
122
|
+
*
|
|
123
|
+
* If this is an `Array` then each item in the array can be:
|
|
124
|
+
* - Another array with two items, `[find, replace]`;
|
|
125
|
+
* - A `boolean`, will change the current `useAll` settting.
|
|
126
|
+
*
|
|
127
|
+
* If this is any other kind of `object` then the enumerable
|
|
128
|
+
* property *keys* will be used as `find` strings, and the
|
|
129
|
+
* associated *values* will be used as the `replacement` values.
|
|
130
|
+
*
|
|
131
|
+
* @param {boolean} [useAll] Which replacement method will be used.
|
|
132
|
+
* If `true` we use `replaceAll()`, if `false` we use `replace()`.
|
|
133
|
+
* The default is `false` if `value` is an `Array`, or `true` otherwise.
|
|
134
|
+
* @returns {string} The output string with all replacements performed.
|
|
135
|
+
* @alias module:@lumjs/core/strings.replaceItems
|
|
136
|
+
*/
|
|
98
137
|
function replaceItems(string, replacements, useAll)
|
|
99
138
|
{
|
|
100
139
|
needType(S, string);
|
package/lib/types/basics.js
CHANGED
|
@@ -4,6 +4,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
4
4
|
* See if a value is a non-null `object`.
|
|
5
5
|
* @param {*} v - The value we're testing.
|
|
6
6
|
* @returns {boolean}
|
|
7
|
+
* @alias module:@lumjs/core/types.isObj
|
|
7
8
|
*/
|
|
8
9
|
function isObj(v) { return (typeof v === O && v !== null); }
|
|
9
10
|
|
|
@@ -12,6 +13,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
12
13
|
* Like `isObj()`, `null` does not count as an `object`.
|
|
13
14
|
* @param {*} v - The value we're testing.
|
|
14
15
|
* @returns {boolean}
|
|
16
|
+
* @alias module:@lumjs/core/types.isComplex
|
|
15
17
|
*/
|
|
16
18
|
function isComplex(v) { return (typeof v === F || isObj(v)); }
|
|
17
19
|
|
|
@@ -19,6 +21,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
19
21
|
* See if a value is *nil* (i.e. either `null` or `undefined`).
|
|
20
22
|
* @param {*} v - The value we're testing.
|
|
21
23
|
* @returns {boolean}
|
|
24
|
+
* @alias module:@lumjs/core/types.isNil
|
|
22
25
|
*/
|
|
23
26
|
function isNil(v) { return (v === undefined || v === null); }
|
|
24
27
|
|
|
@@ -26,6 +29,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
26
29
|
* See if a value is not *nil* (i.e. neither `null` nor `undefined`).
|
|
27
30
|
* @param {*} v - The value we're testing.
|
|
28
31
|
* @returns {boolean}
|
|
32
|
+
* @alias module:@lumjs/core/types.notNil
|
|
29
33
|
*/
|
|
30
34
|
function notNil(v) { return (v !== undefined && v !== null); }
|
|
31
35
|
|
|
@@ -35,14 +39,17 @@ const {O, F, S, SY} = require('./js');
|
|
|
35
39
|
* is neither *nil* nor *complex*.
|
|
36
40
|
* @param {*} v - The value we're testing.
|
|
37
41
|
* @returns {boolean}
|
|
42
|
+
* @alias module:@lumjs/core/types.isScalar
|
|
38
43
|
*/
|
|
39
44
|
function isScalar(v) { return (notNil(v) && !isComplex(v)); }
|
|
40
45
|
|
|
41
46
|
/**
|
|
42
47
|
* See if a value is an `Array` object.
|
|
43
48
|
* This is literally just a copy of `Array.isArray`.
|
|
49
|
+
* @function
|
|
44
50
|
* @param {*} v - The value we're testing.
|
|
45
51
|
* @returns {boolean}
|
|
52
|
+
* @alias module:@lumjs/core/types.isArray
|
|
46
53
|
*/
|
|
47
54
|
const isArray = Array.isArray;
|
|
48
55
|
|
|
@@ -50,6 +57,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
50
57
|
* See if a value is a `TypedArray` object.
|
|
51
58
|
* @param {*} v - The value we're testing.
|
|
52
59
|
* @returns {boolean}
|
|
60
|
+
* @alias module:@lumjs/core/types.isTypedArray
|
|
53
61
|
*/
|
|
54
62
|
function isTypedArray(v)
|
|
55
63
|
{
|
|
@@ -62,6 +70,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
62
70
|
* @param {boolean} [typed=false] If `true` we want a `TypedArray`.
|
|
63
71
|
* If `false` (default) we want a regular `Array`.
|
|
64
72
|
* @returns {boolean}
|
|
73
|
+
* @alias module:@lumjs/core/types.nonEmptyArray
|
|
65
74
|
*/
|
|
66
75
|
function nonEmptyArray(v, typed=false)
|
|
67
76
|
{
|
|
@@ -75,6 +84,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
75
84
|
* See if a value is an `arguments` object.
|
|
76
85
|
* @param {*} v - The value we're testing.
|
|
77
86
|
* @returns {boolean}
|
|
87
|
+
* @alias module:@lumjs/core/types.isArguments
|
|
78
88
|
*/
|
|
79
89
|
function isArguments(v)
|
|
80
90
|
{
|
|
@@ -85,6 +95,7 @@ const {O, F, S, SY} = require('./js');
|
|
|
85
95
|
* See if a value is a Property name.
|
|
86
96
|
* @param {*} v - The value we're testing.
|
|
87
97
|
* @returns {boolean}
|
|
98
|
+
* @alias module:@lumjs/core/types.isProperty
|
|
88
99
|
*/
|
|
89
100
|
function isProperty(v)
|
|
90
101
|
{
|
|
@@ -109,6 +120,7 @@ function isProperty(v)
|
|
|
109
120
|
*
|
|
110
121
|
* @param {object} obj - The object we are testing.
|
|
111
122
|
* @returns {boolean} - Is the object a valid descriptor?
|
|
123
|
+
* @alias module:@lumjs/core/types.doesDescriptor
|
|
112
124
|
*/
|
|
113
125
|
function doesDescriptor(obj)
|
|
114
126
|
{
|
package/lib/types/def.js
CHANGED
|
@@ -4,6 +4,9 @@ const {F, B} = require('./js');
|
|
|
4
4
|
const {isObj, isNil, isProperty, doesDescriptor} = require('./basics');
|
|
5
5
|
const copy = require('../obj/copyall');
|
|
6
6
|
|
|
7
|
+
// A really really cheap version of clone().
|
|
8
|
+
const clone = obj => copy({}, obj);
|
|
9
|
+
|
|
7
10
|
/**
|
|
8
11
|
* A wrapper around `Object.defineProperty()`.
|
|
9
12
|
*
|
|
@@ -12,16 +15,22 @@ const copy = require('../obj/copyall');
|
|
|
12
15
|
*
|
|
13
16
|
* @param {(object|function)} obj - The object to add a property to.
|
|
14
17
|
* @param {?(string|boolean|object)} name - If a `string`, the property name.
|
|
18
|
+
* Property names may also be `symbol` values.
|
|
15
19
|
*
|
|
16
20
|
* If this is `null` or `undefined` then the `value` is ignored entirely,
|
|
17
21
|
* and instead a bound version of this function is created with the
|
|
18
|
-
* `obj` already passed as the first parameter
|
|
19
|
-
*
|
|
22
|
+
* `obj` already passed as the first parameter, and any of the options from
|
|
23
|
+
* `opts` added to a new default set of options bound to the function.
|
|
24
|
+
* Can be useful if you need to add a lot of properties to the same object.
|
|
20
25
|
*
|
|
21
26
|
* If this is a `boolean`, then the same logic as if it was `null` or
|
|
22
27
|
* `undefined` will apply, except that an `enumerable` property with this
|
|
23
28
|
* value will also be added to the descriptors.
|
|
24
29
|
*
|
|
30
|
+
* If this is an `object`, we also ignore `value` entirely, as each of
|
|
31
|
+
* the keys of this object will be used as the name of a property,
|
|
32
|
+
* and the value associated with the key will be the value to assign it.
|
|
33
|
+
*
|
|
25
34
|
* @param {*} value - Used to determine the value of the property.
|
|
26
35
|
*
|
|
27
36
|
* If it is an a valid descriptor object (as per `doesDescriptor()`),
|
|
@@ -29,31 +38,29 @@ const copy = require('../obj/copyall');
|
|
|
29
38
|
* property defined, one will be added, and will be set to `true`.
|
|
30
39
|
* This behaviour may be overridden by the `opts` parameter.
|
|
31
40
|
*
|
|
32
|
-
* If this
|
|
33
|
-
*
|
|
41
|
+
* If this and `opts` are both `function` values, then this will
|
|
42
|
+
* be used as a *getter*, and `opts` will be used a *setter*.
|
|
34
43
|
*
|
|
35
44
|
* Any other value passed here will be used as the `value` in a
|
|
36
45
|
* pre-canned descriptor, also with `configurable` set to `true`.
|
|
37
46
|
*
|
|
38
|
-
* @param {
|
|
47
|
+
* @param {*} [opts] - A multi-purpose option.
|
|
39
48
|
*
|
|
40
49
|
* If this is an `object` then it is reserved for named options.
|
|
41
|
-
*
|
|
50
|
+
* The named options `configurable`, `enumerable`, and `writable`
|
|
51
|
+
* can be used to define default values to the descriptor properties
|
|
52
|
+
* of the same name.
|
|
42
53
|
*
|
|
43
54
|
* If `value` and this are both `function` values, then `value` will
|
|
44
55
|
* be used as a *getter* and this will be used as a *setter*.
|
|
45
56
|
*
|
|
46
|
-
* If `value` is a `function` and this is `true`, then `value` will
|
|
47
|
-
* be used as a *getter* and no *setter* will be assigned.
|
|
48
|
-
*
|
|
49
|
-
* If `value` is a `function` and this is `false`, then `value` will
|
|
50
|
-
* be used as a *setter* and no *getter* will be assigned.
|
|
51
|
-
*
|
|
52
57
|
* If `value` is a valid descriptor object, then setting this to `false`
|
|
53
58
|
* will disable the assumption that it is the descriptor to set.
|
|
54
59
|
* Setting this to `true` on will instruct the function to make a clone
|
|
55
60
|
* of the descriptor object before modifying any properties in it.
|
|
56
61
|
*
|
|
62
|
+
* This defaults to `undefined`, except if
|
|
63
|
+
*
|
|
57
64
|
* @returns {*} Normally the `obj` argument with new property added.
|
|
58
65
|
*
|
|
59
66
|
* The exception is if this is a bound copy of the function created
|
|
@@ -66,6 +73,7 @@ const copy = require('../obj/copyall');
|
|
|
66
73
|
* def(myObj)('name', "Bob")('age', 42);
|
|
67
74
|
* ```
|
|
68
75
|
*
|
|
76
|
+
* @alias module:@lumjs/core/types.def
|
|
69
77
|
*/
|
|
70
78
|
function def(obj, name, value, opts)
|
|
71
79
|
{
|
|
@@ -132,17 +140,12 @@ function def(obj, name, value, opts)
|
|
|
132
140
|
|
|
133
141
|
if (opts !== false && doesDescriptor(value))
|
|
134
142
|
{ // The value is a descriptor, let's use it.
|
|
135
|
-
desc = (opts === true) ?
|
|
143
|
+
desc = (opts === true) ? clone(value) : value;
|
|
136
144
|
}
|
|
137
145
|
else if (typeof value === F && typeof opts === F)
|
|
138
146
|
{ // A getter and setter were both specified.
|
|
139
147
|
desc = {get: value, set: opts};
|
|
140
148
|
}
|
|
141
|
-
else if (typeof value === F && typeof opts === B)
|
|
142
|
-
{ // A function value with a boolean opts is a getter or setter.
|
|
143
|
-
const prop = opts ? 'get' : 'set';
|
|
144
|
-
desc = {[prop]: value};
|
|
145
|
-
}
|
|
146
149
|
else
|
|
147
150
|
{ // The value is just a value, so let's assign it.
|
|
148
151
|
desc = {value};
|