@nejs/basic-extensions 1.0.0 → 1.2.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.
@@ -0,0 +1,484 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _a, _Descriptor_desc;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.DescriptorExtension = void 0;
16
+ const extension_1 = require("@nejs/extension");
17
+ const objectextensions_js_1 = require("./objectextensions.js");
18
+ const reflectextensions_js_1 = require("./reflectextensions.js");
19
+ const isObject = objectextensions_js_1.ObjectExtensions.patchEntries.isObject.computed;
20
+ const isValidKey = objectextensions_js_1.ObjectExtensions.patchEntries.isValidKey.computed;
21
+ const isString = objectextensions_js_1.ObjectExtensions.patchEntries.isString.computed;
22
+ const hasSome = reflectextensions_js_1.ReflectExtensions.patchEntries.hasSome.computed;
23
+ class Descriptor {
24
+ /**
25
+ * Creates a new instance of Descriptor either from another object or
26
+ * around the supplied object descriptor value.
27
+ *
28
+ * @param {object} object either an object descriptor or the object
29
+ * from which to get the descriptor
30
+ * @param {symbol|string} key a valid key for accessing the descriptor
31
+ * on the aforesupplied object.
32
+ */
33
+ constructor(object, key) {
34
+ _Descriptor_desc.set(this, _a.enigmatic
35
+ /**
36
+ * Creates a new instance of Descriptor either from another object or
37
+ * around the supplied object descriptor value.
38
+ *
39
+ * @param {object} object either an object descriptor or the object
40
+ * from which to get the descriptor
41
+ * @param {symbol|string} key a valid key for accessing the descriptor
42
+ * on the aforesupplied object.
43
+ */
44
+ );
45
+ __classPrivateFieldSet(this, _Descriptor_desc, object, "f");
46
+ if (object && key) {
47
+ __classPrivateFieldSet(this, _Descriptor_desc, Object.getOwnPropertyDescriptor(object, key), "f");
48
+ }
49
+ if (!_a.isDescriptor(__classPrivateFieldGet(this, _Descriptor_desc, "f"))) {
50
+ throw new Error(`Not a valid descriptor:`, __classPrivateFieldGet(this, _Descriptor_desc, "f"));
51
+ }
52
+ }
53
+ /**
54
+ * Getter around the `configurable` object descriptor property of
55
+ * this instance of Descriptor.
56
+ *
57
+ * @returns {boolean} a boolean value or undefined if the internal
58
+ * descriptor store is invalid.
59
+ */
60
+ get configurable() {
61
+ return !!__classPrivateFieldGet(this, _Descriptor_desc, "f")?.configurable ?? undefined;
62
+ }
63
+ /**
64
+ * Sets the `configurable` value of this object. If the internal descriptor
65
+ * store store is invalid, the value is thrown away
66
+ *
67
+ * @param {boolean} value the value to set for the `configurable` descriptor
68
+ * property. If this value is not a `boolean` it will be converted to one
69
+ */
70
+ set configurable(value) {
71
+ (__classPrivateFieldGet(this, _Descriptor_desc, "f") || {}).configurable = !!value;
72
+ }
73
+ /**
74
+ * Getter around the `enumerable` object descriptor property of
75
+ * this instance of Descriptor.
76
+ *
77
+ * @returns {boolean} a boolean value or undefined if the internal
78
+ * descriptor store is invalid.
79
+ */
80
+ get enumerable() {
81
+ return __classPrivateFieldGet(this, _Descriptor_desc, "f")?.enumerable;
82
+ }
83
+ /**
84
+ * Sets the `enumerable` value of this object. If the internal descriptor
85
+ * store is invalid, the value is thrown away
86
+ *
87
+ * @param {boolean} value the value to set for the `enumerable` descriptor
88
+ * property. If this value is not a `boolean` it will be converted to one
89
+ */
90
+ set enumerable(value) {
91
+ (__classPrivateFieldGet(this, _Descriptor_desc, "f") || {}).enumerable = value;
92
+ }
93
+ /**
94
+ * Getter around the `writable` object descriptor property of
95
+ * this instance of Descriptor.
96
+ *
97
+ * @returns {boolean} a boolean value or undefined if the internal
98
+ * descriptor store is invalid.
99
+ */
100
+ get writable() {
101
+ return __classPrivateFieldGet(this, _Descriptor_desc, "f")?.writable;
102
+ }
103
+ /**
104
+ * Sets the `writable` value of this object. If the internal descriptor
105
+ * store is invalid, the value is thrown away
106
+ *
107
+ * @param {boolean} value the value to set for the `writable` descriptor
108
+ * property. If this value is not a `boolean` it will be converted to one
109
+ */
110
+ set writable(value) {
111
+ (__classPrivateFieldGet(this, _Descriptor_desc, "f") || {}).writable = value;
112
+ }
113
+ /**
114
+ * Getter around the `value` object descriptor property of
115
+ * this instance of Descriptor.
116
+ *
117
+ * @returns {any} any value stored in this descriptor
118
+ */
119
+ get value() {
120
+ return __classPrivateFieldGet(this, _Descriptor_desc, "f")?.value;
121
+ }
122
+ /**
123
+ * Sets the `value` value of this object. If the internal descriptor
124
+ * store is invalid, the value is thrown away
125
+ *
126
+ * @param {any} value the value to set for the `value` descriptor
127
+ * property.
128
+ */
129
+ set value(value) {
130
+ (__classPrivateFieldGet(this, _Descriptor_desc, "f") || {}).value = value;
131
+ }
132
+ /**
133
+ * Getter around the `get` object descriptor property of
134
+ * this instance of Descriptor.
135
+ *
136
+ * @returns {function} a function if the getter for this descriptor is
137
+ * defined or `undefined` if the internal descriptor object or the getter
138
+ * is undefined.
139
+ */
140
+ get get() {
141
+ return __classPrivateFieldGet(this, _Descriptor_desc, "f")?.get;
142
+ }
143
+ /**
144
+ * Sets the `get` value of this object. If the internal descriptor
145
+ * store is invalid, the value is thrown away
146
+ *
147
+ * @param {function} value the getter function for this descriptor
148
+ */
149
+ set get(value) {
150
+ (__classPrivateFieldGet(this, _Descriptor_desc, "f") || {}).get = value;
151
+ }
152
+ /**
153
+ * Getter around the `set` object descriptor property of
154
+ * this instance of Descriptor.
155
+ *
156
+ * @returns {function} a function if the setter for this descriptor is
157
+ * defined or `undefined` if the internal descriptor object or the setter
158
+ * is undefined.
159
+ */
160
+ get set() {
161
+ return __classPrivateFieldGet(this, _Descriptor_desc, "f")?.writable;
162
+ }
163
+ /**
164
+ * Sets the `set` value of this object. If the internal descriptor
165
+ * store is invalid, the value is thrown away
166
+ *
167
+ * @param {function} value the setter function for this descriptor
168
+ */
169
+ set set(value) {
170
+ (__classPrivateFieldGet(this, _Descriptor_desc, "f") || {}).set = value;
171
+ }
172
+ /**
173
+ * Take the descriptor defined by this objects values and apply them to
174
+ * the specified object using the specified key.
175
+ *
176
+ * @param {object} object the object to apply this descriptor to
177
+ * @param {string|symbol} forKey the string or symbol for which this
178
+ * descriptor will abe applied
179
+ */
180
+ applyTo(object, forKey) {
181
+ if (!isObject(object) || !isValidKey(forKey)) {
182
+ throw new Error(`Cannot apply descriptor to non-object or invalid key`);
183
+ }
184
+ Object.defineProperty(object, forKey, __classPrivateFieldGet(this, _Descriptor_desc, "f"));
185
+ }
186
+ /**
187
+ * Converts this descriptor object into a base representation
188
+ *
189
+ * @param {string} hint one of `string`, `number` or default;
190
+ * @returns if the hint is 'string', then a string identifying the enum
191
+ * and its type is returned. `number` will always be NaN since it is incoret
192
+ */
193
+ [(_Descriptor_desc = new WeakMap(), Symbol.toPrimitive)](hint) {
194
+ switch (hint) {
195
+ case 'string':
196
+ if (this.isAccessor) {
197
+ const hasGetter = Reflect.has(__classPrivateFieldGet(this, _Descriptor_desc, "f"), 'get') ? `getter` : '';
198
+ const hasSetter = Reflect.has(__classPrivateFieldGet(this, _Descriptor_desc, "f"), 'set') ? `setter` : '';
199
+ const separator = hasGetter && hasSetter ? ', ' : '';
200
+ return `Accessor (${hasGetter}${separator}${hasSetter})`;
201
+ }
202
+ else if (this.isData) {
203
+ const hasGetter = Reflect.has(__classPrivateFieldGet(this, _Descriptor_desc, "f"), 'value') ? `value` : '';
204
+ const hasSetter = Reflect.has(__classPrivateFieldGet(this, _Descriptor_desc, "f"), 'writable') ? `writable` : '';
205
+ const separator = hasGetter && hasSetter ? ', ' : '';
206
+ return `Data (${hasGetter}${separator}${hasSetter})`;
207
+ }
208
+ break;
209
+ case 'number':
210
+ return NaN;
211
+ default:
212
+ return __classPrivateFieldGet(this, _Descriptor_desc, "f");
213
+ }
214
+ }
215
+ /**
216
+ * The function `getData` retrieves the value of a property from an object if it
217
+ * exists and is a data property.
218
+ *
219
+ * @param object - The "object" parameter is the object from which we want to
220
+ * retrieve data.
221
+ * @param property - The `property` parameter is the name of the property that
222
+ * you want to retrieve the data from.
223
+ * @returns either the value of the specified property if it exists and is a data
224
+ * property, or undefined if the property does not exist or is not a data
225
+ * property.
226
+ */
227
+ static getData(object, property) {
228
+ if (!isObject(object) || !isString(property)) {
229
+ return null;
230
+ }
231
+ const descriptors = _a.all(object);
232
+ if (descriptors.has(property)) {
233
+ const descriptor = descriptors.get(property);
234
+ if (_a.isData(descriptor)) {
235
+ return descriptor.value;
236
+ }
237
+ }
238
+ return undefined;
239
+ }
240
+ /**
241
+ * The function `getAccessor` checks if an object has a getter/setter accessor
242
+ * for a given property and returns the accessor functions if found.
243
+ *
244
+ * @param object - The `object` parameter is the object from which we want to
245
+ * retrieve the accessor for a specific property.
246
+ * @param property - The `property` parameter is the name of the property for
247
+ * which we want to get the accessor.
248
+ * @returns an object that contains the getter and setter functions for the
249
+ * specified property of the given object. If the property is an accessor
250
+ * property (defined with a getter and/or setter), the returned object will also
251
+ * have additional properties such as "accessor" and "descriptor". If the
252
+ * property is not found or is not an accessor property, the function returns
253
+ * undefined.
254
+ */
255
+ static getAccessor(object, property) {
256
+ if (!isObject(object))
257
+ return null;
258
+ const [GETTER, SETTER, OBJECT] = [0, 1, 2];
259
+ const results = [undefined, undefined, undefined];
260
+ const descriptors = this.all(object);
261
+ const isDescriptor = _a.isDescriptor(object);
262
+ if (descriptors.has(property) || isDescriptor) {
263
+ const descriptor = isDescriptor ? object : descriptors.get(property);
264
+ if (_a.isAccessor(descriptor)) {
265
+ results[OBJECT] = descriptors.object(property);
266
+ results[GETTER] = descriptor?.get;
267
+ results[SETTER] = descriptor?.set;
268
+ Object.assign(results, {
269
+ get() { this[GETTER].bind(this[OBJECT])(); },
270
+ set(value) { this[SETTER].bind(this[OBJECT])(value); },
271
+ get accessor() { return true; },
272
+ get descriptor() { return descriptor; },
273
+ get boundDescriptor() {
274
+ return {
275
+ ...descriptor,
276
+ get: descriptor.get?.bind(object),
277
+ set: descriptor.set?.bind(object),
278
+ };
279
+ }
280
+ });
281
+ return results;
282
+ }
283
+ }
284
+ return undefined;
285
+ }
286
+ /**
287
+ * The function returns an object with enumerable and configurable properties
288
+ * based on the input parameters.
289
+ *
290
+ * @param [enumerable=false] - A boolean value indicating whether the property
291
+ * can be enumerated (listed) when iterating over the object's properties.
292
+ * @param [configurable=false] - The `configurable` parameter determines whether
293
+ * the property can be deleted or its attributes can be modified. If
294
+ * `configurable` is set to `true`, the property can be deleted and its
295
+ * attributes can be changed. If `configurable` is set to `false`, the property
296
+ * cannot be deleted and
297
+ * @returns An object with the properties `enumerable` and `configurable` is
298
+ * being returned. The values of these properties are determined by the arguments
299
+ * passed to the `base` function.
300
+ */
301
+ static base(enumerable = false, configurable = false) {
302
+ return {
303
+ enumerable,
304
+ configurable
305
+ };
306
+ }
307
+ /**
308
+ * The function "newAccessor" creates a new property descriptor object with a
309
+ * getter and setter function, along with optional enumerable and configurable
310
+ * flags.
311
+ *
312
+ * @param getter - The getter parameter is a function that will be used as the
313
+ * getter for the property. It will be called when the property is accessed.
314
+ * @param setter - The `setter` parameter is a function that will be used as the
315
+ * setter for the property. It will be called whenever the property is assigned a
316
+ * new value.
317
+ * @param [] - - `getter`: A function that will be used as the getter for the
318
+ * property.
319
+ * @returns an object with properties "get", "set", "enumerable", and
320
+ * "configurable".
321
+ */
322
+ static accessor(getter, setter, { enumerable, configurable } = _a.base()) {
323
+ return {
324
+ get: getter,
325
+ set: setter,
326
+ enumerable,
327
+ configurable
328
+ };
329
+ }
330
+ /**
331
+ * The function "newData" creates a new data object with customizable properties.
332
+ *
333
+ * @param value - The value parameter represents the value that will be assigned
334
+ * to the property.
335
+ * @param [writable=true] - The `writable` parameter determines whether the value
336
+ * of the property can be changed. If `writable` is set to `true`, the value can
337
+ * be changed. If `writable` is set to `false`, the value cannot be changed.
338
+ * @param [] - - `value`: The value to be assigned to the property.
339
+ * @returns an object with properties `value`, `enumerable`, `writable`, and
340
+ * `configurable`.
341
+ */
342
+ static data(value, writable = true, { enumerable, configurable } = _a.base()) {
343
+ return {
344
+ value,
345
+ enumerable,
346
+ writable,
347
+ configurable
348
+ };
349
+ }
350
+ /**
351
+ * The function checks if an object is a valid object descriptor in JavaScript.
352
+ *
353
+ * @param object - The `object` parameter is the object that we want to check if
354
+ * it is a descriptor.
355
+ * @returns a boolean value.
356
+ */
357
+ static isDescriptor(object) {
358
+ const knownKeys = [
359
+ ..._a.SHARED_KEYS,
360
+ ..._a.ACCESSOR_KEYS,
361
+ ..._a.DATA_KEYS,
362
+ ];
363
+ let isa = (hasSome(knownKeys) && (_a.isAccessor(object) ||
364
+ _a.isData(object)));
365
+ return isa;
366
+ }
367
+ /**
368
+ * The function checks if a given property or descriptor is a data property.
369
+ *
370
+ * @param descriptor_orProp - The `descriptor_orProp` parameter can be either a
371
+ * descriptor or a property name.
372
+ * @param object - The `object` parameter is the object that you want to check
373
+ * for data properties.
374
+ * @returns a boolean value. It returns `true` if the `descriptor` object has any
375
+ * keys that match the `DATA_KEYS` array, otherwise it returns `false`.
376
+ */
377
+ static isData(object_orProp, property) {
378
+ const needsDescriptor = (((typeof object_orProp === 'object') || object_orProp instanceof Object) &&
379
+ property instanceof String);
380
+ const descriptor = (needsDescriptor
381
+ ? _a.for(object_orProp, property)
382
+ : object_orProp);
383
+ const { ACCESSOR_KEYS, DATA_KEYS } = this;
384
+ let validData = false;
385
+ if (hasSome(descriptor, ACCESSOR_KEYS)) {
386
+ validData = false;
387
+ }
388
+ else if (hasSome(descriptor, DATA_KEYS)) {
389
+ validData = true;
390
+ }
391
+ return false;
392
+ }
393
+ /**
394
+ * The function checks if a given property descriptor or property of an object is
395
+ * an accessor.
396
+ *
397
+ * @param object_orProp - The `descriptor_orProp` parameter can be either a
398
+ * descriptor object or a property name.
399
+ * @param property - The `object` parameter is the object that you want to check
400
+ * for accessor properties.
401
+ * @returns a boolean value. It returns true if the descriptor or property passed
402
+ * as an argument is an accessor descriptor, and false otherwise.
403
+ */
404
+ static isAccessor(object_orProp, property) {
405
+ const needsDescriptor = ((object_orProp && property) &&
406
+ ((typeof object_orProp === 'object') || object_orProp instanceof Object) &&
407
+ (property instanceof String || (typeof property === 'symbol')));
408
+ const descriptor = (needsDescriptor
409
+ ? _a.for(object_orProp, property)
410
+ : object_orProp);
411
+ const { ACCESSOR_KEYS, DATA_KEYS } = this;
412
+ let validAccessor = false;
413
+ if (hasSome(descriptor, DATA_KEYS)) {
414
+ validAccessor = false;
415
+ }
416
+ else if (hasSome(descriptor, ACCESSOR_KEYS)) {
417
+ validAccessor = true;
418
+ }
419
+ return validAccessor;
420
+ }
421
+ /**
422
+ * A base descriptor (new for each read) that is both enumerable and configurable
423
+ *
424
+ * @returns The method `flexible` is returning the result of calling the `base`
425
+ * method with the arguments `true` and `true`.
426
+ */
427
+ static get flexible() {
428
+ return this.base(true, true);
429
+ }
430
+ /**
431
+ * A base descriptor (new for each read) that is not enumerable but is configurable
432
+ *
433
+ * @returns The method `enigmatic` is returning the result of calling the `base`
434
+ * method with the arguments `false` and `true`.
435
+ */
436
+ static get enigmatic() {
437
+ return this.base(false, true);
438
+ }
439
+ /**
440
+ * A base descriptor (new for each read) that is neither enumerable nor configurable
441
+ *
442
+ * @returns The code is returning the result of calling the `base` method with
443
+ * the arguments `false` and `false`.
444
+ */
445
+ static get intrinsic() {
446
+ return this.base(false, false);
447
+ }
448
+ /**
449
+ * A base descriptor (new for each read) that enumerable but not configurable
450
+ *
451
+ * @returns The method is returning the result of calling the `base` method with
452
+ * the arguments `true` and `false`.
453
+ */
454
+ static get transparent() {
455
+ return this.base(true, false);
456
+ }
457
+ /**
458
+ * The function returns an array of shared descriptor keys.
459
+ *
460
+ * @returns An array containing the strings 'configurable' and 'enumerable'.
461
+ */
462
+ static get SHARED_KEYS() {
463
+ return ['configurable', 'enumerable'];
464
+ }
465
+ /**
466
+ * The function returns an array of accessor descriptor keys.
467
+ *
468
+ * @returns An array containing the strings 'get' and 'set' is being returned.
469
+ */
470
+ static get ACCESSOR_KEYS() {
471
+ return ['get', 'set'];
472
+ }
473
+ /**
474
+ * The function returns an array of data descriptor keys.
475
+ *
476
+ * @returns An array containing the strings 'value' and 'writable' is being
477
+ * returned.
478
+ */
479
+ static get DATA_KEYS() {
480
+ return ['value', 'writable'];
481
+ }
482
+ }
483
+ _a = Descriptor;
484
+ exports.DescriptorExtension = new extension_1.Extension(Descriptor);
@@ -3,5 +3,7 @@ export function disableAll(owners: any): void;
3
3
  import { ObjectExtensions } from './objectextensions.js';
