@lumjs/core 1.3.1 → 1.5.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,23 @@ See [Changelogs](index.md) for more information on the changelogs.
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.5.0] - 2022-09-27
10
+ ### Changed
11
+ - Updated a few DocBlocks.
12
+ - Added a couple explicitly named options to `def()`
13
+ - Rewrote the default module to use `def()` to define its exported properties.
14
+ - Made `arrays`, `strings`, `flags`, `opt`, `modules`, and `observable` use `lazy()`.
15
+ ### Fixed
16
+ - Fixed `def()` so that the descriptor defaults are applied properly.
17
+ - Fixed `lazy()` so it returns a proper value and not a descriptor on first access.
18
+
19
+ ## [1.4.0] - 2022-09-26
20
+ ### Changed
21
+ - Moved `lazy` into `types` module by default (leaving an alias in `core`).
22
+ - Completely rewrote the `lazy` function entirely.
23
+ - Greatly extended the documentation for `lazy` function.
24
+ - Removed an unused development artefact from `types.needs`.
25
+
9
26
  ## [1.3.1] - 2022-09-23
10
27
  ### Fixed
11
28
  - The `arrays` module exports `powerset` and `random` as it should.
@@ -74,7 +91,9 @@ See [Changelogs](index.md) for more information on the changelogs.
74
91
  - See [1.0-beta.md](1.0-beta.md) for the beta versions of `1.0`
