@nejs/basic-extensions 1.6.1 → 1.7.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/.esdoc.json +9 -0
- package/README.md +2025 -11
- package/bin/clean +6 -0
- package/dist/@nejs/basic-extensions.bundle.1.6.1.js +4 -0
- package/dist/@nejs/basic-extensions.bundle.1.6.1.js.map +7 -0
- package/dist/cjs/arrayextensions.js +1 -0
- package/dist/cjs/arrayextensions.js.map +1 -0
- package/dist/cjs/functionextensions.js +1 -0
- package/dist/cjs/functionextensions.js.map +1 -0
- package/dist/cjs/globals.js +2 -1
- package/dist/cjs/globals.js.map +1 -0
- package/dist/cjs/index.d.ts +10 -19
- package/dist/cjs/index.js +69 -76
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mapextensions.d.ts +2 -0
- package/dist/cjs/mapextensions.js +30 -0
- package/dist/cjs/mapextensions.js.map +1 -0
- package/dist/cjs/newClasses/asyncIterable.d.ts +123 -0
- package/dist/cjs/{asyncIterable.js → newClasses/asyncIterable.js} +7 -4
- package/dist/cjs/newClasses/asyncIterable.js.map +1 -0
- package/dist/cjs/newClasses/descriptor.d.ts +401 -0
- package/dist/cjs/{descriptor.js → newClasses/descriptor.js} +150 -80
- package/dist/cjs/newClasses/descriptor.js.map +1 -0
- package/dist/cjs/newClasses/iterable.d.ts +125 -0
- package/dist/cjs/{iterable.js → newClasses/iterable.js} +36 -10
- package/dist/cjs/newClasses/iterable.js.map +1 -0
- package/dist/cjs/newClasses/refmap.d.ts +238 -0
- package/dist/cjs/newClasses/refmap.js +433 -0
- package/dist/cjs/newClasses/refmap.js.map +1 -0
- package/dist/cjs/newClasses/refset.d.ts +186 -0
- package/dist/cjs/{refset.js → newClasses/refset.js} +4 -2
- package/dist/cjs/newClasses/refset.js.map +1 -0
- package/dist/cjs/objectextensions.d.ts +7 -6
- package/dist/cjs/objectextensions.js +85 -43
- package/dist/cjs/objectextensions.js.map +1 -0
- package/dist/cjs/reflectextensions.js +16 -12
- package/dist/cjs/reflectextensions.js.map +1 -0
- package/dist/cjs/stringextensions.js +1 -0
- package/dist/cjs/stringextensions.js.map +1 -0
- package/dist/cjs/symbolextensions.js +3 -1
- package/dist/cjs/symbolextensions.js.map +1 -0
- package/dist/cjs/weakrefextensions.js +1 -0
- package/dist/cjs/weakrefextensions.js.map +1 -0
- package/dist/mjs/arrayextensions.js +1 -0
- package/dist/mjs/arrayextensions.js.map +1 -0
- package/dist/mjs/functionextensions.js +1 -0
- package/dist/mjs/functionextensions.js.map +1 -0
- package/dist/mjs/globals.js +2 -1
- package/dist/mjs/globals.js.map +1 -0
- package/dist/mjs/index.d.ts +10 -19
- package/dist/mjs/index.js +67 -60
- package/dist/mjs/index.js.map +1 -0
- package/dist/mjs/mapextensions.d.ts +2 -0
- package/dist/mjs/mapextensions.js +27 -0
- package/dist/mjs/mapextensions.js.map +1 -0
- package/dist/mjs/newClasses/asyncIterable.d.ts +123 -0
- package/dist/mjs/{asyncIterable.js → newClasses/asyncIterable.js} +106 -105
- package/dist/mjs/newClasses/asyncIterable.js.map +1 -0
- package/dist/mjs/newClasses/descriptor.d.ts +401 -0
- package/dist/mjs/{descriptor.js → newClasses/descriptor.js} +129 -67
- package/dist/mjs/newClasses/descriptor.js.map +1 -0
- package/dist/mjs/newClasses/iterable.d.ts +125 -0
- package/dist/mjs/newClasses/iterable.js +199 -0
- package/dist/mjs/newClasses/iterable.js.map +1 -0
- package/dist/mjs/newClasses/refmap.d.ts +238 -0
- package/dist/mjs/newClasses/refmap.js +417 -0
- package/dist/mjs/newClasses/refmap.js.map +1 -0
- package/dist/mjs/newClasses/refset.d.ts +186 -0
- package/dist/mjs/{refset.js → newClasses/refset.js} +3 -2
- package/dist/mjs/newClasses/refset.js.map +1 -0
- package/dist/mjs/objectextensions.d.ts +7 -6
- package/dist/mjs/objectextensions.js +84 -42
- package/dist/mjs/objectextensions.js.map +1 -0
- package/dist/mjs/reflectextensions.js +16 -12
- package/dist/mjs/reflectextensions.js.map +1 -0
- package/dist/mjs/stringextensions.js +1 -0
- package/dist/mjs/stringextensions.js.map +1 -0
- package/dist/mjs/symbolextensions.js +3 -1
- package/dist/mjs/symbolextensions.js.map +1 -0
- package/dist/mjs/weakrefextensions.js +1 -0
- package/dist/mjs/weakrefextensions.js.map +1 -0
- package/docs/assets/anchor.js +350 -0
- package/docs/assets/bass-addons.css +12 -0
- package/docs/assets/bass.css +544 -0
- package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/docs/assets/fonts/LICENSE.txt +93 -0
- package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/docs/assets/fonts/source-code-pro.css +23 -0
- package/docs/assets/github.css +123 -0
- package/docs/assets/site.js +168 -0
- package/docs/assets/split.css +15 -0
- package/docs/assets/split.js +782 -0
- package/docs/assets/style.css +147 -0
- package/docs/index.html +13060 -0
- package/jsdoc-config.json +31 -0
- package/package.json +12 -3
- package/src/globals.js +1 -1
- package/src/index.js +75 -82
- package/src/mapextensions.js +30 -0
- package/src/{asyncIterable.js → newClasses/asyncIterable.js} +117 -117
- package/src/{descriptor.js → newClasses/descriptor.js} +137 -74
- package/src/newClasses/iterable.js +221 -0
- package/src/newClasses/refmap.js +483 -0
- package/src/{refset.js → newClasses/refset.js} +2 -2
- package/src/objectextensions.js +97 -46
- package/src/reflectextensions.js +16 -14
- package/src/symbolextensions.js +2 -1
- package/tests/index.test.js +1 -1
- package/tests/{asyncIterable.test.js → newClasses/asyncIterable.test.js} +3 -4
- package/tests/newClasses/descriptor.test.js +252 -0
- package/tests/{iterable.test.js → newClasses/iterable.test.js} +2 -4
- package/tests/newClasses/refmap.test.js +69 -0
- package/tests/{refset.test.js → newClasses/refset.test.js} +2 -4
- package/tests/objectextensions.test.js +128 -0
- package/tsconfig.base.json +2 -1
- package/dist/@nejs/basic-extensions.bundle.1.6.0.js +0 -2
- package/dist/@nejs/basic-extensions.bundle.1.6.0.js.map +0 -7
- package/dist/cjs/asyncIterable.d.ts +0 -3
- package/dist/cjs/descriptor.d.ts +0 -2
- package/dist/cjs/iterable.d.ts +0 -3
- package/dist/cjs/refset.d.ts +0 -2
- package/dist/mjs/asyncIterable.d.ts +0 -3
- package/dist/mjs/descriptor.d.ts +0 -2
- package/dist/mjs/iterable.d.ts +0 -3
- package/dist/mjs/iterable.js +0 -184
- package/dist/mjs/refset.d.ts +0 -2
- package/src/iterable.js +0 -203
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
export class Descriptor {
|
|
2
|
+
/**
|
|
3
|
+
* Shorthand for Object.getOwnPropertyDescriptor()
|
|
4
|
+
*
|
|
5
|
+
* @param {object} object a non-null object instance
|
|
6
|
+
* @param {string|symbol} key a symbol or string referencing which key on the
|
|
7
|
+
* object to return a descriptor for.
|
|
8
|
+
* @returns an object descriptor for the requested field or null
|
|
9
|
+
*/
|
|
10
|
+
static for(object: object, key: string | symbol, wrap?: boolean): PropertyDescriptor | Descriptor | null | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* The function `getData` retrieves the value of a property from an object
|
|
13
|
+
* if it exists and is a data property.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} object - The "object" parameter is the object from which
|
|
16
|
+
* we want to retrieve data.
|
|
17
|
+
* @param {string|symbol} property - The `property` parameter is the name of
|
|
18
|
+
* the property that you want to retrieve the data from.
|
|
19
|
+
* @returns either the value of the specified property if it exists and is
|
|
20
|
+
* a data property, or undefined if the property does not exist or is not
|
|
21
|
+
* a data property.
|
|
22
|
+
*/
|
|
23
|
+
static getData(object: object, property: string | symbol): any;
|
|
24
|
+
/**
|
|
25
|
+
* The function `getAccessor` checks if an object has a getter/setter accessor
|
|
26
|
+
* for a given property and returns the accessor functions if found.
|
|
27
|
+
*
|
|
28
|
+
* @param object - The `object` parameter is the object from which we want to
|
|
29
|
+
* retrieve the accessor for a specific property.
|
|
30
|
+
* @param property - The `property` parameter is the name of the property for
|
|
31
|
+
* which we want to get the accessor.
|
|
32
|
+
* @returns an object that contains the getter and setter functions for the
|
|
33
|
+
* specified property of the given object. If the property is an accessor
|
|
34
|
+
* property (defined with a getter and/or setter), the returned object will
|
|
35
|
+
* also have additional properties such as "accessor" and "descriptor". If
|
|
36
|
+
* the property is not found or is not an accessor property, the function
|
|
37
|
+
* returns undefined.
|
|
38
|
+
*/
|
|
39
|
+
static getAccessor(object: any, property: any): any;
|
|
40
|
+
/**
|
|
41
|
+
* The function returns an object with enumerable and configurable properties
|
|
42
|
+
* based on the input parameters.
|
|
43
|
+
*
|
|
44
|
+
* @param [enumerable=false] - A boolean value indicating whether the property
|
|
45
|
+
* can be enumerated (listed) when iterating over the object's properties.
|
|
46
|
+
* @param [configurable=false] - The `configurable` parameter determines
|
|
47
|
+
* whether the property can be deleted or its attributes can be modified.
|
|
48
|
+
* If `configurable` is set to `true`, the property can be deleted and its
|
|
49
|
+
* attributes can be changed. If `configurable` is set to `false`, the
|
|
50
|
+
* property cannot be deleted and
|
|
51
|
+
* @returns An object with the properties `enumerable` and `configurable` is
|
|
52
|
+
* being returned. The values of these properties are determined by the
|
|
53
|
+
* arguments passed to the `base` function.
|
|
54
|
+
*/
|
|
55
|
+
static base(enumerable?: boolean | undefined, configurable?: boolean | undefined): {
|
|
56
|
+
enumerable: boolean;
|
|
57
|
+
configurable: boolean;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The function "newAccessor" creates a new property descriptor object with a
|
|
61
|
+
* getter and setter function, along with optional enumerable and configurable
|
|
62
|
+
* flags.
|
|
63
|
+
*
|
|
64
|
+
* @param getter - The getter parameter is a function that will be used as the
|
|
65
|
+
* getter for the property. It will be called when the property is accessed.
|
|
66
|
+
* @param setter - The `setter` parameter is a function that will be used as
|
|
67
|
+
* the setter for the property. It will be called whenever the property is
|
|
68
|
+
* assigned a new value.
|
|
69
|
+
* @param [] - - `getter`: A function that will be used as the getter for the
|
|
70
|
+
* property.
|
|
71
|
+
* @returns an object with properties "get", "set", "enumerable", and
|
|
72
|
+
* "configurable".
|
|
73
|
+
*/
|
|
74
|
+
static accessor(getter: any, setter: any, { enumerable, configurable }?: {
|
|
75
|
+
enumerable: boolean;
|
|
76
|
+
configurable: boolean;
|
|
77
|
+
} | undefined): {
|
|
78
|
+
get: any;
|
|
79
|
+
set: any;
|
|
80
|
+
enumerable: boolean;
|
|
81
|
+
configurable: boolean;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* The function "newData" creates a new data object with customizable
|
|
85
|
+
* properties.
|
|
86
|
+
*
|
|
87
|
+
* @param value - The value parameter represents the value that will be
|
|
88
|
+
* assigned to the property.
|
|
89
|
+
* @param [writable=true] - The `writable` parameter determines whether the
|
|
90
|
+
* value of the property can be changed. If `writable` is set to `true`, the
|
|
91
|
+
* value can be changed. If `writable` is set to `false`, the value cannot be
|
|
92
|
+
* changed.
|
|
93
|
+
* @param [] - - `value`: The value to be assigned to the property.
|
|
94
|
+
* @returns an object with properties `value`, `enumerable`, `writable`, and
|
|
95
|
+
* `configurable`.
|
|
96
|
+
*/
|
|
97
|
+
static data(value: any, writable?: boolean | undefined, { enumerable, configurable }?: {
|
|
98
|
+
enumerable: boolean;
|
|
99
|
+
configurable: boolean;
|
|
100
|
+
} | undefined): {
|
|
101
|
+
value: any;
|
|
102
|
+
enumerable: boolean;
|
|
103
|
+
writable: boolean;
|
|
104
|
+
configurable: boolean;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* The function checks if an object is a likely an object descriptor in
|
|
108
|
+
* JavaScript. This is determined as an object with some of the known
|
|
109
|
+
* descriptor keys (e.g. enumerable, configurable, value, writable, get,
|
|
110
|
+
* or set). Technically, any object could serve as a descriptor but this
|
|
111
|
+
* function only returns true if known descriptor keys are found.
|
|
112
|
+
*
|
|
113
|
+
* @param object - The `object` parameter is the object that we want to
|
|
114
|
+
* check if it is a descriptor.
|
|
115
|
+
* @returns a boolean value.
|
|
116
|
+
*/
|
|
117
|
+
static isDescriptor(object: any): any;
|
|
118
|
+
/**
|
|
119
|
+
* The function checks if a given property or descriptor is a data property.
|
|
120
|
+
*
|
|
121
|
+
* brie
|
|
122
|
+
*
|
|
123
|
+
* @param descriptor_orProp - The `descriptor_orProp` parameter can be
|
|
124
|
+
* either a descriptor or a property name.
|
|
125
|
+
* @param object - The `object` parameter is the object that you want to
|
|
126
|
+
* check for data properties.
|
|
127
|
+
* @returns a boolean value. It returns `true` if the `descriptor` object
|
|
128
|
+
* has any keys that match the `DATA_KEYS` array, otherwise it returns
|
|
129
|
+
* `false`.
|
|
130
|
+
*/
|
|
131
|
+
static isData(object_orProp: any, property: any): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* The function checks if a given property descriptor or property of an
|
|
134
|
+
* object is an accessor.
|
|
135
|
+
*
|
|
136
|
+
* @param object_orProp - The `descriptor_orProp` parameter can be either a
|
|
137
|
+
* descriptor object or a property name.
|
|
138
|
+
* @param property - The `object` parameter is the object that you want to
|
|
139
|
+
* check for accessor properties.
|
|
140
|
+
* @returns a boolean value. It returns true if the descriptor or property
|
|
141
|
+
* passed as an argument is an accessor descriptor, and false otherwise.
|
|
142
|
+
*/
|
|
143
|
+
static isAccessor(object_orProp: any, property: any): boolean;
|
|
144
|
+
/**
|
|
145
|
+
* A base descriptor (new for each read) that is both enumerable and
|
|
146
|
+
* configurable
|
|
147
|
+
*
|
|
148
|
+
* @returns The method `flexible` is returning the result of calling the
|
|
149
|
+
* `base` method with the arguments `true` and `true`.
|
|
150
|
+
*/
|
|
151
|
+
static get flexible(): {
|
|
152
|
+
enumerable: boolean;
|
|
153
|
+
configurable: boolean;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* A base descriptor (new for each read) that is not enumerable but is
|
|
157
|
+
* configurable
|
|
158
|
+
*
|
|
159
|
+
* @returns The method `enigmatic` is returning the result of calling
|
|
160
|
+
* the `base` method with the arguments `false` and `true`.
|
|
161
|
+
*/
|
|
162
|
+
static get enigmatic(): {
|
|
163
|
+
enumerable: boolean;
|
|
164
|
+
configurable: boolean;
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* A base descriptor (new for each read) that is neither enumerable
|
|
168
|
+
* nor configurable
|
|
169
|
+
*
|
|
170
|
+
* @returns The code is returning the result of calling the `base` method with
|
|
171
|
+
* the arguments `false` and `false`.
|
|
172
|
+
*/
|
|
173
|
+
static get intrinsic(): {
|
|
174
|
+
enumerable: boolean;
|
|
175
|
+
configurable: boolean;
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* A base descriptor (new for each read) that enumerable but not configurable
|
|
179
|
+
*
|
|
180
|
+
* @returns The method is returning the result of calling the `base`
|
|
181
|
+
* method with the arguments `true` and `false`.
|
|
182
|
+
*/
|
|
183
|
+
static get transparent(): {
|
|
184
|
+
enumerable: boolean;
|
|
185
|
+
configurable: boolean;
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* The function returns an array of shared descriptor keys.
|
|
189
|
+
*
|
|
190
|
+
* @returns An array containing the strings 'configurable' and 'enumerable'.
|
|
191
|
+
*/
|
|
192
|
+
static get SHARED_KEYS(): string[];
|
|
193
|
+
/**
|
|
194
|
+
* The function returns an array of accessor descriptor keys.
|
|
195
|
+
*
|
|
196
|
+
* @returns An array containing the strings 'get' and 'set' is being returned.
|
|
197
|
+
*/
|
|
198
|
+
static get ACCESSOR_KEYS(): string[];
|
|
199
|
+
/**
|
|
200
|
+
* The function returns an array of data descriptor keys.
|
|
201
|
+
*
|
|
202
|
+
* @returns An array containing the strings 'value' and 'writable' is being
|
|
203
|
+
* returned.
|
|
204
|
+
*/
|
|
205
|
+
static get DATA_KEYS(): string[];
|
|
206
|
+
/**
|
|
207
|
+
* Creates a new instance of Descriptor either from another object or
|
|
208
|
+
* around the supplied object descriptor value.
|
|
209
|
+
*
|
|
210
|
+
* @param {object} object either an object descriptor or the object
|
|
211
|
+
* from which to get the descriptor
|
|
212
|
+
* @param {symbol|string} key a valid key for accessing the descriptor
|
|
213
|
+
* on the aforesupplied object.
|
|
214
|
+
*/
|
|
215
|
+
constructor(object: object, key: symbol | string);
|
|
216
|
+
/**
|
|
217
|
+
* Detects whether or not this instance is an accessor object descriptor
|
|
218
|
+
*
|
|
219
|
+
* @returns {boolean} true if this object has a getter or setter and is not
|
|
220
|
+
* a data descriptor
|
|
221
|
+
*/
|
|
222
|
+
get isAccessor(): boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Detects whether or not this instance is an data object descriptor
|
|
225
|
+
*
|
|
226
|
+
* @returns {boolean} true if this object has a value property and is not
|
|
227
|
+
* an accessor descriptor
|
|
228
|
+
*/
|
|
229
|
+
get isData(): boolean;
|
|
230
|
+
/**
|
|
231
|
+
* Detects whether or not this instance is a valid object descriptor
|
|
232
|
+
*
|
|
233
|
+
* @returns {boolean} true if this descriptor store is a valid descriptor
|
|
234
|
+
*/
|
|
235
|
+
get isDescriptor(): boolean;
|
|
236
|
+
/**
|
|
237
|
+
* Sets the `configurable` value of this object. If the internal descriptor
|
|
238
|
+
* store store is invalid, the value is thrown away
|
|
239
|
+
*
|
|
240
|
+
* @param {boolean} value the value to set for the `configurable` descriptor
|
|
241
|
+
* property. If this value is not a `boolean` it will be converted to one
|
|
242
|
+
*/
|
|
243
|
+
set configurable(value: boolean);
|
|
244
|
+
/**
|
|
245
|
+
* Getter around the `configurable` object descriptor property of
|
|
246
|
+
* this instance of Descriptor.
|
|
247
|
+
*
|
|
248
|
+
* @returns {boolean} a boolean value or undefined if the internal
|
|
249
|
+
* descriptor store is invalid.
|
|
250
|
+
*/
|
|
251
|
+
get configurable(): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Sets the `enumerable` value of this object. If the internal descriptor
|
|
254
|
+
* store is invalid, the value is thrown away
|
|
255
|
+
*
|
|
256
|
+
* @param {boolean} value the value to set for the `enumerable` descriptor
|
|
257
|
+
* property. If this value is not a `boolean` it will be converted to one
|
|
258
|
+
*/
|
|
259
|
+
set enumerable(value: boolean);
|
|
260
|
+
/**
|
|
261
|
+
* Getter around the `enumerable` object descriptor property of
|
|
262
|
+
* this instance of Descriptor.
|
|
263
|
+
*
|
|
264
|
+
* @returns {boolean} a boolean value or undefined if the internal
|
|
265
|
+
* descriptor store is invalid.
|
|
266
|
+
*/
|
|
267
|
+
get enumerable(): boolean;
|
|
268
|
+
/**
|
|
269
|
+
* Sets the `writable` value of this object. If the internal descriptor
|
|
270
|
+
* store is invalid, the value is thrown away
|
|
271
|
+
*
|
|
272
|
+
* @param {boolean} value the value to set for the `writable` descriptor
|
|
273
|
+
* property. If this value is not a `boolean` it will be converted to one
|
|
274
|
+
*/
|
|
275
|
+
set writable(value: boolean);
|
|
276
|
+
/**
|
|
277
|
+
* Getter around the `writable` object descriptor property of
|
|
278
|
+
* this instance of Descriptor.
|
|
279
|
+
*
|
|
280
|
+
* @returns {boolean} a boolean value or undefined if the internal
|
|
281
|
+
* descriptor store is invalid.
|
|
282
|
+
*/
|
|
283
|
+
get writable(): boolean;
|
|
284
|
+
/**
|
|
285
|
+
* Sets the `value` value of this object. If the internal descriptor
|
|
286
|
+
* store is invalid, the value is thrown away
|
|
287
|
+
*
|
|
288
|
+
* @param {any} value the value to set for the `value` descriptor
|
|
289
|
+
* property.
|
|
290
|
+
*/
|
|
291
|
+
set value(value: any);
|
|
292
|
+
/**
|
|
293
|
+
* Getter around the `value` object descriptor property of
|
|
294
|
+
* this instance of Descriptor.
|
|
295
|
+
*
|
|
296
|
+
* @returns {any} any value stored in this descriptor
|
|
297
|
+
*/
|
|
298
|
+
get value(): any;
|
|
299
|
+
/**
|
|
300
|
+
* Sets the `get` value of this object. If the internal descriptor
|
|
301
|
+
* store is invalid, the value is thrown away
|
|
302
|
+
*
|
|
303
|
+
* @param {function} value the getter function for this descriptor
|
|
304
|
+
*/
|
|
305
|
+
set get(value: Function);
|
|
306
|
+
/**
|
|
307
|
+
* Getter around the `get` object descriptor property of
|
|
308
|
+
* this instance of Descriptor.
|
|
309
|
+
*
|
|
310
|
+
* @returns {function} a function if the getter for this descriptor is
|
|
311
|
+
* defined or `undefined` if the internal descriptor object or the getter
|
|
312
|
+
* is undefined.
|
|
313
|
+
*/
|
|
314
|
+
get get(): Function;
|
|
315
|
+
/**
|
|
316
|
+
* Retrieves the {@link get} function for this accessor and binds it to
|
|
317
|
+
* the object from which the descriptor was derived, if that value is set.
|
|
318
|
+
* Otherwise this method is identical to the {@link get} accessor.
|
|
319
|
+
*
|
|
320
|
+
* @returns {function} the getter if one is defined. If possible this
|
|
321
|
+
* getter will be bound the associated and previously set `object`.
|
|
322
|
+
*/
|
|
323
|
+
get boundGet(): Function;
|
|
324
|
+
/**
|
|
325
|
+
* Sets the `set` value of this object. If the internal descriptor
|
|
326
|
+
* store is invalid, the value is thrown away
|
|
327
|
+
*
|
|
328
|
+
* @param {function} value the setter function for this descriptor
|
|
329
|
+
*/
|
|
330
|
+
set set(value: Function);
|
|
331
|
+
/**
|
|
332
|
+
* Getter around the `set` object descriptor property of
|
|
333
|
+
* this instance of Descriptor.
|
|
334
|
+
*
|
|
335
|
+
* @returns {function} a function if the setter for this descriptor is
|
|
336
|
+
* defined or `undefined` if the internal descriptor object or the setter
|
|
337
|
+
* is undefined.
|
|
338
|
+
*/
|
|
339
|
+
get set(): Function;
|
|
340
|
+
/**
|
|
341
|
+
* Retrieves the {@link set} function for this accessor and binds it to
|
|
342
|
+
* the object from which the descriptor was derived, if that value is set.
|
|
343
|
+
* Otherwise this method is identical to the {@link set} accessor.
|
|
344
|
+
*
|
|
345
|
+
* @returns {function} the setter if one is defined. If possible this
|
|
346
|
+
* setter will be bound the associated and previously set `object`.
|
|
347
|
+
*/
|
|
348
|
+
get boundSet(): Function;
|
|
349
|
+
/**
|
|
350
|
+
* The function checks the descriptor's associated object has been set on this
|
|
351
|
+
* instance of `Descriptor`.
|
|
352
|
+
*
|
|
353
|
+
* @returns {boolean} `true` if the `object` property has been set,
|
|
354
|
+
* `false` otherwise
|
|
355
|
+
*/
|
|
356
|
+
get hasObject(): boolean;
|
|
357
|
+
/**
|
|
358
|
+
* Sets the descriptor's associated `object` value. This is usually the
|
|
359
|
+
* parent object from which the descriptor was derived.
|
|
360
|
+
*
|
|
361
|
+
* @param {object} value sets the object for which this descriptor is to
|
|
362
|
+
* be associated with.
|
|
363
|
+
*/
|
|
364
|
+
set object(value: object);
|
|
365
|
+
/**
|
|
366
|
+
* Returns the descriptor's associated `object` value. This is usually the
|
|
367
|
+
* parent object from which the descriptor was derived. If the value is preset
|
|
368
|
+
* it will be returned. Undefined will be returned otherwise
|
|
369
|
+
*
|
|
370
|
+
* @returns {object} the associated object for this descriptor or undefined
|
|
371
|
+
* if it has not yet been set.
|
|
372
|
+
*/
|
|
373
|
+
get object(): object;
|
|
374
|
+
/**
|
|
375
|
+
* Take the descriptor defined by this objects values and apply them to
|
|
376
|
+
* the specified object using the specified key.
|
|
377
|
+
*
|
|
378
|
+
* @param {object} object the object to apply this descriptor to
|
|
379
|
+
* @param {string|symbol} forKey the string or symbol for which this
|
|
380
|
+
* descriptor will abe applied
|
|
381
|
+
*/
|
|
382
|
+
applyTo(object: object, forKey: string | symbol): object;
|
|
383
|
+
/**
|
|
384
|
+
* Converts this descriptor object into a base representation
|
|
385
|
+
*
|
|
386
|
+
* @param {string} hint one of `string`, `number` or default;
|
|
387
|
+
* @returns if the hint is 'string', then a string identifying the enum
|
|
388
|
+
* and its type is returned. `number` will always be NaN since it is incoret
|
|
389
|
+
*/
|
|
390
|
+
[Symbol.toPrimitive](hint: string): string | number | object | undefined;
|
|
391
|
+
/**
|
|
392
|
+
* Ensures that the constructor of this object instance's name
|
|
393
|
+
* is returned if the string tag for this instance is queried
|
|
394
|
+
*
|
|
395
|
+
* @returns {string} the name of the class
|
|
396
|
+
*/
|
|
397
|
+
get [Symbol.toStringTag](): string;
|
|
398
|
+
#private;
|
|
399
|
+
}
|
|
400
|
+
export const DescriptorExtensions: Extension;
|
|
401
|
+
import { Extension } from '@nejs/extension';
|
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import { Extension } from '@nejs/extension';
|
|
2
|
-
import { ObjectExtensions } from '
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
import { ObjectExtensions } from '../objectextensions.js';
|
|
3
|
+
import { ReflectExtensions } from '../reflectextensions.js';
|
|
4
|
+
const { isObject, isValidKey } = ObjectExtensions.patches;
|
|
5
|
+
const { hasSome } = ReflectExtensions.patches;
|
|
6
|
+
export class Descriptor {
|
|
7
|
+
/**
|
|
8
|
+
* The default private descriptor value is that of `enigmatic`
|
|
9
|
+
*
|
|
10
|
+
* @private
|
|
11
|
+
* @type {object}
|
|
12
|
+
*/
|
|
13
|
+
#desc = undefined;
|
|
14
|
+
/**
|
|
15
|
+
* An optionally associated object, usually the parent from which
|
|
16
|
+
* the descriptor was taken, or undefined if none was able to be
|
|
17
|
+
* derived.
|
|
18
|
+
*
|
|
19
|
+
* @type {object}
|
|
20
|
+
*/
|
|
21
|
+
#object = undefined;
|
|
11
22
|
/**
|
|
12
23
|
* Creates a new instance of Descriptor either from another object or
|
|
13
24
|
* around the supplied object descriptor value.
|
|
@@ -18,11 +29,19 @@ class Descriptor {
|
|
|
18
29
|
* on the aforesupplied object.
|
|
19
30
|
*/
|
|
20
31
|
constructor(object, key) {
|
|
21
|
-
|
|
22
|
-
|
|
32
|
+
if ((object ?? key) === undefined) {
|
|
33
|
+
this.#desc = Descriptor.enigmatic;
|
|
34
|
+
}
|
|
35
|
+
if (Descriptor.isDescriptor(object)) {
|
|
36
|
+
this.#desc = object;
|
|
37
|
+
this.#object = undefined;
|
|
38
|
+
}
|
|
39
|
+
else if (isObject(object) && isValidKey(key)) {
|
|
23
40
|
this.#desc = Object.getOwnPropertyDescriptor(object, key);
|
|
41
|
+
this.#object = object;
|
|
24
42
|
}
|
|
25
43
|
if (!this.isDescriptor) {
|
|
44
|
+
console.error('Current descriptor:', this.#desc);
|
|
26
45
|
throw new Error(`Not a valid descriptor:`, this.#desc);
|
|
27
46
|
}
|
|
28
47
|
}
|
|
@@ -142,6 +161,17 @@ class Descriptor {
|
|
|
142
161
|
get get() {
|
|
143
162
|
return this.#desc?.get;
|
|
144
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Retrieves the {@link get} function for this accessor and binds it to
|
|
166
|
+
* the object from which the descriptor was derived, if that value is set.
|
|
167
|
+
* Otherwise this method is identical to the {@link get} accessor.
|
|
168
|
+
*
|
|
169
|
+
* @returns {function} the getter if one is defined. If possible this
|
|
170
|
+
* getter will be bound the associated and previously set `object`.
|
|
171
|
+
*/
|
|
172
|
+
get boundGet() {
|
|
173
|
+
return (isObject(this.#object) ? this.get?.bind(this.#object) : this.get);
|
|
174
|
+
}
|
|
145
175
|
/**
|
|
146
176
|
* Sets the `get` value of this object. If the internal descriptor
|
|
147
177
|
* store is invalid, the value is thrown away
|
|
@@ -160,7 +190,18 @@ class Descriptor {
|
|
|
160
190
|
* is undefined.
|
|
161
191
|
*/
|
|
162
192
|
get set() {
|
|
163
|
-
return this.#desc
|
|
193
|
+
return (this.#desc || {}).set;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Retrieves the {@link set} function for this accessor and binds it to
|
|
197
|
+
* the object from which the descriptor was derived, if that value is set.
|
|
198
|
+
* Otherwise this method is identical to the {@link set} accessor.
|
|
199
|
+
*
|
|
200
|
+
* @returns {function} the setter if one is defined. If possible this
|
|
201
|
+
* setter will be bound the associated and previously set `object`.
|
|
202
|
+
*/
|
|
203
|
+
get boundSet() {
|
|
204
|
+
return (isObject(this.#object) ? this.set?.bind(this.#object) : this.set);
|
|
164
205
|
}
|
|
165
206
|
/**
|
|
166
207
|
* Sets the `set` value of this object. If the internal descriptor
|
|
@@ -171,6 +212,49 @@ class Descriptor {
|
|
|
171
212
|
set set(value) {
|
|
172
213
|
(this.#desc || {}).set = value;
|
|
173
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* The function checks the descriptor's associated object has been set on this
|
|
217
|
+
* instance of `Descriptor`.
|
|
218
|
+
*
|
|
219
|
+
* @returns {boolean} `true` if the `object` property has been set,
|
|
220
|
+
* `false` otherwise
|
|
221
|
+
*/
|
|
222
|
+
get hasObject() { return isObject(this.#object); }
|
|
223
|
+
/**
|
|
224
|
+
* Returns the descriptor's associated `object` value. This is usually the
|
|
225
|
+
* parent object from which the descriptor was derived. If the value is preset
|
|
226
|
+
* it will be returned. Undefined will be returned otherwise
|
|
227
|
+
*
|
|
228
|
+
* @returns {object} the associated object for this descriptor or undefined
|
|
229
|
+
* if it has not yet been set.
|
|
230
|
+
*/
|
|
231
|
+
get object() { return this.#object; }
|
|
232
|
+
/**
|
|
233
|
+
* Sets the descriptor's associated `object` value. This is usually the
|
|
234
|
+
* parent object from which the descriptor was derived.
|
|
235
|
+
*
|
|
236
|
+
* @param {object} value sets the object for which this descriptor is to
|
|
237
|
+
* be associated with.
|
|
238
|
+
*/
|
|
239
|
+
set object(value) { this.#object = Object(value); }
|
|
240
|
+
/**
|
|
241
|
+
* The function returns a string representation of a descriptor object with
|
|
242
|
+
* additional information about its type when used in the NodeJS repl.
|
|
243
|
+
*
|
|
244
|
+
* @param {number} depth - The `depth` parameter is used to specify the
|
|
245
|
+
* maximum depth to which nested objects and arrays will be formatted. If
|
|
246
|
+
* the depth is exceeded, the output will be truncated with ellipses.
|
|
247
|
+
* @param {object} options - The `options` parameter is an object that
|
|
248
|
+
* contains various configuration options for the `inspect` function.
|
|
249
|
+
* These options can be used to customize the output of the inspection.
|
|
250
|
+
* @param {function} inspect - The `inspect` parameter is a function that
|
|
251
|
+
* is used to convert an object into a string representation. It is
|
|
252
|
+
* typically used for debugging purposes or when displaying an object's
|
|
253
|
+
* properties.
|
|
254
|
+
* @returns a string that represents a descriptor. The string includes the
|
|
255
|
+
* type of the descriptor (either "Accessor" or "Data") and the result of
|
|
256
|
+
* inspecting the descriptor object using the provided options and depth.
|
|
257
|
+
*/
|
|
174
258
|
[Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
|
|
175
259
|
const type = this.isAccessor ? ' (Accessor)' : this.isData ? ' (Data)' : '';
|
|
176
260
|
return `Descriptor${type} ${inspect(this.#desc, { ...options, depth })}`;
|
|
@@ -183,11 +267,13 @@ class Descriptor {
|
|
|
183
267
|
* object to return a descriptor for.
|
|
184
268
|
* @returns an object descriptor for the requested field or null
|
|
185
269
|
*/
|
|
186
|
-
static for(object, key) {
|
|
187
|
-
if (!isObject(object)
|
|
270
|
+
static for(object, key, wrap = false) {
|
|
271
|
+
if (!isObject(object) || !isValidKey(key) || !Reflect.has(object, key)) {
|
|
188
272
|
return null;
|
|
189
273
|
}
|
|
190
|
-
return
|
|
274
|
+
return (wrap
|
|
275
|
+
? new Descriptor(Object.getOwnPropertyDescriptor(object, key))
|
|
276
|
+
: Object.getOwnPropertyDescriptor(object, key));
|
|
191
277
|
}
|
|
192
278
|
/**
|
|
193
279
|
* Take the descriptor defined by this objects values and apply them to
|
|
@@ -245,26 +331,23 @@ class Descriptor {
|
|
|
245
331
|
* The function `getData` retrieves the value of a property from an object
|
|
246
332
|
* if it exists and is a data property.
|
|
247
333
|
*
|
|
248
|
-
* @param object - The "object" parameter is the object from which
|
|
249
|
-
* retrieve data.
|
|
250
|
-
* @param property - The `property` parameter is the name of
|
|
251
|
-
* you want to retrieve the data from.
|
|
334
|
+
* @param {object} object - The "object" parameter is the object from which
|
|
335
|
+
* we want to retrieve data.
|
|
336
|
+
* @param {string|symbol} property - The `property` parameter is the name of
|
|
337
|
+
* the property that you want to retrieve the data from.
|
|
252
338
|
* @returns either the value of the specified property if it exists and is
|
|
253
339
|
* a data property, or undefined if the property does not exist or is not
|
|
254
340
|
* a data property.
|
|
255
341
|
*/
|
|
256
342
|
static getData(object, property) {
|
|
257
|
-
if (!isObject(object) || !
|
|
258
|
-
return
|
|
343
|
+
if (!isObject(object) || !Reflect.has(object, property)) {
|
|
344
|
+
return undefined;
|
|
259
345
|
}
|
|
260
|
-
const
|
|
261
|
-
if (
|
|
262
|
-
|
|
263
|
-
if (Descriptor.isData(descriptor)) {
|
|
264
|
-
return descriptor.value;
|
|
265
|
-
}
|
|
346
|
+
const descriptor = Descriptor.for(object, property, true);
|
|
347
|
+
if (!descriptor.isData) {
|
|
348
|
+
return null;
|
|
266
349
|
}
|
|
267
|
-
return
|
|
350
|
+
return descriptor.value;
|
|
268
351
|
}
|
|
269
352
|
/**
|
|
270
353
|
* The function `getAccessor` checks if an object has a getter/setter accessor
|
|
@@ -282,35 +365,14 @@ class Descriptor {
|
|
|
282
365
|
* returns undefined.
|
|
283
366
|
*/
|
|
284
367
|
static getAccessor(object, property) {
|
|
285
|
-
if (!isObject(object))
|
|
368
|
+
if (!isObject(object) || !Reflect.has(object, property)) {
|
|
369
|
+
return undefined;
|
|
370
|
+
}
|
|
371
|
+
const descriptor = Descriptor.for(object, property, true);
|
|
372
|
+
if (!descriptor.isAccessor) {
|
|
286
373
|
return null;
|
|
287
|
-
const [GETTER, SETTER, OBJECT] = [0, 1, 2];
|
|
288
|
-
const results = [undefined, undefined, undefined];
|
|
289
|
-
const descriptors = this.all(object);
|
|
290
|
-
const isDescriptor = Descriptor.isDescriptor(object);
|
|
291
|
-
if (descriptors.has(property) || isDescriptor) {
|
|
292
|
-
const descriptor = isDescriptor ? object : descriptors.get(property);
|
|
293
|
-
if (Descriptor.isAccessor(descriptor)) {
|
|
294
|
-
results[OBJECT] = descriptors.object(property);
|
|
295
|
-
results[GETTER] = descriptor?.get;
|
|
296
|
-
results[SETTER] = descriptor?.set;
|
|
297
|
-
Object.assign(results, {
|
|
298
|
-
get() { this[GETTER].bind(this[OBJECT])(); },
|
|
299
|
-
set(value) { this[SETTER].bind(this[OBJECT])(value); },
|
|
300
|
-
get accessor() { return true; },
|
|
301
|
-
get descriptor() { return descriptor; },
|
|
302
|
-
get boundDescriptor() {
|
|
303
|
-
return {
|
|
304
|
-
...descriptor,
|
|
305
|
-
get: descriptor.get?.bind(object),
|
|
306
|
-
set: descriptor.set?.bind(object),
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
return results;
|
|
311
|
-
}
|
|
312
374
|
}
|
|
313
|
-
return
|
|
375
|
+
return descriptor.get.bind(object)();
|
|
314
376
|
}
|
|
315
377
|
/**
|
|
316
378
|
* The function returns an object with enumerable and configurable properties
|
|
@@ -379,8 +441,11 @@ class Descriptor {
|
|
|
379
441
|
};
|
|
380
442
|
}
|
|
381
443
|
/**
|
|
382
|
-
* The function checks if an object is a
|
|
383
|
-
* JavaScript.
|
|
444
|
+
* The function checks if an object is a likely an object descriptor in
|
|
445
|
+
* JavaScript. This is determined as an object with some of the known
|
|
446
|
+
* descriptor keys (e.g. enumerable, configurable, value, writable, get,
|
|
447
|
+
* or set). Technically, any object could serve as a descriptor but this
|
|
448
|
+
* function only returns true if known descriptor keys are found.
|
|
384
449
|
*
|
|
385
450
|
* @param object - The `object` parameter is the object that we want to
|
|
386
451
|
* check if it is a descriptor.
|
|
@@ -397,6 +462,8 @@ class Descriptor {
|
|
|
397
462
|
/**
|
|
398
463
|
* The function checks if a given property or descriptor is a data property.
|
|
399
464
|
*
|
|
465
|
+
* brie
|
|
466
|
+
*
|
|
400
467
|
* @param descriptor_orProp - The `descriptor_orProp` parameter can be
|
|
401
468
|
* either a descriptor or a property name.
|
|
402
469
|
* @param object - The `object` parameter is the object that you want to
|
|
@@ -411,12 +478,9 @@ class Descriptor {
|
|
|
411
478
|
const descriptor = (needsDescriptor
|
|
412
479
|
? Descriptor.for(object_orProp, property)
|
|
413
480
|
: object_orProp);
|
|
414
|
-
const {
|
|
481
|
+
const { DATA_KEYS } = this;
|
|
415
482
|
let validData = false;
|
|
416
|
-
if (hasSome(descriptor,
|
|
417
|
-
validData = false;
|
|
418
|
-
}
|
|
419
|
-
else if (hasSome(descriptor, DATA_KEYS)) {
|
|
483
|
+
if (hasSome(descriptor, DATA_KEYS)) {
|
|
420
484
|
validData = true;
|
|
421
485
|
}
|
|
422
486
|
return validData;
|
|
@@ -439,12 +503,9 @@ class Descriptor {
|
|
|
439
503
|
const descriptor = (needsDescriptor
|
|
440
504
|
? Descriptor.for(object_orProp, property)
|
|
441
505
|
: object_orProp);
|
|
442
|
-
const { ACCESSOR_KEYS
|
|
506
|
+
const { ACCESSOR_KEYS } = this;
|
|
443
507
|
let validAccessor = false;
|
|
444
|
-
if (hasSome(descriptor,
|
|
445
|
-
validAccessor = false;
|
|
446
|
-
}
|
|
447
|
-
else if (hasSome(descriptor, ACCESSOR_KEYS)) {
|
|
508
|
+
if (hasSome(descriptor, ACCESSOR_KEYS)) {
|
|
448
509
|
validAccessor = true;
|
|
449
510
|
}
|
|
450
511
|
return validAccessor;
|
|
@@ -515,3 +576,4 @@ class Descriptor {
|
|
|
515
576
|
}
|
|
516
577
|
}
|
|
517
578
|
export const DescriptorExtensions = new Extension(Descriptor);
|
|
579
|
+
//# sourceMappingURL=descriptor.js.map
|