4
4
  import { FunctionExtensions } from './functionextensions.js';
5
5
  import { ReflectExtensions } from './reflectextensions.js';
6
+ import { StringExtensions } from './stringextensions.js';
7
+ import { SymbolExtensions } from './symbolextensions.js';
6
8
  import { ArrayPrototypeExtensions } from './arrayextensions.js';
7
- export { ObjectExtensions, FunctionExtensions, ReflectExtensions, ArrayPrototypeExtensions };
9
+ export { ObjectExtensions, FunctionExtensions, ReflectExtensions, StringExtensions, SymbolExtensions, ArrayPrototypeExtensions };
package/dist/cjs/index.js CHANGED
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ArrayPrototypeExtensions = exports.ReflectExtensions = exports.FunctionExtensions = exports.ObjectExtensions = exports.disableAll = exports.enableAll = void 0;
3
+ exports.ArrayPrototypeExtensions = exports.SymbolExtensions = exports.StringExtensions = exports.ReflectExtensions = exports.FunctionExtensions = exports.ObjectExtensions = exports.disableAll = exports.enableAll = void 0;
4
4
  const functionextensions_js_1 = require("./functionextensions.js");
5
5
  Object.defineProperty(exports, "FunctionExtensions", { enumerable: true, get: function () { return functionextensions_js_1.FunctionExtensions; } });
