@nejs/basic-extensions 1.3.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 +14 -0
- 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 +25 -1
- package/dist/cjs/objectextensions.js +71 -45
- package/dist/mjs/descriptor.js +14 -0
- 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 +23 -1
- package/dist/mjs/objectextensions.js +71 -45
- package/package.json +2 -2
- package/src/descriptor.js +16 -0
- package/src/functionextensions.js +1 -1
- package/src/globals.js +216 -0
- package/src/index.js +31 -0
- package/src/objectextensions.js +75 -46
- package/dist/@nejs/basic-extensions.bundle.1.2.0.js +0 -2
- package/dist/@nejs/basic-extensions.bundle.1.2.0.js.map +0 -7
|
@@ -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
|
@@ -171,6 +171,20 @@ class Descriptor {
|
|
|
171
171
|
set set(value) {
|
|
172
172
|
(this.#desc || {}).set = value;
|
|
173
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
|
+
}
|
|
174
188
|
/**
|
|
175
189
|
* Take the descriptor defined by this objects values and apply them to
|
|
176
190
|
* the specified object using the specified key.
|
|
@@ -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
|
@@ -5,6 +5,7 @@ import { StringExtensions } from './stringextensions.js';
|
|
|
5
5
|
import { SymbolExtensions } from './symbolextensions.js';
|
|
6
6
|
import { ArrayPrototypeExtensions } from './arrayextensions.js';
|
|
7
7
|
import { DescriptorExtension } from './descriptor.js';
|
|
8
|
+
import { GlobalFunctionsAndProps } from './globals.js';
|
|
8
9
|
import { Patch } from '@nejs/extension';
|
|
9
10
|
const Owners = [
|
|
10
11
|
Object,
|
|
@@ -15,6 +16,7 @@ const Owners = [
|
|
|
15
16
|
Array.prototype,
|
|
16
17
|
];
|
|
17
18
|
const NetNew = [
|
|
19
|
+
GlobalFunctionsAndProps,
|
|
18
20
|
DescriptorExtension,
|
|
19
21
|
];
|
|
20
22
|
export function enableAll(owners) {
|
|
@@ -41,4 +43,24 @@ export function disableAll(owners) {
|
|
|
41
43
|
extension.revert();
|
|
42
44
|
});
|
|
43
45
|
}
|
|
44
|
-
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, };
|
|
@@ -9,16 +9,45 @@ import { Patch } from '@nejs/extension';
|
|
|
9
9
|
*/
|
|
10
10
|
export const ObjectExtensions = new Patch(Object, {
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* Retrieves the string tag of an object. The string tag is a representation of
|
|
13
|
+
* the object's type, as defined by its `Object.prototype.toString` method. This
|
|
14
|
+
* utility method is helpful for getting a more descriptive type of an object than
|
|
15
|
+
* what is returned by the `typeof` operator, especially for custom objects.
|
|
15
16
|
*
|
|
16
|
-
* @param {*} value - The
|
|
17
|
-
* @returns {
|
|
18
|
-
* otherwise `false`.
|
|
17
|
+
* @param {*} value - The object whose string tag is to be retrieved.
|
|
18
|
+
* @returns {string} - The string tag of the object, indicating its type.
|
|
19
19
|
*/
|
|
20
|
-
|
|
21
|
-
return (
|
|
20
|
+
getStringTag(value) {
|
|
21
|
+
return /\s(.+)]/.exec(Object.prototype.toString.call(value))[1];
|
|
22
|
+
},
|
|
23
|
+
/**
|
|
24
|
+
* Determines the type of the given value based on its string tag. This method
|
|
25
|
+
* uses `Object.getStringTag` to obtain the string tag of the value, which
|
|
26
|
+
* represents its more specific type (e.g., Array, Map, Set) rather than just
|
|
27
|
+
* 'object'. The method then maps this string tag to the corresponding type
|
|
28
|
+
* present in the provided `owner` object, which defaults to `globalThis`.
|
|
29
|
+
* This utility method is especially useful for identifying the specific
|
|
30
|
+
* constructor or class of an object, beyond the basic types identified by
|
|
31
|
+
* the `typeof` operator.
|
|
32
|
+
*
|
|
33
|
+
* @param {any} value - The value whose type is to be determined.
|
|
34
|
+
* @param {object} [owner=globalThis] - The object in which to look up the
|
|
35
|
+
* constructor corresponding to the string tag. Defaults to `globalThis`, which
|
|
36
|
+
* covers global constructors like `Array`, `Object`, etc.
|
|
37
|
+
* @returns {Function|object|null|undefined} - Returns the constructor or type
|
|
38
|
+
* of the value based on its string tag. For 'Null' and 'Undefined', it returns
|
|
39
|
+
* `null` and `undefined`, respectively. For other types, it returns the
|
|
40
|
+
* corresponding constructor (e.g., `Array` for arrays) if available in the
|
|
41
|
+
* `owner` object.
|
|
42
|
+
*/
|
|
43
|
+
getType(value, owner = globalThis) {
|
|
44
|
+
const stringTag = Object.getStringTag(value);
|
|
45
|
+
switch (stringTag) {
|
|
46
|
+
case 'Null': return null;
|
|
47
|
+
case 'Undefined': return undefined;
|
|
48
|
+
default:
|
|
49
|
+
return owner[stringTag];
|
|
50
|
+
}
|
|
22
51
|
},
|
|
23
52
|
/**
|
|
24
53
|
* Determines if the provided value is an object. This method checks whether the
|
|
@@ -33,16 +62,42 @@ export const ObjectExtensions = new Patch(Object, {
|
|
|
33
62
|
return value && (value instanceof Object || typeof value === 'object');
|
|
34
63
|
},
|
|
35
64
|
/**
|
|
36
|
-
*
|
|
37
|
-
* the object's type, as defined by its `Object.prototype.toString` method. This
|
|
38
|
-
* utility method is helpful for getting a more descriptive type of an object than
|
|
39
|
-
* what is returned by the `typeof` operator, especially for custom objects.
|
|
65
|
+
* Checks to see if the supplied value is a primitive value.
|
|
40
66
|
*
|
|
41
|
-
* @param {
|
|
42
|
-
* @returns
|
|
67
|
+
* @param {any} value the value to test to see if it is a primitive value type
|
|
68
|
+
* @returns true if the object is considered widely to be a primitive value,
|
|
69
|
+
* false otherwise.
|
|
43
70
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
71
|
+
isPrimitive(value) {
|
|
72
|
+
// Check for null as a special case because typeof null
|
|
73
|
+
// is 'object'
|
|
74
|
+
if (value === null) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
// Check for other primitives
|
|
78
|
+
switch (typeof value) {
|
|
79
|
+
case 'string':
|
|
80
|
+
case 'number':
|
|
81
|
+
case 'bigint':
|
|
82
|
+
case 'boolean':
|
|
83
|
+
case 'undefined':
|
|
84
|
+
case 'symbol':
|
|
85
|
+
return true;
|
|
86
|
+
default:
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
/**
|
|
91
|
+
* Checks if the given value is a valid key for an object. In JavaScript, a valid
|
|
92
|
+
* key can be either a string or a symbol. This method is useful for validating
|
|
93
|
+
* object keys before using them in operations like setting or getting object properties.
|
|
94
|
+
*
|
|
95
|
+
* @param {*} value - The value to be checked.
|
|
96
|
+
* @returns {boolean} - Returns `true` if the value is a valid object key (string or symbol),
|
|
97
|
+
* otherwise `false`.
|
|
98
|
+
*/
|
|
99
|
+
isValidKey(value) {
|
|
100
|
+
return (typeof value === 'string' || typeof value === 'symbol');
|
|
46
101
|
},
|
|
47
102
|
/**
|
|
48
103
|
* Strips an object down to only the keys specified. Optionally, any
|
|
@@ -80,33 +135,4 @@ export const ObjectExtensions = new Patch(Object, {
|
|
|
80
135
|
}
|
|
81
136
|
return result;
|
|
82
137
|
},
|
|
83
|
-
/**
|
|
84
|
-
* Determines the type of the given value based on its string tag. This method
|
|
85
|
-
* uses `Object.getStringTag` to obtain the string tag of the value, which
|
|
86
|
-
* represents its more specific type (e.g., Array, Map, Set) rather than just
|
|
87
|
-
* 'object'. The method then maps this string tag to the corresponding type
|
|
88
|
-
* present in the provided `owner` object, which defaults to `globalThis`.
|
|
89
|
-
* This utility method is especially useful for identifying the specific
|
|
90
|
-
* constructor or class of an object, beyond the basic types identified by
|
|
91
|
-
* the `typeof` operator.
|
|
92
|
-
*
|
|
93
|
-
* @param {any} value - The value whose type is to be determined.
|
|
94
|
-
* @param {object} [owner=globalThis] - The object in which to look up the
|
|
95
|
-
* constructor corresponding to the string tag. Defaults to `globalThis`, which
|
|
96
|
-
* covers global constructors like `Array`, `Object`, etc.
|
|
97
|
-
* @returns {Function|object|null|undefined} - Returns the constructor or type
|
|
98
|
-
* of the value based on its string tag. For 'Null' and 'Undefined', it returns
|
|
99
|
-
* `null` and `undefined`, respectively. For other types, it returns the
|
|
100
|
-
* corresponding constructor (e.g., `Array` for arrays) if available in the
|
|
101
|
-
* `owner` object.
|
|
102
|
-
*/
|
|
103
|
-
getType(value, owner = globalThis) {
|
|
104
|
-
const stringTag = Object.getStringTag(value);
|
|
105
|
-
switch (stringTag) {
|
|
106
|
-
case 'Null': return null;
|
|
107
|
-
case 'Undefined': return undefined;
|
|
108
|
-
default:
|
|
109
|
-
return owner[stringTag];
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
138
|
});
|
package/package.json
CHANGED
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
"test": "jest"
|
|
47
47
|
},
|
|
48
48
|
"type": "module",
|
|
49
|
-
"version": "1.
|
|
49
|
+
"version": "1.4.0",
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@nejs/extension": "^1.2.1"
|
|
52
52
|
},
|
|
53
|
-
"browser": "dist/@nejs/basic-extensions.bundle.1.
|
|
53
|
+
"browser": "dist/@nejs/basic-extensions.bundle.1.3.0.js"
|
|
54
54
|
}
|
package/src/descriptor.js
CHANGED
|
@@ -192,6 +192,22 @@ class Descriptor {
|
|
|
192
192
|
(this.#desc || {}).set = value
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Shorthand for Object.getOwnPropertyDescriptor()
|
|
197
|
+
*
|
|
198
|
+
* @param {object} object a non-null object instance
|
|
199
|
+
* @param {string|symbol} key a symbol or string referencing which key on the
|
|
200
|
+
* object to return a descriptor for.
|
|
201
|
+
* @returns an object descriptor for the requested field or null
|
|
202
|
+
*/
|
|
203
|
+
static for(object, key) {
|
|
204
|
+
if (!isObject(object) && !isValidKey(key)) {
|
|
205
|
+
return null
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return Object.getOwnPropertyDescriptor(object, key)
|
|
209
|
+
}
|
|
210
|
+
|
|
195
211
|
/**
|
|
196
212
|
* Take the descriptor defined by this objects values and apply them to
|
|
197
213
|
* the specified object using the specified key.
|
|
@@ -18,7 +18,7 @@ export const FunctionExtensions = new Patch(Function, {
|
|
|
18
18
|
* @returns {boolean} Returns `true` if the value is a class, otherwise `false`.
|
|
19
19
|
*/
|
|
20
20
|
isClass(value) {
|
|
21
|
-
return value instanceof Function && String(value)
|
|
21
|
+
return value instanceof Function && !!/^class\s/.exec(String(value))
|
|
22
22
|
},
|
|
23
23
|
|
|
24
24
|
/**
|