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