6
6
  const objectextensions_js_1 = require("./objectextensions.js");
7
7
  Object.defineProperty(exports, "ObjectExtensions", { enumerable: true, get: function () { return objectextensions_js_1.ObjectExtensions; } });
8
8
  const reflectextensions_js_1 = require("./reflectextensions.js");
9
9
  Object.defineProperty(exports, "ReflectExtensions", { enumerable: true, get: function () { return reflectextensions_js_1.ReflectExtensions; } });
10
+ const stringextensions_js_1 = require("./stringextensions.js");
11
+ Object.defineProperty(exports, "StringExtensions", { enumerable: true, get: function () { return stringextensions_js_1.StringExtensions; } });
12
+ const symbolextensions_js_1 = require("./symbolextensions.js");
13
+ Object.defineProperty(exports, "SymbolExtensions", { enumerable: true, get: function () { return symbolextensions_js_1.SymbolExtensions; } });
10
14
  const arrayextensions_js_1 = require("./arrayextensions.js");
11
15
  Object.defineProperty(exports, "ArrayPrototypeExtensions", { enumerable: true, get: function () { return arrayextensions_js_1.ArrayPrototypeExtensions; } });
12
16
  const extension_1 = require("@nejs/extension");
@@ -14,6 +18,8 @@ const Owners = [
14
18
  Object,
15
19
  Function,
16
20
  Reflect,
21
+ String,
22
+ Symbol,
17
23
  Array.prototype,
18
24
  ];
