@lumjs/core 1.25.2 → 1.30.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 +14 -0
- package/TODO.md +2 -0
- package/lib/console.js +54 -0
- package/lib/enum.js +27 -6
- package/lib/index.js +12 -16
- package/lib/meta.js +68 -88
- package/lib/obj/clone.js +1 -1
- package/lib/obj/copyprops.js +1 -1
- package/lib/obj/merge.js +1 -1
- package/lib/obj/ns.js +8 -3
- package/lib/objectid.js +34 -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 +43 -16
- package/lib/types/def.js +49 -36
- package/lib/types/dt.js +147 -18
- package/lib/types/index.js +35 -59
- package/lib/types/isa.js +4 -0
- package/lib/types/js.js +29 -12
- package/lib/types/lazy.js +3 -2
- package/lib/types/needs.js +0 -1
- package/package.json +7 -3
- package/lib/opt.js +0 -404
package/lib/types/dt.js
CHANGED
|
@@ -1,16 +1,65 @@
|
|
|
1
1
|
const def = require('./def');
|
|
2
|
+
const {F} = require('./js');
|
|
3
|
+
const {isObj,isProperty} = require('./basics');
|
|
4
|
+
|
|
5
|
+
const V = Symbol('@lumjs/core/types~dt:V');
|
|
6
|
+
const A = Object.freeze(
|
|
7
|
+
{
|
|
8
|
+
i: 'is',
|
|
9
|
+
n: 'not',
|
|
10
|
+
e: 'e',
|
|
11
|
+
c: 'c',
|
|
12
|
+
w: 'w',
|
|
13
|
+
v: 'val',
|
|
14
|
+
g: 'getter',
|
|
15
|
+
s: 'setter',
|
|
16
|
+
d: 'desc',
|
|
17
|
+
p: 'dt',
|
|
18
|
+
});
|
|
19
|
+
const P = Object.freeze(
|
|
20
|
+
{
|
|
21
|
+
e: 'enumerable',
|
|
22
|
+
c: 'configurable',
|
|
23
|
+
w: 'writable',
|
|
24
|
+
g: 'get',
|
|
25
|
+
s: 'set',
|
|
26
|
+
v: 'value',
|
|
27
|
+
});
|
|
2
28
|
|
|
3
29
|
/**
|
|
4
30
|
* Build a descriptor template using a simple property syntax.
|
|
5
31
|
*
|
|
6
|
-
*
|
|
32
|
+
* Generally used via magic properties on the `def` function:
|
|
33
|
+
*
|
|
34
|
+
* ```js
|
|
35
|
+
* def(o1, 'p1', val1, def.e); // Add 'enumerable' property.
|
|
36
|
+
* def(o2, 'p2', val2, def.not.c); // Remove 'configurable' status.
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* The supported magic properties are:
|
|
40
|
+
*
|
|
41
|
+
* | Prop | Description |
|
|
42
|
+
* | ----- | --------------------------------------------------------- |
|
|
43
|
+
* | `is` | Make subsequent rule accessors set their value to `true` |
|
|
44
|
+
* | `not` | Make subsequent rule accessors set their value to `false` |
|
|
45
|
+
* | `c` | Accessor that sets `configurable` descriptor rule value |
|
|
46
|
+
* | `e` | Accessor that sets `enumerable` descriptor rule value |
|
|
47
|
+
* | `w` | Accessor that sets `writable` descriptor rule value |
|
|
7
48
|
*
|
|
49
|
+
* Similar magic properties may be added to bound instances of `def`;
|
|
50
|
+
* I'll add further docs for that later. Look at the tests for examples.
|
|
51
|
+
*
|
|
52
|
+
* @class
|
|
8
53
|
* @alias module:@lumjs/core/types~DescriptorTemplate
|
|
54
|
+
*
|
|
55
|
+
* The constructor function is also available via `def.DT`,
|
|
56
|
+
* but using the built-in properties is easier.
|
|
9
57
|
*/
|
|
10
|
-
function DescriptorTemplate()
|
|
58
|
+
function DescriptorTemplate(opts={})
|
|
11
59
|
{
|
|
12
|
-
if (!new.target) return new DescriptorTemplate();
|
|
13
|
-
def(this,
|
|
60
|
+
if (!new.target) return new DescriptorTemplate(opts);
|
|
61
|
+
def(this, V, true);
|
|
62
|
+
def(this, A.d, {value: (isObj(opts.desc) ? opts.desc : this)});
|
|
14
63
|
}
|
|
15
64
|
|
|
16
65
|
const dp = DescriptorTemplate.prototype;
|
|
@@ -21,7 +70,7 @@ function pa(prop)
|
|
|
21
70
|
{
|
|
22
71
|
get()
|
|
23
72
|
{ // Set a regular, enumerable, writable value.
|
|
24
|
-
this[prop] = this
|
|
73
|
+
this[A.d][prop] = this[V];
|
|
25
74
|
return this;
|
|
26
75
|
}
|
|
27
76
|
});
|
|
@@ -33,38 +82,118 @@ function va(value)
|
|
|
33
82
|
{
|
|
34
83
|
get()
|
|
35
84
|
{ // Set a hidden, but configurable value.
|
|
36
|
-
def(this,
|
|
85
|
+
def(this, V, value);
|
|
37
86
|
return this;
|
|
38
87
|
}
|
|
39
88
|
});
|
|
40
89
|
}
|
|
41
90
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
91
|
+
function af(p)
|
|
92
|
+
{
|
|
93
|
+
return function(fn)
|
|
94
|
+
{
|
|
95
|
+
const d = this[A.d];
|
|
96
|
+
delete d[P.v];
|
|
97
|
+
delete d[P.w];
|
|
98
|
+
d[p] = fn;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
47
102
|
|
|
48
|
-
|
|
103
|
+
def(dp)
|
|
104
|
+
(A.i, va(true))
|
|
105
|
+
(A.n, va(false))
|
|
106
|
+
(A.e, pa(P.e))
|
|
107
|
+
(A.c, pa(P.c))
|
|
108
|
+
(A.w, pa(P.w))
|
|
109
|
+
(A.g, af(P.g))
|
|
110
|
+
(A.s, af(P.s))
|
|
111
|
+
(A.v, function(v1, v2)
|
|
112
|
+
{ // A function to set a value with.
|
|
113
|
+
const d = this[A.d];
|
|
114
|
+
if (typeof v2 === F && typeof v1 === F)
|
|
115
|
+
{
|
|
116
|
+
delete d[P.v];
|
|
117
|
+
delete d[P.w];
|
|
118
|
+
d.get = v1;
|
|
119
|
+
d.set = v2;
|
|
120
|
+
}
|
|
121
|
+
else
|
|
122
|
+
{
|
|
123
|
+
delete d.get;
|
|
124
|
+
delete d.set;
|
|
125
|
+
d.value = v1;
|
|
126
|
+
}
|
|
127
|
+
return this;
|
|
128
|
+
})
|
|
129
|
+
; // def(dp)
|
|
130
|
+
|
|
131
|
+
// for addInit()
|
|
132
|
+
function na(dp, opts)
|
|
49
133
|
{
|
|
50
134
|
return(
|
|
51
135
|
{
|
|
52
136
|
get()
|
|
53
|
-
{
|
|
54
|
-
const dt = new DescriptorTemplate();
|
|
55
|
-
return dt[
|
|
137
|
+
{
|
|
138
|
+
const dt = new DescriptorTemplate(opts);
|
|
139
|
+
return dt[dp];
|
|
56
140
|
}
|
|
57
141
|
});
|
|
58
142
|
}
|
|
59
143
|
|
|
60
|
-
|
|
144
|
+
// For addInstance()
|
|
145
|
+
function ni(ip, dp)
|
|
146
|
+
{
|
|
147
|
+
return(
|
|
148
|
+
{
|
|
149
|
+
get()
|
|
150
|
+
{
|
|
151
|
+
this[ip][dp];
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
61
156
|
|
|
62
|
-
|
|
157
|
+
const DTA = [A.i,A.n,A.e,A.c,A.w];
|
|
158
|
+
|
|
159
|
+
DescriptorTemplate.addInit = function(target, opts={})
|
|
63
160
|
{
|
|
64
161
|
for (const a of DTA)
|
|
65
162
|
{
|
|
66
|
-
def(target, a, na(a));
|
|
163
|
+
def(target, a, na(a, opts));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
DescriptorTemplate.addInstance = function(target, opts={})
|
|
168
|
+
{
|
|
169
|
+
const prop = isProperty(opts.prop) ? opts.prop : A.p;
|
|
170
|
+
const dt = new DescriptorTemplate(opts);
|
|
171
|
+
|
|
172
|
+
def(target, prop, {value: dt});
|
|
173
|
+
|
|
174
|
+
for (const a of DTA)
|
|
175
|
+
{
|
|
176
|
+
def(target, a, ni(prop, a));
|
|
67
177
|
}
|
|
178
|
+
|
|
179
|
+
return target;
|
|
68
180
|
}
|
|
69
181
|
|
|
70
182
|
module.exports = DescriptorTemplate;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Set a data value or accessor getter/setter combo.
|
|
186
|
+
*
|
|
187
|
+
* @function module:@lumjs/core/types~DescriptorTemplate#val
|
|
188
|
+
*
|
|
189
|
+
* @param {mixed} v1 - Value to assign, or Getter function
|
|
190
|
+
*
|
|
191
|
+
* This is used as a Getter is if `v2` is also a `function`.
|
|
192
|
+
* In any other case this will assign a data `value` property.
|
|
193
|
+
*
|
|
194
|
+
* @param {function} [v2] - Setter function
|
|
195
|
+
*
|
|
196
|
+
* This is only used if `v1` is also a `function`.
|
|
197
|
+
*
|
|
198
|
+
* @returns {object} `this`
|
|
199
|
+
*/
|
package/lib/types/index.js
CHANGED
|
@@ -1,71 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
4
|
+
* The complete `types` module.
|
|
3
5
|
*
|
|
4
6
|
* As `@lumjs/core` is the foundation for all my JS libraries,
|
|
5
|
-
* this
|
|
6
|
-
*
|
|
7
|
+
* this module is the foundation for `@lumjs/core`.
|
|
8
|
+
*
|
|
9
|
+
* Re-exports everything from the {@link @lumjs/core/types/basics}
|
|
10
|
+
* sub-module with the same names, and adds a whole lot more!
|
|
7
11
|
*
|
|
8
12
|
* @module @lumjs/core/types
|
|
9
|
-
* @property {string} O - "object"
|
|
10
|
-
* @property {string} F - "function"
|
|
11
|
-
* @property {string} S - "string"
|
|
12
|
-
* @property {string} B - "binary"
|
|
13
|
-
* @property {string} N - "number"
|
|
14
|
-
* @property {string} U - "undefined"
|
|
15
|
-
* @property {string} SY - "symbol"
|
|
16
|
-
* @property {string} BI - "bigint"
|
|
17
13
|
*/
|
|
18
14
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const {needObj, needType, needs} = require('./needs');
|
|
42
|
-
|
|
43
|
-
// A few standalone items.
|
|
44
|
-
|
|
45
|
-
const def = require('./def');
|
|
46
|
-
const lazy = require('./lazy');
|
|
47
|
-
const TYPES = require('./typelist');
|
|
48
|
-
const stringify = require('./stringify');
|
|
49
|
-
const ownCount = require('./owncount');
|
|
50
|
-
|
|
51
|
-
// An alias for legacy reasons.
|
|
52
|
-
const console = require('../console');
|
|
53
|
-
|
|
54
|
-
// Okay, add all those to our exports.
|
|
55
|
-
// Further tests can be added by `TYPES.add()` later.
|
|
56
|
-
module.exports =
|
|
15
|
+
const def = require('./def'), lazy = require('./lazy');
|
|
16
|
+
|
|
17
|
+
// Compose in sub-modules.
|
|
18
|
+
Object.assign(exports,
|
|
19
|
+
require('./basics'),
|
|
20
|
+
require('./root'),
|
|
21
|
+
require('./isa'),
|
|
22
|
+
require('./needs'),
|
|
23
|
+
{ // A few standalone exports.
|
|
24
|
+
def, lazy,
|
|
25
|
+
TYPES: require('./typelist'),
|
|
26
|
+
stringify: require('./stringify'),
|
|
27
|
+
ownCount: require('./owncount'),
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// Replace the configurable constant props with readonly ones
|
|
32
|
+
exports.JS.addTo(exports);
|
|
33
|
+
|
|
34
|
+
// Deprecated alias for `console` module, lazy-loaded if requested.
|
|
35
|
+
const {wrapDepr} = require('../meta');
|
|
36
|
+
wrapDepr(exports, 'console',
|
|
57
37
|
{
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
doesDescriptorTemplate, ownCount, isIterable, isConstructor,
|
|
63
|
-
isArrayOf, isListOf, isMapOf, isObjOf, OfTest,
|
|
64
|
-
console,
|
|
65
|
-
}
|
|
38
|
+
dep: 'core.types.console',
|
|
39
|
+
rep: 'core.console',
|
|
40
|
+
get: () => require('../console'),
|
|
41
|
+
});
|
|
66
42
|
|
|
67
43
|
// This will be the module for TYPES.add()
|
|
68
|
-
def(TYPES, '$module', module);
|
|
44
|
+
def(exports.TYPES, '$module', module);
|
|
69
45
|
|
|
70
46
|
// Extend `unbound` with add() and remove() methods.
|
|
71
|
-
require('./unbound/extend')(unbound);
|
|
47
|
+
require('./unbound/extend')(exports.unbound);
|
package/lib/types/isa.js
CHANGED
|
@@ -16,6 +16,7 @@ const def = require('./def');
|
|
|
16
16
|
*/
|
|
17
17
|
function isInstance(v, f, needProto=false)
|
|
18
18
|
{
|
|
19
|
+
deprecated('types.isInstace','instanceof');
|
|
19
20
|
if (!isObj(v)) return false; // Not an object.
|
|
20
21
|
if (needProto && (typeof v.prototype !== O || v.prototype === null))
|
|
21
22
|
{ // Has no prototype.
|
|
@@ -909,3 +910,6 @@ def(isPlainObjectOf, 'rules', function()
|
|
|
909
910
|
});
|
|
910
911
|
|
|
911
912
|
exports.isObjOf = isPlainObjectOf;
|
|
913
|
+
|
|
914
|
+
// Just in case let's load this down here.
|
|
915
|
+
const {deprecated} = require('../meta');
|
package/lib/types/js.js
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// A package-private collection of constants.
|
|
4
|
+
// Exported as a part of `basics` and `index`.
|
|
5
|
+
const JS =
|
|
6
|
+
{
|
|
7
|
+
O: 'object',
|
|
8
|
+
F: 'function',
|
|
9
|
+
S: 'string',
|
|
10
|
+
B: 'boolean',
|
|
11
|
+
N: 'number',
|
|
12
|
+
U: 'undefined',
|
|
13
|
+
SY: 'symbol',
|
|
14
|
+
BI: 'bigint',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function addTo(target)
|
|
18
|
+
{
|
|
19
|
+
for (const key in this)
|
|
20
|
+
{
|
|
21
|
+
const value = this[key];
|
|
22
|
+
const desc = {value, enumerable: true};
|
|
23
|
+
Object.defineProperty(target, key, desc);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Object.defineProperty(JS, 'addTo', {value: addTo});
|
|
28
|
+
|
|
29
|
+
module.exports = Object.freeze(JS);
|
package/lib/types/lazy.js
CHANGED
|
@@ -41,6 +41,7 @@ const {doesDescriptor} = require('./basics');
|
|
|
41
41
|
* A function to generate the property value.
|
|
42
42
|
*
|
|
43
43
|
* @callback module:@lumjs/core/types~LazyGetter
|
|
44
|
+
* @param {module:@lumjs/core/types~LazyDef} info - A metadata object
|
|
44
45
|
* @returns {*} The generated *value* of the property
|
|
45
46
|
*
|
|
46
47
|
* By default if this is `undefined` the value will **not** be
|
|
@@ -60,7 +61,7 @@ const {doesDescriptor} = require('./basics');
|
|
|
60
61
|
* Regardless of the value of `this.assign`, this value will
|
|
61
62
|
* be *returned* as the property value.
|
|
62
63
|
*
|
|
63
|
-
* @this {module:@lumjs/core/types~LazyDef}
|
|
64
|
+
* @this {module:@lumjs/core/types~LazyDef} The `info` metadata object
|
|
64
65
|
*/
|
|
65
66
|
|
|
66
67
|
/**
|
|
@@ -194,7 +195,7 @@ function lazy(target, name, initfunc, opts={})
|
|
|
194
195
|
|
|
195
196
|
desc.get = function()
|
|
196
197
|
{
|
|
197
|
-
return defval(initfunc.call(context));
|
|
198
|
+
return defval(initfunc.call(context,context));
|
|
198
199
|
}
|
|
199
200
|
|
|
200
201
|
if (typeof opts.set === F)
|
package/lib/types/needs.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumjs/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.30.0",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"exports":
|
|
6
6
|
{
|
|
@@ -16,10 +16,13 @@
|
|
|
16
16
|
"./modules": "./lib/modules.js",
|
|
17
17
|
"./obj": "./lib/obj/index.js",
|
|
18
18
|
"./observable": "./lib/observable.js",
|
|
19
|
-
"./opt": "./lib/opt.js",
|
|
19
|
+
"./opt": "./lib/opt/index.js",
|
|
20
|
+
"./opt/args": "./lib/opt/args.js",
|
|
20
21
|
"./strings": "./lib/strings.js",
|
|
21
22
|
"./traits": "./lib/traits.js",
|
|
22
23
|
"./types": "./lib/types/index.js",
|
|
24
|
+
"./types/basics": "./lib/types/basics.js",
|
|
25
|
+
"./types/def": "./lib/types/def.js",
|
|
23
26
|
|
|
24
27
|
"./package.json": "./package.json"
|
|
25
28
|
},
|
|
@@ -31,7 +34,8 @@
|
|
|
31
34
|
},
|
|
32
35
|
"devDependencies":
|
|
33
36
|
{
|
|
34
|
-
"@lumjs/
|
|
37
|
+
"@lumjs/opts": "^1.0.0",
|
|
38
|
+
"@lumjs/tests": "^2.0.0"
|
|
35
39
|
},
|
|
36
40
|
"scripts":
|
|
37
41
|
{
|