@nejs/basic-extensions 2.10.0 → 2.11.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/dist/@nejs/basic-extensions.bundle.2.11.0.js +19 -0
- package/dist/@nejs/basic-extensions.bundle.2.11.0.js.map +7 -0
- package/dist/cjs/classes/descriptor.d.ts +67 -155
- package/dist/cjs/classes/descriptor.js +184 -277
- package/dist/cjs/classes/descriptor.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/symbol.extensions.js +1 -1
- package/dist/cjs/symbol.extensions.js.map +1 -1
- package/dist/cjs/utils/descriptor.utils.d.ts +146 -0
- package/dist/cjs/utils/descriptor.utils.js +614 -0
- package/dist/cjs/utils/descriptor.utils.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +59 -0
- package/dist/cjs/utils/index.js +34 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/toolkit.d.ts +3 -2
- package/dist/cjs/utils/toolkit.js +3 -2
- package/dist/cjs/utils/toolkit.js.map +1 -1
- package/dist/mjs/classes/descriptor.d.ts +67 -155
- package/dist/mjs/classes/descriptor.js +184 -277
- package/dist/mjs/classes/descriptor.js.map +1 -1
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/symbol.extensions.js +1 -1
- package/dist/mjs/symbol.extensions.js.map +1 -1
- package/dist/mjs/utils/descriptor.utils.d.ts +146 -0
- package/dist/mjs/utils/descriptor.utils.js +606 -0
- package/dist/mjs/utils/descriptor.utils.js.map +1 -0
- package/dist/mjs/utils/index.d.ts +59 -0
- package/dist/mjs/utils/index.js +34 -1
- package/dist/mjs/utils/index.js.map +1 -1
- package/dist/mjs/utils/toolkit.d.ts +3 -2
- package/dist/mjs/utils/toolkit.js +3 -2
- package/dist/mjs/utils/toolkit.js.map +1 -1
- package/package.json +2 -2
- package/src/classes/descriptor.js +205 -304
- package/src/index.js +1 -0
- package/src/symbol.extensions.js +1 -1
- package/src/utils/descriptor.utils.js +707 -0
- package/src/utils/index.js +73 -1
- package/src/utils/toolkit.js +4 -2
- package/tests/newClasses/descriptor.test.js +4 -8
- package/dist/@nejs/basic-extensions.bundle.2.10.0.js +0 -19
- package/dist/@nejs/basic-extensions.bundle.2.10.0.js.map +0 -7
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Extension } from '@nejs/extension';
|
|
2
|
+
import { accessor as uAccessor, data as uData, isDescriptor as uIsDescriptor, kAccessorDescriptorKeys, kDataDescriptorKeys, kDescriptorKeys, kSharedDescriptorKeys } from '../utils/descriptor.utils.js';
|
|
2
3
|
export class Descriptor {
|
|
3
4
|
/**
|
|
4
5
|
* The default private descriptor value is that of `enigmatic`
|
|
@@ -6,7 +7,7 @@ export class Descriptor {
|
|
|
6
7
|
* @private
|
|
7
8
|
* @type {object}
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
|
+
_desc = undefined;
|
|
10
11
|
/**
|
|
11
12
|
* An optionally associated object, usually the parent from which
|
|
12
13
|
* the descriptor was taken, or undefined if none was able to be
|
|
@@ -14,7 +15,7 @@ export class Descriptor {
|
|
|
14
15
|
*
|
|
15
16
|
* @type {object}
|
|
16
17
|
*/
|
|
17
|
-
|
|
18
|
+
_object = undefined;
|
|
18
19
|
/**
|
|
19
20
|
* Constructs a Descriptor instance which wraps and manages an object
|
|
20
21
|
* property descriptor. The constructor can handle an existing descriptor
|
|
@@ -32,24 +33,31 @@ export class Descriptor {
|
|
|
32
33
|
* valid.
|
|
33
34
|
*/
|
|
34
35
|
constructor(object, key) {
|
|
35
|
-
if (
|
|
36
|
-
this
|
|
36
|
+
if (arguments.length === 0) {
|
|
37
|
+
this._desc = Descriptor.enigmatic;
|
|
37
38
|
}
|
|
38
39
|
else if (Descriptor.isDescriptor(object)) {
|
|
39
|
-
this
|
|
40
|
-
this
|
|
40
|
+
this._desc = object;
|
|
41
|
+
this._object = isObject(key) ? key : undefined;
|
|
41
42
|
}
|
|
42
43
|
else if (isObject(object) && isValidKey(key)) {
|
|
43
|
-
|
|
44
|
-
this
|
|
44
|
+
console.log('new Descriptor(%o, %o)', object, key);
|
|
45
|
+
this._desc = Object.getOwnPropertyDescriptor(object, key);
|
|
46
|
+
this._object = object;
|
|
45
47
|
}
|
|
46
48
|
if (!this.isDescriptor) {
|
|
49
|
+
const objectMsg = object === globalThis
|
|
50
|
+
? '[GLOBAL]'
|
|
51
|
+
: (typeof key === 'object' ? JSON.stringify(object) : String(object));
|
|
52
|
+
const keyMsg = key === globalThis
|
|
53
|
+
? '[GLOBAL]'
|
|
54
|
+
: (typeof key === 'object' ? JSON.stringify(key) : String(key));
|
|
47
55
|
console.error(`
|
|
48
56
|
Descriptor(object: ${object}, key: ${key}) FAILED:
|
|
49
|
-
object: ${
|
|
50
|
-
key: ${
|
|
51
|
-
descriptor: `, this
|
|
52
|
-
throw new Error(`Not a valid descriptor:`, this
|
|
57
|
+
object: ${objectMsg}
|
|
58
|
+
key: ${keyMsg}
|
|
59
|
+
descriptor: `, this._desc);
|
|
60
|
+
throw new Error(`Not a valid descriptor:`, this._desc);
|
|
53
61
|
}
|
|
54
62
|
}
|
|
55
63
|
/**
|
|
@@ -59,7 +67,7 @@ export class Descriptor {
|
|
|
59
67
|
* a data descriptor
|
|
60
68
|
*/
|
|
61
69
|
get isAccessor() {
|
|
62
|
-
return
|
|
70
|
+
return uIsDescriptor(this._desc, true).isAccessor;
|
|
63
71
|
}
|
|
64
72
|
/**
|
|
65
73
|
* Detects whether or not this instance is an data object descriptor
|
|
@@ -68,7 +76,7 @@ export class Descriptor {
|
|
|
68
76
|
* an accessor descriptor
|
|
69
77
|
*/
|
|
70
78
|
get isData() {
|
|
71
|
-
return
|
|
79
|
+
return uIsDescriptor(this._desc, true).isData;
|
|
72
80
|
}
|
|
73
81
|
/**
|
|
74
82
|
* Detects whether or not this instance is a valid object descriptor
|
|
@@ -76,97 +84,7 @@ export class Descriptor {
|
|
|
76
84
|
* @returns {boolean} true if this descriptor store is a valid descriptor
|
|
77
85
|
*/
|
|
78
86
|
get isDescriptor() {
|
|
79
|
-
return
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Getter around the `configurable` object descriptor property of
|
|
83
|
-
* this instance of Descriptor.
|
|
84
|
-
*
|
|
85
|
-
* @returns {boolean} a boolean value or undefined if the internal
|
|
86
|
-
* descriptor store is invalid.
|
|
87
|
-
*/
|
|
88
|
-
get configurable() {
|
|
89
|
-
return !!this.#desc?.configurable;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Sets the `configurable` value of this object. If the internal descriptor
|
|
93
|
-
* store store is invalid, the value is thrown away
|
|
94
|
-
*
|
|
95
|
-
* @param {boolean} value the value to set for the `configurable` descriptor
|
|
96
|
-
* property. If this value is not a `boolean` it will be converted to one
|
|
97
|
-
*/
|
|
98
|
-
set configurable(value) {
|
|
99
|
-
(this.#desc || {}).configurable = !!value;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Getter around the `enumerable` object descriptor property of
|
|
103
|
-
* this instance of Descriptor.
|
|
104
|
-
*
|
|
105
|
-
* @returns {boolean} a boolean value or undefined if the internal
|
|
106
|
-
* descriptor store is invalid.
|
|
107
|
-
*/
|
|
108
|
-
get enumerable() {
|
|
109
|
-
return this.#desc?.enumerable;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Sets the `enumerable` value of this object. If the internal descriptor
|
|
113
|
-
* store is invalid, the value is thrown away
|
|
114
|
-
*
|
|
115
|
-
* @param {boolean} value the value to set for the `enumerable` descriptor
|
|
116
|
-
* property. If this value is not a `boolean` it will be converted to one
|
|
117
|
-
*/
|
|
118
|
-
set enumerable(value) {
|
|
119
|
-
(this.#desc || {}).enumerable = value;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Getter around the `writable` object descriptor property of
|
|
123
|
-
* this instance of Descriptor.
|
|
124
|
-
*
|
|
125
|
-
* @returns {boolean} a boolean value or undefined if the internal
|
|
126
|
-
* descriptor store is invalid.
|
|
127
|
-
*/
|
|
128
|
-
get writable() {
|
|
129
|
-
return this.#desc?.writable;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Sets the `writable` value of this object. If the internal descriptor
|
|
133
|
-
* store is invalid, the value is thrown away
|
|
134
|
-
*
|
|
135
|
-
* @param {boolean} value the value to set for the `writable` descriptor
|
|
136
|
-
* property. If this value is not a `boolean` it will be converted to one
|
|
137
|
-
*/
|
|
138
|
-
set writable(value) {
|
|
139
|
-
(this.#desc || {}).writable = value;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Getter around the `value` object descriptor property of
|
|
143
|
-
* this instance of Descriptor.
|
|
144
|
-
*
|
|
145
|
-
* @returns {any} any value stored in this descriptor
|
|
146
|
-
*/
|
|
147
|
-
get value() {
|
|
148
|
-
return this.#desc?.value;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Sets the `value` value of this object. If the internal descriptor
|
|
152
|
-
* store is invalid, the value is thrown away
|
|
153
|
-
*
|
|
154
|
-
* @param {any} value the value to set for the `value` descriptor
|
|
155
|
-
* property.
|
|
156
|
-
*/
|
|
157
|
-
set value(value) {
|
|
158
|
-
(this.#desc || {}).value = value;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Getter around the `get` object descriptor property of
|
|
162
|
-
* this instance of Descriptor.
|
|
163
|
-
*
|
|
164
|
-
* @returns {function} a function if the getter for this descriptor is
|
|
165
|
-
* defined or `undefined` if the internal descriptor object or the getter
|
|
166
|
-
* is undefined.
|
|
167
|
-
*/
|
|
168
|
-
get get() {
|
|
169
|
-
return this.#desc?.get;
|
|
87
|
+
return uIsDescriptor(this._desc);
|
|
170
88
|
}
|
|
171
89
|
/**
|
|
172
90
|
* Retrieves the {@link get} function for this accessor and binds it to
|
|
@@ -177,27 +95,7 @@ export class Descriptor {
|
|
|
177
95
|
* getter will be bound the associated and previously set `object`.
|
|
178
96
|
*/
|
|
179
97
|
get boundGet() {
|
|
180
|
-
return (isObject(this
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Sets the `get` value of this object. If the internal descriptor
|
|
184
|
-
* store is invalid, the value is thrown away
|
|
185
|
-
*
|
|
186
|
-
* @param {function} value the getter function for this descriptor
|
|
187
|
-
*/
|
|
188
|
-
set get(value) {
|
|
189
|
-
(this.#desc || {}).get = value;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Getter around the `set` object descriptor property of
|
|
193
|
-
* this instance of Descriptor.
|
|
194
|
-
*
|
|
195
|
-
* @returns {function} a function if the setter for this descriptor is
|
|
196
|
-
* defined or `undefined` if the internal descriptor object or the setter
|
|
197
|
-
* is undefined.
|
|
198
|
-
*/
|
|
199
|
-
get set() {
|
|
200
|
-
return (this.#desc || {}).set;
|
|
98
|
+
return (isObject(this._object) ? this.get?.bind(this._object) : this.get);
|
|
201
99
|
}
|
|
202
100
|
/**
|
|
203
101
|
* Retrieves the {@link set} function for this accessor and binds it to
|
|
@@ -208,16 +106,7 @@ export class Descriptor {
|
|
|
208
106
|
* setter will be bound the associated and previously set `object`.
|
|
209
107
|
*/
|
|
210
108
|
get boundSet() {
|
|
211
|
-
return (isObject(this
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Sets the `set` value of this object. If the internal descriptor
|
|
215
|
-
* store is invalid, the value is thrown away
|
|
216
|
-
*
|
|
217
|
-
* @param {function} value the setter function for this descriptor
|
|
218
|
-
*/
|
|
219
|
-
set set(value) {
|
|
220
|
-
(this.#desc || {}).set = value;
|
|
109
|
+
return (isObject(this._object) ? this.set?.bind(this._object) : this.set);
|
|
221
110
|
}
|
|
222
111
|
/**
|
|
223
112
|
* The function checks the descriptor's associated object has been set on this
|
|
@@ -226,7 +115,7 @@ export class Descriptor {
|
|
|
226
115
|
* @returns {boolean} `true` if the `object` property has been set,
|
|
227
116
|
* `false` otherwise
|
|
228
117
|
*/
|
|
229
|
-
get hasObject() { return isObject(this
|
|
118
|
+
get hasObject() { return isObject(this._object); }
|
|
230
119
|
/**
|
|
231
120
|
* Returns the descriptor's associated `object` value. This is usually the
|
|
232
121
|
* parent object from which the descriptor was derived. If the value is preset
|
|
@@ -235,7 +124,7 @@ export class Descriptor {
|
|
|
235
124
|
* @returns {object} the associated object for this descriptor or undefined
|
|
236
125
|
* if it has not yet been set.
|
|
237
126
|
*/
|
|
238
|
-
get object() { return this
|
|
127
|
+
get object() { return this._object; }
|
|
239
128
|
/**
|
|
240
129
|
* Sets the descriptor's associated `object` value. This is usually the
|
|
241
130
|
* parent object from which the descriptor was derived.
|
|
@@ -243,45 +132,7 @@ export class Descriptor {
|
|
|
243
132
|
* @param {object} value sets the object for which this descriptor is to
|
|
244
133
|
* be associated with.
|
|
245
134
|
*/
|
|
246
|
-
set object(value) { this
|
|
247
|
-
/**
|
|
248
|
-
* The function returns a string representation of a descriptor object with
|
|
249
|
-
* additional information about its type when used in the NodeJS repl.
|
|
250
|
-
*
|
|
251
|
-
* @param {number} depth - The `depth` parameter is used to specify the
|
|
252
|
-
* maximum depth to which nested objects and arrays will be formatted. If
|
|
253
|
-
* the depth is exceeded, the output will be truncated with ellipses.
|
|
254
|
-
* @param {object} options - The `options` parameter is an object that
|
|
255
|
-
* contains various configuration options for the `inspect` function.
|
|
256
|
-
* These options can be used to customize the output of the inspection.
|
|
257
|
-
* @param {function} inspect - The `inspect` parameter is a function that
|
|
258
|
-
* is used to convert an object into a string representation. It is
|
|
259
|
-
* typically used for debugging purposes or when displaying an object's
|
|
260
|
-
* properties.
|
|
261
|
-
* @returns a string that represents a descriptor. The string includes the
|
|
262
|
-
* type of the descriptor (either "Accessor" or "Data") and the result of
|
|
263
|
-
* inspecting the descriptor object using the provided options and depth.
|
|
264
|
-
*/
|
|
265
|
-
[Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
|
|
266
|
-
const type = this.isAccessor ? ' (Accessor)' : this.isData ? ' (Data)' : '';
|
|
267
|
-
return `Descriptor${type} ${inspect(this.#desc, { ...options, depth })}`;
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Shorthand for Object.getOwnPropertyDescriptor()
|
|
271
|
-
*
|
|
272
|
-
* @param {object} object a non-null object instance
|
|
273
|
-
* @param {string|symbol} key a symbol or string referencing which key on the
|
|
274
|
-
* object to return a descriptor for.
|
|
275
|
-
* @returns an object descriptor for the requested field or null
|
|
276
|
-
*/
|
|
277
|
-
static for(object, key, wrap = false) {
|
|
278
|
-
if (!isObject(object) || !isValidKey(key) || !Reflect.has(object, key)) {
|
|
279
|
-
return null;
|
|
280
|
-
}
|
|
281
|
-
return (wrap
|
|
282
|
-
? new Descriptor(Object.getOwnPropertyDescriptor(object, key))
|
|
283
|
-
: Object.getOwnPropertyDescriptor(object, key));
|
|
284
|
-
}
|
|
135
|
+
set object(value) { this._object = Object(value); }
|
|
285
136
|
/**
|
|
286
137
|
* Take the descriptor defined by this objects values and apply them to
|
|
287
138
|
* the specified object using the specified key.
|
|
@@ -310,7 +161,7 @@ export class Descriptor {
|
|
|
310
161
|
* a descriptor.
|
|
311
162
|
*/
|
|
312
163
|
toObject(bindAccessors = false) {
|
|
313
|
-
let descriptor = { ...this
|
|
164
|
+
let descriptor = { ...this._desc };
|
|
314
165
|
if (bindAccessors && this.isAccessor) {
|
|
315
166
|
if (this.hasObject) {
|
|
316
167
|
descriptor = {
|
|
@@ -329,6 +180,28 @@ export class Descriptor {
|
|
|
329
180
|
}
|
|
330
181
|
return descriptor;
|
|
331
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* The function returns a string representation of a descriptor object with
|
|
185
|
+
* additional information about its type when used in the NodeJS repl.
|
|
186
|
+
*
|
|
187
|
+
* @param {number} depth - The `depth` parameter is used to specify the
|
|
188
|
+
* maximum depth to which nested objects and arrays will be formatted. If
|
|
189
|
+
* the depth is exceeded, the output will be truncated with ellipses.
|
|
190
|
+
* @param {object} options - The `options` parameter is an object that
|
|
191
|
+
* contains various configuration options for the `inspect` function.
|
|
192
|
+
* These options can be used to customize the output of the inspection.
|
|
193
|
+
* @param {function} inspect - The `inspect` parameter is a function that
|
|
194
|
+
* is used to convert an object into a string representation. It is
|
|
195
|
+
* typically used for debugging purposes or when displaying an object's
|
|
196
|
+
* properties.
|
|
197
|
+
* @returns a string that represents a descriptor. The string includes the
|
|
198
|
+
* type of the descriptor (either "Accessor" or "Data") and the result of
|
|
199
|
+
* inspecting the descriptor object using the provided options and depth.
|
|
200
|
+
*/
|
|
201
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
|
|
202
|
+
const type = this.isAccessor ? ' (Accessor)' : this.isData ? ' (Data)' : '';
|
|
203
|
+
return `Descriptor${type} ${inspect(this._desc, { ...options, depth })}`;
|
|
204
|
+
}
|
|
332
205
|
/**
|
|
333
206
|
* Converts this descriptor object into a base representation
|
|
334
207
|
*
|
|
@@ -340,16 +213,20 @@ export class Descriptor {
|
|
|
340
213
|
switch (hint) {
|
|
341
214
|
case 'string':
|
|
342
215
|
if (this.isAccessor) {
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
216
|
+
const props = [];
|
|
217
|
+
if (Reflect.has(this._desc, 'get'))
|
|
218
|
+
props.push('getter');
|
|
219
|
+
if (Reflect.has(this._desc, 'set'))
|
|
220
|
+
props.push('setter');
|
|
221
|
+
return `Accessor (${props.join(', ')})`;
|
|
347
222
|
}
|
|
348
223
|
else if (this.isData) {
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
224
|
+
const props = [];
|
|
225
|
+
if (Reflect.has(this._desc, 'value'))
|
|
226
|
+
props.push('value');
|
|
227
|
+
if (Reflect.has(this._desc, 'writable'))
|
|
228
|
+
props.push('writable');
|
|
229
|
+
return `Data (${props.join(', ')})`;
|
|
353
230
|
}
|
|
354
231
|
break;
|
|
355
232
|
case 'number':
|
|
@@ -450,13 +327,8 @@ export class Descriptor {
|
|
|
450
327
|
* @returns an object with properties "get", "set", "enumerable", and
|
|
451
328
|
* "configurable".
|
|
452
329
|
*/
|
|
453
|
-
static accessor(
|
|
454
|
-
return
|
|
455
|
-
get: getter,
|
|
456
|
-
set: setter,
|
|
457
|
-
enumerable,
|
|
458
|
-
configurable
|
|
459
|
-
};
|
|
330
|
+
static accessor() {
|
|
331
|
+
return uAccessor(...arguments);
|
|
460
332
|
}
|
|
461
333
|
/**
|
|
462
334
|
* The function "newData" creates a new data object with customizable
|
|
@@ -472,13 +344,24 @@ export class Descriptor {
|
|
|
472
344
|
* @returns an object with properties `value`, `enumerable`, `writable`, and
|
|
473
345
|
* `configurable`.
|
|
474
346
|
*/
|
|
475
|
-
static data(value, writable = true, { enumerable, configurable } =
|
|
476
|
-
return
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
347
|
+
static data(value, writable = true, { enumerable, configurable } = { configurable: true, enumerable: true }) {
|
|
348
|
+
return uData(value, writable, enumerable, configurable);
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Shorthand for Object.getOwnPropertyDescriptor()
|
|
352
|
+
*
|
|
353
|
+
* @param {object} object a non-null object instance
|
|
354
|
+
* @param {string|symbol} key a symbol or string referencing which key on the
|
|
355
|
+
* object to return a descriptor for.
|
|
356
|
+
* @returns an object descriptor for the requested field or null
|
|
357
|
+
*/
|
|
358
|
+
static for(object, key, wrap = false) {
|
|
359
|
+
if (!isObject(object) || !isValidKey(key) || !Reflect.has(object, key)) {
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
return (wrap
|
|
363
|
+
? new Descriptor(Object.getOwnPropertyDescriptor(object, key))
|
|
364
|
+
: Object.getOwnPropertyDescriptor(object, key));
|
|
482
365
|
}
|
|
483
366
|
/**
|
|
484
367
|
* The function checks if an object is a likely an object descriptor in
|
|
@@ -487,107 +370,101 @@ export class Descriptor {
|
|
|
487
370
|
* or set). Technically, any object could serve as a descriptor but this
|
|
488
371
|
* function only returns true if known descriptor keys are found.
|
|
489
372
|
*
|
|
490
|
-
* @param object -
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
...Descriptor.ACCESSOR_KEYS,
|
|
498
|
-
...Descriptor.DATA_KEYS,
|
|
499
|
-
];
|
|
500
|
-
return hasSome(object, knownKeys);
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* The function checks if a given property or descriptor is a data property.
|
|
373
|
+
* @param {any} object - Any value we want to check for being a descriptor.
|
|
374
|
+
* @param {boolean} returnStatsInstead defaults to false, but if the value
|
|
375
|
+
* is `true` then an object with reasoning behind the decision of whether
|
|
376
|
+
* or not the
|
|
377
|
+
* @returns {IsDescriptorResponse} either a {@link boolean} value or
|
|
378
|
+
* an object conforming to {@link IsDescriptorStats} if `returnStatsInstead`
|
|
379
|
+
* is `true`
|
|
504
380
|
*
|
|
505
|
-
*
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
* @param object - The `object` parameter is the object that you want to
|
|
510
|
-
* check for data properties.
|
|
511
|
-
* @returns a boolean value. It returns `true` if the `descriptor` object
|
|
512
|
-
* has any keys that match the `DATA_KEYS` array, otherwise it returns
|
|
513
|
-
* `false`.
|
|
514
|
-
*/
|
|
515
|
-
static isData(object_orProp, property) {
|
|
516
|
-
const needsDescriptor = (((typeof object_orProp === 'object') || object_orProp instanceof Object) &&
|
|
517
|
-
property instanceof String);
|
|
518
|
-
const descriptor = (needsDescriptor
|
|
519
|
-
? Descriptor.for(object_orProp, property)
|
|
520
|
-
: object_orProp);
|
|
521
|
-
const { DATA_KEYS } = this;
|
|
522
|
-
let validData = false;
|
|
523
|
-
if (hasSome(descriptor, DATA_KEYS)) {
|
|
524
|
-
validData = true;
|
|
525
|
-
}
|
|
526
|
-
return validData;
|
|
381
|
+
* @see {@link DescriptorUtils.isDescriptor}
|
|
382
|
+
*/
|
|
383
|
+
static isDescriptor(object, returnStatsInstead = false) {
|
|
384
|
+
return uIsDescriptor(object, returnStatsInstead);
|
|
527
385
|
}
|
|
528
386
|
/**
|
|
529
387
|
* The function checks if a given property descriptor or property of an
|
|
530
388
|
* object is an accessor.
|
|
531
389
|
*
|
|
532
|
-
* @param
|
|
533
|
-
* descriptor object or a property name.
|
|
534
|
-
* @param property
|
|
535
|
-
* check
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
390
|
+
* @param {object} objectOrDescriptor - The `objectOrDescriptor` parameter
|
|
391
|
+
* can be either a descriptor object or a property name.
|
|
392
|
+
* @param {(string|number|symbol)?} property the property name you wish to
|
|
393
|
+
* check the validity as an accessor descriptor. Only expected if the
|
|
394
|
+
* `objectOrDescriptor` parameter is the object that would contain this
|
|
395
|
+
* property.
|
|
396
|
+
* @returns {@link Boolean} returning `true` if the `descriptor` object
|
|
397
|
+
* has any keys that match the {@link Descriptor.ACCESSOR_KEYS} array,
|
|
398
|
+
* otherwise it returns `false`.
|
|
399
|
+
*/
|
|
400
|
+
static isAccessor(objectOrDescriptor, property) {
|
|
401
|
+
const needsDescriptor = objectOrDescriptor &&
|
|
402
|
+
property &&
|
|
403
|
+
isObject(objectOrDescriptor) &&
|
|
404
|
+
isValidKey(property);
|
|
405
|
+
const descriptor = needsDescriptor
|
|
406
|
+
? Descriptor.for(objectOrDescriptor, property)
|
|
407
|
+
: objectOrDescriptor;
|
|
408
|
+
return uIsDescriptor(descriptor, true).isAccessor;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* The function checks if a given property or descriptor is a data property.
|
|
412
|
+
*
|
|
413
|
+
* @param {object} objectOrDescriptor - The `objectOrDescriptor` parameter
|
|
414
|
+
* can be either a descriptor object or a property name.
|
|
415
|
+
* @param {(string|number|symbol)?} property the property name you wish to
|
|
416
|
+
* check the validity as an accessor descriptor. Only expected if the
|
|
417
|
+
* `objectOrDescriptor` parameter is the object that would contain this
|
|
418
|
+
* property.
|
|
419
|
+
* @returns {@link Boolean} returning `true` if the `descriptor` object
|
|
420
|
+
* has any keys that match the {@link Descriptor.DATA_KEYS} array, otherwise
|
|
421
|
+
* it returns `false`.
|
|
422
|
+
*/
|
|
423
|
+
static isData(objectOrDescriptor, property) {
|
|
424
|
+
const needsDescriptor = objectOrDescriptor &&
|
|
425
|
+
property &&
|
|
426
|
+
isObject(objectOrDescriptor) &&
|
|
427
|
+
isValidKey(property);
|
|
428
|
+
const descriptor = needsDescriptor
|
|
429
|
+
? Descriptor.for(objectOrDescriptor, property)
|
|
430
|
+
: objectOrDescriptor;
|
|
431
|
+
return uIsDescriptor(descriptor, true).isData;
|
|
552
432
|
}
|
|
553
433
|
/**
|
|
554
434
|
* A base descriptor (new for each read) that is both enumerable and
|
|
555
435
|
* configurable
|
|
556
436
|
*
|
|
557
|
-
* @returns
|
|
558
|
-
* `base` method with the arguments `true` and `true`.
|
|
437
|
+
* @returns `{ enumerable: true, configurable: true }`
|
|
559
438
|
*/
|
|
560
439
|
static get flexible() {
|
|
561
|
-
return
|
|
440
|
+
return { enumerable: true, configurable: true };
|
|
562
441
|
}
|
|
563
442
|
/**
|
|
564
443
|
* A base descriptor (new for each read) that is not enumerable but is
|
|
565
444
|
* configurable
|
|
566
445
|
*
|
|
567
|
-
* @returns
|
|
568
|
-
* the `base` method with the arguments `false` and `true`.
|
|
446
|
+
* @returns `{ enumerable: false, configurable: true }`
|
|
569
447
|
*/
|
|
570
448
|
static get enigmatic() {
|
|
571
|
-
return
|
|
449
|
+
return { enumerable: false, configurable: true };
|
|
572
450
|
}
|
|
573
451
|
/**
|
|
574
452
|
* A base descriptor (new for each read) that is neither enumerable
|
|
575
|
-
* nor configurable
|
|
453
|
+
* nor configurable.
|
|
576
454
|
*
|
|
577
|
-
* @returns
|
|
578
|
-
* the arguments `false` and `false`.
|
|
455
|
+
* @returns `{ enumerable: false, configurable: false }`
|
|
579
456
|
*/
|
|
580
457
|
static get intrinsic() {
|
|
581
|
-
return
|
|
458
|
+
return { enumerable: false, configurable: false };
|
|
582
459
|
}
|
|
583
460
|
/**
|
|
584
|
-
* A base descriptor (new for each read) that enumerable but
|
|
461
|
+
* A base descriptor (new for each read) that is enumerable but
|
|
462
|
+
* not configurable
|
|
585
463
|
*
|
|
586
|
-
* @returns
|
|
587
|
-
* method with the arguments `true` and `false`.
|
|
464
|
+
* @returns `{ enumerable: true, configurable: false }`
|
|
588
465
|
*/
|
|
589
466
|
static get transparent() {
|
|
590
|
-
return
|
|
467
|
+
return { enumerable: true, configurable: false };
|
|
591
468
|
}
|
|
592
469
|
/**
|
|
593
470
|
* The function returns an array of shared descriptor keys.
|
|
@@ -595,7 +472,7 @@ export class Descriptor {
|
|
|
595
472
|
* @returns An array containing the strings 'configurable' and 'enumerable'.
|
|
596
473
|
*/
|
|
597
474
|
static get SHARED_KEYS() {
|
|
598
|
-
return
|
|
475
|
+
return kSharedDescriptorKeys;
|
|
599
476
|
}
|
|
600
477
|
/**
|
|
601
478
|
* The function returns an array of accessor descriptor keys.
|
|
@@ -603,7 +480,7 @@ export class Descriptor {
|
|
|
603
480
|
* @returns An array containing the strings 'get' and 'set' is being returned.
|
|
604
481
|
*/
|
|
605
482
|
static get ACCESSOR_KEYS() {
|
|
606
|
-
return
|
|
483
|
+
return kAccessorDescriptorKeys;
|
|
607
484
|
}
|
|
608
485
|
/**
|
|
609
486
|
* The function returns an array of data descriptor keys.
|
|
@@ -612,15 +489,45 @@ export class Descriptor {
|
|
|
612
489
|
* returned.
|
|
613
490
|
*/
|
|
614
491
|
static get DATA_KEYS() {
|
|
615
|
-
return
|
|
492
|
+
return kDataDescriptorKeys;
|
|
493
|
+
}
|
|
494
|
+
static {
|
|
495
|
+
for (const key of kDescriptorKeys) {
|
|
496
|
+
Object.defineProperties(Descriptor.prototype, {
|
|
497
|
+
[key]: uAccessor(function getMaker(storage) {
|
|
498
|
+
return function get() {
|
|
499
|
+
return this._desc[key];
|
|
500
|
+
};
|
|
501
|
+
}, function setMaker(storage) {
|
|
502
|
+
return function set(value) {
|
|
503
|
+
this._desc[key] = value;
|
|
504
|
+
};
|
|
505
|
+
})
|
|
506
|
+
});
|
|
507
|
+
}
|
|
616
508
|
}
|
|
617
509
|
}
|
|
618
510
|
export const DescriptorExtensions = new Extension(Descriptor);
|
|
619
|
-
function
|
|
620
|
-
|
|
621
|
-
|
|
511
|
+
function typeOrType(type, Class, notNullish = true) {
|
|
512
|
+
return (value) => ((!notNullish || (notNullish && value !== null && value !== undefined)) &&
|
|
513
|
+
(typeof value === type || (value && value instanceof Class)));
|
|
514
|
+
}
|
|
515
|
+
function isObject(o) { return typeOrType('object', Object)(o); }
|
|
516
|
+
function isString(o) { return typeOrType('string', String)(o); }
|
|
517
|
+
function isNumber(o) { return typeOrType('number', Number)(o); }
|
|
518
|
+
function isSymbol(o) { return typeOrType('symbol', Symbol)(o); }
|
|
519
|
+
function isFunction(o) { return typeOrType('function', Function)(o); }
|
|
520
|
+
function isValidKey(o) { return isString(o) || isNumber(o) || isSymbol(o); }
|
|
521
|
+
function hasAll(object, ...keys) { return hasQuantity('every', object, keys); }
|
|
522
|
+
function hasSome(object, ...keys) { return hasQuantity('some', object, keys); }
|
|
523
|
+
function hasQuantity(quantityFn, object, keys) {
|
|
524
|
+
return isObject(object) && (keys.flat(Infinity)
|
|
525
|
+
.map(key => Reflect.has(object, key))[quantityFn](has => has));
|
|
526
|
+
}
|
|
527
|
+
function hasOne(object, ...keys) {
|
|
622
528
|
return isObject(object) && (keys.flat(Infinity)
|
|
623
529
|
.map(key => Reflect.has(object, key))
|
|
624
|
-
.
|
|
530
|
+
.filter(has => has)
|
|
531
|
+
.length === 1);
|
|
625
532
|
}
|
|
626
533
|
//# sourceMappingURL=descriptor.js.map
|