19
25
  function enableAll(owners) {
@@ -45,7 +45,43 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
45
45
  * @returns {string} - The string tag of the object, indicating its type.
46
46
  */
47
47
  getStringTag(value) {
48
- return /(\w+)]/.exec(Object.prototype.toString.call(value))[1];
48
+ return /\s(.+)]/.exec(Object.prototype.toString.call(value))[1];
49
+ },
50
+ /**
51
+ * Strips an object down to only the keys specified. Optionally, any
52
+ * accessors can be made to retain their context on the source object.
53
+ *
54
+ * @param {object} object the object to pare down
55
+ * @param {Array<string|symbol>} keys the keys that should appear in the
56
+ * final reduced object
57
+ * @param {boolean} [bindAccessors = true] if this value is true
58
+ * then any accessors from the source object will continue to have their
59
+ * `this` value bound to the source. If the getter or setter on that object
60
+ * is defined using an arrow function, this will not work as intended.
61
+ * @returns {object} an object containing only the keys and symbols specified
62
+ * in the `keys` parameter.
63
+ */
64
+ stripTo(object, keys, bindAccessors = true) {
65
+ const result = {};
66
+ if (!Array.isArray(keys)) {
67
+ return result;
68
+ }
69
+ for (let key of keys) {
70
+ if (Reflect.has(object, key)) {
71
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
72
+ if (Reflect.has(descriptor, 'get') || Reflect.has(descriptor, 'set')) {
73
+ if (bindAccessors) {
74
+ descriptor.get = descriptor?.get?.bind(object);
75
+ descriptor.set = descriptor?.set?.bind(object);
76
+ }
77
+ Object.defineProperty(result, descriptor);
78
+ }
79
+ else {
80
+ Object.defineProperty(result, descriptor);
81
+ }
82
+ }
83
+ }
84
+ return result;
49
85
  },
