@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/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
- * TODO: document this further.
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, '_v', true);
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._v;
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, '_v', value);
85
+ def(this, V, value);
37
86
  return this;
38
87
  }
39
88
  });
40
89
  }
41
90
 
42
- def(dp, 'is', va(true));
43
- def(dp, 'not', va(false));
44
- def(dp, 'e', pa('enumerable'));
45
- def(dp, 'c', pa('configurable'));
46
- def(dp, 'w', pa('writable'));
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
- function na(accessor)
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[accessor];
137
+ {
138
+ const dt = new DescriptorTemplate(opts);
139
+ return dt[dp];
56
140
  }
57
141
  });
58
142
  }
59
143
 
60
- const DTA = ['is','not','e','c','w'];
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
- DescriptorTemplate.addTo = function(target)
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
+ */
@@ -1,71 +1,47 @@
1
+ "use strict";
2
+
1
3
  /**
2
- * Fundamental Types sub-module.
4
+ * The complete `types` module.
3
5
  *
4
6
  * As `@lumjs/core` is the foundation for all my JS libraries,
5
- * this sub-module is the foundation for `@lumjs/core`.
6
- * Everything else is built upon this.
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
- // Constants representing core Javascript types.
20
- const {O, F, S, B, N, U, SY, BI} = require('./js');
21
-
22
- // Basic type check functions.
23
- const
24
- {
25
- isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
26
- nonEmptyArray, isArguments, isProperty, doesDescriptor, isIterable,
27
- isConstructor, doesDescriptorTemplate,
28
- } = require('./basics');
29
-
30
- // Root namespace helpers.
31
- const {root, unbound} = require('./root');
32
-
33
- // Advanced type checks.
34
- const
35
- {
36
- isInstance, isType, isa,
37
- isArrayOf, isListOf, isMapOf, isObjOf, OfTest,
38
- } = require('./isa');
39
-
40
- // Error-throwing type checks.
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
- O, F, S, B, N, U, SY, BI, TYPES, root, unbound, def, lazy,
59
- isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
60
- nonEmptyArray, isArguments, isProperty, doesDescriptor,
61
- isInstance, isType, isa, needObj, needType, needs, stringify,
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
- // Fundamental core types.
2
- const O='object', F='function', S='string', B='boolean', N='number',
3
- U='undefined', SY='symbol', BI='bigint';
4
-
5
- /**
6
- * One or two character identifiers for the core JS type names.
7
- *
8
- * These are only the strings returned by the `typeof` operator.
9
- * See the `TYPES` object (defined in `typelist.js`) for a list
10
- * that includes special types and compound pseudo-types, etc.
11
- */
12
- module.exports = {O, F, S, B, N, U, SY, BI};
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} A metadata object.
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)
@@ -1,7 +1,6 @@
1
1
  const {F, S, B} = require('./js');
2
2
  const {isType, isa} = require('./isa');
3
3
  const {isObj, isComplex} = require('./basics');
4
- const console = require('../console');
5
4
 
6
5
  /**
7
6
  * If a value is not an object, throw an error.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumjs/core",
3
- "version": "1.25.2",
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/tests": "^1.8.0"
37
+ "@lumjs/opts": "^1.0.0",
38
+ "@lumjs/tests": "^2.0.0"
35
39
  },
36
40
  "scripts":
37
41
  {