@lumjs/core 1.3.1 → 1.4.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.
@@ -6,6 +6,13 @@ See [Changelogs](index.md) for more information on the changelogs.
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.4.0] - 2022-09-26
10
+ ### Changed
11
+ - Moved `lazy` into `types` module by default (leaving an alias in `core`).
12
+ - Completely rewrote the `lazy` function entirely.
13
+ - Greatly extended the documentation for `lazy` function.
14
+ - Removed an unused development artefact from `types.needs`.
15
+
9
16
  ## [1.3.1] - 2022-09-23
10
17
  ### Fixed
11
18
  - The `arrays` module exports `powerset` and `random` as it should.
@@ -74,7 +81,8 @@ See [Changelogs](index.md) for more information on the changelogs.
74
81
  - See [1.0-beta.md](1.0-beta.md) for the beta versions of `1.0`
75
82
  - See [lum.js](https://github.com/supernovus/lum.js) for the original library set this is replacing.
76
83
 
77
- [Unreleased]: https://github.com/supernovus/lum.core.js/compare/v1.3.1...HEAD
84
+ [Unreleased]: https://github.com/supernovus/lum.core.js/compare/v1.4.0...HEAD
85
+ [1.4.0]: https://github.com/supernovus/lum.core.js/compare/v1.3.1...v1.4.0
78
86
  [1.3.1]: https://github.com/supernovus/lum.core.js/compare/v1.3.0...v1.3.1
79
87
  [1.3.0]: https://github.com/supernovus/lum.core.js/compare/v1.2.1...v1.3.0
80
88
  [1.2.1]: https://github.com/supernovus/lum.core.js/compare/v1.2.0...v1.2.1
package/lib/index.js CHANGED
@@ -112,9 +112,6 @@ const Enum = require('./enum');
112
112
  */
113
113
  const observable = require('./observable');
114
114
 
115
- // One function exported directly with no sub-module available.
116
- const lazy = require('./lazy');
117
-
118
115
  /**
119
116
  * Define properties on an object or function.
120
117
  * @alias module:@lumjs/core.def
@@ -123,6 +120,14 @@ const lazy = require('./lazy');
123
120
  */
124
121
  const def = types.def;
125
122
 
123
+ /**
124
+ * Define properties on an object or function.
125
+ * @alias module:@lumjs/core.lazy
126
+ * @function
127
+ * @see module:@lumjs/core/types.lazy
128
+ */
129
+ const lazy = types.lazy;
130
+
126
131
  module.exports =
127
132
  {
128
133
  types, context, flags, obj, opt, modules, arrays, strings,
@@ -39,6 +39,7 @@ const {needObj, needType, needs} = require('./needs');
39
39
  // A few standalone items.
40
40
 
41
41
  const def = require('./def');
42
+ const lazy = require('./lazy');
42
43
  const TYPES = require('./typelist');
43
44
  const stringify = require('./stringify');
44
45
 
@@ -46,7 +47,7 @@ const stringify = require('./stringify');
46
47
  // Further tests can be added by `TYPES.add()` later.
47
48
  module.exports =
48
49
  {
49
- O, F, S, B, N, U, SY, BI, TYPES, root, unbound, def,
50
+ O, F, S, B, N, U, SY, BI, TYPES, root, unbound, def, lazy,
50
51
  isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
51
52
  nonEmptyArray, isArguments, isProperty, doesDescriptor,
52
53
  isInstance, isType, isa, needObj, needType, needs, stringify,
@@ -0,0 +1,196 @@
1
+ const def = require('./def');
2
+ const {S,F} = require('./js');
3
+ const {COMPLEX} = require('./typelist');
4
+ const {needType,needObj} = require('./needs');
5
+
6
+ /**
7
+ * Metadata for the property definition.
8
+ *
9
+ * @typedef module:@lumjs/core/types~LazyDef
10
+ * @property {string} name - The `name` passed to `lazy()`
11
+ * @property {(object|function)} target - The `target` passed to `lazy()`
12
+ * @property {object} opts - The `opts` passed to `lazy()`
13
+ * @property {object} arguments - The full `arguments` passed to `lazy()`
14
+ *
15
+ * @property {boolean} [assign] Override default assignment rules?
16
+ *
17
+ * This property may be added by the `LazyGetter` or `LazySetter`
18
+ * functions to override the default assignment rules.
19
+ *
20
+ * If this is `true` the return value will be assigned, replacing the
21
+ * lazy accessor property, even if the value is `undefined`.
22
+ *
23
+ * If this is `false`, the value will be returned, but will **not** be
24
+ * *assigned*, so the lazy accessor property will remain.
25
+ *
26
+ * @property {*} [def] Special options for `def()`
27
+ *
28
+ * The [def()]{@link module:@lumjs/core/types.def} function has an
29
+ * optional fourth parameter called `opts` which is used for a few
30
+ * specialized purposes. If this property is set, it will be used as
31
+ * the value for `opts` when assigning the return value to the property.
32
+ *
33
+ * Leave it `undefined` to use the default `def()` behaviour.
34
+ *
35
+ */
36
+
37
+ /**
38
+ * A function to generate the property value.
39
+ *
40
+ * @callback module:@lumjs/core/types~LazyGetter
41
+ * @returns {*} The generated *value* of the property
42
+ *
43
+ * By default if this is `undefined` the value will **not** be
44
+ * assigned, and instead the accessor property will remain in
45
+ * place to be used on subsequent calls until a value other than
46
+ * `undefined` is returned.
47
+ *
48
+ * This assignment behaviour can be overridden by the function
49
+ * if it sets `this.assign` to an explicit boolean value.
50
+ *
51
+ * As we are using [def()]{@link module:@lumjs/core/types.def}
52
+ * to assign the value, by default if the return value appears
53
+ * to be a valid *Descriptor* object, it will be used as the
54
+ * property descriptor. See `def()` for further details on how
55
+ * it handles the various arguments.
56
+ *
57
+ * Regardless of the value of `this.assign`, this value will
58
+ * be *returned* as the property value.
59
+ *
60
+ * @this {module:@lumjs/core/types~LazyDef} A metadata object.
61
+ */
62
+
63
+ /**
64
+ * A function to handle attempts at assignment.
65
+ *
66
+ * Very similar to [LazyGetter]{@link module:@lumjs/core/types~LazyGetter}
67
+ * but called when property assignment is attempted on the
68
+ * lazy accessor property.
69
+ *
70
+ * If you explicitly want to forbid assignment, you can throw
71
+ * an `Error` from this function.
72
+ *
73
+ * @callback module:@lumjs/core/types~LazySetter
74
+ * @param {*} value - The value attempting to be assigned.
75
+ * @returns {*} The *actual* assignment value (if any.)
76
+ *
77
+ * The same assignment rules apply as in the
78
+ * [LazyGetter]{@link module:@lumjs/core/types~LazyGetter}
79
+ * callback.
80
+ *
81
+ * @this {module:@lumjs/core/types~LazyDef} A metadata object.
82
+ */
83
+
84
+ /**
85
+ * Build a lazy initializer property.
86
+ *
87
+ * This builds an *accessor* property that will replace itself
88
+ * with a initialized property, the value of which is obtained from
89
+ * a supplied initialization function. Any subsequent use of the
90
+ * property will obviously be using the initialized property directly.
91
+ *
92
+ * The general purpose is if there is a property that requires
93
+ * a fairly intensive load time, or requires a large library that
94
+ * you may not always *need* for basic things, you can use this
95
+ * to ensure that the property is only initialized when needed.
96
+ *
97
+ * This is an extension of the [def()]{@link module:@lumjs/core/types.def}
98
+ * method, and indeed an alias called `def.lazy()` is also available.
99
+ *
100
+ * @param {(object|function)} target - The object to add the property to.
101
+ *
102
+ * As with `def()` itself, this can be either an `object` or `function`,
103
+ * as in Javascript the latter is an extension of the former.
104
+ *
105
+ * @param {string} name - The name of the property to add.
106
+ *
107
+ * @param {module:@lumjs/core/types~LazyGetter} initfunc
108
+ * The function to initialize the property.
109
+ *
110
+ * This will normally only ever be called the first time the property
111
+ * is accessed in a *read* operation. The return value from this
112
+ * will be assigned to the property using `def()`, replacing the
113
+ * lazy accessor property.
114
+ *
115
+ * @param {object} [opts] Options to customize behavior.
116
+ *
117
+ * @param {module:@lumjs/core/types~LazySetter} [opts.set]
118
+ * A function to handle assignment attempts.
119
+ *
120
+ * This obviously only applies to the *lazy accessor* property,
121
+ * and will have no affect once the property has been replaced
122
+ * by its initialized value.
123
+ *
124
+ * @param {boolean} [opts.enumerable=false] Is the property enumerable?
125
+ *
126
+ * This applies to the *lazy accessor* property only.
127
+ * You can set custom descriptor rules in the `initfunc`
128
+ * if you need them on the initialized property.
129
+ *
130
+ * @param {?boolean} [opts.assign] The *default* value for the `assign` property
131
+ * in the [LazyDef]{@link module:@lumjs/core/types~LazyDef} object.
132
+ *
133
+ * @param {*} [opts.def] The *default* value for the `def` property in
134
+ * the [LazyDef]{@link module:@lumjs/core/types~LazyDef} object.
135
+ *
136
+ * @returns {(object|function)} The `target` argument
137
+ *
138
+ * @alias module:@lumjs/core/types.lazy
139
+ */
140
+ function lazy(target, name, initfunc, opts={})
141
+ {
142
+ needType(COMPLEX, target, 'obj must be an object');
143
+ needType(S, name, 'name must be a string');
144
+ needType(F, initfunc, 'initfunc must be a function');
145
+ needObj(opts, 'opts parameter was not an object');
146
+
147
+ // The `this` object for the functions.
148
+ const context =
149
+ {
150
+ name,
151
+ target,
152
+ opts,
153
+ arguments,
154
+ assign: opts.assign,
155
+ def: opts.def,
156
+ }
157
+
158
+ // The descriptor for the lazy accessor.
159
+ const desc =
160
+ {
161
+ configurable: true,
162
+ enumerable: opts.enumerable ?? false,
163
+ };
164
+
165
+ // Replace the property if rules are correct.
166
+ const defval = function(value)
167
+ {
168
+ const assign = (context.assign ?? value !== undefined);
169
+ if (assign)
170
+ { // Replace the lazy accessor with the returned value.
171
+ def(target, name, value, context.def);
172
+ }
173
+ return value;
174
+ }
175
+
176
+ desc.get = function()
177
+ {
178
+ return defval(initfunc.call(context));
179
+ }
180
+
181
+ if (typeof opts.set === F)
182
+ { // We want to assign a lazy setter as well.
183
+ desc.set = function(value)
184
+ {
185
+ defval(opts.set.call(context, value));
186
+ }
187
+ }
188
+
189
+ // Assign the lazy accessor property.
190
+ def(target, name, desc);
191
+ }
192
+
193
+ // Gotta be one of the greatest lines...
194
+ def(def, 'lazy', lazy);
195
+
196
+ module.exports = lazy;
@@ -59,15 +59,6 @@ function needType (type, v, msg, unused)
59
59
  }
60
60
 
61
61
  exports.needType = needType;
62
-
63
- // Options parser for needs();
64
- const NEEDS_PARSER = function(type, v)
65
- { // `this` is the options object itself.
66
- if (typeof type.is === F)
67
- { // We assume `is()` methods are the type check.
68
- if (type.is(v)) return true;
69
- }
70
- }
71
62
 
72
63
  /**
73
64
  * A wrapper around `isa()` that will throw an error on failure.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumjs/core",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "main": "lib/index.js",
5
5
  "exports":
6
6
  {
package/lib/lazy.js DELETED
@@ -1,100 +0,0 @@
1
-
2
- const {S,F,TYPES:{COMPLEX},needType,needObj,def} = require('./types');
3
-
4
- /**
5
- * @callback module:@lumjs/core~InitFunc
6
- * @param {string} name - The `name` parameter passed to `lazy()`
7
- * @this {object} - The `obj` parameter passed to `lazy()`
8
- */
9
-
10
- /**
11
- * Build a lazy initializer property.
12
- *
13
- * Basically the first time the property is accessed it's built.
14
- * Subsequent accesses will use the already built property.
15
- * This is an extension of the {@link def} method, and indeed an
16
- * alias called `def.lazy()` is also available.
17
- *
18
- * @param {Object} obj - The object to add the property to.
19
- * @param {string} name - The name of the property to add.
20
- * @param {module:@lumjs/core~InitFunc} initfunc
21
- * The function to initialize the property.
22
- * @param {*} [onset] How to handle assignment.
23
- *
24
- * If this is `true` then the new value will be assigned directly,
25
- * skipping the initialization process entirely.
26
- *
27
- * If this is `false` then any attempt at assignment will throw
28
- * a `ReferenceError` with a message indicating the property is read-only.
29
- *
30
- * If this is a `function` it will take two arguments, the
31
- * first being the value that is trying to be assigned, and
32
- * the second being the currently assigned value.
33
- * As with any getter or setter, `this` will be the `obj` itself.
34
- * The function must return the value to be assigned.
35
- * If it returns `undefined`, then the value was not valid,
36
- * and will not be assigned.
37
- *
38
- * If this is anything else, assignment will do nothing at all.
39
- *
40
- * @param {Object} [desc={}] Descriptor rules for the property.
41
- * We only support two descriptor rules with this function, and
42
- * their default values are the same as the `def()` function.
43
- * - `configurable` → `true`
44
- * - `enumerable` → `false`
45
- * Any other descriptor properties are invalid here.
46
- *
47
- * @return {Object} The object we defined the property on.
48
- * @alias module:@lumjs/core.lazy
49
- */
50
- function lazy(obj, name, initfunc, onset, desc={})
51
- {
52
- needType(COMPLEX, obj, 'obj must be an object');
53
- needType(S, name, 'name must be a string');
54
- needType(F, initfunc, 'initfunc must be a function');
55
- needObj(desc, 'desc parameter was not an object');
56
-
57
- let value;
58
-
59
- desc.get = function()
60
- {
61
- if (value === undefined)
62
- {
63
- value = initfunc.call(this, name);
64
- }
65
- return value;
66
- }
67
-
68
- if (onset === true)
69
- { // Allow direct assignment.
70
- desc.set = function(newval)
71
- {
72
- value = newval;
73
- }
74
- }
75
- else if (onset === false)
76
- { // Throw an error on assignment.
77
- desc.set = function()
78
- {
79
- throw new ReferenceError("The "+name+" property is read-only");
80
- }
81
- }
82
- else if (typeof onset === F)
83
- { // A proxy method for assignment.
84
- desc.set = function(newval)
85
- {
86
- const setval = onset.call(this, newval, value);
87
- if (setval !== undefined)
88
- {
89
- value = setval;
90
- }
91
- }
92
- }
93
-
94
- def(obj, name, desc);
95
- }
96
-
97
- // Gotta be one of the greatest lines...
98
- def(def, 'lazy', lazy);
99
-
100
- module.exports = lazy;