50
86
  /**
51
87
  * Determines the type of the given value based on its string tag. This method
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ReflectExtensions = void 0;
4
4
  const extension_1 = require("@nejs/extension");
5
+ const objectextensions_js_1 = require("./objectextensions.js");
5
6
  /**
6
7
  * The `ReflectExtensions` class is a patch applied to the built-in JavaScript
7
8
  * `Reflect` object. It extends `Reflect` with additional utility methods that
@@ -30,6 +31,25 @@ exports.ReflectExtensions = new extension_1.Patch(Reflect, {
30
31
  .map(key => Reflect.has(object, key))
31
32
  .every(has => has));
32
33
  },
34
+ ownDescriptors(object) {
35
+ const result = {};
36
+ const revertOnDone = () => revertOnDone.doIt ? objectextensions_js_1.ObjectExtensions.revert() : '';
37
+ revertOnDone.doIt = false;
38
+ if (!Object.isObject) {
39
+ revertOnDone.doIt = true;
40
+ objectextensions_js_1.ObjectExtensions.apply();
41
+ }
42
+ if (!Object.isObject(object)) {
43
+ revertOnDone();
44
+ return {};
45
+ }
46
+ const keys = Reflect.ownKeys(object);
47
+ for (const key of keys) {
48
+ result[key] = Object.getOwnPropertyDescriptor(key);
49
+ }
50
+ revertOnDone();
51
+ return result;
52
+ },
33
53
  /**
34
54
  * The function checks if an object has at least one of the specified keys.
35
55
  *
@@ -0,0 +1,10 @@
1
+ /**
2
+ * `StringExtensions` is a patch for the JavaScript built-in `String` class. It
3
+ * adds utility methods to the `String` class without modifying the global namespace
4
+ * directly. This patch includes methods for key validation, object type checking,
5
+ * and retrieving the string tag of an object. These methods are useful for
6
+ * enhancing the capabilities of the standard `String` class with additional
7
+ * utility functions.
8
+ */
9
+ export const StringExtensions: Patch;
10
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringExtensions = void 0;
4
+ const extension_1 = require("@nejs/extension");
5
+ /**
6
+ * `StringExtensions` is a patch for the JavaScript built-in `String` class. It
7
+ * adds utility methods to the `String` class without modifying the global namespace
8
+ * directly. This patch includes methods for key validation, object type checking,
9
+ * and retrieving the string tag of an object. These methods are useful for
10
+ * enhancing the capabilities of the standard `String` class with additional
11
+ * utility functions.
12
+ */
13
+ exports.StringExtensions = new extension_1.Patch(String, {
14
+ /**
15
+ * The `isString` method does exactly what one would it expect. It returns
16
+ * true if the string matches typeof or instanceof as a string.
17
+ *
18
+ * @param {*} value checks to see if the `value` is a string
19
+ * @returns {boolean} `true` if it is a `String`, `false` otherwise
20
+ */
21
+ isString(value) {
22
+ if (value && (typeof value === 'string' || value instanceof String)) {
23
+ return value.length > 0;
24
+ }
25
+ return false;
26
+ },
27
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * `SymbolExtensions` is a patch for the JavaScript built-in `Symbol` class. It
3
+ * adds utility methods to the `Symbol` class without modifying the global namespace
4
+ * directly. This patch includes methods for key validation, object type checking,
5
+ * and retrieving the string tag of an object. These methods are useful for
6
+ * enhancing the capabilities of the standard `Symbol` class with additional
7
+ * utility functions.
8
+ */
9
+ export const SymbolExtensions: Patch;
10
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolExtensions = void 0;
4
+ const extension_1 = require("@nejs/extension");
5
+ /**
6
+ * `SymbolExtensions` is a patch for the JavaScript built-in `Symbol` class. It
7
+ * adds utility methods to the `Symbol` class without modifying the global namespace
8
+ * directly. This patch includes methods for key validation, object type checking,
9
+ * and retrieving the string tag of an object. These methods are useful for
10
+ * enhancing the capabilities of the standard `Symbol` class with additional
11
+ * utility functions.
12
+ */
13
+ exports.SymbolExtensions = new extension_1.Patch(Symbol, {
14
+ /**
15
+ * The `isSymbol` method does exactly what one would it expect. It returns
16
+ * true if the string matches typeof or instanceof as a symbol.
17
+ *
18
+ * @param {*} value checks to see if the `value` is a string
19
+ * @returns {boolean} `true` if it is a `Symbol`, `false` otherwise
20
+ */
21
+ isSymbol(value) {
22
+ if (value && (typeof value === 'symbol')) {
23
+ return true;
24
+ }
25
+ return false;
26
+ },
27
+ });