@lumjs/core 1.7.0 → 1.8.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/index.js +29 -66
- package/lib/types/def.js +5 -1
- package/lib/types/dt.js +70 -0
- package/lib/types/lazy.js +5 -0
- package/package.json +3 -5
- package/lib/modulebuilder.js +0 -367
package/lib/index.js
CHANGED
|
@@ -19,117 +19,80 @@
|
|
|
19
19
|
*/
|
|
20
20
|
const types = require('./types');
|
|
21
21
|
|
|
22
|
-
/**
|
|
23
|
-
* A helper for building modules
|
|
24
|
-
*
|
|
25
|
-
* @alias module:@lumjs/core.ModuleBuilder
|
|
26
|
-
* @see module:@lumjs/core/modulebuilder
|
|
27
|
-
*/
|
|
28
|
-
const Builder = require('./modulebuilder');
|
|
29
|
-
|
|
30
|
-
// And we'll use the builder to define the rest of this.
|
|
31
|
-
const {has,can,from} = Builder.build(module);
|
|
32
|
-
|
|
33
22
|
/**
|
|
34
23
|
* Define properties on an object or function
|
|
35
24
|
* @name module:@lumjs/core.def
|
|
36
|
-
* @function
|
|
37
25
|
* @see module:@lumjs/core/types.def
|
|
38
26
|
*/
|
|
27
|
+
const def = types.def;
|
|
39
28
|
|
|
40
29
|
/**
|
|
41
|
-
* Define properties on an object or function
|
|
42
|
-
* @
|
|
43
|
-
* @function
|
|
30
|
+
* Define *lazy* properties on an object or function
|
|
31
|
+
* @alias module:@lumjs/core.lazy
|
|
44
32
|
* @see module:@lumjs/core/types.lazy
|
|
45
33
|
*/
|
|
34
|
+
const lazy = types.lazy;
|
|
46
35
|
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
has('def', {value: types.def});
|
|
50
|
-
has('lazy', {value: types.lazy});
|
|
51
|
-
|
|
52
|
-
// The module builder itself.
|
|
53
|
-
has('ModuleBuilder', {value: Builder});
|
|
36
|
+
// A descriptor template for def/lazy.
|
|
37
|
+
const E = def.e;
|
|
54
38
|
|
|
55
|
-
|
|
56
|
-
* Return a set of functions for building a module.
|
|
57
|
-
*
|
|
58
|
-
* @name module:@lumjs/core.buildModule
|
|
59
|
-
* @function
|
|
60
|
-
* @see module:@lumjs/core/modulebuilder.build
|
|
61
|
-
*/
|
|
62
|
-
has('buildModule',
|
|
63
|
-
{
|
|
64
|
-
value: function(m,o)
|
|
65
|
-
{
|
|
66
|
-
return Builder.build(m,o);
|
|
67
|
-
},
|
|
68
|
-
});
|
|
39
|
+
console.debug('E', E);
|
|
69
40
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
* @name module:@lumjs/core.newBuilder
|
|
74
|
-
* @function
|
|
75
|
-
* @see module:@lumjs/core/modulebuilder.new
|
|
76
|
-
*/
|
|
77
|
-
has('newBuilder',
|
|
78
|
-
{
|
|
79
|
-
value: function(m, o)
|
|
80
|
-
{
|
|
81
|
-
return Builder.new(m,o);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
41
|
+
def(exports, 'types', types, E);
|
|
42
|
+
def(exports, 'def', def, E);
|
|
43
|
+
def(exports, 'lazy', lazy, E);
|
|
84
44
|
|
|
85
45
|
/**
|
|
86
46
|
* Array utility functions «Lazy»
|
|
87
47
|
* @name module:@lumjs/core.arrays
|
|
88
48
|
* @type {module:@lumjs/core/arrays}
|
|
89
49
|
*/
|
|
90
|
-
|
|
50
|
+
lazy(exports, 'arrays', () => require('./arrays'), E);
|
|
91
51
|
|
|
92
52
|
/**
|
|
93
53
|
* Information about the JS context we're running in
|
|
94
54
|
* @name module:@lumjs/core.context
|
|
95
55
|
* @type {module:@lumjs/core/context}
|
|
96
56
|
*/
|
|
97
|
-
|
|
57
|
+
def(exports, 'context', require('./context'), E);
|
|
98
58
|
|
|
99
59
|
/**
|
|
100
60
|
* Functions for working with strings and locales «Lazy»
|
|
101
61
|
* @name module:@lumjs/core.strings
|
|
102
62
|
* @type {module:@lumjs/core/strings}
|
|
103
63
|
*/
|
|
104
|
-
|
|
64
|
+
lazy(exports, 'strings', () => require('./strings'), E);
|
|
105
65
|
|
|
106
66
|
/**
|
|
107
67
|
* Functions for working with binary flags «Lazy»
|
|
108
68
|
* @name module:@lumjs/core.flags
|
|
109
69
|
* @type {module:@lumjs/core/flags}
|
|
110
70
|
*/
|
|
111
|
-
|
|
71
|
+
lazy(exports, 'flags', () => require('./flags'), E);
|
|
112
72
|
|
|
113
73
|
/**
|
|
114
|
-
* Functions for manipulating objects
|
|
74
|
+
* Functions for manipulating objects «Lazy»
|
|
115
75
|
* @name module:@lumjs/core.obj
|
|
116
76
|
* @type {module:@lumjs/core/obj}
|
|
117
77
|
*/
|
|
118
|
-
|
|
78
|
+
lazy(exports, 'obj', () => require('./obj'), E);
|
|
119
79
|
|
|
120
80
|
/**
|
|
121
81
|
* Functions for getting values and properties with fallback defaults «Lazy»
|
|
122
82
|
* @name module:@lumjs/core.opt
|
|
123
83
|
* @type {module:@lumjs/core/opt}
|
|
124
84
|
*/
|
|
125
|
-
|
|
85
|
+
lazy(exports, 'opt', () => require('./opt'), E);
|
|
126
86
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
87
|
+
// Get a bunch of properties from a submodule.
|
|
88
|
+
function from(modname, ...libs)
|
|
89
|
+
{
|
|
90
|
+
const submod = require('./'+modname);
|
|
91
|
+
for (const lib of libs)
|
|
92
|
+
{
|
|
93
|
+
def(exports, lib, submod[lib], E);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
133
96
|
|
|
134
97
|
// ObjectID stuff is imported directly without registering a sub-module.
|
|
135
98
|
from('objectid', 'randomNumber', 'InternalObjectId');
|
|
@@ -165,12 +128,12 @@ from('objectid', 'randomNumber', 'InternalObjectId');
|
|
|
165
128
|
from('meta', 'stacktrace', 'AbstractClass', 'Functions', 'NYI');
|
|
166
129
|
|
|
167
130
|
/**
|
|
168
|
-
* Create a magic *Enum* object
|
|
131
|
+
* Create a magic *Enum* object «Lazy»
|
|
169
132
|
* @name module:@lumjs/core.Enum
|
|
170
133
|
* @function
|
|
171
134
|
* @see module:@lumjs/core/enum
|
|
172
135
|
*/
|
|
173
|
-
|
|
136
|
+
lazy(exports, 'Enum', () => require('./enum'), E);
|
|
174
137
|
|
|
175
138
|
/**
|
|
176
139
|
* Make an object support the *Observable* API «Lazy»
|
|
@@ -178,4 +141,4 @@ can('Enum', {module: 'enum'});
|
|
|
178
141
|
* @function
|
|
179
142
|
* @see module:@lumjs/core/observable
|
|
180
143
|
*/
|
|
181
|
-
|
|
144
|
+
lazy(exports, 'observable', () => require('./observable'), E);
|
package/lib/types/def.js
CHANGED
package/lib/types/dt.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const def = require('./def');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Build a descriptor template using a simple property syntax.
|
|
5
|
+
*
|
|
6
|
+
* TODO: document this further.
|
|
7
|
+
*
|
|
8
|
+
* @alias module:@lumjs/core/types~DescriptorTemplate
|
|
9
|
+
*/
|
|
10
|
+
function DescriptorTemplate()
|
|
11
|
+
{
|
|
12
|
+
if (!new.target) return new DescriptorTemplate();
|
|
13
|
+
def(this, '_v', true);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const dp = DescriptorTemplate.prototype;
|
|
17
|
+
|
|
18
|
+
function pa(prop)
|
|
19
|
+
{
|
|
20
|
+
return(
|
|
21
|
+
{
|
|
22
|
+
get()
|
|
23
|
+
{ // Set a regular, enumerable, writable value.
|
|
24
|
+
this[prop] = this._v;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function va(value)
|
|
31
|
+
{
|
|
32
|
+
return(
|
|
33
|
+
{
|
|
34
|
+
get()
|
|
35
|
+
{ // Set a hidden, but configurable value.
|
|
36
|
+
def(this, '_v', value);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
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'));
|
|
47
|
+
|
|
48
|
+
function na(accessor)
|
|
49
|
+
{
|
|
50
|
+
return(
|
|
51
|
+
{
|
|
52
|
+
get()
|
|
53
|
+
{
|
|
54
|
+
const dt = new DescriptorTemplate();
|
|
55
|
+
return dt[accessor];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const DTA = ['is','not','e','c','w'];
|
|
61
|
+
|
|
62
|
+
DescriptorTemplate.addTo = function(target)
|
|
63
|
+
{
|
|
64
|
+
for (const a of DTA)
|
|
65
|
+
{
|
|
66
|
+
def(target, a, na(a));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = DescriptorTemplate;
|
package/lib/types/lazy.js
CHANGED
|
@@ -24,6 +24,8 @@ const {doesDescriptor} = require('./basics');
|
|
|
24
24
|
* If this is `false`, the value will be returned, but will **not** be
|
|
25
25
|
* *assigned*, so the lazy accessor property will remain.
|
|
26
26
|
*
|
|
27
|
+
* Leave it `undefined` to use the default assignment behaviour.
|
|
28
|
+
*
|
|
27
29
|
* @property {*} [def] Special options for `def()`
|
|
28
30
|
*
|
|
29
31
|
* The [def()]{@link module:@lumjs/core/types.def} function has an
|
|
@@ -210,4 +212,7 @@ function lazy(target, name, initfunc, opts={})
|
|
|
210
212
|
// Gotta be one of the greatest lines...
|
|
211
213
|
def(def, 'lazy', lazy);
|
|
212
214
|
|
|
215
|
+
// And this is a close second...
|
|
216
|
+
def(lazy, 'def', def);
|
|
217
|
+
|
|
213
218
|
module.exports = lazy;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumjs/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"exports":
|
|
6
6
|
{
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
"./flags": "./lib/flags.js",
|
|
13
13
|
"./obj": "./lib/obj/index.js",
|
|
14
14
|
"./opt": "./lib/opt.js",
|
|
15
|
-
"./modulebuilder": "./lib/modulebuilder.js",
|
|
16
15
|
"./modules": "./lib/modules.js",
|
|
17
16
|
"./meta": "./lib/meta.js",
|
|
18
17
|
"./enum": "./lib/enum.js",
|
|
@@ -27,12 +26,11 @@
|
|
|
27
26
|
},
|
|
28
27
|
"devDependencies":
|
|
29
28
|
{
|
|
30
|
-
"@lumjs/tests": "^1.
|
|
29
|
+
"@lumjs/tests": "^1.7.1"
|
|
31
30
|
},
|
|
32
31
|
"scripts":
|
|
33
32
|
{
|
|
34
|
-
"
|
|
35
|
-
"test": "prove -e node --ext js ./test",
|
|
33
|
+
"test": "node ./node_modules/@lumjs/tests/bin/lumtest.js",
|
|
36
34
|
"build-docs": "jsdoc -c ./jsdoc.json"
|
|
37
35
|
}
|
|
38
36
|
}
|
package/lib/modulebuilder.js
DELETED
|
@@ -1,367 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
{
|
|
4
|
-
S,F,B,
|
|
5
|
-
isObj,needObj,needType,
|
|
6
|
-
def,lazy
|
|
7
|
-
} = require('./types');
|
|
8
|
-
|
|
9
|
-
const clone = require('./obj/copyall').duplicateOne;
|
|
10
|
-
|
|
11
|
-
// Methods we want to export in the functional API.
|
|
12
|
-
const BUILD_METHODS = ['has', 'can', 'from', 'set'];
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A class to make building modules easier.
|
|
16
|
-
*
|
|
17
|
-
* Basically wraps calls to `require()`, `def()`, `lazy()`,
|
|
18
|
-
* and assignments to `module.exports` into a simple set of methods.
|
|
19
|
-
*
|
|
20
|
-
* @exports module:@lumjs/core/modulebuilder
|
|
21
|
-
*/
|
|
22
|
-
class ModuleBuilder
|
|
23
|
-
{
|
|
24
|
-
/**
|
|
25
|
-
* Build a new ModuleBuilder instance.
|
|
26
|
-
*
|
|
27
|
-
* @param {object} targetModule - The `module` context variable.
|
|
28
|
-
* @param {object} [opts] - Options to change some default settings.
|
|
29
|
-
* @param {boolean} [opts.configurable=false] Default `configurable` value.
|
|
30
|
-
* @param {boolean} [opts.enumerable=true] Default `enumerable` value.
|
|
31
|
-
* @param {boolean} [opts.writable=false] Default `writable` value.
|
|
32
|
-
*
|
|
33
|
-
* @param {number} [opts.nested=NESTED_ERROR] How to handle nested names.
|
|
34
|
-
*
|
|
35
|
-
* If the `name` parameter in either the `has()` or `can()` method is passed
|
|
36
|
-
* as the path to a nested module with one or more `/` characters,
|
|
37
|
-
* this mode will determine how we derive the property name.
|
|
38
|
-
*
|
|
39
|
-
* For each mode, we'll use an example `name` of `./some/nested/path`.
|
|
40
|
-
*
|
|
41
|
-
* - `ModuleBuilder.NESTED_ERROR` `[0]` (default mode)
|
|
42
|
-
* Throw an `Error` saying paths are unhandled.
|
|
43
|
-
* - `ModuleBuilder.NESTED_FIRST` `[1]`
|
|
44
|
-
* Use the first (non-dot) path element, e.g. `some`
|
|
45
|
-
* - `ModuleBuilder.NESTED_LAST` `[2]`
|
|
46
|
-
* Use the last path element, e.g. `path`
|
|
47
|
-
* - `ModuleBuilder.NESTED_CAMEL` `[3]`
|
|
48
|
-
* Convert the name to camelCase, e.g. `someNestedPath`
|
|
49
|
-
*
|
|
50
|
-
* It's always possible to set the `conf.module` parameter manually as well,
|
|
51
|
-
* which avoids the need to generate a separate parameter name.
|
|
52
|
-
*
|
|
53
|
-
*/
|
|
54
|
-
constructor(targetModule, opts={})
|
|
55
|
-
{
|
|
56
|
-
needObj(opts, 'opts was not an object');
|
|
57
|
-
needObj(targetModule, 'targetModule was not an object');
|
|
58
|
-
needObj(targetModule.exports, 'targetModule.exports was not an object');
|
|
59
|
-
needType(F, targetModule.require, 'targetModule.require was not a function');
|
|
60
|
-
|
|
61
|
-
this.module = targetModule;
|
|
62
|
-
|
|
63
|
-
this.configurable = opts.configurable ?? false;
|
|
64
|
-
this.enumerable = opts.enumerable ?? true;
|
|
65
|
-
this.writable = opts.writable ?? false;
|
|
66
|
-
|
|
67
|
-
this.nested = opts.nested ?? ModuleBuilder.NESTED_ERROR;
|
|
68
|
-
|
|
69
|
-
this.strings = opts.strings;
|
|
70
|
-
this.locale = opts.locale;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Get a descriptor for a module export.
|
|
74
|
-
requireDescriptor(name, conf={})
|
|
75
|
-
{
|
|
76
|
-
if (typeof conf.getter === F || typeof conf.setter === F)
|
|
77
|
-
{ // It's an accessor-style descriptor already.
|
|
78
|
-
return conf;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
let value = conf.value;
|
|
82
|
-
|
|
83
|
-
if (value === undefined)
|
|
84
|
-
{
|
|
85
|
-
if (typeof conf.module === S)
|
|
86
|
-
name = conf.module;
|
|
87
|
-
if (!name.startsWith('./'))
|
|
88
|
-
name = './'+name;
|
|
89
|
-
|
|
90
|
-
value = this.module.require(name);
|
|
91
|
-
|
|
92
|
-
if (value && conf.prop && value[conf.prop] !== undefined)
|
|
93
|
-
{
|
|
94
|
-
value = value[conf.prop];
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const configurable = conf.configurable ?? this.configurable;
|
|
99
|
-
const enumerable = conf.enumerable ?? this.enumerable;
|
|
100
|
-
const writable = conf.writable ?? this.writable;
|
|
101
|
-
|
|
102
|
-
return {configurable, enumerable, writable, value};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Normalize a property name.
|
|
106
|
-
$normalizeProperty(name)
|
|
107
|
-
{
|
|
108
|
-
if (name.startsWith('./'))
|
|
109
|
-
{ // Get rid of the prefix.
|
|
110
|
-
name = name.substring(2);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (name.includes('/'))
|
|
114
|
-
{ // Multiple paths found.
|
|
115
|
-
const names = name.split('/');
|
|
116
|
-
if (this.nested === ModuleBuilder.NESTED_FIRST)
|
|
117
|
-
{
|
|
118
|
-
name = names[0];
|
|
119
|
-
}
|
|
120
|
-
else if (this.nested === ModuleBuilder.NESTED_LAST)
|
|
121
|
-
{
|
|
122
|
-
name = names[names.length-1];
|
|
123
|
-
}
|
|
124
|
-
else if (this.nested === ModuleBuilder.NESTED_CAMEL)
|
|
125
|
-
{
|
|
126
|
-
name = this.$normalizeWithCamelCase(names);
|
|
127
|
-
}
|
|
128
|
-
else
|
|
129
|
-
{
|
|
130
|
-
throw new Error("No valid nested path handling method was set");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return name;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
$normalizeWithCamelCase(names)
|
|
138
|
-
{
|
|
139
|
-
if (this.strings === undefined)
|
|
140
|
-
{
|
|
141
|
-
this.strings = require('./strings');
|
|
142
|
-
}
|
|
143
|
-
if (this.locale === undefined)
|
|
144
|
-
{
|
|
145
|
-
this.locale = this.strings.getLocale();
|
|
146
|
-
}
|
|
147
|
-
let name = names.shift().toLocaleLowerCase(this.locale);
|
|
148
|
-
for (const path in names)
|
|
149
|
-
{
|
|
150
|
-
name += this.strings.ucfirst(path, true, this.locale);
|
|
151
|
-
}
|
|
152
|
-
return name;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Export a module directly.
|
|
157
|
-
*
|
|
158
|
-
* @param {string} name - The property name to export.
|
|
159
|
-
*
|
|
160
|
-
* @param {object} [conf] Additional export configuration options.
|
|
161
|
-
* @param {string} [conf.module=`./${name}`] The module to `require()`.
|
|
162
|
-
* @param {string} [conf.prop] If set, we want an exported property
|
|
163
|
-
* of this name from the loaded module.
|
|
164
|
-
*
|
|
165
|
-
* If not set, the entire loaded module will be our exported value.
|
|
166
|
-
*
|
|
167
|
-
* @param {boolean} [conf.configurable=this.configurable]
|
|
168
|
-
* Descriptor `configurable` value.
|
|
169
|
-
* @param {boolean} [conf.enumerable=this.enumerable]
|
|
170
|
-
* Descriptor `enumerable` value.
|
|
171
|
-
* @param {boolean} [conf.writable=this.writable]
|
|
172
|
-
* Descriptor `writable` value.
|
|
173
|
-
*
|
|
174
|
-
* @param {*} [conf.value] An explicit value to set.
|
|
175
|
-
*
|
|
176
|
-
* This skips the `require()` call entirely.
|
|
177
|
-
*
|
|
178
|
-
* @returns {object} `this`
|
|
179
|
-
*/
|
|
180
|
-
has(name, conf={})
|
|
181
|
-
{
|
|
182
|
-
const pname = this.$normalizeProperty(name);
|
|
183
|
-
const desc = this.requireDescriptor(name, conf);
|
|
184
|
-
def(this.module.exports, pname, desc);
|
|
185
|
-
return this;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Export a lazy-loaded module.
|
|
190
|
-
*
|
|
191
|
-
* @param {string} name - The property name to export.
|
|
192
|
-
*
|
|
193
|
-
* @param {object} [conf] Additional export configuration options.
|
|
194
|
-
*
|
|
195
|
-
* In addition to all the options supported by
|
|
196
|
-
* [has()]{@link module:@lumjs/core/modules.has}
|
|
197
|
-
* this also supports one further option.
|
|
198
|
-
*
|
|
199
|
-
* @param {object} [conf.lazy] Advanced options for the `lazy()` call.
|
|
200
|
-
*
|
|
201
|
-
* If not specified, sane defaults will be used, that ensures the
|
|
202
|
-
* `enumerable` descriptor property for the lazy getter is set the
|
|
203
|
-
* same as the final descriptor property once its loaded.
|
|
204
|
-
*
|
|
205
|
-
* @returns {object} `this`
|
|
206
|
-
*/
|
|
207
|
-
can(name, conf={})
|
|
208
|
-
{
|
|
209
|
-
const pname = this.$normalizeProperty(name);
|
|
210
|
-
const enumerable = conf.enumerable ?? this.enumerable;
|
|
211
|
-
const lazyOpts = isObj(conf.lazy) ? conf.lazy : {enumerable};
|
|
212
|
-
const getter = () => this.requireDescriptor(name, conf);
|
|
213
|
-
lazy(this.module.exports, pname, getter, lazyOpts);
|
|
214
|
-
return this;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Re-export some exported properties from a module.
|
|
219
|
-
*
|
|
220
|
-
* By default this uses `has()` to define the exports, but that
|
|
221
|
-
* can be changed using special parameter values.
|
|
222
|
-
*
|
|
223
|
-
* @param {string} modname - The module to export the properties from.
|
|
224
|
-
* @param {...any} libs - What we're exporting.
|
|
225
|
-
*
|
|
226
|
-
* If this is a `string` it is the name of an exported property we want to
|
|
227
|
-
* re-export directly.
|
|
228
|
-
*
|
|
229
|
-
* If this is `true`, all subsequent values will be exported using `can()`.
|
|
230
|
-
*
|
|
231
|
-
* If this is `false`, all subsequent values will be exported using `has()`.
|
|
232
|
-
*
|
|
233
|
-
* If this is an `object`, then it's considered the `conf` parameter for
|
|
234
|
-
* the `has()` or `can()` methods. If the `conf` does not have a `module`
|
|
235
|
-
* property, the `modname` will be assigned as the `module` property.
|
|
236
|
-
* You cannot set the `prop` property, as it's overwritten for every
|
|
237
|
-
* exported property.
|
|
238
|
-
*
|
|
239
|
-
* @returns {object} `this`
|
|
240
|
-
*/
|
|
241
|
-
from(modname, ...libs)
|
|
242
|
-
{
|
|
243
|
-
let func = 'has';
|
|
244
|
-
let curConf = {module: modname};
|
|
245
|
-
for (const lib of libs)
|
|
246
|
-
{
|
|
247
|
-
if (isObj(lib))
|
|
248
|
-
{ // Change the current config.
|
|
249
|
-
curConf = clone(lib);
|
|
250
|
-
if (curConf.module === undefined)
|
|
251
|
-
{ // Make sure the module name is assigned.
|
|
252
|
-
curConf.module = modname;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
else if (typeof lib === B)
|
|
256
|
-
{ // Change the function we're calling.
|
|
257
|
-
func = lib ? 'can' : 'has';
|
|
258
|
-
}
|
|
259
|
-
else if (typeof lib === S)
|
|
260
|
-
{
|
|
261
|
-
const conf = clone(curConf);
|
|
262
|
-
conf.prop = lib;
|
|
263
|
-
this[func](lib, conf);
|
|
264
|
-
}
|
|
265
|
-
else
|
|
266
|
-
{
|
|
267
|
-
throw new TypeError("libs must be strings, booleans, or objects");
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return this;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Define an exported property in our module.
|
|
275
|
-
*
|
|
276
|
-
* This is literally just a wrapper around the
|
|
277
|
-
* [def()]{@link module:@lumjs/core/types.def} method,
|
|
278
|
-
* that passes `this.module.exports` as the `obj` parameter.
|
|
279
|
-
*
|
|
280
|
-
* @param {*} name - See `def()` for details.
|
|
281
|
-
* @param {*} value - See `def()` for details.
|
|
282
|
-
* @param {*} opts - See `def()` for details.
|
|
283
|
-
* @returns {(object|function)} What is returned may vary:
|
|
284
|
-
*
|
|
285
|
-
* If the `def()` returns a *bound* copy of itself, we'll return that
|
|
286
|
-
* bound function directly.
|
|
287
|
-
*
|
|
288
|
-
* In any other case, we return `this` ModuleBuilder instance.
|
|
289
|
-
*/
|
|
290
|
-
set(name, value, opts)
|
|
291
|
-
{
|
|
292
|
-
const retval = def(this.module.exports, name, value, opts);
|
|
293
|
-
if (typeof retval === F && isObj(retval.$this) && retval.$this.bound === retval)
|
|
294
|
-
{ // A bound copy was returned, we're going to return it directly.
|
|
295
|
-
return retval;
|
|
296
|
-
}
|
|
297
|
-
return this;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Create a functional API for the ModuleBuilder class.
|
|
302
|
-
*
|
|
303
|
-
* Basically makes a new `ModuleBuilder` instance, then creates standalone
|
|
304
|
-
* closure functions that wrap the main instance methods.
|
|
305
|
-
*
|
|
306
|
-
* These functions can be imported into a namespace directly.
|
|
307
|
-
* They each have a special `builder` property which is a reference
|
|
308
|
-
* to the underlying `ModuleBuilder` instance.
|
|
309
|
-
*
|
|
310
|
-
* There's also a `builder` property in the exported function list,
|
|
311
|
-
* as well as a copy of the `def()` method for quick exporting.
|
|
312
|
-
*
|
|
313
|
-
* Example usage:
|
|
314
|
-
*
|
|
315
|
-
* ```js
|
|
316
|
-
* const {has,can,from,set}
|
|
317
|
-
* = require('@lumjs/core').ModuleBuilder.build(module);
|
|
318
|
-
* // or the shortcut: require('@lumjs/core).buildModule(module);
|
|
319
|
-
*
|
|
320
|
-
* // exports.foo = require('./foo');
|
|
321
|
-
* has('foo');
|
|
322
|
-
*
|
|
323
|
-
* // exports.someNestedPath = require('./some/nested/path');
|
|
324
|
-
* has('./some/nested/path');
|
|
325
|
-
*
|
|
326
|
-
* ```
|
|
327
|
-
*
|
|
328
|
-
* @param {object} targetModule - The `module` for the Builder instance.
|
|
329
|
-
* @param {object} [opts] - Any options for the Builder instance.
|
|
330
|
-
* @returns {object} An object containing the closure functions.
|
|
331
|
-
*/
|
|
332
|
-
static build(targetModule, opts)
|
|
333
|
-
{
|
|
334
|
-
const builder = new this(targetModule, opts);
|
|
335
|
-
const funcs = {builder, def};
|
|
336
|
-
for (const name of BUILD_METHODS)
|
|
337
|
-
{
|
|
338
|
-
const func = function()
|
|
339
|
-
{
|
|
340
|
-
return builder[name](...arguments);
|
|
341
|
-
}
|
|
342
|
-
def(func, 'builder', builder);
|
|
343
|
-
funcs[name] = func;
|
|
344
|
-
}
|
|
345
|
-
return funcs;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* A static alternative to `new ModuleBuilder()`;
|
|
350
|
-
*
|
|
351
|
-
* @param {object} targetModule
|
|
352
|
-
* @param {object} [opts]
|
|
353
|
-
* @returns {object} The new `ModuleBuilder` instance.
|
|
354
|
-
*/
|
|
355
|
-
static new(targetModule, opts)
|
|
356
|
-
{
|
|
357
|
-
return new this(targetModule, opts);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
def(ModuleBuilder, 'NESTED_ERROR', 0)
|
|
363
|
-
def(ModuleBuilder, 'NESTED_FIRST', 1);
|
|
364
|
-
def(ModuleBuilder, 'NESTED_LAST', 2);
|
|
365
|
-
def(ModuleBuilder, 'NESTED_CAMEL', 3);
|
|
366
|
-
|
|
367
|
-
module.exports = ModuleBuilder;
|