@nejs/basic-extensions 1.2.0 → 1.4.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.1.3.0.js +2 -0
- package/dist/@nejs/basic-extensions.bundle.1.3.0.js.map +7 -0
- package/dist/cjs/descriptor.js +51 -12
- package/dist/cjs/functionextensions.js +1 -1
- package/dist/cjs/globals.d.ts +2 -0
- package/dist/cjs/globals.js +177 -0
- package/dist/cjs/index.d.ts +4 -1
- package/dist/cjs/index.js +45 -3
- package/dist/cjs/objectextensions.js +71 -45
- package/dist/mjs/descriptor.js +51 -12
- package/dist/mjs/functionextensions.js +1 -1
- package/dist/mjs/globals.d.ts +2 -0
- package/dist/mjs/globals.js +174 -0
- package/dist/mjs/index.d.ts +4 -1
- package/dist/mjs/index.js +43 -3
- package/dist/mjs/objectextensions.js +71 -45
- package/package.json +2 -2
- package/src/descriptor.js +56 -16
- package/src/functionextensions.js +1 -1
- package/src/globals.js +216 -0
- package/src/index.js +47 -4
- package/src/objectextensions.js +75 -46
- package/dist/@nejs/basic-extensions.bundle.1.0.0.js +0 -2
- package/dist/@nejs/basic-extensions.bundle.1.0.0.js.map +0 -7
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ArrayPrototypeExtensions = exports.SymbolExtensions = exports.StringExtensions = exports.ReflectExtensions = exports.FunctionExtensions = exports.ObjectExtensions = exports.disableAll = exports.enableAll = void 0;
|
|
3
|
+
exports.DescriptorExtension = exports.GlobalFunctionsAndProps = exports.ArrayPrototypeExtensions = exports.SymbolExtensions = exports.StringExtensions = exports.ReflectExtensions = exports.FunctionExtensions = exports.ObjectExtensions = exports.all = 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");
|
|
@@ -13,6 +13,10 @@ const symbolextensions_js_1 = require("./symbolextensions.js");
|
|
|
13
13
|
Object.defineProperty(exports, "SymbolExtensions", { enumerable: true, get: function () { return symbolextensions_js_1.SymbolExtensions; } });
|
|
14
14
|
const arrayextensions_js_1 = require("./arrayextensions.js");
|
|
15
15
|
Object.defineProperty(exports, "ArrayPrototypeExtensions", { enumerable: true, get: function () { return arrayextensions_js_1.ArrayPrototypeExtensions; } });
|
|
16
|
+
const descriptor_js_1 = require("./descriptor.js");
|
|
17
|
+
Object.defineProperty(exports, "DescriptorExtension", { enumerable: true, get: function () { return descriptor_js_1.DescriptorExtension; } });
|
|
18
|
+
const globals_js_1 = require("./globals.js");
|
|
19
|
+
Object.defineProperty(exports, "GlobalFunctionsAndProps", { enumerable: true, get: function () { return globals_js_1.GlobalFunctionsAndProps; } });
|
|
16
20
|
const extension_1 = require("@nejs/extension");
|
|
17
21
|
const Owners = [
|
|
18
22
|
Object,
|
|
@@ -22,15 +26,53 @@ const Owners = [
|
|
|
22
26
|
Symbol,
|
|
23
27
|
Array.prototype,
|
|
24
28
|
];
|
|
29
|
+
const NetNew = [
|
|
30
|
+
globals_js_1.GlobalFunctionsAndProps,
|
|
31
|
+
descriptor_js_1.DescriptorExtension,
|
|
32
|
+
];
|
|
25
33
|
function enableAll(owners) {
|
|
26
|
-
|
|
34
|
+
const list = owners || Owners;
|
|
35
|
+
if (!list) {
|
|
36
|
+
throw new Error('Unable to enable features without owners list');
|
|
37
|
+
}
|
|
38
|
+
list.forEach(owner => {
|
|
27
39
|
extension_1.Patch.enableFor(owner);
|
|
28
40
|
});
|
|
41
|
+
NetNew.forEach(extension => {
|
|
42
|
+
extension.apply();
|
|
43
|
+
});
|
|
29
44
|
}
|
|
30
45
|
exports.enableAll = enableAll;
|
|
31
46
|
function disableAll(owners) {
|
|
32
|
-
|
|
47
|
+
const list = owners || Owners;
|
|
48
|
+
if (!list) {
|
|
49
|
+
throw new Error('Unable to disable features without owners list');
|
|
50
|
+
}
|
|
51
|
+
list.forEach(owner => {
|
|
33
52
|
extension_1.Patch.disableFor(owner);
|
|
34
53
|
});
|
|
54
|
+
NetNew.forEach(extension => {
|
|
55
|
+
extension.revert();
|
|
56
|
+
});
|
|
35
57
|
}
|
|
36
58
|
exports.disableAll = disableAll;
|
|
59
|
+
exports.all = (() => {
|
|
60
|
+
let extensions = [
|
|
61
|
+
objectextensions_js_1.ObjectExtensions,
|
|
62
|
+
functionextensions_js_1.FunctionExtensions,
|
|
63
|
+
reflectextensions_js_1.ReflectExtensions,
|
|
64
|
+
stringextensions_js_1.StringExtensions,
|
|
65
|
+
symbolextensions_js_1.SymbolExtensions,
|
|
66
|
+
arrayextensions_js_1.ArrayPrototypeExtensions,
|
|
67
|
+
globals_js_1.GlobalFunctionsAndProps,
|
|
68
|
+
descriptor_js_1.DescriptorExtension,
|
|
69
|
+
];
|
|
70
|
+
const dest = extensions.reduce((accumulator, extension) => {
|
|
71
|
+
Reflect.ownKeys(extension.patchEntries).reduce((_, key) => {
|
|
72
|
+
accumulator[key] = extension.patchEntries[key].computed;
|
|
73
|
+
return accumulator;
|
|
74
|
+
}, accumulator);
|
|
75
|
+
return accumulator;
|
|
76
|
+
}, {});
|
|
77
|
+
return dest;
|
|
78
|
+
})();
|
|
@@ -12,16 +12,45 @@ const extension_1 = require("@nejs/extension");
|
|
|
12
12
|
*/
|
|
13
13
|
exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* Retrieves the string tag of an object. The string tag is a representation of
|
|
16
|
+
* the object's type, as defined by its `Object.prototype.toString` method. This
|
|
17
|
+
* utility method is helpful for getting a more descriptive type of an object than
|
|
18
|
+
* what is returned by the `typeof` operator, especially for custom objects.
|
|
18
19
|
*
|
|
19
|
-
* @param {*} value - The
|
|
20
|
-
* @returns {
|
|
21
|
-
* otherwise `false`.
|
|
20
|
+
* @param {*} value - The object whose string tag is to be retrieved.
|
|
21
|
+
* @returns {string} - The string tag of the object, indicating its type.
|
|
22
22
|
*/
|
|
23
|
-
|
|
24
|
-
return (
|
|
23
|
+
getStringTag(value) {
|
|
24
|
+
return /\s(.+)]/.exec(Object.prototype.toString.call(value))[1];
|
|
25
|
+
},
|
|
26
|
+
/**
|
|
27
|
+
* Determines the type of the given value based on its string tag. This method
|
|
28
|
+
* uses `Object.getStringTag` to obtain the string tag of the value, which
|
|
29
|
+
* represents its more specific type (e.g., Array, Map, Set) rather than just
|
|
30
|
+
* 'object'. The method then maps this string tag to the corresponding type
|
|
31
|
+
* present in the provided `owner` object, which defaults to `globalThis`.
|
|
32
|
+
* This utility method is especially useful for identifying the specific
|
|
33
|
+
* constructor or class of an object, beyond the basic types identified by
|
|
34
|
+
* the `typeof` operator.
|
|
35
|
+
*
|
|
36
|
+
* @param {any} value - The value whose type is to be determined.
|
|
37
|
+
* @param {object} [owner=globalThis] - The object in which to look up the
|
|
38
|
+
* constructor corresponding to the string tag. Defaults to `globalThis`, which
|
|
39
|
+
* covers global constructors like `Array`, `Object`, etc.
|
|
40
|
+
* @returns {Function|object|null|undefined} - Returns the constructor or type
|
|
41
|
+
* of the value based on its string tag. For 'Null' and 'Undefined', it returns
|
|
42
|
+
* `null` and `undefined`, respectively. For other types, it returns the
|
|
43
|
+
* corresponding constructor (e.g., `Array` for arrays) if available in the
|
|
44
|
+
* `owner` object.
|
|
45
|
+
*/
|
|
46
|
+
getType(value, owner = globalThis) {
|
|
47
|
+
const stringTag = Object.getStringTag(value);
|
|
48
|
+
switch (stringTag) {
|
|
49
|
+
case 'Null': return null;
|
|
50
|
+
case 'Undefined': return undefined;
|
|
51
|
+
default:
|
|
52
|
+
return owner[stringTag];
|
|
53
|
+
}
|
|
25
54
|
},
|
|
26
55
|
/**
|
|
27
56
|
* Determines if the provided value is an object. This method checks whether the
|
|
@@ -36,16 +65,42 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
36
65
|
return value && (value instanceof Object || typeof value === 'object');
|
|
37
66
|
},
|
|
38
67
|
/**
|
|
39
|
-
*
|
|
40
|
-
* the object's type, as defined by its `Object.prototype.toString` method. This
|
|
41
|
-
* utility method is helpful for getting a more descriptive type of an object than
|
|
42
|
-
* what is returned by the `typeof` operator, especially for custom objects.
|
|
68
|
+
* Checks to see if the supplied value is a primitive value.
|
|
43
69
|
*
|
|
44
|
-
* @param {
|
|
45
|
-
* @returns
|
|
70
|
+
* @param {any} value the value to test to see if it is a primitive value type
|
|
71
|
+
* @returns true if the object is considered widely to be a primitive value,
|
|
72
|
+
* false otherwise.
|
|
46
73
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
74
|
+
isPrimitive(value) {
|
|
75
|
+
// Check for null as a special case because typeof null
|
|
76
|
+
// is 'object'
|
|
77
|
+
if (value === null) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
// Check for other primitives
|
|
81
|
+
switch (typeof value) {
|
|
82
|
+
case 'string':
|
|
83
|
+
case 'number':
|
|
84
|
+
case 'bigint':
|
|
85
|
+
case 'boolean':
|
|
86
|
+
case 'undefined':
|
|
87
|
+
case 'symbol':
|
|
88
|
+
return true;
|
|
89
|
+
default:
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
/**
|
|
94
|
+
* Checks if the given value is a valid key for an object. In JavaScript, a valid
|
|
95
|
+
* key can be either a string or a symbol. This method is useful for validating
|
|
96
|
+
* object keys before using them in operations like setting or getting object properties.
|
|
97
|
+
*
|
|
98
|
+
* @param {*} value - The value to be checked.
|
|
99
|
+
* @returns {boolean} - Returns `true` if the value is a valid object key (string or symbol),
|
|
100
|
+
* otherwise `false`.
|
|
101
|
+
*/
|
|
102
|
+
isValidKey(value) {
|
|
103
|
+
return (typeof value === 'string' || typeof value === 'symbol');
|
|
49
104
|
},
|
|
50
105
|
/**
|
|
51
106
|
* Strips an object down to only the keys specified. Optionally, any
|
|
@@ -83,33 +138,4 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
83
138
|
}
|
|
84
139
|
return result;
|
|
85
140
|
},
|
|
86
|
-
/**
|
|
87
|
-
* Determines the type of the given value based on its string tag. This method
|
|
88
|
-
* uses `Object.getStringTag` to obtain the string tag of the value, which
|
|
89
|
-
* represents its more specific type (e.g., Array, Map, Set) rather than just
|
|
90
|
-
* 'object'. The method then maps this string tag to the corresponding type
|
|
91
|
-
* present in the provided `owner` object, which defaults to `globalThis`.
|
|
92
|
-
* This utility method is especially useful for identifying the specific
|
|
93
|
-
* constructor or class of an object, beyond the basic types identified by
|
|
94
|
-
* the `typeof` operator.
|
|
95
|
-
*
|
|
96
|
-
* @param {any} value - The value whose type is to be determined.
|
|
97
|
-
* @param {object} [owner=globalThis] - The object in which to look up the
|
|
98
|
-
* constructor corresponding to the string tag. Defaults to `globalThis`, which
|
|
99
|
-
* covers global constructors like `Array`, `Object`, etc.
|
|
100
|
-
* @returns {Function|object|null|undefined} - Returns the constructor or type
|
|
101
|
-
* of the value based on its string tag. For 'Null' and 'Undefined', it returns
|
|
102
|
-
* `null` and `undefined`, respectively. For other types, it returns the
|
|
103
|
-
* corresponding constructor (e.g., `Array` for arrays) if available in the
|
|
104
|
-
* `owner` object.
|
|
105
|
-
*/
|
|
106
|
-
getType(value, owner = globalThis) {
|
|
107
|
-
const stringTag = Object.getStringTag(value);
|
|
108
|
-
switch (stringTag) {
|
|
109
|
-
case 'Null': return null;
|
|
110
|
-
case 'Undefined': return undefined;
|
|
111
|
-
default:
|
|
112
|
-
return owner[stringTag];
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
141
|
});
|
package/dist/mjs/descriptor.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Extension } from '@nejs/extension';
|
|
2
2
|
import { ObjectExtensions } from './objectextensions.js';
|
|
3
|
+
import { StringExtensions } from './stringextensions.js';
|
|
3
4
|
import { ReflectExtensions } from './reflectextensions.js';
|
|
4
|
-
const isObject = ObjectExtensions.patchEntries
|
|
5
|
-
const isValidKey = ObjectExtensions.patchEntries
|
|
6
|
-
const isString =
|
|
7
|
-
const hasSome = ReflectExtensions.patchEntries
|
|
5
|
+
const isObject = ObjectExtensions.patchEntries?.isObject?.computed;
|
|
6
|
+
const isValidKey = ObjectExtensions.patchEntries?.isValidKey?.computed;
|
|
7
|
+
const isString = StringExtensions.patchEntries?.isString?.computed;
|
|
8
|
+
const hasSome = ReflectExtensions.patchEntries?.hasSome?.computed;
|
|
8
9
|
class Descriptor {
|
|
9
10
|
#desc = Descriptor.enigmatic;
|
|
10
11
|
/**
|
|
@@ -18,13 +19,39 @@ class Descriptor {
|
|
|
18
19
|
*/
|
|
19
20
|
constructor(object, key) {
|
|
20
21
|
this.#desc = object;
|
|
21
|
-
if (object && key) {
|
|
22
|
+
if (isObject(object) && isValidKey(key)) {
|
|
22
23
|
this.#desc = Object.getOwnPropertyDescriptor(object, key);
|
|
23
24
|
}
|
|
24
|
-
if (!
|
|
25
|
+
if (!this.isDescriptor) {
|
|
25
26
|
throw new Error(`Not a valid descriptor:`, this.#desc);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Detects whether or not this instance is an accessor object descriptor
|
|
31
|
+
*
|
|
32
|
+
* @returns {boolean} true if this object has a getter or setter and is not
|
|
33
|
+
* a data descriptor
|
|
34
|
+
*/
|
|
35
|
+
get isAccessor() {
|
|
36
|
+
return Descriptor.isAccessor(this.#desc);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Detects whether or not this instance is an data object descriptor
|
|
40
|
+
*
|
|
41
|
+
* @returns {boolean} true if this object has a value property and is not
|
|
42
|
+
* an accessor descriptor
|
|
43
|
+
*/
|
|
44
|
+
get isData() {
|
|
45
|
+
return Descriptor.isData(this.#desc);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Detects whether or not this instance is a valid object descriptor
|
|
49
|
+
*
|
|
50
|
+
* @returns {boolean} true if this descriptor store is a valid descriptor
|
|
51
|
+
*/
|
|
52
|
+
get isDescriptor() {
|
|
53
|
+
return Descriptor.isDescriptor(this.#desc);
|
|
54
|
+
}
|
|
28
55
|
/**
|
|
29
56
|
* Getter around the `configurable` object descriptor property of
|
|
30
57
|
* this instance of Descriptor.
|
|
@@ -33,7 +60,7 @@ class Descriptor {
|
|
|
33
60
|
* descriptor store is invalid.
|
|
34
61
|
*/
|
|
35
62
|
get configurable() {
|
|
36
|
-
return !!this.#desc?.configurable
|
|
63
|
+
return !!this.#desc?.configurable;
|
|
37
64
|
}
|
|
38
65
|
/**
|
|
39
66
|
* Sets the `configurable` value of this object. If the internal descriptor
|
|
@@ -144,6 +171,20 @@ class Descriptor {
|
|
|
144
171
|
set set(value) {
|
|
145
172
|
(this.#desc || {}).set = value;
|
|
146
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Shorthand for Object.getOwnPropertyDescriptor()
|
|
176
|
+
*
|
|
177
|
+
* @param {object} object a non-null object instance
|
|
178
|
+
* @param {string|symbol} key a symbol or string referencing which key on the
|
|
179
|
+
* object to return a descriptor for.
|
|
180
|
+
* @returns an object descriptor for the requested field or null
|
|
181
|
+
*/
|
|
182
|
+
static for(object, key) {
|
|
183
|
+
if (!isObject(object) && !isValidKey(key)) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
return Object.getOwnPropertyDescriptor(object, key);
|
|
187
|
+
}
|
|
147
188
|
/**
|
|
148
189
|
* Take the descriptor defined by this objects values and apply them to
|
|
149
190
|
* the specified object using the specified key.
|
|
@@ -156,7 +197,7 @@ class Descriptor {
|
|
|
156
197
|
if (!isObject(object) || !isValidKey(forKey)) {
|
|
157
198
|
throw new Error(`Cannot apply descriptor to non-object or invalid key`);
|
|
158
199
|
}
|
|
159
|
-
Object.defineProperty(object, forKey, this.#desc);
|
|
200
|
+
return Object.defineProperty(object, forKey, this.#desc);
|
|
160
201
|
}
|
|
161
202
|
/**
|
|
162
203
|
* Converts this descriptor object into a base representation
|
|
@@ -335,9 +376,7 @@ class Descriptor {
|
|
|
335
376
|
...Descriptor.ACCESSOR_KEYS,
|
|
336
377
|
...Descriptor.DATA_KEYS,
|
|
337
378
|
];
|
|
338
|
-
|
|
339
|
-
Descriptor.isData(object)));
|
|
340
|
-
return isa;
|
|
379
|
+
return hasSome(object, knownKeys);
|
|
341
380
|
}
|
|
342
381
|
/**
|
|
343
382
|
* The function checks if a given property or descriptor is a data property.
|
|
@@ -363,7 +402,7 @@ class Descriptor {
|
|
|
363
402
|
else if (hasSome(descriptor, DATA_KEYS)) {
|
|
364
403
|
validData = true;
|
|
365
404
|
}
|
|
366
|
-
return
|
|
405
|
+
return validData;
|
|
367
406
|
}
|
|
368
407
|
/**
|
|
369
408
|
* The function checks if a given property descriptor or property of an object is
|
|
@@ -17,7 +17,7 @@ export const FunctionExtensions = new Patch(Function, {
|
|
|
17
17
|
* @returns {boolean} Returns `true` if the value is a class, otherwise `false`.
|
|
18
18
|
*/
|
|
19
19
|
isClass(value) {
|
|
20
|
-
return value instanceof Function && String(value)
|
|
20
|
+
return value instanceof Function && !!/^class\s/.exec(String(value));
|
|
21
21
|
},
|
|
22
22
|
/**
|
|
23
23
|
* Checks if a given value is a regular function. This method verifies if the value is
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Patch } from '@nejs/extension';
|
|
2
|
+
import { FunctionExtensions } from './functionextensions.js';
|
|
3
|
+
const { isClass, isFunction } = FunctionExtensions.patchEntries.isClass.computed;
|
|
4
|
+
const CustomInspect = Symbol.for('nodejs.util.inspect.custom');
|
|
5
|
+
export const GlobalFunctionsAndProps = new Patch(globalThis, {
|
|
6
|
+
asBigIntObject(bigIntPrimitive) {
|
|
7
|
+
const base = { configurable: true, enumerable: false };
|
|
8
|
+
const object = { value: bigIntPrimitive };
|
|
9
|
+
Object.defineProperties(object, {
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
[Symbol.toPrimitive]: { value: function () { return bigIntPrimitive; }, ...base },
|
|
12
|
+
[Symbol.toStringTag]: { value: BigInt.name, ...base },
|
|
13
|
+
[Symbol.species]: { get() { return BigInt; }, ...base },
|
|
14
|
+
[CustomInspect]: { ...base, value(depth, opts, inspect) {
|
|
15
|
+
return inspect(this[Symbol.toPrimitive](), { ...opts, depth });
|
|
16
|
+
} }
|
|
17
|
+
});
|
|
18
|
+
Object.setPrototypeOf(object, BigInt.prototype);
|
|
19
|
+
Reflect.ownKeys(BigInt.prototype).forEach(key => {
|
|
20
|
+
if (typeof object[key] !== 'function') {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
object[key] = (function (...args) {
|
|
24
|
+
return BigInt.prototype[key].apply(this, args);
|
|
25
|
+
}).bind(object.value);
|
|
26
|
+
});
|
|
27
|
+
return object;
|
|
28
|
+
},
|
|
29
|
+
/**
|
|
30
|
+
* Transforms an object to mimic a specified prototype, altering its type conversion
|
|
31
|
+
* and inspection behaviors. This function is especially useful for creating objects
|
|
32
|
+
* that need to behave like different primitive types under various operations.
|
|
33
|
+
*
|
|
34
|
+
* @param {Object} object - The object to be transformed.
|
|
35
|
+
* @param {Function|Object} [prototype=String.prototype] - The prototype or class to
|
|
36
|
+
* emulate. If a function is provided, its prototype is used. Defaults to
|
|
37
|
+
* String.prototype.
|
|
38
|
+
* @param {Function} [toPrimitive=(hint, val) => String(val)] - A function defining how
|
|
39
|
+
* the object should be converted to a primitive value. It receives a type hint
|
|
40
|
+
* ('number', 'string', or 'default') and the object, returning the primitive value.
|
|
41
|
+
* @returns {Object|null} The transformed object, or null if neither a class nor a
|
|
42
|
+
* prototype could be derived from the provided prototype parameter.
|
|
43
|
+
*/
|
|
44
|
+
maskAs(object, classPrototype, options) {
|
|
45
|
+
const { prototype, toPrimitive } = GenericMask({ ...options, prototype: classPrototype });
|
|
46
|
+
const base = { configurable: true, enumerable: false };
|
|
47
|
+
const proto = isFunction(prototype) ? prototype.prototype : prototype;
|
|
48
|
+
const klass = isClass(prototype) ? prototype : proto?.constructor;
|
|
49
|
+
if (!klass && !proto) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
Object.setPrototypeOf(object, proto);
|
|
53
|
+
Object.defineProperties(object, {
|
|
54
|
+
valueOf: { value() { return String(toPrimitive('default', object)); }, ...base },
|
|
55
|
+
[Symbol.toPrimitive]: { value(hint) { return toPrimitive(hint, object); }, ...base },
|
|
56
|
+
[Symbol.toStringTag]: { value: klass.name, ...base },
|
|
57
|
+
[Symbol.species]: { get() { return klass; }, ...base },
|
|
58
|
+
[CustomInspect]: { ...base, value(depth, opts, inspect) {
|
|
59
|
+
return inspect(this[Symbol.toPrimitive](), { ...opts, depth });
|
|
60
|
+
} }
|
|
61
|
+
});
|
|
62
|
+
return object;
|
|
63
|
+
},
|
|
64
|
+
/**
|
|
65
|
+
* Masks an object as a string-like object by setting its prototype to String and
|
|
66
|
+
* defining how it converts to primitive types. This is particularly useful when an
|
|
67
|
+
* object needs to behave like a string in certain contexts, such as type coercion or
|
|
68
|
+
* logging.
|
|
69
|
+
*
|
|
70
|
+
* @param {Object} object - The object to be masked as a string.
|
|
71
|
+
* @param {string} [stringKey='value'] - The object property key used for the string
|
|
72
|
+
* representation. Defaults to 'value'.
|
|
73
|
+
* @param {Function} [toPrimitive] - Optional custom function for primitive conversion.
|
|
74
|
+
* If omitted, a default function handling various conversion hints is used.
|
|
75
|
+
* @returns {Object|null} The string-masked object, or null if the object doesn't have
|
|
76
|
+
* the specified stringKey property.
|
|
77
|
+
*/
|
|
78
|
+
maskAsString(object, stringKey, toPrimitive) {
|
|
79
|
+
if (object && Reflect.has(object, stringKey)) {
|
|
80
|
+
return maskAs(object, StringMask(stringKey ?? 'value', toPrimitive));
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* Masks an object as a number-like object. This allows the object to behave like a
|
|
86
|
+
* number in operations like arithmetic and type coercion. It sets the prototype to
|
|
87
|
+
* Number and defines custom conversion behavior.
|
|
88
|
+
*
|
|
89
|
+
* @param {Object} object - The object to be masked as a number representation.
|
|
90
|
+
* Defaults to 'value'.
|
|
91
|
+
* @param {Function} [toPrimitive] - Optional custom function for primitive
|
|
92
|
+
* conversion. If not provided, a default function handling different conversion
|
|
93
|
+
* hints is used.
|
|
94
|
+
* @returns {Object|null} The number-masked object, or null if the object doesn't
|
|
95
|
+
* have the specified numberKey property.
|
|
96
|
+
*/
|
|
97
|
+
maskAsNumber(object, numberKey, toPrimitive) {
|
|
98
|
+
if (object && Reflect.has(object, numberKey)) {
|
|
99
|
+
return maskAs(object, NumberMask(numberKey ?? 'value', toPrimitive));
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
},
|
|
103
|
+
/**
|
|
104
|
+
* Generates options for generic masking of an object, providing defaults for
|
|
105
|
+
* prototype and toPrimitive function if not specified.
|
|
106
|
+
*
|
|
107
|
+
* @param {Object} options - The options object including prototype, targetKey,
|
|
108
|
+
* and toPrimitive function.
|
|
109
|
+
* @returns {Object} The options object with defaults applied as necessary.
|
|
110
|
+
*/
|
|
111
|
+
GenericMask({ prototype, targetKey = 'value', toPrimitive }) {
|
|
112
|
+
const options = { targetKey, toPrimitive, prototype };
|
|
113
|
+
if (!isFunction(toPrimitive)) {
|
|
114
|
+
options.toPrimitive = (hint, object) => {
|
|
115
|
+
let property = object[targetKey];
|
|
116
|
+
let isNum = (typeof property === 'number' && Number.isFinite(property)) ||
|
|
117
|
+
(typeof property === 'string' &&
|
|
118
|
+
!isNaN(parseFloat(property)) && isFinite(property));
|
|
119
|
+
switch (hint) {
|
|
120
|
+
case 'string': return isNum ? String(property) : (property ?? String(object));
|
|
121
|
+
case 'number': return isNum ? Number(property) : NaN;
|
|
122
|
+
case 'default':
|
|
123
|
+
default:
|
|
124
|
+
return isNum ? Number(property) : property;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return options;
|
|
129
|
+
},
|
|
130
|
+
/**
|
|
131
|
+
* Generates options for string masking of an object, providing a default toPrimitive
|
|
132
|
+
* function if not specified.
|
|
133
|
+
*
|
|
134
|
+
* @param {string} targetKey - The object property key for string representation.
|
|
135
|
+
* @param {Function} toPrimitive - Custom function for primitive conversion.
|
|
136
|
+
* @returns {Object} Options for string masking.
|
|
137
|
+
*/
|
|
138
|
+
StringMask(targetKey, toPrimitive) {
|
|
139
|
+
const options = { targetKey, toPrimitive, prototype: String.prototype };
|
|
140
|
+
if (!isFunction(toPrimitive)) {
|
|
141
|
+
options.toPrimitive = function toPrimitive(hint, object) {
|
|
142
|
+
switch (hint) {
|
|
143
|
+
case 'default': return object[targetKey];
|
|
144
|
+
case 'number': return parseInt(object[targetKey], 36);
|
|
145
|
+
case 'string': return String(object[targetKey]);
|
|
146
|
+
default: return object;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return options;
|
|
151
|
+
},
|
|
152
|
+
/**
|
|
153
|
+
* Generates options for number masking of an object, providing a default toPrimitive
|
|
154
|
+
* function if not specified.
|
|
155
|
+
*
|
|
156
|
+
* @param {string} targetKey - The object property key for number representation.
|
|
157
|
+
* @param {Function} toPrimitive - Custom function for primitive conversion.
|
|
158
|
+
* @returns {Object} Options for number masking.
|
|
159
|
+
*/
|
|
160
|
+
NumberMask(targetKey, toPrimitive) {
|
|
161
|
+
const options = { targetKey, toPrimitive, prototype: Number.prototype };
|
|
162
|
+
if (!isFunction(toPrimitive)) {
|
|
163
|
+
options.toPrimitive = function toPrimitive(hint, object) {
|
|
164
|
+
switch (hint) {
|
|
165
|
+
case 'default': return object[targetKey];
|
|
166
|
+
case 'number': return Number(object[targetKey]);
|
|
167
|
+
case 'string': return String(object[targetKey]);
|
|
168
|
+
default: return object;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return options;
|
|
173
|
+
},
|
|
174
|
+
});
|
package/dist/mjs/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export function enableAll(owners: any): void;
|
|
2
2
|
export function disableAll(owners: any): void;
|
|
3
|
+
export const all: {};
|
|
3
4
|
import { ObjectExtensions } from './objectextensions.js';
|
|
4
5
|
import { FunctionExtensions } from './functionextensions.js';
|
|
5
6
|
import { ReflectExtensions } from './reflectextensions.js';
|
|
6
7
|
import { StringExtensions } from './stringextensions.js';
|
|
7
8
|
import { SymbolExtensions } from './symbolextensions.js';
|
|
8
9
|
import { ArrayPrototypeExtensions } from './arrayextensions.js';
|
|
9
|
-
|
|
10
|
+
import { GlobalFunctionsAndProps } from './globals.js';
|
|
11
|
+
import { DescriptorExtension } from './descriptor.js';
|
|
12
|
+
export { ObjectExtensions, FunctionExtensions, ReflectExtensions, StringExtensions, SymbolExtensions, ArrayPrototypeExtensions, GlobalFunctionsAndProps, DescriptorExtension };
|
package/dist/mjs/index.js
CHANGED
|
@@ -4,6 +4,8 @@ import { ReflectExtensions } from './reflectextensions.js';
|
|
|
4
4
|
import { StringExtensions } from './stringextensions.js';
|
|
5
5
|
import { SymbolExtensions } from './symbolextensions.js';
|
|
6
6
|
import { ArrayPrototypeExtensions } from './arrayextensions.js';
|
|
7
|
+
import { DescriptorExtension } from './descriptor.js';
|
|
8
|
+
import { GlobalFunctionsAndProps } from './globals.js';
|
|
7
9
|
import { Patch } from '@nejs/extension';
|
|
8
10
|
const Owners = [
|
|
9
11
|
Object,
|
|
@@ -13,14 +15,52 @@ const Owners = [
|
|
|
13
15
|
Symbol,
|
|
14
16
|
Array.prototype,
|
|
15
17
|
];
|
|
18
|
+
const NetNew = [
|
|
19
|
+
GlobalFunctionsAndProps,
|
|
20
|
+
DescriptorExtension,
|
|
21
|
+
];
|
|
16
22
|
export function enableAll(owners) {
|
|
17
|
-
|
|
23
|
+
const list = owners || Owners;
|
|
24
|
+
if (!list) {
|
|
25
|
+
throw new Error('Unable to enable features without owners list');
|
|
26
|
+
}
|
|
27
|
+
list.forEach(owner => {
|
|
18
28
|
Patch.enableFor(owner);
|
|
19
29
|
});
|
|
30
|
+
NetNew.forEach(extension => {
|
|
31
|
+
extension.apply();
|
|
32
|
+
});
|
|
20
33
|
}
|
|
21
34
|
export function disableAll(owners) {
|
|
22
|
-
|
|
35
|
+
const list = owners || Owners;
|
|
36
|
+
if (!list) {
|
|
37
|
+
throw new Error('Unable to disable features without owners list');
|
|
38
|
+
}
|
|
39
|
+
list.forEach(owner => {
|
|
23
40
|
Patch.disableFor(owner);
|
|
24
41
|
});
|
|
42
|
+
NetNew.forEach(extension => {
|
|
43
|
+
extension.revert();
|
|
44
|
+
});
|
|
25
45
|
}
|
|
26
|
-
export
|
|
46
|
+
export const all = (() => {
|
|
47
|
+
let extensions = [
|
|
48
|
+
ObjectExtensions,
|
|
49
|
+
FunctionExtensions,
|
|
50
|
+
ReflectExtensions,
|
|
51
|
+
StringExtensions,
|
|
52
|
+
SymbolExtensions,
|
|
53
|
+
ArrayPrototypeExtensions,
|
|
54
|
+
GlobalFunctionsAndProps,
|
|
55
|
+
DescriptorExtension,
|
|
56
|
+
];
|
|
57
|
+
const dest = extensions.reduce((accumulator, extension) => {
|
|
58
|
+
Reflect.ownKeys(extension.patchEntries).reduce((_, key) => {
|
|
59
|
+
accumulator[key] = extension.patchEntries[key].computed;
|
|
60
|
+
return accumulator;
|
|
61
|
+
}, accumulator);
|
|
62
|
+
return accumulator;
|
|
63
|
+
}, {});
|
|
64
|
+
return dest;
|
|
65
|
+
})();
|
|
66
|
+
export { ObjectExtensions, FunctionExtensions, ReflectExtensions, StringExtensions, SymbolExtensions, ArrayPrototypeExtensions, GlobalFunctionsAndProps, DescriptorExtension, };
|