@lumjs/core 1.38.2 → 1.38.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/arrays/index.js +1 -1
- package/lib/arrays/typed.js +139 -0
- package/lib/events/index.js +103 -70
- package/lib/events/observable.js +2 -237
- package/lib/index.js +13 -12
- package/lib/meta.js +2 -2
- package/lib/obj/assignd.js +52 -5
- package/lib/obj/df.js +221 -74
- package/lib/obj/getprotos.js +25 -0
- package/lib/obj/index.js +22 -2
- package/lib/objectid.js +42 -12
- package/lib/observable.js +1 -0
- package/lib/types/basics.js +29 -3
- package/lib/types/def.js +4 -1
- package/lib/types/index.js +10 -2
- package/lib/types/lazy.js +12 -219
- package/lib/types/stringify.js +45 -5
- package/package.json +3 -2
- package/lib/events/event.js +0 -89
- package/lib/events/listener.js +0 -139
- package/lib/events/registry.js +0 -875
package/lib/objectid.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
const {notNil,def,isNil,F,N,S,SY} = require('./types');
|
|
2
|
+
const {notNil,def,isNil,isObj,F,N,S,SY} = require('./types');
|
|
3
3
|
const argOpts = require('./opt/args');
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,12 +7,14 @@ const argOpts = require('./opt/args');
|
|
|
7
7
|
*
|
|
8
8
|
* @param {(object|number)} [opts] Options
|
|
9
9
|
*
|
|
10
|
-
*
|
|
10
|
+
* If this is a number, then it's the `opts.seed` value.
|
|
11
|
+
*
|
|
12
|
+
* @param {number} [opts.seed=0] A base number to use.
|
|
11
13
|
*
|
|
12
14
|
* If this is omitted, or an invalid value (including `0`),
|
|
13
|
-
* the default is to use `Date.now()` as
|
|
15
|
+
* the default is to use `Date.now()` as `opts.seed`.
|
|
14
16
|
*
|
|
15
|
-
* @param {number} [mode=1] Determine how to handle return value
|
|
17
|
+
* @param {number} [opts.mode=1] Determine how to handle return value
|
|
16
18
|
*
|
|
17
19
|
* | Mode | Description |
|
|
18
20
|
* | ------- | --------------------------------------------------------- |
|
|
@@ -21,12 +23,16 @@ const argOpts = require('./opt/args');
|
|
|
21
23
|
* | `2` | `Math.floor()` |
|
|
22
24
|
* | `3` | `Math.ceil()` |
|
|
23
25
|
*
|
|
26
|
+
* @param {number} [mode=1] Positional version of `opts.mode`.
|
|
27
|
+
*
|
|
24
28
|
* @returns {number}
|
|
25
29
|
* @alias module:@lumjs/core.randomNumber
|
|
26
30
|
*/
|
|
27
31
|
function randomNumber(seed=0, mode=1)
|
|
28
32
|
{
|
|
29
|
-
|
|
33
|
+
const opts = argOpts({seed, mode}, 'seed', 0);
|
|
34
|
+
if (typeof opts.seed !== N || opts.seed === 0)
|
|
35
|
+
opts.seed = Date.now();
|
|
30
36
|
|
|
31
37
|
const rand = Math.random() * seed;
|
|
32
38
|
|
|
@@ -56,18 +62,42 @@ class UniqueObjectIds
|
|
|
56
62
|
{
|
|
57
63
|
def(this, '$options', {value: opts});
|
|
58
64
|
|
|
59
|
-
|
|
65
|
+
let radix = null;
|
|
66
|
+
|
|
67
|
+
if (isObj(opts.random))
|
|
68
|
+
{ // Random ids with custom options.
|
|
69
|
+
def(this, '$randOpts', {value: opts.random});
|
|
70
|
+
if (validBase(opts.random.radix))
|
|
71
|
+
{
|
|
72
|
+
radix = opts.random.radix;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (validBase(opts.random))
|
|
60
76
|
{ // Use random ids.
|
|
61
|
-
def(this, '$
|
|
77
|
+
def(this, '$randOpts', {value: {}});
|
|
78
|
+
radix = opts.random;
|
|
62
79
|
}
|
|
63
80
|
else if (validBase(opts.timestamp))
|
|
64
81
|
{ // Use timestamp-based ids.
|
|
65
82
|
def(this, '$timeIds', {value: {}});
|
|
83
|
+
radix = opts.timestamp;
|
|
66
84
|
}
|
|
67
85
|
else
|
|
68
86
|
{ // Use incremental ids.
|
|
69
87
|
def(this, '$incIds', {value: {}});
|
|
70
88
|
}
|
|
89
|
+
|
|
90
|
+
if (!radix)
|
|
91
|
+
{
|
|
92
|
+
radix = validBase(opts.radix) ? opts.radix : 16;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
def(this, '$radix', radix);
|
|
96
|
+
|
|
97
|
+
if (this.$randOpts)
|
|
98
|
+
{
|
|
99
|
+
def(this, '$randIds', {value: {}});
|
|
100
|
+
}
|
|
71
101
|
|
|
72
102
|
const propType = (typeof opts.idProperty);
|
|
73
103
|
const hasProp = (propType === S || propType === SY);
|
|
@@ -104,6 +134,8 @@ class UniqueObjectIds
|
|
|
104
134
|
return obj[idProp];
|
|
105
135
|
}
|
|
106
136
|
|
|
137
|
+
let radix = this.$radix;
|
|
138
|
+
|
|
107
139
|
let cno = this.$options.className ?? {};
|
|
108
140
|
if (typeof cno === F) cno = {setup: cno};
|
|
109
141
|
else if (cno === 'lc') cno = {lowercase: true};
|
|
@@ -139,7 +171,7 @@ class UniqueObjectIds
|
|
|
139
171
|
const ids = this.$incIds;
|
|
140
172
|
if (ids[className])
|
|
141
173
|
{ // An existing value was found.
|
|
142
|
-
idNum = (++ids[className]).toString();
|
|
174
|
+
idNum = (++ids[className]).toString(radix);
|
|
143
175
|
}
|
|
144
176
|
else
|
|
145
177
|
{ // No existing values yet.
|
|
@@ -153,16 +185,14 @@ class UniqueObjectIds
|
|
|
153
185
|
}
|
|
154
186
|
else
|
|
155
187
|
{ // Something other than auto-increment.
|
|
156
|
-
let ids
|
|
188
|
+
let ids;
|
|
157
189
|
if (this.$randIds)
|
|
158
190
|
{ // Using a random id.
|
|
159
191
|
ids = this.$randIds;
|
|
160
|
-
radix = this.$options.random;
|
|
161
192
|
}
|
|
162
193
|
else if (this.$timeIds)
|
|
163
194
|
{ // Using timestamp ids.
|
|
164
195
|
ids = this.$timeIds;
|
|
165
|
-
radix = this.$options.timestamp;
|
|
166
196
|
}
|
|
167
197
|
else
|
|
168
198
|
{ // Something went horribly wrong.
|
|
@@ -170,7 +200,7 @@ class UniqueObjectIds
|
|
|
170
200
|
}
|
|
171
201
|
|
|
172
202
|
const getId = () => (this.$randIds
|
|
173
|
-
? randomNumber()
|
|
203
|
+
? randomNumber(this.$randOpts)
|
|
174
204
|
: Date.now())
|
|
175
205
|
.toString(radix);
|
|
176
206
|
|
package/lib/observable.js
CHANGED
package/lib/types/basics.js
CHANGED
|
@@ -17,6 +17,13 @@
|
|
|
17
17
|
const JS = require('./js');
|
|
18
18
|
const {O, F, S, SY} = JS;
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* The _abstract prototype_ of all typed arrays.
|
|
22
|
+
* This is part of every modern JS runtime, but is not directly accessible.
|
|
23
|
+
* @alias module:@lumjs/core/types/basics.TypedArray
|
|
24
|
+
*/
|
|
25
|
+
const TypedArray = Object.getPrototypeOf(Int8Array);
|
|
26
|
+
|
|
20
27
|
/**
|
|
21
28
|
* See if a value is a non-null `object`.
|
|
22
29
|
* @param {*} v - The value we're testing.
|
|
@@ -78,7 +85,7 @@ const isArray = Array.isArray;
|
|
|
78
85
|
*/
|
|
79
86
|
function isTypedArray(v)
|
|
80
87
|
{
|
|
81
|
-
return (
|
|
88
|
+
return (v instanceof TypedArray);
|
|
82
89
|
}
|
|
83
90
|
|
|
84
91
|
/**
|
|
@@ -147,6 +154,10 @@ function isIterable(v)
|
|
|
147
154
|
* For the purposes of this test, a class constructor is
|
|
148
155
|
* any Function that has a `prototype` Object property.
|
|
149
156
|
*
|
|
157
|
+
* NOTE: almost every function has a prototype property,
|
|
158
|
+
* only Closures and some built-in methods don't.
|
|
159
|
+
* So this is not a foolproof method by any stretch.
|
|
160
|
+
*
|
|
150
161
|
* @param {*} v - Value to test
|
|
151
162
|
* @returns {boolean}
|
|
152
163
|
* @alias module:@lumjs/core/types/basics.isConstructor
|
|
@@ -185,6 +196,20 @@ function isClassObject(v)
|
|
|
185
196
|
return (isObj(v) && typeof v.constructor === F && v.constructor !== Object);
|
|
186
197
|
}
|
|
187
198
|
|
|
199
|
+
/**
|
|
200
|
+
* Is a value an object with a null prototype?
|
|
201
|
+
*
|
|
202
|
+
* An object with a null prototype won't have any of the usual
|
|
203
|
+
* meta-programming methods or properties from `Object.prototype`.
|
|
204
|
+
*
|
|
205
|
+
* @param {*} v - Value to test
|
|
206
|
+
* @returns {boolean}
|
|
207
|
+
*/
|
|
208
|
+
function isDiscreteObject(v)
|
|
209
|
+
{
|
|
210
|
+
return (isObj(v) && Object.getPrototypeOf(v) === null);
|
|
211
|
+
}
|
|
212
|
+
|
|
188
213
|
/**
|
|
189
214
|
* See if an object can be used as a valid *descriptor*.
|
|
190
215
|
*
|
|
@@ -278,8 +303,9 @@ module.exports =
|
|
|
278
303
|
{
|
|
279
304
|
isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
|
|
280
305
|
isIterable, nonEmptyArray, isArguments, isProperty, isConstructor,
|
|
281
|
-
isPlainObject, isClassObject,
|
|
282
|
-
|
|
306
|
+
isPlainObject, isClassObject, isDiscreteObject,
|
|
307
|
+
doesDescriptor, doesDescriptorTemplate,
|
|
308
|
+
JS, TypedArray,
|
|
283
309
|
}
|
|
284
310
|
|
|
285
311
|
JS.addTo(module.exports);
|
package/lib/types/def.js
CHANGED
|
@@ -11,7 +11,7 @@ const clone = (...args) => Object.assign({}, ...args);
|
|
|
11
11
|
* It's gotten almost as convoluted as the old prop() method it replaced
|
|
12
12
|
* from my original Nano.js libraries.
|
|
13
13
|
*
|
|
14
|
-
* I have written new `obj.{df,dfa,
|
|
14
|
+
* I have written new `obj.{df,dfa,dfor}` functions that will replace this
|
|
15
15
|
* function going forward. The related `types.lazy()` function will also
|
|
16
16
|
* be refactored to use the new functions, and moved to the `obj` module.
|
|
17
17
|
*
|
|
@@ -248,3 +248,6 @@ module.exports = def;
|
|
|
248
248
|
const DT = require('./dt');
|
|
249
249
|
DT.addInit(def);
|
|
250
250
|
def.DT = DT;
|
|
251
|
+
|
|
252
|
+
// And a reference to the old lazy
|
|
253
|
+
def(def, 'lazy', {get: () => require('./lazy')});
|
package/lib/types/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @module @lumjs/core/types
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
const def = require('./def')
|
|
12
|
+
const def = require('./def');
|
|
13
13
|
|
|
14
14
|
// Compose in sub-modules.
|
|
15
15
|
Object.assign(exports,
|
|
@@ -19,7 +19,7 @@ Object.assign(exports,
|
|
|
19
19
|
require('./stringify'),
|
|
20
20
|
require('./needs'),
|
|
21
21
|
{ // A few standalone exports.
|
|
22
|
-
def,
|
|
22
|
+
def,
|
|
23
23
|
TYPES: require('./typelist'),
|
|
24
24
|
ownCount: require('./owncount'),
|
|
25
25
|
},
|
|
@@ -42,3 +42,11 @@ def(exports.TYPES, '$module', module);
|
|
|
42
42
|
|
|
43
43
|
// Extend `unbound` with add() and remove() methods.
|
|
44
44
|
require('./unbound/extend')(exports.unbound);
|
|
45
|
+
|
|
46
|
+
// Deprecated alias for `lazy` function.
|
|
47
|
+
wrapDepr(exports, 'lazy',
|
|
48
|
+
{
|
|
49
|
+
dep: 'core.types.lazy',
|
|
50
|
+
rep: 'core.obj.lazy',
|
|
51
|
+
get: () => require('../obj/df').lazy,
|
|
52
|
+
});
|
package/lib/types/lazy.js
CHANGED
|
@@ -1,225 +1,18 @@
|
|
|
1
|
-
const
|
|
2
|
-
const {
|
|
3
|
-
const {COMPLEX} = require('./typelist');
|
|
4
|
-
const {needType,needObj} = require('./needs');
|
|
5
|
-
const {doesDescriptor} = require('./basics');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Metadata for the property definition.
|
|
9
|
-
*
|
|
10
|
-
* @typedef module:@lumjs/core/types~LazyDef
|
|
11
|
-
* @property {string} name - The `name` passed to `lazy()`
|
|
12
|
-
* @property {(object|function)} target - The `target` passed to `lazy()`
|
|
13
|
-
* @property {object} opts - The `opts` passed to `lazy()`
|
|
14
|
-
* @property {object} arguments - The full `arguments` passed to `lazy()`
|
|
15
|
-
*
|
|
16
|
-
* @property {boolean} [assign] Override default assignment rules?
|
|
17
|
-
*
|
|
18
|
-
* This property may be added by the `LazyGetter` or `LazySetter`
|
|
19
|
-
* functions to override the default assignment rules.
|
|
20
|
-
*
|
|
21
|
-
* If this is `true` the return value will be assigned, replacing the
|
|
22
|
-
* lazy accessor property, even if the value is `undefined`.
|
|
23
|
-
*
|
|
24
|
-
* If this is `false`, the value will be returned, but will **not** be
|
|
25
|
-
* *assigned*, so the lazy accessor property will remain.
|
|
26
|
-
*
|
|
27
|
-
* Leave it `undefined` to use the default assignment behaviour.
|
|
28
|
-
*
|
|
29
|
-
* @property {*} [def] Special options for `def()`
|
|
30
|
-
*
|
|
31
|
-
* The [def()]{@link module:@lumjs/core/types.def} function has an
|
|
32
|
-
* optional fourth parameter called `opts` which is used for a few
|
|
33
|
-
* specialized purposes. If this property is set, it will be used as
|
|
34
|
-
* the value for `opts` when assigning the return value to the property.
|
|
35
|
-
*
|
|
36
|
-
* Leave it `undefined` to use the default `def()` behaviour.
|
|
37
|
-
*
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* A function to generate the property value.
|
|
42
|
-
*
|
|
43
|
-
* @callback module:@lumjs/core/types~LazyGetter
|
|
44
|
-
* @param {module:@lumjs/core/types~LazyDef} info - A metadata object
|
|
45
|
-
* @returns {*} The generated *value* of the property
|
|
46
|
-
*
|
|
47
|
-
* By default if this is `undefined` the value will **not** be
|
|
48
|
-
* assigned, and instead the accessor property will remain in
|
|
49
|
-
* place to be used on subsequent calls until a value other than
|
|
50
|
-
* `undefined` is returned.
|
|
51
|
-
*
|
|
52
|
-
* This assignment behaviour can be overridden by the function
|
|
53
|
-
* if it sets `this.assign` to an explicit boolean value.
|
|
54
|
-
*
|
|
55
|
-
* As we are using [def()]{@link module:@lumjs/core/types.def}
|
|
56
|
-
* to assign the value, by default if the return value appears
|
|
57
|
-
* to be a valid *Descriptor* object, it will be used as the
|
|
58
|
-
* property descriptor. See `def()` for further details on how
|
|
59
|
-
* it handles the various arguments.
|
|
60
|
-
*
|
|
61
|
-
* Regardless of the value of `this.assign`, this value will
|
|
62
|
-
* be *returned* as the property value.
|
|
63
|
-
*
|
|
64
|
-
* @this {module:@lumjs/core/types~LazyDef} The `info` metadata object
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* A function to handle attempts at assignment.
|
|
69
|
-
*
|
|
70
|
-
* Very similar to [LazyGetter]{@link module:@lumjs/core/types~LazyGetter}
|
|
71
|
-
* but called when property assignment is attempted on the
|
|
72
|
-
* lazy accessor property.
|
|
73
|
-
*
|
|
74
|
-
* If you explicitly want to forbid assignment, you can throw
|
|
75
|
-
* an `Error` from this function.
|
|
76
|
-
*
|
|
77
|
-
* @callback module:@lumjs/core/types~LazySetter
|
|
78
|
-
* @param {*} value - The value attempting to be assigned.
|
|
79
|
-
* @returns {*} The *actual* assignment value (if any.)
|
|
80
|
-
*
|
|
81
|
-
* The same assignment rules apply as in the
|
|
82
|
-
* [LazyGetter]{@link module:@lumjs/core/types~LazyGetter}
|
|
83
|
-
* callback.
|
|
84
|
-
*
|
|
85
|
-
* @this {module:@lumjs/core/types~LazyDef} A metadata object.
|
|
86
|
-
*/
|
|
1
|
+
const {deprecated} = require('../meta');
|
|
2
|
+
const {lazy} = require('../obj/df');
|
|
87
3
|
|
|
88
4
|
/**
|
|
89
5
|
* Build a lazy initializer property.
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* a supplied initialization function. Any subsequent use of the
|
|
94
|
-
* property will obviously be using the initialized property directly.
|
|
95
|
-
*
|
|
96
|
-
* The general purpose is if there is a property that requires
|
|
97
|
-
* a fairly intensive load time, or requires a large library that
|
|
98
|
-
* you may not always *need* for basic things, you can use this
|
|
99
|
-
* to ensure that the property is only initialized when needed.
|
|
100
|
-
*
|
|
101
|
-
* This is an extension of the [def()]{@link module:@lumjs/core/types.def}
|
|
102
|
-
* method, and indeed an alias called `def.lazy()` is also available.
|
|
103
|
-
*
|
|
104
|
-
* IMPORTANT: In a later v1.18.x release, this will be refactored to use the
|
|
105
|
-
* [obj.df()]{@link module:@lumjs/core/obj.df} function that will replace
|
|
106
|
-
* def(). There's already an alias called `df.lazy()` available now.
|
|
107
|
-
* This will also be moved to the `obj` module, with a deprecated alias left
|
|
108
|
-
* in the `types` module for the duration of the 1.x lifecycle.
|
|
109
|
-
*
|
|
110
|
-
* @param {(object|function)} target - The object to add the property to.
|
|
111
|
-
*
|
|
112
|
-
* As with `def()` itself, this can be either an `object` or `function`,
|
|
113
|
-
* as in Javascript the latter is an extension of the former.
|
|
114
|
-
*
|
|
115
|
-
* @param {string} name - The name of the property to add.
|
|
116
|
-
*
|
|
117
|
-
* @param {module:@lumjs/core/types~LazyGetter} initfunc
|
|
118
|
-
* The function to initialize the property.
|
|
119
|
-
*
|
|
120
|
-
* This will normally only ever be called the first time the property
|
|
121
|
-
* is accessed in a *read* operation. The return value from this
|
|
122
|
-
* will be assigned to the property using `def()`, replacing the
|
|
123
|
-
* lazy accessor property.
|
|
124
|
-
*
|
|
125
|
-
* @param {object} [opts] Options to customize behavior.
|
|
126
|
-
*
|
|
127
|
-
* @param {module:@lumjs/core/types~LazySetter} [opts.set]
|
|
128
|
-
* A function to handle assignment attempts.
|
|
129
|
-
*
|
|
130
|
-
* This obviously only applies to the *lazy accessor* property,
|
|
131
|
-
* and will have no affect once the property has been replaced
|
|
132
|
-
* by its initialized value.
|
|
133
|
-
*
|
|
134
|
-
* @param {boolean} [opts.enumerable=false] Is the property enumerable?
|
|
135
|
-
*
|
|
136
|
-
* This applies to the *lazy accessor* property only.
|
|
137
|
-
* You can set custom descriptor rules in the `initfunc`
|
|
138
|
-
* if you need them on the initialized property.
|
|
139
|
-
*
|
|
140
|
-
* @param {?boolean} [opts.assign] The *default* value for the `assign` property
|
|
141
|
-
* in the [LazyDef]{@link module:@lumjs/core/types~LazyDef} object.
|
|
142
|
-
*
|
|
143
|
-
* @param {*} [opts.def] The *default* value for the `def` property in
|
|
144
|
-
* the [LazyDef]{@link module:@lumjs/core/types~LazyDef} object.
|
|
145
|
-
*
|
|
146
|
-
* @returns {(object|function)} The `target` argument
|
|
147
|
-
*
|
|
148
|
-
* @alias module:@lumjs/core/types.lazy
|
|
6
|
+
* @name module:@lumjs/core/types.lazy
|
|
7
|
+
* @deprecated Moved to `obj` module; this alias will be removed in v2.x
|
|
8
|
+
* @see {@link module:@lumjs/core/obj.lazy} for API docs.
|
|
149
9
|
*/
|
|
150
|
-
function
|
|
10
|
+
module.exports = function (...args)
|
|
151
11
|
{
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const context =
|
|
159
|
-
{
|
|
160
|
-
name,
|
|
161
|
-
target,
|
|
162
|
-
opts,
|
|
163
|
-
arguments,
|
|
164
|
-
assign: opts.assign,
|
|
165
|
-
def: opts.def,
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// The descriptor for the lazy accessor.
|
|
169
|
-
const desc =
|
|
170
|
-
{
|
|
171
|
-
configurable: true,
|
|
172
|
-
enumerable: opts.enumerable ?? false,
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
// Replace the property if rules are correct.
|
|
176
|
-
const defval = function(value)
|
|
177
|
-
{
|
|
178
|
-
const assign = (context.assign ?? value !== undefined);
|
|
179
|
-
if (assign)
|
|
180
|
-
{ // Replace the lazy accessor with the returned value.
|
|
181
|
-
def(target, name, value, context.def);
|
|
182
|
-
// Now return the newly assigned value.
|
|
183
|
-
return target[name];
|
|
184
|
-
}
|
|
185
|
-
else if (doesDescriptor(value))
|
|
186
|
-
{ // A descriptor was returned, extract the real value.
|
|
187
|
-
if (typeof value.get === F)
|
|
188
|
-
{
|
|
189
|
-
return value.get();
|
|
190
|
-
}
|
|
191
|
-
else
|
|
192
|
-
{
|
|
193
|
-
return value.value;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
else
|
|
197
|
-
{ // Just return the original value.
|
|
198
|
-
return value;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
desc.get = function()
|
|
203
|
-
{
|
|
204
|
-
return defval(initfunc.call(context,context));
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (typeof opts.set === F)
|
|
208
|
-
{ // We want to assign a lazy setter as well.
|
|
209
|
-
desc.set = function(value)
|
|
210
|
-
{
|
|
211
|
-
defval(opts.set.call(context, value));
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Assign the lazy accessor property.
|
|
216
|
-
return def(target, name, desc);
|
|
12
|
+
const retval = lazy(...args);
|
|
13
|
+
return deprecated(
|
|
14
|
+
'core.types.def.lazy',
|
|
15
|
+
'core.obj.lazy',
|
|
16
|
+
retval
|
|
17
|
+
);
|
|
217
18
|
}
|
|
218
|
-
|
|
219
|
-
// Gotta be one of the greatest lines...
|
|
220
|
-
def(def, 'lazy', lazy);
|
|
221
|
-
|
|
222
|
-
// And this is a close second...
|
|
223
|
-
def(lazy, 'def', def);
|
|
224
|
-
|
|
225
|
-
module.exports = lazy;
|
package/lib/types/stringify.js
CHANGED
|
@@ -11,6 +11,17 @@ const DEF =
|
|
|
11
11
|
MAXD: 1,
|
|
12
12
|
NEWD: 0,
|
|
13
13
|
}
|
|
14
|
+
const ANY = '{...}';
|
|
15
|
+
|
|
16
|
+
function _opts(opts) {
|
|
17
|
+
return (typeof opts === N
|
|
18
|
+
? { maxDepth: opts }
|
|
19
|
+
: (isObj(opts)
|
|
20
|
+
? opts
|
|
21
|
+
: {}
|
|
22
|
+
)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
14
25
|
|
|
15
26
|
/**
|
|
16
27
|
* Stringify a Javascript value.
|
|
@@ -40,10 +51,7 @@ const DEF =
|
|
|
40
51
|
*/
|
|
41
52
|
function stringify (what, opts={}, addNew=null)
|
|
42
53
|
{
|
|
43
|
-
|
|
44
|
-
{
|
|
45
|
-
opts = {maxDepth: opts}
|
|
46
|
-
}
|
|
54
|
+
opts = _opts(opts);
|
|
47
55
|
|
|
48
56
|
if (typeof addNew === N)
|
|
49
57
|
{
|
|
@@ -95,6 +103,8 @@ class Stringify
|
|
|
95
103
|
*/
|
|
96
104
|
constructor(opts={})
|
|
97
105
|
{
|
|
106
|
+
opts = _opts(opts);
|
|
107
|
+
|
|
98
108
|
this.options = opts;
|
|
99
109
|
this.maxDepth = opts.maxDepth ?? DEF.MAXD;
|
|
100
110
|
this.newDepth = opts.newDepth ?? DEF.NEWD;
|
|
@@ -238,7 +248,37 @@ class Stringify
|
|
|
238
248
|
}
|
|
239
249
|
|
|
240
250
|
// If we reached here, it's another kind of object entirely.
|
|
241
|
-
|
|
251
|
+
if (recurse)
|
|
252
|
+
{
|
|
253
|
+
let cstr = nameFor() + '{';
|
|
254
|
+
let keys = Object.keys(what);
|
|
255
|
+
for (let k=0; k < keys.length; k++)
|
|
256
|
+
{
|
|
257
|
+
let key = keys[k], val = what[key];
|
|
258
|
+
if (k > 0) cstr += ', ';
|
|
259
|
+
cstr += key + ':' + this.stringify(val, curd+1)
|
|
260
|
+
}
|
|
261
|
+
cstr += '}';
|
|
262
|
+
return cstr;
|
|
263
|
+
}
|
|
264
|
+
else
|
|
265
|
+
{
|
|
266
|
+
if (this.options.jsonObjects)
|
|
267
|
+
{
|
|
268
|
+
let json;
|
|
269
|
+
try {
|
|
270
|
+
json = JSON.stringify(what);
|
|
271
|
+
} catch (err) {
|
|
272
|
+
console.error(err);
|
|
273
|
+
json = ANY;
|
|
274
|
+
}
|
|
275
|
+
return nameFor() + json;
|
|
276
|
+
}
|
|
277
|
+
else
|
|
278
|
+
{
|
|
279
|
+
return nameFor() + ANY;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
242
282
|
|
|
243
283
|
} // if isObj
|
|
244
284
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumjs/core",
|
|
3
|
-
"version": "1.38.
|
|
3
|
+
"version": "1.38.4",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"exports":
|
|
6
6
|
{
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
},
|
|
32
32
|
"dependencies":
|
|
33
33
|
{
|
|
34
|
-
"@lumjs/opts": "^1.0.0"
|
|
34
|
+
"@lumjs/opts": "^1.0.0",
|
|
35
|
+
"@lumjs/events-observable": "^1.0.1"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies":
|
|
37
38
|
{
|
package/lib/events/event.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const {SY,F,isObj} = require('../types');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Event object to emit to handler callbacks.
|
|
7
|
-
*
|
|
8
|
-
* @prop {module:@lumjs/core/events.Listener} eventListener
|
|
9
|
-
* The event Listener instance this event was emitted from.
|
|
10
|
-
* @prop {(string|Symbol)} type - The event type that was triggered.
|
|
11
|
-
* @prop {string} name - The event name that was triggered;
|
|
12
|
-
* if `type` is a string this will be the same value,
|
|
13
|
-
* for a Symbol type this will be the `type.description` value.
|
|
14
|
-
* @prop {object} target - Target object for this event.
|
|
15
|
-
* @prop {Array} args - Arguments passed to `emit()`
|
|
16
|
-
* @prop {object} options - Composes options from the
|
|
17
|
-
* Registry and the Listener. Listener options take priority.
|
|
18
|
-
* @prop {?object} data
|
|
19
|
-
* If `args[0]` is any kind of `object` other than another Event
|
|
20
|
-
* instance, it will be used as the `data` property.
|
|
21
|
-
* If `args[0]` is not an `object`, this property will be `null`.
|
|
22
|
-
* @prop {module:@lumjs/core/events.Event} origEvent
|
|
23
|
-
* Unless `this.prevEvent` is set, this should always be
|
|
24
|
-
* a reference to `this` instance itself.
|
|
25
|
-
* @prop {?module:@lumjs/core/events.Event} prevEvent
|
|
26
|
-
* If `args[0]` is another Event instance this property
|
|
27
|
-
* will be set with its value, as well as the following
|
|
28
|
-
* changes to the default behavior:
|
|
29
|
-
*
|
|
30
|
-
* - `this.data` will be set to `prevEvent.data`.
|
|
31
|
-
* - `this.origEvent` will be set to `prevEvent.origEvent`
|
|
32
|
-
*
|
|
33
|
-
* @prop {module:@lumjs/core/events~Status} emitStatus
|
|
34
|
-
*
|
|
35
|
-
* @alias module:@lumjs/core/events.Event
|
|
36
|
-
*/
|
|
37
|
-
class LumEvent
|
|
38
|
-
{
|
|
39
|
-
/**
|
|
40
|
-
* Create a new Event instance; should not be called directly.
|
|
41
|
-
* @protected
|
|
42
|
-
* @param {module:@lumjs/core/events.Listener} listener
|
|
43
|
-
* @param {object} target
|
|
44
|
-
* @param {(string|Symbol)} type
|
|
45
|
-
* @param {Array} args
|
|
46
|
-
* @param {object} status
|
|
47
|
-
*/
|
|
48
|
-
constructor(listener, target, type, args, status)
|
|
49
|
-
{
|
|
50
|
-
const reg = listener.registry;
|
|
51
|
-
this.eventListener = listener;
|
|
52
|
-
this.args = args;
|
|
53
|
-
this.target = target;
|
|
54
|
-
this.type = type;
|
|
55
|
-
this.name = (typeof type === SY) ? type.description : type;
|
|
56
|
-
this.emitStatus = status;
|
|
57
|
-
this.options = Object.assign({},
|
|
58
|
-
reg.options,
|
|
59
|
-
listener.options,
|
|
60
|
-
status.options);
|
|
61
|
-
|
|
62
|
-
this.data = null;
|
|
63
|
-
this.prevEvent = null;
|
|
64
|
-
this.origEvent = this;
|
|
65
|
-
|
|
66
|
-
if (isObj(args[0]))
|
|
67
|
-
{ // The first argument is an object.
|
|
68
|
-
const ao = args[0];
|
|
69
|
-
if (ao instanceof LumEvent)
|
|
70
|
-
{ // A previous event.
|
|
71
|
-
this.prevEvent = ao;
|
|
72
|
-
this.origEvent = ao.origEvent;
|
|
73
|
-
this.data = ao.data;
|
|
74
|
-
}
|
|
75
|
-
else
|
|
76
|
-
{ // Use it as a data object.
|
|
77
|
-
this.data = ao;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (typeof this.options.setupEvent === F)
|
|
82
|
-
{
|
|
83
|
-
this.options.setupEvent.call(listener, this);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
module.exports = LumEvent;
|