@lumjs/core 1.38.0 → 1.38.1
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/obj/assignd.js +14 -5
- package/lib/obj/df.js +68 -21
- package/lib/obj/index.js +2 -2
- package/lib/types/def.js +5 -5
- package/lib/types/lazy.js +3 -3
- package/package.json +1 -1
package/lib/obj/assignd.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const {dfa} = require('./df');
|
|
4
|
+
const {isObj,isNil} = require('../types');
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Copy properties into a target object.
|
|
7
8
|
*
|
|
8
|
-
* Like Object.assign(), but it uses getOwnPropertyDescriptors()
|
|
9
|
-
*
|
|
9
|
+
* Like Object.assign(), but it uses getOwnPropertyDescriptors() to get
|
|
10
|
+
* the properties to copy, and [dfa()]{@link module:@lumjs/core/obj.dfa}
|
|
11
|
+
* to assign them.
|
|
10
12
|
*
|
|
11
13
|
* Basically a super simple, non-recursive replacement for my copyProps()
|
|
12
14
|
* and cp() functions that were overly complex and tried to do way too much.
|
|
@@ -21,8 +23,15 @@ function assignd (target, ...sources)
|
|
|
21
23
|
{
|
|
22
24
|
for (const src of sources)
|
|
23
25
|
{
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
if (isObj(src))
|
|
27
|
+
{
|
|
28
|
+
const descs = Object.getOwnPropertyDescriptors(src);
|
|
29
|
+
dfa(target, descs);
|
|
30
|
+
}
|
|
31
|
+
else if (!isNil(src))
|
|
32
|
+
{
|
|
33
|
+
console.error("Invalid assignd source", {src, sources, target});
|
|
34
|
+
}
|
|
26
35
|
}
|
|
27
36
|
return target;
|
|
28
37
|
}
|
package/lib/obj/df.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
const {doesDescriptor,needObj,needType,lazy,B,TYPES} = require('../types');
|
|
4
4
|
|
|
5
5
|
const DEF_DESC = {configurable: true}
|
|
6
|
-
const DEF_OPTS = {autoDesc: true}
|
|
7
6
|
|
|
8
7
|
const cp = Object.assign;
|
|
9
8
|
const clone = (...args) => cp({}, ...args);
|
|
@@ -66,29 +65,38 @@ function descriptor(desc, opts)
|
|
|
66
65
|
* @param {(string|symbol)} name - Property identifier
|
|
67
66
|
* @param {*} value - Value to assign
|
|
68
67
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* used as the template descriptor to assign the property.
|
|
72
|
-
*
|
|
73
|
-
* In any other case, this will be used as the `value` property of
|
|
74
|
-
* a generated descriptor to be assigned.
|
|
68
|
+
* See `opts.useDesc` for how this value is handled in regards to
|
|
69
|
+
* whether it is treated as a property descriptor or a plain data value.
|
|
75
70
|
*
|
|
76
71
|
* @param {object} [opts] Options
|
|
77
72
|
*
|
|
73
|
+
* See {@link module:@lumjs/core/obj~descriptor} for options that
|
|
74
|
+
* are used as descriptor defaults when assigning the property.
|
|
75
|
+
*
|
|
76
|
+
* @param {?boolean} [opts.useDesc=null] Treat `value` is a descriptor?
|
|
77
|
+
*
|
|
78
|
+
* If this is set to `true` then the `value` will be assumed to be a
|
|
79
|
+
* valid descriptor object; no tests will be done to confirm, so be sure!
|
|
80
|
+
*
|
|
81
|
+
* If this is set to `false` then a descriptor of `{value}` will be created.
|
|
82
|
+
* Again, no tests will be done, so this is a way to assign properties that
|
|
83
|
+
* have `value`, `get`, or `set` properties in them.
|
|
84
|
+
*
|
|
85
|
+
* If this is `null` (the default), then the `value` will be tested with
|
|
86
|
+
* [doesDescriptor()]{@link module:@lumjs/core/types.doesDescriptor},
|
|
87
|
+
* and that will determine if the value is treated as a descriptor or not.
|
|
88
|
+
*
|
|
78
89
|
* @returns {object} `obj`
|
|
79
90
|
* @alias module:@lumjs/core/obj.df
|
|
80
91
|
*/
|
|
81
|
-
function df(obj, name, value, opts)
|
|
92
|
+
function df(obj, name, value, opts={})
|
|
82
93
|
{
|
|
83
94
|
const log = {obj,name,value,opts};
|
|
84
95
|
needObj(obj, {log, fun: true});
|
|
85
96
|
needType(TYPES.PROP, name, {log});
|
|
86
97
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const desc = descriptor(((opts.autoDesc && doesDescriptor(value))
|
|
90
|
-
? value
|
|
91
|
-
: {value}), opts);
|
|
98
|
+
const useDesc = opts.useDesc ?? doesDescriptor(value);
|
|
99
|
+
const desc = descriptor((useDesc ? value : {value}), opts);
|
|
92
100
|
|
|
93
101
|
return Object.defineProperty(obj, name, desc);
|
|
94
102
|
}
|
|
@@ -121,11 +129,35 @@ function dfa(obj, values, opts)
|
|
|
121
129
|
return obj;
|
|
122
130
|
}
|
|
123
131
|
|
|
132
|
+
df(df, 'all', dfa);
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* A wrapper around df() that defines an *accessor property* with
|
|
136
|
+
* BOTH a getter and a setter specified explicitly.
|
|
137
|
+
*
|
|
138
|
+
* @param {(object|function)} obj - Target to define property in
|
|
139
|
+
* @param {(string|symbol)} name - Property identifier
|
|
140
|
+
* @param {function} getter - Getter function
|
|
141
|
+
* @param {function} setter - Setter function
|
|
142
|
+
* @param {object} [opts] Options
|
|
143
|
+
*
|
|
144
|
+
* The actual options passed to df() will be a clone, with the
|
|
145
|
+
* `useDesc` option explicitly forced to `true`.
|
|
146
|
+
*
|
|
147
|
+
*/
|
|
148
|
+
function dfb(obj, name, getter, setter, opts)
|
|
149
|
+
{
|
|
150
|
+
const log = {obj,name,getter,setter,opts};
|
|
151
|
+
opts = clone(opts, {useDesc: true});
|
|
152
|
+
return df(obj, name, {get: getter, set: setter}, opts);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
df(df, 'both', dfb);
|
|
156
|
+
|
|
124
157
|
/**
|
|
125
158
|
* Build magic closures to define properties using a chained syntax.
|
|
126
159
|
*
|
|
127
|
-
* This actually creates
|
|
128
|
-
* that wraps dfa(), and one that wraps the lazy() function.
|
|
160
|
+
* This actually creates closures for df(), dfa(), dfb(), and lazy().
|
|
129
161
|
* You can switch between them using magic properties.
|
|
130
162
|
*
|
|
131
163
|
* The closures when called will pass their arguments to the function
|
|
@@ -136,13 +168,15 @@ function dfa(obj, values, opts)
|
|
|
136
168
|
*
|
|
137
169
|
* - `one(name, value)` → A closure that wraps df()
|
|
138
170
|
* - `all(values)` → A closure that wraps dfa()
|
|
171
|
+
* - `both(name, g, s)` → A closure that wraps dfb()
|
|
172
|
+
* - `lazy(name, func)` → A closure that wraps lazy()
|
|
139
173
|
* - `update(opts)` → Update the options used by the closures.
|
|
140
174
|
* Uses Object.assign() to copy options from the specied object
|
|
141
175
|
* to the internal options object from the original dfc() call.
|
|
142
176
|
* This is a method call, NOT a closure function.
|
|
143
177
|
* - `opts` → The actual options object used by the closures.
|
|
144
178
|
* - `obj` → The target object used by the closures.
|
|
145
|
-
* - `
|
|
179
|
+
* - `for(obj)` → A wrapper method that will call dfc().
|
|
146
180
|
* When using this method, a *new set of closures* will be created,
|
|
147
181
|
* with the current options, but a new target object.
|
|
148
182
|
* If you use this in a chained statement, any calls after this
|
|
@@ -170,6 +204,12 @@ function dfc(obj, opts={})
|
|
|
170
204
|
return fnAll;
|
|
171
205
|
}
|
|
172
206
|
|
|
207
|
+
const fnBoth = function (name, getter, setter)
|
|
208
|
+
{
|
|
209
|
+
dfb(obj, name, getter, setter, opts);
|
|
210
|
+
return fnBoth;
|
|
211
|
+
}
|
|
212
|
+
|
|
173
213
|
const fnLazy = function (name, initfunc)
|
|
174
214
|
{
|
|
175
215
|
lazy(obj, name, initfunc, opts);
|
|
@@ -182,6 +222,8 @@ function dfc(obj, opts={})
|
|
|
182
222
|
opts: {value: opts},
|
|
183
223
|
one: {value: fnOne},
|
|
184
224
|
all: {value: fnAll},
|
|
225
|
+
both: {value: fnBoth},
|
|
226
|
+
lazy: {value: fnLazy},
|
|
185
227
|
update:
|
|
186
228
|
{
|
|
187
229
|
value()
|
|
@@ -190,7 +232,7 @@ function dfc(obj, opts={})
|
|
|
190
232
|
return this;
|
|
191
233
|
},
|
|
192
234
|
},
|
|
193
|
-
|
|
235
|
+
for:
|
|
194
236
|
{
|
|
195
237
|
value(newobj)
|
|
196
238
|
{
|
|
@@ -199,15 +241,20 @@ function dfc(obj, opts={})
|
|
|
199
241
|
},
|
|
200
242
|
}
|
|
201
243
|
|
|
202
|
-
|
|
203
|
-
|
|
244
|
+
// Quick alias
|
|
245
|
+
ctx.next = ctx.for;
|
|
246
|
+
|
|
247
|
+
dps(fnOne, ctx);
|
|
248
|
+
dps(fnAll, ctx);
|
|
249
|
+
dps(fnBoth, ctx);
|
|
250
|
+
dps(fnLazy, ctx);
|
|
204
251
|
|
|
205
252
|
return fnOne;
|
|
206
253
|
}
|
|
207
254
|
|
|
208
|
-
|
|
255
|
+
df(df, 'for', dfc);
|
|
209
256
|
|
|
210
257
|
module.exports =
|
|
211
258
|
{
|
|
212
|
-
df, dfa, dfc, lazy,
|
|
259
|
+
df, dfa, dfb, dfc, lazy,
|
|
213
260
|
}
|
package/lib/obj/index.js
CHANGED
|
@@ -8,7 +8,7 @@ const assignd = require('./assignd');
|
|
|
8
8
|
const {copyAll,duplicateOne,duplicateAll} = require('./copyall');
|
|
9
9
|
const copyProps = require('./copyprops');
|
|
10
10
|
const {CLONE,clone,addClone,cloneIfLocked} = require('./clone');
|
|
11
|
-
const {df,dfa,dfc,lazy} = require('./df');
|
|
11
|
+
const {df,dfa,dfb,dfc,lazy} = require('./df');
|
|
12
12
|
const {flip, flipKeyVal, flipMap} = require('./flip');
|
|
13
13
|
const {getMethods,signatureOf,MethodFilter} = require('./getmethods');
|
|
14
14
|
const getProperty = require('./getproperty');
|
|
@@ -27,7 +27,7 @@ const
|
|
|
27
27
|
module.exports =
|
|
28
28
|
{
|
|
29
29
|
assignd, cp, CLONE, clone, addClone, cloneIfLocked, lock, addLock,
|
|
30
|
-
df, dfa, dfc, lazy,
|
|
30
|
+
df, dfa, dfb, dfc, lazy,
|
|
31
31
|
mergeNested, syncNested, copyProps, copyAll, ns,
|
|
32
32
|
getObjectPath, setObjectPath, getNamespace, setNamespace,
|
|
33
33
|
getProperty, duplicateAll, duplicateOne, getMethods, signatureOf,
|
package/lib/types/def.js
CHANGED
|
@@ -7,13 +7,13 @@ const clone = (...args) => Object.assign({}, ...args);
|
|
|
7
7
|
/**
|
|
8
8
|
* A wrapper around `Object.defineProperty()` with added flair!
|
|
9
9
|
*
|
|
10
|
-
* FUTURE DEPRECATION
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* **FUTURE DEPRECATION**: this function will be deprecated in v1.19.0!
|
|
11
|
+
* It's gotten almost as convoluted as the old prop() method it replaced
|
|
12
|
+
* from my original Nano.js libraries.
|
|
13
|
+
*
|
|
13
14
|
* I have written new `obj.{df,dfa,dfc}` functions that will replace this
|
|
14
15
|
* function going forward. The related `types.lazy()` function will also
|
|
15
|
-
* be refactored to use the new functions, and moved to the `obj` module
|
|
16
|
-
* although for the `2.x` lifetime an alias will remain in the `type` module.
|
|
16
|
+
* be refactored to use the new functions, and moved to the `obj` module.
|
|
17
17
|
*
|
|
18
18
|
* @param {(object|function)} obj - The object to add a property to.
|
|
19
19
|
*
|
package/lib/types/lazy.js
CHANGED
|
@@ -101,11 +101,11 @@ const {doesDescriptor} = require('./basics');
|
|
|
101
101
|
* This is an extension of the [def()]{@link module:@lumjs/core/types.def}
|
|
102
102
|
* method, and indeed an alias called `def.lazy()` is also available.
|
|
103
103
|
*
|
|
104
|
-
* IMPORTANT: In
|
|
104
|
+
* IMPORTANT: In a later v1.18.x release, this will be refactored to use the
|
|
105
105
|
* [obj.df()]{@link module:@lumjs/core/obj.df} function that will replace
|
|
106
106
|
* def(). There's already an alias called `df.lazy()` available now.
|
|
107
|
-
* This will also be moved to the `obj` module, with
|
|
108
|
-
* in the `types` module for the duration of the
|
|
107
|
+
* This will also be moved to the `obj` module, with a deprecated alias left
|
|
108
|
+
* in the `types` module for the duration of the 1.x lifecycle.
|
|
109
109
|
*
|
|
110
110
|
* @param {(object|function)} target - The object to add the property to.
|
|
111
111
|
*
|