75
92
  - See [lum.js](https://github.com/supernovus/lum.js) for the original library set this is replacing.
76
93
 
77
- [Unreleased]: https://github.com/supernovus/lum.core.js/compare/v1.3.1...HEAD
94
+ [Unreleased]: https://github.com/supernovus/lum.core.js/compare/v1.5.0...HEAD
95
+ [1.5.0]: https://github.com/supernovus/lum.core.js/compare/v1.4.0...v1.5.0
96
+ [1.4.0]: https://github.com/supernovus/lum.core.js/compare/v1.3.1...v1.4.0
78
97
  [1.3.1]: https://github.com/supernovus/lum.core.js/compare/v1.3.0...v1.3.1
79
98
  [1.3.0]: https://github.com/supernovus/lum.core.js/compare/v1.2.1...v1.3.0
80
99
  [1.2.1]: https://github.com/supernovus/lum.core.js/compare/v1.2.0...v1.2.1
package/lib/index.js CHANGED
@@ -4,89 +4,161 @@
4
4
  * This is the foundation upon which all the rest of my JS libraries
5
5
  * are built. It provides fundamental helper functions and classes.
6
6
  *
7
+ * Properties marked as «Lazy» will be loaded on-demand the first time
8
+ * the property is accessed. All other properties are always loaded.
9
+ *
7
10
  * @module @lumjs/core
8
11
  */
9
12
 
10
13
  /**
11
- * Constants and functions for basic type checking.
14
+ * Constants and functions for type checks
15
+ * and other fundamental functions
16
+ *
12
17
  * @alias module:@lumjs/core.types
13
18
  * @see module:@lumjs/core/types
14
19
  */
15
20
  const types = require('./types');
16
21
 
17
22
  /**
18
- * Array utility functions.
19
- * @alias module:@lumjs/core.arrays
20
- * @see module:@lumjs/core/arrays
23
+ * Define properties on an object or function
24
+ * @name module:@lumjs/core.def
25
+ * @function
26
+ * @see module:@lumjs/core/types.def
21
27
  */
22
- const arrays = require('./arrays');
23
28
 
24
29
  /**
25
- * Information about the JS context we're running in.
26
- * @alias module:@lumjs/core.context
27
- * @see module:@lumjs/core/context
30
+ * Define properties on an object or function
31
+ * @name module:@lumjs/core.lazy
32
+ * @function
33
+ * @see module:@lumjs/core/types.lazy
28
34
  */
29
- const context = require('./context');
35
+
36
+ // Get a descriptor for one of our sub-modules.
37
+ function lib(name, def={})
38
+ {
39
+ let value = def.value;
40
+
41
+ if (value === undefined)
42
+ {
43
+ const module = def.module ?? name;
44
+ value = require('./'+module);
45
+
46
+ if (value && def.prop && value[def.prop] !== undefined)
47
+ {
48
+ value = value[def.prop];
49
+ }
50
+ }
51
+
52
+ const desc =
53
+ {
54
+ configurable: false,
55
+ enumerable: true,
56
+ writable: false,
57
+ value,
58
+ }
59
+
60
+ return desc;
61
+ }
62
+
63
+ // Export one of our always loaded properties.
64
+ function has(name, def)
65
+ {
66
+ types.def(exports, name, lib(name, def));
67
+ }
68
+
69
+ // Export one of our lazy loaded properties.
70
+ function can(name, def)
71
+ {
72
+ types.lazy(exports, name, () => lib(name, def), {enumerable: true});
73
+ }
74
+
75
+ // Export a set of always loaded properties from a sub-module
76
+ function from(modname, ...libs)
77
+ {
78
+ for (const lib of libs)
79
+ {
80
+ has(lib, {module: modname, prop: lib});
81
+ }
82
+ }
83
+
84
+ // Our fundamental bits.
85
+ has('types', {value: types});
86
+ has('def', {value: types.def});
87
+ has('lazy', {value: types.lazy});
88
+
89
+ /**
90
+ * Array utility functions «Lazy»
91
+ * @name module:@lumjs/core.arrays
92
+ * @type {module:@lumjs/core/arrays}
93
+ */
94
+ can('arrays');
30
95
 
31
96
  /**
32
- * Functions for working with strings and locales.
33
- * @alias module:@lumjs/core.strings
34
- * @see module:@lumjs/core/strings
97
+ * Information about the JS context we're running in
98
+ * @name module:@lumjs/core.context
99
+ * @type {module:@lumjs/core/context}
35
100
  */
36
- const strings = require('./strings');
101
+ has('context');
37
102
 
38
103
  /**
39
- * Functions for working with binary flags.
40
- * @alias module:@lumjs/core.flags
41
- * @see module:@lumjs/core/flags
104
+ * Functions for working with strings and locales «Lazy»
105
+ * @name module:@lumjs/core.strings
106
+ * @type {module:@lumjs/core/strings}
42
107
  */
43
- const flags = require('./flags');
108
+ can('strings');
44
109
 
45
110
  /**
46
- * Functions for manipulating objects.
47
- * @alis module:@lumjs/core.obj
48
- * @see module:@lumjs/core/obj
111
+ * Functions for working with binary flags «Lazy»
112
+ * @name module:@lumjs/core.flags
113
+ * @type {module:@lumjs/core/flags}
49
114
  */
50
- const obj = require('./obj');
115
+ can('flags');
51
116
 
52
117
  /**
53
- * Functions for getting values and properties with fallback defaults.
54
- * @alias module:@lumjs/core.opt
55
- * @see module:@lumjs/core/opt
118
+ * Functions for manipulating objects
119
+ * @name module:@lumjs/core.obj
120
+ * @type {module:@lumjs/core/obj}
56
121
  */
57
- const opt = require('./opt');
122
+ has('obj');
58
123
 
59
124
  /**
60
- * Meta functions related to JS modules.
125
+ * Functions for getting values and properties with fallback defaults «Lazy»
126
+ * @name module:@lumjs/core.opt
127
+ * @type {module:@lumjs/core/opt}
128
+ */
129
+ can('opt');
130
+
131
+ /**
132
+ * Meta functions related to JS modules «Lazy»
61
133
  * @alias module:@lumjs/core.modules
62
134
  * @see module:@lumjs/core/modules
63
135
  */
64
- const modules = require('./modules');
136
+ can('modules');
65
137
 
66
138
  // ObjectID stuff is imported directly without registering a sub-module.
67
- const {randomNumber, InternalObjectId} = require('./objectid');
139
+ from('objectid', 'randomNumber', 'InternalObjectId');
68
140
 
69
141
  /**
70
- * Get a simplistic debugging stacktrace.
142
+ * Get a simplistic debugging stacktrace
71
143
  * @name module:@lumjs/core.stacktrace
72
144
  * @function
73
145
  * @see module:@lumjs/core/meta.stacktrace
74
146
  */
75
147
 
76
148
  /**
77
- * A simple base class for making *abstract* classes.
149
+ * A simple base class for making *abstract* classes
78
150
  * @name module:@lumjs/core.AbstractClass
79
151
  * @see module:@lumjs/core/meta.AbstractClass
80
152
  */
81
153
 
82
154
  /**
83
- * A *factory* for special types of JS `function` constructors.
155
+ * A *factory* for special types of JS `function` constructors
84
156
  * @name module:@lumjs/core.Functions
85
157
  * @see module:@lumjs/core/meta.Functions
86
158
  */
87
159
 
88
160
  /**
89
- * Throw an `Error` saying that a feature is not yet implemented.
161
+ * Throw an `Error` saying that a feature is not yet implemented
90
162
  * @name module:@lumjs/core.NYI
91
163
  * @function
92
164
  * @see module:@lumjs/core/meta.NYI
@@ -94,38 +166,20 @@ const {randomNumber, InternalObjectId} = require('./objectid');
94
166
 
95
167
  // These are exported directly, but a meta sub-module also exists.
96
168
  // Unlike most sub-modules there is no `meta` property in the main library.
97
- const {stacktrace,AbstractClass,Functions,NYI} = require('./meta');
169
+ from('meta', 'stacktrace', 'AbstractClass', 'Functions', 'NYI');
98
170
 
99
171
  /**
100
- * Create a magic *Enum* object.
101
- * @alias module:@lumjs/core.Enum
172
+ * Create a magic *Enum* object
173
+ * @name module:@lumjs/core.Enum
102
174
  * @function
103
175
  * @see module:@lumjs/core/enum
104
176
  */
105
- const Enum = require('./enum');
177
+ has('Enum', {module: 'enum'});
106
178
 
107
179
  /**
108
- * Make an object support the *Observable* API.
109
- * @alias module:@lumjs/core.observable
180
+ * Make an object support the *Observable* API «Lazy»
181
+ * @name module:@lumjs/core.observable
110
182
  * @function
111
183
  * @see module:@lumjs/core/observable
112
184
  */
113
- const observable = require('./observable');
114
-
115
- // One function exported directly with no sub-module available.
116
- const lazy = require('./lazy');
117
-
118
- /**
119
- * Define properties on an object or function.
120
- * @alias module:@lumjs/core.def
121
- * @function
122
- * @see module:@lumjs/core/types.def
123
- */
124
- const def = types.def;
125
-
126
- module.exports =
127
- {
128
- types, context, flags, obj, opt, modules, arrays, strings,
129
- def, randomNumber, InternalObjectId, Enum, lazy, observable,
130
- stacktrace, AbstractClass, Functions, NYI,
131
- }
185
+ can('observable');
package/lib/types/def.js CHANGED
@@ -14,60 +14,83 @@ const clone = obj => copy({}, obj);
14
14
  * It replaces the `prop()` method from the old Lum.js v4.
15
15
  *
16
16
  * @param {(object|function)} obj - The object to add a property to.
17
- * @param {?(string|boolean|object)} name - If a `string`, the property name.
18
- * Property names may also be `symbol` values.
17
+ * @param {?(string|symbol|boolean|object)} name
18
+ * If a `string` or `symbol`, it's the property name.
19
19
  *
20
- * If this is `null` or `undefined` then the `value` is ignored entirely,
21
- * and instead a bound version of this function is created with the
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
+ * If this is `null` or `undefined` then the `value` is ignored entirely,
21
+ * and instead a bound version of this function is created with the
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.
25
25
  *
26
- * If this is a `boolean`, then the same logic as if it was `null` or
27
- * `undefined` will apply, except that an `enumerable` property with this
28
- * value will also be added to the descriptors.
26
+ * If this is a `boolean`, then the same logic as if it was `null` or
27
+ * `undefined` will apply, except that an `enumerable` property with this
28
+ * value will also be added to the descriptors.
29
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.
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
33
  *
34
34
  * @param {*} value - Used to determine the value of the property.
35
35
  *
36
- * If it is an a valid descriptor object (as per `doesDescriptor()`),
37
- * it will be used as the descriptor. If it has no `configurable`
38
- * property defined, one will be added, and will be set to `true`.
39
- * This behaviour may be overridden by the `opts` parameter.
36
+ * If it is an a valid descriptor object (as per `doesDescriptor()`),
37
+ * it will be used as the descriptor. If it has no `configurable`
38
+ * property defined, one will be added, and will be set to `true`.
39
+ * This behaviour may be overridden by the `opts` parameter.
40
40
  *
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*.
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*.
43
43
  *
44
- * Any other value passed here will be used as the `value` in a
45
- * pre-canned descriptor, also with `configurable` set to `true`.
44
+ * Any other value passed here will be used as the `value` in a
45
+ * pre-canned descriptor, also with `configurable` set to `true`.
46
46
  *
47
47
  * @param {*} [opts] - A multi-purpose option.
48
48
  *
49
- * If this is an `object` then it is reserved for named options.
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.
49
+ * If this is an `object` then it is reserved for named options.
53
50
  *
54
- * If `value` and this are both `function` values, then `value` will
55
- * be used as a *getter* and this will be used as a *setter*.
51
+ * The named options `configurable`, `enumerable`, and `writable`
52
+ * can be used to define default values to the descriptor properties
53
+ * of the same name.
56
54
  *
57
- * If `value` is a valid descriptor object, then setting this to `false`
58
- * will disable the assumption that it is the descriptor to set.
59
- * Setting this to `true` on will instruct the function to make a clone
60
- * of the descriptor object before modifying any properties in it.
55
+ * If `value` and this are both `function` values, then `value` will
56
+ * be used as a *getter* and this will be used as a *setter*.
61
57
  *
62
- * This defaults to `undefined`, except if
58
+ * If this is `true`, it sets `opts.cloneDesc` to `true`.
59
+ *
60
+ * If this is `false`, it sets `opts.autoDesc` to `false`.
61
+ *
62
+ * This defaults to `undefined`, except in bound functions.
63
+ *
64
+ * @param {boolean} [opts.autoDesc=true] Automatically detect descriptors?
65
+ *
66
+ * If `true` (the default value) then the automatic descriptor detection
67
+ * is used any time the `value` is an `object`.
68
+ *
69
+ * If `false` the descriptor detection is disabled, and we'll always
70
+ * wrap all values in a new internal descriptor.
71
+ *
72
+ * @param {boolean} [opts.cloneDesc=false] Clone descriptors?
73
+ *
74
+ * If `true` then we'll clone detected descriptors before making
75
+ * any changes to them.
76
+ *
77
+ * If `false` (the default value), we'll use passed descriptors
78
+ * directly, so any changes will be made on the original.
79
+ *
80
+ * @param {boolean} [opts.configurable=true] Default `configurable` value
81
+ * @param {boolean} [opts.enumerable=false] Default `enumerable` value
82
+ * @param {boolean} [opts.writable=false] Default `writable` value
83
+ *
84
+ * Only used with *data descriptors* and will be ignored with
85
+ * *accessor* descriptors.
63
86
  *
64
87
  * @returns {*} Normally the `obj` argument with new property added.
65
88
  *
66
- * The exception is if this is a bound copy of the function created
67
- * using the syntax described in the `name` parameter documentation.
68
- * In that case the return value is the bound copy itself.
69
- * While that might seem strange, it allows for chaining in a way
70
- * that otherwise would not be possible, take this example:
89
+ * The exception is if this is a bound copy of the function created
90
+ * using the syntax described in the `name` parameter documentation.
91
+ * In that case the return value is the bound copy itself.
92
+ * While that might seem strange, it allows for chaining in a way
93
+ * that otherwise would not be possible, take this example:
71
94
  *
72
95
  * ```
73
96
  * def(myObj)('name', "Bob")('age', 42);
@@ -136,11 +159,31 @@ function def(obj, name, value, opts)
136
159
  throw new TypeError("Property name must be a string or a Symbol");
137
160
  }
138
161
 
162
+ function getOpt(name, defVal)
163
+ {
164
+ const revVal = !defVal;
165
+ if (isObj(opts) && typeof opts[name] === B)
166
+ { // Found the option.
167
+ return opts[name];
168
+ }
169
+ else if (opts === revVal)
170
+ { // A special default rule was found.
171
+ return revVal;
172
+ }
173
+ else
174
+ { // Return the default.
175
+ return defVal;
176
+ }
177
+ }
178
+
179
+ const autoDesc = getOpt('autoDesc', true);
180
+ const cloneDesc = getOpt('cloneDesc', false);
181
+
139
182
  let desc;
140
183
 
141
- if (opts !== false && doesDescriptor(value))
184
+ if (autoDesc && doesDescriptor(value))
142
185
  { // The value is a descriptor, let's use it.
143
- desc = (opts === true) ? clone(value) : value;
186
+ desc = cloneDesc ? clone(value) : value;
144
187
  }
145
188
  else if (typeof value === F && typeof opts === F)
146
189
  { // A getter and setter were both specified.
@@ -151,27 +194,28 @@ function def(obj, name, value, opts)
151
194
  desc = {value};
152
195
  }
153
196
 
154
- if (isObj(opts))
155
- { // If opts is an object, let's look for some supported defaults.
156
- const cd = (opt, defval) =>
157
- {
158
- if (typeof opts[opt] === B && typeof desc[opt] !== B)
159
- { // Default descriptor option specified in `opts`
160
- desc[opt] = opts[opt];
161
- }
162
- else if (typeof defval === B)
163
- { // A fallback default.
164
- desc[opt] = defval;
165
- }
197
+ const cd = (opt, defval) =>
198
+ {
199
+ if (typeof desc[opt] === B)
200
+ { // Property was set explicitly.
201
+ return;
166
202
  }
167
-
168
- cd('configurable', true);
169
- cd('enumerable');
170
- if (desc.get === undefined && desc.set === undefined)
171
- { // Only look for this one on data descriptors.
172
- cd('writable');
203
+ else if (isObj(opts) && typeof opts[opt] === B)
204
+ { // Default descriptor option specified in `opts`
205
+ desc[opt] = opts[opt];
206
+ }
207
+ else if (typeof defval === B)
208
+ { // A fallback default.
209
+ desc[opt] = defval;
173
210
  }
174
- } // if isObj(opts)
211
+ }
212
+
213
+ cd('configurable', true);
214
+ cd('enumerable');
215
+ if (desc.get === undefined && desc.set === undefined)
216
+ { // Only look for this one on data descriptors.
217
+ cd('writable');
218
+ }
175
219
 
176
220
  // Now after all that, let's actually define the property!
177
221
  Object.defineProperty(obj, name, desc);
@@ -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,213 @@
1
+ const def = require('./def');
2
+ const {S,F} = require('./js');
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
+ * @property {*} [def] Special options for `def()`
28
+ *
29
+ * The [def()]{@link module:@lumjs/core/types.def} function has an
30
+ * optional fourth parameter called `opts` which is used for a few
31
+ * specialized purposes. If this property is set, it will be used as
32
+ * the value for `opts` when assigning the return value to the property.
33
+ *
34
+ * Leave it `undefined` to use the default `def()` behaviour.
35
+ *
36
+ */
37
+
38
+ /**
39
+ * A function to generate the property value.
40
+ *
41
+ * @callback module:@lumjs/core/types~LazyGetter
42
+ * @returns {*} The generated *value* of the property
43
+ *
44
+ * By default if this is `undefined` the value will **not** be
45
+ * assigned, and instead the accessor property will remain in
46
+ * place to be used on subsequent calls until a value other than
47
+ * `undefined` is returned.
48
+ *
49
+ * This assignment behaviour can be overridden by the function
50
+ * if it sets `this.assign` to an explicit boolean value.
51
+ *
52
+ * As we are using [def()]{@link module:@lumjs/core/types.def}
53
+ * to assign the value, by default if the return value appears
54
+ * to be a valid *Descriptor* object, it will be used as the
55
+ * property descriptor. See `def()` for further details on how
56
+ * it handles the various arguments.
57
+ *
58
+ * Regardless of the value of `this.assign`, this value will
59
+ * be *returned* as the property value.
60
+ *
61
+ * @this {module:@lumjs/core/types~LazyDef} A metadata object.
62
+ */
63
+
64
+ /**
65
+ * A function to handle attempts at assignment.
66
+ *
67
+ * Very similar to [LazyGetter]{@link module:@lumjs/core/types~LazyGetter}
68
+ * but called when property assignment is attempted on the
69
+ * lazy accessor property.
70
+ *
71
+ * If you explicitly want to forbid assignment, you can throw
72
+ * an `Error` from this function.
73
+ *
74
+ * @callback module:@lumjs/core/types~LazySetter
75
+ * @param {*} value - The value attempting to be assigned.
76
+ * @returns {*} The *actual* assignment value (if any.)
77
+ *
78
+ * The same assignment rules apply as in the
79
+ * [LazyGetter]{@link module:@lumjs/core/types~LazyGetter}
80
+ * callback.
81
+ *
82
+ * @this {module:@lumjs/core/types~LazyDef} A metadata object.
83
+ */
84
+
85
+ /**
86
+ * Build a lazy initializer property.
87
+ *
88
+ * This builds an *accessor* property that will replace itself
89
+ * with a initialized property, the value of which is obtained from
90
+ * a supplied initialization function. Any subsequent use of the
91
+ * property will obviously be using the initialized property directly.
92
+ *
93
+ * The general purpose is if there is a property that requires
94
+ * a fairly intensive load time, or requires a large library that
95
+ * you may not always *need* for basic things, you can use this
96
+ * to ensure that the property is only initialized when needed.
97
+ *
98
+ * This is an extension of the [def()]{@link module:@lumjs/core/types.def}
99
+ * method, and indeed an alias called `def.lazy()` is also available.
100
+ *
101
+ * @param {(object|function)} target - The object to add the property to.
102
+ *
103
+ * As with `def()` itself, this can be either an `object` or `function`,
104
+ * as in Javascript the latter is an extension of the former.
105
+ *
106
+ * @param {string} name - The name of the property to add.
107
+ *
108
+ * @param {module:@lumjs/core/types~LazyGetter} initfunc
109
+ * The function to initialize the property.
110
+ *
111
+ * This will normally only ever be called the first time the property
112
+ * is accessed in a *read* operation. The return value from this
113
+ * will be assigned to the property using `def()`, replacing the
114
+ * lazy accessor property.
115
+ *
116
+ * @param {object} [opts] Options to customize behavior.
117
+ *
118
+ * @param {module:@lumjs/core/types~LazySetter} [opts.set]
119
+ * A function to handle assignment attempts.
120
+ *
121
+ * This obviously only applies to the *lazy accessor* property,
122
+ * and will have no affect once the property has been replaced
123
+ * by its initialized value.
124
+ *
125
+ * @param {boolean} [opts.enumerable=false] Is the property enumerable?
126
+ *
127
+ * This applies to the *lazy accessor* property only.
128
+ * You can set custom descriptor rules in the `initfunc`
129
+ * if you need them on the initialized property.
130
+ *
131
+ * @param {?boolean} [opts.assign] The *default* value for the `assign` property
132
+ * in the [LazyDef]{@link module:@lumjs/core/types~LazyDef} object.
133
+ *
134
+ * @param {*} [opts.def] The *default* value for the `def` property in
135
+ * the [LazyDef]{@link module:@lumjs/core/types~LazyDef} object.
136
+ *
137
+ * @returns {(object|function)} The `target` argument
138
+ *
139
+ * @alias module:@lumjs/core/types.lazy
140
+ */
141
+ function lazy(target, name, initfunc, opts={})
142
+ {
143
+ needType(COMPLEX, target, 'obj must be an object');
144
+ needType(S, name, 'name must be a string');
145
+ needType(F, initfunc, 'initfunc must be a function');
146
+ needObj(opts, 'opts parameter was not an object');
147
+
148
+ // The `this` object for the functions.
149
+ const context =
150
+ {
151
+ name,
152
+ target,
153
+ opts,
154
+ arguments,
155
+ assign: opts.assign,
156
+ def: opts.def,
157
+ }
158
+
159
+ // The descriptor for the lazy accessor.
160
+ const desc =
161
+ {
162
+ configurable: true,
163
+ enumerable: opts.enumerable ?? false,
164
+ };
165
+
166
+ // Replace the property if rules are correct.
167
+ const defval = function(value)
168
+ {
169
+ const assign = (context.assign ?? value !== undefined);
170
+ if (assign)
171
+ { // Replace the lazy accessor with the returned value.
172
+ def(target, name, value, context.def);
173
+ // Now return the newly assigned value.
174
+ return target[name];
175
+ }
176
+ else if (doesDescriptor(value))
177
+ { // A descriptor was returned, extract the real value.
178
+ if (typeof value.get === F)
179
+ {
180
+ return value.get();
181
+ }
182
+ else
183
+ {
184
+ return value.value;
185
+ }
186
+ }
187
+ else
188
+ { // Just return the original value.
189
+ return value;
190
+ }
191
+ }
192
+
193
+ desc.get = function()
194
+ {
195
+ return defval(initfunc.call(context));
196
+ }
197
+
198
+ if (typeof opts.set === F)
199
+ { // We want to assign a lazy setter as well.
200
+ desc.set = function(value)
201
+ {
202
+ defval(opts.set.call(context, value));
203
+ }
204
+ }
205
+
206
+ // Assign the lazy accessor property.
207
+ def(target, name, desc);
208
+ }
209
+
210
+ // Gotta be one of the greatest lines...
211
+ def(def, 'lazy', lazy);
212
+
213
+ 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.5.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;