@nejs/basic-extensions
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -71,1 +71,2 @@ "use strict"; | ||
}); | ||
//# sourceMappingURL=arrayextensions.js.map |
@@ -90,1 +90,2 @@ "use strict"; | ||
}); | ||
//# sourceMappingURL=functionextensions.js.map |
@@ -6,3 +6,3 @@ "use strict"; | ||
const functionextensions_js_1 = require("./functionextensions.js"); | ||
const { isClass, isFunction } = functionextensions_js_1.FunctionExtensions.patchEntries.isClass.computed; | ||
const { isClass, isFunction } = functionextensions_js_1.FunctionExtensions.patches; | ||
const CustomInspect = Symbol.for('nodejs.util.inspect.custom'); | ||
@@ -167,1 +167,2 @@ exports.GlobalFunctionsAndProps = new extension_1.Patch(globalThis, { | ||
}); | ||
//# sourceMappingURL=globals.js.map |
@@ -1,19 +0,10 @@ | ||
export function enableAll(owners: any): void; | ||
export function enableNetNew(): void; | ||
export function disableAll(owners: any): void; | ||
export function disableNetNew(): void; | ||
export const all: {}; | ||
import { ObjectExtensions } from './objectextensions.js'; | ||
import { FunctionExtensions } from './functionextensions.js'; | ||
import { ReflectExtensions } from './reflectextensions.js'; | ||
import { StringExtensions } from './stringextensions.js'; | ||
import { SymbolExtensions } from './symbolextensions.js'; | ||
import { ArrayPrototypeExtensions } from './arrayextensions.js'; | ||
import { GlobalFunctionsAndProps } from './globals.js'; | ||
import { DescriptorExtensions } from './descriptor.js'; | ||
import { AsyncIterableExtensions } from './asyncIterable.js'; | ||
import { AsyncIteratorExtensions } from './asyncIterable.js'; | ||
import { IterableExtensions } from './iterable.js'; | ||
import { IteratorExtensions } from './iterable.js'; | ||
import { RefSetExtensions } from './refset.js'; | ||
export { ObjectExtensions, FunctionExtensions, ReflectExtensions, StringExtensions, SymbolExtensions, ArrayPrototypeExtensions, GlobalFunctionsAndProps, DescriptorExtensions, AsyncIterableExtensions, AsyncIteratorExtensions, IterableExtensions, IteratorExtensions, RefSetExtensions }; | ||
export const all: any; | ||
export default results; | ||
declare namespace results { | ||
export { Extensions as extensions }; | ||
export { Patches as patches }; | ||
export { all }; | ||
} | ||
export const Extensions: {}; | ||
export const Patches: Map<Object, import("@nejs/extension").Patch>; | ||
export const Controls: {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RefSetExtensions = exports.IteratorExtensions = exports.IterableExtensions = exports.AsyncIteratorExtensions = exports.AsyncIterableExtensions = exports.DescriptorExtensions = exports.GlobalFunctionsAndProps = exports.ArrayPrototypeExtensions = exports.SymbolExtensions = exports.StringExtensions = exports.ReflectExtensions = exports.FunctionExtensions = exports.ObjectExtensions = exports.all = exports.disableNetNew = exports.disableAll = exports.enableNetNew = exports.enableAll = void 0; | ||
exports.Controls = exports.Patches = exports.Extensions = exports.all = void 0; | ||
const functionextensions_js_1 = require("./functionextensions.js"); | ||
Object.defineProperty(exports, "FunctionExtensions", { enumerable: true, get: function () { return functionextensions_js_1.FunctionExtensions; } }); | ||
const objectextensions_js_1 = require("./objectextensions.js"); | ||
Object.defineProperty(exports, "ObjectExtensions", { enumerable: true, get: function () { return objectextensions_js_1.ObjectExtensions; } }); | ||
const mapextensions_js_1 = require("./mapextensions.js"); | ||
const reflectextensions_js_1 = require("./reflectextensions.js"); | ||
Object.defineProperty(exports, "ReflectExtensions", { enumerable: true, get: function () { return reflectextensions_js_1.ReflectExtensions; } }); | ||
const stringextensions_js_1 = require("./stringextensions.js"); | ||
Object.defineProperty(exports, "StringExtensions", { enumerable: true, get: function () { return stringextensions_js_1.StringExtensions; } }); | ||
const symbolextensions_js_1 = require("./symbolextensions.js"); | ||
Object.defineProperty(exports, "SymbolExtensions", { enumerable: true, get: function () { return symbolextensions_js_1.SymbolExtensions; } }); | ||
const arrayextensions_js_1 = require("./arrayextensions.js"); | ||
Object.defineProperty(exports, "ArrayPrototypeExtensions", { enumerable: true, get: function () { return arrayextensions_js_1.ArrayPrototypeExtensions; } }); | ||
const descriptor_js_1 = require("./descriptor.js"); | ||
Object.defineProperty(exports, "DescriptorExtensions", { enumerable: true, get: function () { return descriptor_js_1.DescriptorExtensions; } }); | ||
const descriptor_js_1 = require("./newClasses/descriptor.js"); | ||
const globals_js_1 = require("./globals.js"); | ||
Object.defineProperty(exports, "GlobalFunctionsAndProps", { enumerable: true, get: function () { return globals_js_1.GlobalFunctionsAndProps; } }); | ||
const refset_js_1 = require("./refset.js"); | ||
Object.defineProperty(exports, "RefSetExtensions", { enumerable: true, get: function () { return refset_js_1.RefSetExtensions; } }); | ||
const asyncIterable_js_1 = require("./asyncIterable.js"); | ||
Object.defineProperty(exports, "AsyncIteratorExtensions", { enumerable: true, get: function () { return asyncIterable_js_1.AsyncIteratorExtensions; } }); | ||
Object.defineProperty(exports, "AsyncIterableExtensions", { enumerable: true, get: function () { return asyncIterable_js_1.AsyncIterableExtensions; } }); | ||
const iterable_js_1 = require("./iterable.js"); | ||
Object.defineProperty(exports, "IteratorExtensions", { enumerable: true, get: function () { return iterable_js_1.IteratorExtensions; } }); | ||
Object.defineProperty(exports, "IterableExtensions", { enumerable: true, get: function () { return iterable_js_1.IterableExtensions; } }); | ||
const extension_1 = require("@nejs/extension"); | ||
const Owners = [ | ||
Object, | ||
Function, | ||
Reflect, | ||
String, | ||
Symbol, | ||
Array.prototype, | ||
]; | ||
const NetNew = [ | ||
globals_js_1.GlobalFunctionsAndProps, | ||
descriptor_js_1.DescriptorExtensions, | ||
asyncIterable_js_1.AsyncIterableExtensions, | ||
asyncIterable_js_1.AsyncIteratorExtensions, | ||
iterable_js_1.IterableExtensions, | ||
iterable_js_1.IteratorExtensions, | ||
refset_js_1.RefSetExtensions, | ||
]; | ||
function enableAll(owners) { | ||
const list = owners || Owners; | ||
if (!list) { | ||
throw new Error('Unable to enable features without owners list'); | ||
} | ||
list.forEach(owner => { | ||
extension_1.Patch.enableFor(owner); | ||
}); | ||
enableNetNew(); | ||
} | ||
exports.enableAll = enableAll; | ||
function enableNetNew() { | ||
NetNew.forEach(extension => { extension.apply(); }); | ||
} | ||
exports.enableNetNew = enableNetNew; | ||
function disableAll(owners) { | ||
const list = owners || Owners; | ||
if (!list) { | ||
throw new Error('Unable to disable features without owners list'); | ||
} | ||
list.forEach(owner => { | ||
extension_1.Patch.disableFor(owner); | ||
}); | ||
disableNetNew(); | ||
} | ||
exports.disableAll = disableAll; | ||
function disableNetNew() { | ||
NetNew.forEach(extension => { extension.revert(); }); | ||
} | ||
exports.disableNetNew = disableNetNew; | ||
const refset_js_1 = require("./newClasses/refset.js"); | ||
const refmap_js_1 = require("./newClasses/refmap.js"); | ||
const asyncIterable_js_1 = require("./newClasses/asyncIterable.js"); | ||
const iterable_js_1 = require("./newClasses/iterable.js"); | ||
const Patches = new Map([ | ||
[Object, objectextensions_js_1.ObjectExtensions], | ||
[Function, functionextensions_js_1.FunctionExtensions], | ||
[Reflect, reflectextensions_js_1.ReflectExtensions], | ||
[String, stringextensions_js_1.StringExtensions], | ||
[Symbol, symbolextensions_js_1.SymbolExtensions], | ||
[Object.prototype, objectextensions_js_1.ObjectPrototypeExtensions], | ||
[Array.prototype, arrayextensions_js_1.ArrayPrototypeExtensions], | ||
[Map.prototype, mapextensions_js_1.MapPrototypeExtensions], | ||
[globalThis, globals_js_1.GlobalFunctionsAndProps], | ||
]); | ||
exports.Patches = Patches; | ||
const Extensions = { | ||
[descriptor_js_1.DescriptorExtensions.key]: descriptor_js_1.DescriptorExtensions, | ||
[asyncIterable_js_1.AsyncIterableExtensions.key]: asyncIterable_js_1.AsyncIterableExtensions, | ||
[asyncIterable_js_1.AsyncIteratorExtensions.key]: asyncIterable_js_1.AsyncIteratorExtensions, | ||
[iterable_js_1.IterableExtensions.key]: iterable_js_1.IterableExtensions, | ||
[iterable_js_1.IteratorExtensions.key]: iterable_js_1.IteratorExtensions, | ||
[refset_js_1.RefSetExtensions.key]: refset_js_1.RefSetExtensions, | ||
[refmap_js_1.RefMapExtensions.key]: refmap_js_1.RefMapExtensions, | ||
}; | ||
exports.Extensions = Extensions; | ||
const Controls = {}; | ||
exports.Controls = Controls; | ||
Object.assign(Controls, { | ||
enableAll() { | ||
Controls.enablePatches(); | ||
Controls.enableExtensions(); | ||
}, | ||
enablePatches() { | ||
Patches.forEach((extension) => { extension.apply(); }); | ||
}, | ||
enableExtensions() { | ||
Object.values(Extensions).forEach((extension) => { extension.apply(); }); | ||
}, | ||
disableAll(owners) { | ||
Controls.disablePatches(); | ||
Controls.disableExtensions(); | ||
}, | ||
disablePatches() { | ||
Patches.forEach((extension) => { extension.revert(); }); | ||
}, | ||
disableExtensions() { | ||
Object.values(Extensions).forEach((extension) => { extension.revert(); }); | ||
}, | ||
}); | ||
exports.all = (() => { | ||
let extensions = [ | ||
objectextensions_js_1.ObjectExtensions, | ||
functionextensions_js_1.FunctionExtensions, | ||
reflectextensions_js_1.ReflectExtensions, | ||
stringextensions_js_1.StringExtensions, | ||
symbolextensions_js_1.SymbolExtensions, | ||
arrayextensions_js_1.ArrayPrototypeExtensions, | ||
globals_js_1.GlobalFunctionsAndProps, | ||
descriptor_js_1.DescriptorExtensions, | ||
const extensions = [ | ||
...Array.from(Patches.values()), | ||
...Array.from(Object.values(Extensions)), | ||
]; | ||
const dest = extensions.reduce((accumulator, extension) => { | ||
Reflect.ownKeys(extension.patchEntries).reduce((_, key) => { | ||
accumulator[key] = extension.patchEntries[key].computed; | ||
const entry = extension.patchEntries[key]; | ||
if (entry.isAccessor) | ||
accumulator[key] = new descriptor_js_1.Descriptor(entry.descriptor); | ||
else | ||
accumulator[key] = entry.computed; | ||
return accumulator; | ||
@@ -96,1 +81,9 @@ }, accumulator); | ||
})(); | ||
const results = { | ||
...Controls, | ||
extensions: Extensions, | ||
patches: Patches, | ||
all: exports.all, | ||
}; | ||
exports.default = results; | ||
//# sourceMappingURL=index.js.map |
/** | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. It | ||
* adds utility methods to the `Object` class without modifying the global namespace | ||
* directly. This patch includes methods for key validation, object type checking, | ||
* and retrieving the string tag of an object. These methods are useful for | ||
* enhancing the capabilities of the standard `Object` class with additional | ||
* utility functions. | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. | ||
* It adds utility methods to the `Object` class without modifying the global | ||
* namespace directly. This patch includes methods for key validation, object | ||
* type checking, and retrieving the string tag of an object. These methods | ||
* are useful for enhancing the capabilities of the standard `Object` class | ||
* with additional utility functions. | ||
*/ | ||
export const ObjectExtensions: Patch; | ||
export const ObjectPrototypeExtensions: Patch; | ||
import { Patch } from '@nejs/extension'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ObjectExtensions = void 0; | ||
exports.ObjectPrototypeExtensions = exports.ObjectExtensions = void 0; | ||
const extension_1 = require("@nejs/extension"); | ||
/** | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. It | ||
* adds utility methods to the `Object` class without modifying the global namespace | ||
* directly. This patch includes methods for key validation, object type checking, | ||
* and retrieving the string tag of an object. These methods are useful for | ||
* enhancing the capabilities of the standard `Object` class with additional | ||
* utility functions. | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. | ||
* It adds utility methods to the `Object` class without modifying the global | ||
* namespace directly. This patch includes methods for key validation, object | ||
* type checking, and retrieving the string tag of an object. These methods | ||
* are useful for enhancing the capabilities of the standard `Object` class | ||
* with additional utility functions. | ||
*/ | ||
exports.ObjectExtensions = new extension_1.Patch(Object, { | ||
/** | ||
* The function checks if a value is either `undefined` or `null`. | ||
* | ||
* @param {any} value - The parameter "value" is a variable that can hold | ||
* any value. | ||
* @returns {boolean} `true` if the value is either `undefined` or `null`, | ||
* and `false` otherwise. | ||
*/ | ||
isNullDefined(value) { | ||
return value === undefined || value === null; | ||
}, | ||
/** | ||
* Checks to see if the supplied `value` is both an object, and has the | ||
@@ -26,14 +37,21 @@ * appropriate symbol defined. | ||
/** | ||
* Retrieves the string tag of an object. The string tag is a representation of | ||
* the object's type, as defined by its `Object.prototype.toString` method. This | ||
* utility method is helpful for getting a more descriptive type of an object than | ||
* what is returned by the `typeof` operator, especially for custom objects. | ||
* Retrieves the string tag of an object. The string tag is a representation | ||
* of the object's type, as defined by its `Object.prototype.toString` | ||
* method. This utility method is helpful for getting a more descriptive | ||
* type of an object than what is returned by the `typeof` operator, | ||
* especially for custom objects. | ||
* | ||
* @param {*} value - The object whose string tag is to be retrieved. | ||
* @param {boolean} strict - if this is set to true, undefined will be | ||
* returned whenever a supplied object does not have a | ||
* `Symbol.toStringTag` defined, period. if false, the default, | ||
* @returns {string} - The string tag of the object, indicating its type. | ||
*/ | ||
getStringTag(value) { | ||
getStringTag(value, strict = false) { | ||
if (Object.hasStringTag(value)) { | ||
return value[Symbol.toStringTag]; | ||
} | ||
if (strict) { | ||
return undefined; | ||
} | ||
if (value && (typeof value === 'function')) { | ||
@@ -56,9 +74,9 @@ return value.name; | ||
* @param {object} [owner=globalThis] - The object in which to look up the | ||
* constructor corresponding to the string tag. Defaults to `globalThis`, which | ||
* covers global constructors like `Array`, `Object`, etc. | ||
* @returns {Function|object|null|undefined} - Returns the constructor or type | ||
* of the value based on its string tag. For 'Null' and 'Undefined', it returns | ||
* `null` and `undefined`, respectively. For other types, it returns the | ||
* corresponding constructor (e.g., `Array` for arrays) if available in the | ||
* `owner` object. | ||
* constructor corresponding to the string tag. Defaults to `globalThis`, | ||
* which covers global constructors like `Array`, `Object`, etc. | ||
* @returns {Function|object|null|undefined} - Returns the constructor or | ||
* type of the value based on its string tag. For 'Null' and 'Undefined', | ||
* it returns `null` and `undefined`, respectively. For other types, it | ||
* returns the corresponding constructor (e.g., `Array` for arrays) if | ||
* available in the `owner` object. | ||
*/ | ||
@@ -75,9 +93,10 @@ getType(value, owner = globalThis) { | ||
/** | ||
* Determines if the provided value is an object. This method checks whether the | ||
* value is an instance of `Object` or if its type is 'object'. It's a utility | ||
* method for type-checking, ensuring that a value is an object before performing | ||
* operations that are specific to objects. | ||
* Determines if the provided value is an object. This method checks whether | ||
* the value is an instance of `Object` or if its type is 'object'. It's a | ||
* utility method for type-checking, ensuring that a value is an object | ||
* before performing operations that are specific to objects. | ||
* | ||
* @param {*} value - The value to be checked. | ||
* @returns {boolean} - Returns `true` if the value is an object, otherwise `false`. | ||
* @returns {boolean} - Returns `true` if the value is an object, | ||
* otherwise `false`. | ||
*/ | ||
@@ -114,9 +133,10 @@ isObject(value) { | ||
/** | ||
* Checks if the given value is a valid key for an object. In JavaScript, a valid | ||
* key can be either a string or a symbol. This method is useful for validating | ||
* object keys before using them in operations like setting or getting object properties. | ||
* Checks if the given value is a valid key for an object. In JavaScript, a | ||
* valid key can be either a string or a symbol. This method is useful for | ||
* validating object keys before using them in operations like setting or | ||
* getting object properties. | ||
* | ||
* @param {*} value - The value to be checked. | ||
* @returns {boolean} - Returns `true` if the value is a valid object key (string or symbol), | ||
* otherwise `false`. | ||
* @returns {boolean} - Returns `true` if the value is a valid object key | ||
* (string or symbol), otherwise `false`. | ||
*/ | ||
@@ -133,10 +153,13 @@ isValidKey(value) { | ||
* final reduced object | ||
* @param {boolean} [bindAccessors = true] if this value is true | ||
* then any accessors from the source object will continue to have their | ||
* `this` value bound to the source. If the getter or setter on that object | ||
* is defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols specified | ||
* in the `keys` parameter. | ||
* @param {boolean} [bindAccessors = true] if this value is true then any | ||
* accessors from the source object will continue to have their `this` | ||
* value bound to the source. If the getter or setter on that object is | ||
* defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols | ||
* specified in the `keys` parameter. | ||
*/ | ||
stripTo(object, keys, bindAccessors = true) { | ||
if (!object || typeof object !== 'object') { | ||
throw new TypeError('Object.stripTo requires an object to strip. Received', object); | ||
} | ||
const result = {}; | ||
@@ -148,13 +171,12 @@ if (!Array.isArray(keys)) { | ||
if (Reflect.has(object, key)) { | ||
const descriptor = Object.getOwnPropertyDescriptor(object, key); | ||
if (Reflect.has(descriptor, 'get') || Reflect.has(descriptor, 'set')) { | ||
const originalDescriptor = Object.getOwnPropertyDescriptor(object, key); | ||
const descriptor = { ...originalDescriptor }; | ||
if (typeof descriptor.get === 'function' || | ||
typeof descriptor.set === 'function') { | ||
if (bindAccessors) { | ||
descriptor.get = descriptor?.get?.bind(object); | ||
descriptor.set = descriptor?.set?.bind(object); | ||
descriptor.get = descriptor.get?.bind(object); | ||
descriptor.set = descriptor.set?.bind(object); | ||
} | ||
Object.defineProperty(result, descriptor); | ||
} | ||
else { | ||
Object.defineProperty(result, descriptor); | ||
} | ||
Object.defineProperty(result, key, descriptor); | ||
} | ||
@@ -165,1 +187,21 @@ } | ||
}); | ||
exports.ObjectPrototypeExtensions = new extension_1.Patch(Object.prototype, { | ||
/** | ||
* Strips an object down to only the keys specified. Optionally, any | ||
* accessors can be made to retain their context on the source object. | ||
* This is a passthrough to the static {@link Object.stripTo} function | ||
* | ||
* @param {Array<string|symbol>} keys the keys that should appear in the | ||
* final reduced object | ||
* @param {boolean} [bindAccessors = true] if this value is true then any | ||
* accessors from the source object will continue to have their `this` | ||
* value bound to the source. If the getter or setter on that object is | ||
* defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols | ||
* specified in the `keys` parameter. | ||
*/ | ||
stripTo(keys, bindAccessors = true) { | ||
return Object.stripTo(this, keys, bindAccessors); | ||
} | ||
}); | ||
//# sourceMappingURL=objectextensions.js.map |
@@ -6,2 +6,3 @@ "use strict"; | ||
const objectextensions_js_1 = require("./objectextensions.js"); | ||
const { isObject } = objectextensions_js_1.ObjectExtensions.patches; | ||
/** | ||
@@ -35,14 +36,17 @@ * The `ReflectExtensions` class is a patch applied to the built-in JavaScript | ||
}, | ||
/** | ||
* Fetches all descriptors of an object, including those mapped to a | ||
* symbol descriptor value. | ||
* | ||
* @param {object} object the object from whose descriptors need to be | ||
* retrieved. | ||
* @returns {object} with keys mapped to object descriptors | ||
* @throws {TypeError} if the supplied `object` is null or not an object | ||
* a TypeError exception will be thrown | ||
*/ | ||
ownDescriptors(object) { | ||
if (!isObject(object)) { | ||
throw new TypeError('The supplied object must be non-null and an object'); | ||
} | ||
const result = {}; | ||
const revertOnDone = () => revertOnDone.doIt ? objectextensions_js_1.ObjectExtensions.revert() : ''; | ||
revertOnDone.doIt = false; | ||
if (!Object.isObject) { | ||
revertOnDone.doIt = true; | ||
objectextensions_js_1.ObjectExtensions.apply(); | ||
} | ||
if (!Object.isObject(object)) { | ||
revertOnDone(); | ||
return {}; | ||
} | ||
const keys = Reflect.ownKeys(object); | ||
@@ -52,3 +56,2 @@ for (const key of keys) { | ||
} | ||
revertOnDone(); | ||
return result; | ||
@@ -68,3 +71,3 @@ }, | ||
hasSome(object, ...keys) { | ||
return Object.isObject(object) && (keys.flat(Infinity) | ||
return isObject(object) && (keys.flat(Infinity) | ||
.map(key => Reflect.has(object, key)) | ||
@@ -112,1 +115,2 @@ .some(has => has)); | ||
}); | ||
//# sourceMappingURL=reflectextensions.js.map |
@@ -28,1 +28,2 @@ "use strict"; | ||
}); | ||
//# sourceMappingURL=stringextensions.js.map |
@@ -62,3 +62,4 @@ "use strict"; | ||
* `Symbol('name')` | ||
* @returns | ||
* @returns true if the `value` in question is both a `symbol` and has | ||
* returns `undefined` if passed to `Symbol.keyFor` | ||
*/ | ||
@@ -69,1 +70,2 @@ isNonRegistered(value, allowOnlySymbols = false) { | ||
}); | ||
//# sourceMappingURL=symbolextensions.js.map |
@@ -19,1 +19,2 @@ "use strict"; | ||
}); | ||
//# sourceMappingURL=weakrefextensions.js.map |
@@ -68,1 +68,2 @@ import { Patch } from '@nejs/extension'; | ||
}); | ||
//# sourceMappingURL=arrayextensions.js.map |
@@ -87,1 +87,2 @@ import { Patch } from '@nejs/extension'; | ||
}); | ||
//# sourceMappingURL=functionextensions.js.map |
import { Patch } from '@nejs/extension'; | ||
import { FunctionExtensions } from './functionextensions.js'; | ||
const { isClass, isFunction } = FunctionExtensions.patchEntries.isClass.computed; | ||
const { isClass, isFunction } = FunctionExtensions.patches; | ||
const CustomInspect = Symbol.for('nodejs.util.inspect.custom'); | ||
@@ -163,1 +163,2 @@ export const GlobalFunctionsAndProps = new Patch(globalThis, { | ||
}); | ||
//# sourceMappingURL=globals.js.map |
@@ -1,19 +0,10 @@ | ||
export function enableAll(owners: any): void; | ||
export function enableNetNew(): void; | ||
export function disableAll(owners: any): void; | ||
export function disableNetNew(): void; | ||
export const all: {}; | ||
import { ObjectExtensions } from './objectextensions.js'; | ||
import { FunctionExtensions } from './functionextensions.js'; | ||
import { ReflectExtensions } from './reflectextensions.js'; | ||
import { StringExtensions } from './stringextensions.js'; | ||
import { SymbolExtensions } from './symbolextensions.js'; | ||
import { ArrayPrototypeExtensions } from './arrayextensions.js'; | ||
import { GlobalFunctionsAndProps } from './globals.js'; | ||
import { DescriptorExtensions } from './descriptor.js'; | ||
import { AsyncIterableExtensions } from './asyncIterable.js'; | ||
import { AsyncIteratorExtensions } from './asyncIterable.js'; | ||
import { IterableExtensions } from './iterable.js'; | ||
import { IteratorExtensions } from './iterable.js'; | ||
import { RefSetExtensions } from './refset.js'; | ||
export { ObjectExtensions, FunctionExtensions, ReflectExtensions, StringExtensions, SymbolExtensions, ArrayPrototypeExtensions, GlobalFunctionsAndProps, DescriptorExtensions, AsyncIterableExtensions, AsyncIteratorExtensions, IterableExtensions, IteratorExtensions, RefSetExtensions }; | ||
export const all: any; | ||
export default results; | ||
declare namespace results { | ||
export { Extensions as extensions }; | ||
export { Patches as patches }; | ||
export { all }; | ||
} | ||
export const Extensions: {}; | ||
export const Patches: Map<Object, import("@nejs/extension").Patch>; | ||
export const Controls: {}; |
import { FunctionExtensions } from './functionextensions.js'; | ||
import { ObjectExtensions } from './objectextensions.js'; | ||
import { ObjectExtensions, ObjectPrototypeExtensions } from './objectextensions.js'; | ||
import { MapPrototypeExtensions } from './mapextensions.js'; | ||
import { ReflectExtensions } from './reflectextensions.js'; | ||
@@ -7,65 +8,63 @@ import { StringExtensions } from './stringextensions.js'; | ||
import { ArrayPrototypeExtensions } from './arrayextensions.js'; | ||
import { DescriptorExtensions } from './descriptor.js'; | ||
import { DescriptorExtensions, Descriptor } from './newClasses/descriptor.js'; | ||
import { GlobalFunctionsAndProps } from './globals.js'; | ||
import { RefSetExtensions } from './refset.js'; | ||
import { AsyncIteratorExtensions, AsyncIterableExtensions } from './asyncIterable.js'; | ||
import { IteratorExtensions, IterableExtensions } from './iterable.js'; | ||
import { Patch } from '@nejs/extension'; | ||
const Owners = [ | ||
Object, | ||
Function, | ||
Reflect, | ||
String, | ||
Symbol, | ||
Array.prototype, | ||
]; | ||
const NetNew = [ | ||
GlobalFunctionsAndProps, | ||
DescriptorExtensions, | ||
AsyncIterableExtensions, | ||
AsyncIteratorExtensions, | ||
IterableExtensions, | ||
IteratorExtensions, | ||
RefSetExtensions, | ||
]; | ||
export function enableAll(owners) { | ||
const list = owners || Owners; | ||
if (!list) { | ||
throw new Error('Unable to enable features without owners list'); | ||
} | ||
list.forEach(owner => { | ||
Patch.enableFor(owner); | ||
}); | ||
enableNetNew(); | ||
} | ||
export function enableNetNew() { | ||
NetNew.forEach(extension => { extension.apply(); }); | ||
} | ||
export function disableAll(owners) { | ||
const list = owners || Owners; | ||
if (!list) { | ||
throw new Error('Unable to disable features without owners list'); | ||
} | ||
list.forEach(owner => { | ||
Patch.disableFor(owner); | ||
}); | ||
disableNetNew(); | ||
} | ||
export function disableNetNew() { | ||
NetNew.forEach(extension => { extension.revert(); }); | ||
} | ||
import { RefSetExtensions } from './newClasses/refset.js'; | ||
import { RefMapExtensions } from './newClasses/refmap.js'; | ||
import { AsyncIteratorExtensions, AsyncIterableExtensions } from './newClasses/asyncIterable.js'; | ||
import { IteratorExtensions, IterableExtensions } from './newClasses/iterable.js'; | ||
const Patches = new Map([ | ||
[Object, ObjectExtensions], | ||
[Function, FunctionExtensions], | ||
[Reflect, ReflectExtensions], | ||
[String, StringExtensions], | ||
[Symbol, SymbolExtensions], | ||
[Object.prototype, ObjectPrototypeExtensions], | ||
[Array.prototype, ArrayPrototypeExtensions], | ||
[Map.prototype, MapPrototypeExtensions], | ||
[globalThis, GlobalFunctionsAndProps], | ||
]); | ||
const Extensions = { | ||
[DescriptorExtensions.key]: DescriptorExtensions, | ||
[AsyncIterableExtensions.key]: AsyncIterableExtensions, | ||
[AsyncIteratorExtensions.key]: AsyncIteratorExtensions, | ||
[IterableExtensions.key]: IterableExtensions, | ||
[IteratorExtensions.key]: IteratorExtensions, | ||
[RefSetExtensions.key]: RefSetExtensions, | ||
[RefMapExtensions.key]: RefMapExtensions, | ||
}; | ||
const Controls = {}; | ||
Object.assign(Controls, { | ||
enableAll() { | ||
Controls.enablePatches(); | ||
Controls.enableExtensions(); | ||
}, | ||
enablePatches() { | ||
Patches.forEach((extension) => { extension.apply(); }); | ||
}, | ||
enableExtensions() { | ||
Object.values(Extensions).forEach((extension) => { extension.apply(); }); | ||
}, | ||
disableAll(owners) { | ||
Controls.disablePatches(); | ||
Controls.disableExtensions(); | ||
}, | ||
disablePatches() { | ||
Patches.forEach((extension) => { extension.revert(); }); | ||
}, | ||
disableExtensions() { | ||
Object.values(Extensions).forEach((extension) => { extension.revert(); }); | ||
}, | ||
}); | ||
export const all = (() => { | ||
let extensions = [ | ||
ObjectExtensions, | ||
FunctionExtensions, | ||
ReflectExtensions, | ||
StringExtensions, | ||
SymbolExtensions, | ||
ArrayPrototypeExtensions, | ||
GlobalFunctionsAndProps, | ||
DescriptorExtensions, | ||
const extensions = [ | ||
...Array.from(Patches.values()), | ||
...Array.from(Object.values(Extensions)), | ||
]; | ||
const dest = extensions.reduce((accumulator, extension) => { | ||
Reflect.ownKeys(extension.patchEntries).reduce((_, key) => { | ||
accumulator[key] = extension.patchEntries[key].computed; | ||
const entry = extension.patchEntries[key]; | ||
if (entry.isAccessor) | ||
accumulator[key] = new Descriptor(entry.descriptor); | ||
else | ||
accumulator[key] = entry.computed; | ||
return accumulator; | ||
@@ -77,2 +76,10 @@ }, accumulator); | ||
})(); | ||
export { ObjectExtensions, FunctionExtensions, ReflectExtensions, StringExtensions, SymbolExtensions, ArrayPrototypeExtensions, GlobalFunctionsAndProps, DescriptorExtensions, AsyncIterableExtensions, AsyncIteratorExtensions, IterableExtensions, IteratorExtensions, RefSetExtensions, }; | ||
const results = { | ||
...Controls, | ||
extensions: Extensions, | ||
patches: Patches, | ||
all, | ||
}; | ||
export default results; | ||
export { Extensions, Patches, Controls, }; | ||
//# sourceMappingURL=index.js.map |
/** | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. It | ||
* adds utility methods to the `Object` class without modifying the global namespace | ||
* directly. This patch includes methods for key validation, object type checking, | ||
* and retrieving the string tag of an object. These methods are useful for | ||
* enhancing the capabilities of the standard `Object` class with additional | ||
* utility functions. | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. | ||
* It adds utility methods to the `Object` class without modifying the global | ||
* namespace directly. This patch includes methods for key validation, object | ||
* type checking, and retrieving the string tag of an object. These methods | ||
* are useful for enhancing the capabilities of the standard `Object` class | ||
* with additional utility functions. | ||
*/ | ||
export const ObjectExtensions: Patch; | ||
export const ObjectPrototypeExtensions: Patch; | ||
import { Patch } from '@nejs/extension'; |
import { Patch } from '@nejs/extension'; | ||
/** | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. It | ||
* adds utility methods to the `Object` class without modifying the global namespace | ||
* directly. This patch includes methods for key validation, object type checking, | ||
* and retrieving the string tag of an object. These methods are useful for | ||
* enhancing the capabilities of the standard `Object` class with additional | ||
* utility functions. | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. | ||
* It adds utility methods to the `Object` class without modifying the global | ||
* namespace directly. This patch includes methods for key validation, object | ||
* type checking, and retrieving the string tag of an object. These methods | ||
* are useful for enhancing the capabilities of the standard `Object` class | ||
* with additional utility functions. | ||
*/ | ||
export const ObjectExtensions = new Patch(Object, { | ||
/** | ||
* The function checks if a value is either `undefined` or `null`. | ||
* | ||
* @param {any} value - The parameter "value" is a variable that can hold | ||
* any value. | ||
* @returns {boolean} `true` if the value is either `undefined` or `null`, | ||
* and `false` otherwise. | ||
*/ | ||
isNullDefined(value) { | ||
return value === undefined || value === null; | ||
}, | ||
/** | ||
* Checks to see if the supplied `value` is both an object, and has the | ||
@@ -23,14 +34,21 @@ * appropriate symbol defined. | ||
/** | ||
* Retrieves the string tag of an object. The string tag is a representation of | ||
* the object's type, as defined by its `Object.prototype.toString` method. This | ||
* utility method is helpful for getting a more descriptive type of an object than | ||
* what is returned by the `typeof` operator, especially for custom objects. | ||
* Retrieves the string tag of an object. The string tag is a representation | ||
* of the object's type, as defined by its `Object.prototype.toString` | ||
* method. This utility method is helpful for getting a more descriptive | ||
* type of an object than what is returned by the `typeof` operator, | ||
* especially for custom objects. | ||
* | ||
* @param {*} value - The object whose string tag is to be retrieved. | ||
* @param {boolean} strict - if this is set to true, undefined will be | ||
* returned whenever a supplied object does not have a | ||
* `Symbol.toStringTag` defined, period. if false, the default, | ||
* @returns {string} - The string tag of the object, indicating its type. | ||
*/ | ||
getStringTag(value) { | ||
getStringTag(value, strict = false) { | ||
if (Object.hasStringTag(value)) { | ||
return value[Symbol.toStringTag]; | ||
} | ||
if (strict) { | ||
return undefined; | ||
} | ||
if (value && (typeof value === 'function')) { | ||
@@ -53,9 +71,9 @@ return value.name; | ||
* @param {object} [owner=globalThis] - The object in which to look up the | ||
* constructor corresponding to the string tag. Defaults to `globalThis`, which | ||
* covers global constructors like `Array`, `Object`, etc. | ||
* @returns {Function|object|null|undefined} - Returns the constructor or type | ||
* of the value based on its string tag. For 'Null' and 'Undefined', it returns | ||
* `null` and `undefined`, respectively. For other types, it returns the | ||
* corresponding constructor (e.g., `Array` for arrays) if available in the | ||
* `owner` object. | ||
* constructor corresponding to the string tag. Defaults to `globalThis`, | ||
* which covers global constructors like `Array`, `Object`, etc. | ||
* @returns {Function|object|null|undefined} - Returns the constructor or | ||
* type of the value based on its string tag. For 'Null' and 'Undefined', | ||
* it returns `null` and `undefined`, respectively. For other types, it | ||
* returns the corresponding constructor (e.g., `Array` for arrays) if | ||
* available in the `owner` object. | ||
*/ | ||
@@ -72,9 +90,10 @@ getType(value, owner = globalThis) { | ||
/** | ||
* Determines if the provided value is an object. This method checks whether the | ||
* value is an instance of `Object` or if its type is 'object'. It's a utility | ||
* method for type-checking, ensuring that a value is an object before performing | ||
* operations that are specific to objects. | ||
* Determines if the provided value is an object. This method checks whether | ||
* the value is an instance of `Object` or if its type is 'object'. It's a | ||
* utility method for type-checking, ensuring that a value is an object | ||
* before performing operations that are specific to objects. | ||
* | ||
* @param {*} value - The value to be checked. | ||
* @returns {boolean} - Returns `true` if the value is an object, otherwise `false`. | ||
* @returns {boolean} - Returns `true` if the value is an object, | ||
* otherwise `false`. | ||
*/ | ||
@@ -111,9 +130,10 @@ isObject(value) { | ||
/** | ||
* Checks if the given value is a valid key for an object. In JavaScript, a valid | ||
* key can be either a string or a symbol. This method is useful for validating | ||
* object keys before using them in operations like setting or getting object properties. | ||
* Checks if the given value is a valid key for an object. In JavaScript, a | ||
* valid key can be either a string or a symbol. This method is useful for | ||
* validating object keys before using them in operations like setting or | ||
* getting object properties. | ||
* | ||
* @param {*} value - The value to be checked. | ||
* @returns {boolean} - Returns `true` if the value is a valid object key (string or symbol), | ||
* otherwise `false`. | ||
* @returns {boolean} - Returns `true` if the value is a valid object key | ||
* (string or symbol), otherwise `false`. | ||
*/ | ||
@@ -130,10 +150,13 @@ isValidKey(value) { | ||
* final reduced object | ||
* @param {boolean} [bindAccessors = true] if this value is true | ||
* then any accessors from the source object will continue to have their | ||
* `this` value bound to the source. If the getter or setter on that object | ||
* is defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols specified | ||
* in the `keys` parameter. | ||
* @param {boolean} [bindAccessors = true] if this value is true then any | ||
* accessors from the source object will continue to have their `this` | ||
* value bound to the source. If the getter or setter on that object is | ||
* defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols | ||
* specified in the `keys` parameter. | ||
*/ | ||
stripTo(object, keys, bindAccessors = true) { | ||
if (!object || typeof object !== 'object') { | ||
throw new TypeError('Object.stripTo requires an object to strip. Received', object); | ||
} | ||
const result = {}; | ||
@@ -145,13 +168,12 @@ if (!Array.isArray(keys)) { | ||
if (Reflect.has(object, key)) { | ||
const descriptor = Object.getOwnPropertyDescriptor(object, key); | ||
if (Reflect.has(descriptor, 'get') || Reflect.has(descriptor, 'set')) { | ||
const originalDescriptor = Object.getOwnPropertyDescriptor(object, key); | ||
const descriptor = { ...originalDescriptor }; | ||
if (typeof descriptor.get === 'function' || | ||
typeof descriptor.set === 'function') { | ||
if (bindAccessors) { | ||
descriptor.get = descriptor?.get?.bind(object); | ||
descriptor.set = descriptor?.set?.bind(object); | ||
descriptor.get = descriptor.get?.bind(object); | ||
descriptor.set = descriptor.set?.bind(object); | ||
} | ||
Object.defineProperty(result, descriptor); | ||
} | ||
else { | ||
Object.defineProperty(result, descriptor); | ||
} | ||
Object.defineProperty(result, key, descriptor); | ||
} | ||
@@ -162,1 +184,21 @@ } | ||
}); | ||
export const ObjectPrototypeExtensions = new Patch(Object.prototype, { | ||
/** | ||
* Strips an object down to only the keys specified. Optionally, any | ||
* accessors can be made to retain their context on the source object. | ||
* This is a passthrough to the static {@link Object.stripTo} function | ||
* | ||
* @param {Array<string|symbol>} keys the keys that should appear in the | ||
* final reduced object | ||
* @param {boolean} [bindAccessors = true] if this value is true then any | ||
* accessors from the source object will continue to have their `this` | ||
* value bound to the source. If the getter or setter on that object is | ||
* defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols | ||
* specified in the `keys` parameter. | ||
*/ | ||
stripTo(keys, bindAccessors = true) { | ||
return Object.stripTo(this, keys, bindAccessors); | ||
} | ||
}); | ||
//# sourceMappingURL=objectextensions.js.map |
import { Patch } from '@nejs/extension'; | ||
import { ObjectExtensions } from './objectextensions.js'; | ||
const { isObject } = ObjectExtensions.patches; | ||
/** | ||
@@ -31,14 +32,17 @@ * The `ReflectExtensions` class is a patch applied to the built-in JavaScript | ||
}, | ||
/** | ||
* Fetches all descriptors of an object, including those mapped to a | ||
* symbol descriptor value. | ||
* | ||
* @param {object} object the object from whose descriptors need to be | ||
* retrieved. | ||
* @returns {object} with keys mapped to object descriptors | ||
* @throws {TypeError} if the supplied `object` is null or not an object | ||
* a TypeError exception will be thrown | ||
*/ | ||
ownDescriptors(object) { | ||
if (!isObject(object)) { | ||
throw new TypeError('The supplied object must be non-null and an object'); | ||
} | ||
const result = {}; | ||
const revertOnDone = () => revertOnDone.doIt ? ObjectExtensions.revert() : ''; | ||
revertOnDone.doIt = false; | ||
if (!Object.isObject) { | ||
revertOnDone.doIt = true; | ||
ObjectExtensions.apply(); | ||
} | ||
if (!Object.isObject(object)) { | ||
revertOnDone(); | ||
return {}; | ||
} | ||
const keys = Reflect.ownKeys(object); | ||
@@ -48,3 +52,2 @@ for (const key of keys) { | ||
} | ||
revertOnDone(); | ||
return result; | ||
@@ -64,3 +67,3 @@ }, | ||
hasSome(object, ...keys) { | ||
return Object.isObject(object) && (keys.flat(Infinity) | ||
return isObject(object) && (keys.flat(Infinity) | ||
.map(key => Reflect.has(object, key)) | ||
@@ -108,1 +111,2 @@ .some(has => has)); | ||
}); | ||
//# sourceMappingURL=reflectextensions.js.map |
@@ -25,1 +25,2 @@ import { Patch } from '@nejs/extension'; | ||
}); | ||
//# sourceMappingURL=stringextensions.js.map |
@@ -59,3 +59,4 @@ import { Patch } from '@nejs/extension'; | ||
* `Symbol('name')` | ||
* @returns | ||
* @returns true if the `value` in question is both a `symbol` and has | ||
* returns `undefined` if passed to `Symbol.keyFor` | ||
*/ | ||
@@ -66,1 +67,2 @@ isNonRegistered(value, allowOnlySymbols = false) { | ||
}); | ||
//# sourceMappingURL=symbolextensions.js.map |
@@ -16,1 +16,2 @@ import { Patch } from '@nejs/extension'; | ||
}); | ||
//# sourceMappingURL=weakrefextensions.js.map |
@@ -6,6 +6,13 @@ { | ||
"@jest/expect": "^29.7.0", | ||
"clean-documentation-theme": "^0.5.2", | ||
"docdash": "^2.0.2", | ||
"documentation": "^14.0.2", | ||
"documentation-devseed-theme": "^0.1.7", | ||
"esbuild": "^0.19.10", | ||
"esdoc": "^1.1.0", | ||
"esdoc-standard-plugin": "^1.0.0", | ||
"jest": "^29.7.0", | ||
"jest-cli": "^29.7.0", | ||
"jest-localstorage-mock": "^2.4.26", | ||
"jsdoc": "^4.0.2", | ||
"prompts": "^2.4.2", | ||
@@ -46,2 +53,4 @@ "rimraf": "^5.0.5", | ||
"clean": "bin/clean", | ||
"jsdoc": "jsdoc -c jsdoc-config.json -p -a all -R README.md", | ||
"documentation": "documentation build src/** -f html --github -o docs && documentation readme src/index.js --section=API", | ||
"module": "bin/build", | ||
@@ -51,7 +60,7 @@ "test": "jest" | ||
"type": "module", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"dependencies": { | ||
"@nejs/extension": "^1.3.0" | ||
"@nejs/extension": "^2.1.0" | ||
}, | ||
"browser": "dist/@nejs/basic-extensions.bundle.1.6.0.js" | ||
"browser": "dist/@nejs/basic-extensions.bundle.1.6.1.js" | ||
} |
import { Patch } from '@nejs/extension' | ||
import { FunctionExtensions } from './functionextensions.js' | ||
const { isClass, isFunction } = FunctionExtensions.patchEntries.isClass.computed | ||
const { isClass, isFunction } = FunctionExtensions.patches | ||
const CustomInspect = Symbol.for('nodejs.util.inspect.custom') | ||
@@ -6,0 +6,0 @@ |
151
src/index.js
import { FunctionExtensions } from './functionextensions.js' | ||
import { ObjectExtensions } from './objectextensions.js' | ||
import { ObjectExtensions, ObjectPrototypeExtensions } from './objectextensions.js' | ||
import { MapPrototypeExtensions } from './mapextensions.js' | ||
import { ReflectExtensions } from './reflectextensions.js' | ||
@@ -7,5 +8,6 @@ import { StringExtensions } from './stringextensions.js' | ||
import { ArrayPrototypeExtensions } from './arrayextensions.js' | ||
import { DescriptorExtensions } from './descriptor.js' | ||
import { DescriptorExtensions, Descriptor } from './newClasses/descriptor.js' | ||
import { GlobalFunctionsAndProps } from './globals.js' | ||
import { RefSetExtensions } from './refset.js' | ||
import { RefSetExtensions } from './newClasses/refset.js' | ||
import { RefMapExtensions } from './newClasses/refmap.js' | ||
@@ -15,3 +17,3 @@ import { | ||
AsyncIterableExtensions | ||
} from './asyncIterable.js' | ||
} from './newClasses/asyncIterable.js' | ||
@@ -21,73 +23,61 @@ import { | ||
IterableExtensions | ||
} from './iterable.js' | ||
} from './newClasses/iterable.js' | ||
import { Patch } from '@nejs/extension' | ||
const Patches = new Map([ | ||
[Object, ObjectExtensions], | ||
[Function, FunctionExtensions], | ||
[Reflect, ReflectExtensions], | ||
[String, StringExtensions], | ||
[Symbol, SymbolExtensions], | ||
const Owners = [ | ||
Object, | ||
Function, | ||
Reflect, | ||
String, | ||
Symbol, | ||
[Object.prototype, ObjectPrototypeExtensions], | ||
[Array.prototype, ArrayPrototypeExtensions], | ||
[Map.prototype, MapPrototypeExtensions], | ||
[globalThis, GlobalFunctionsAndProps], | ||
]) | ||
Array.prototype, | ||
] | ||
const Extensions = { | ||
[DescriptorExtensions.key]: DescriptorExtensions, | ||
[AsyncIterableExtensions.key]: AsyncIterableExtensions, | ||
[AsyncIteratorExtensions.key]: AsyncIteratorExtensions, | ||
[IterableExtensions.key]: IterableExtensions, | ||
[IteratorExtensions.key]: IteratorExtensions, | ||
[RefSetExtensions.key]: RefSetExtensions, | ||
[RefMapExtensions.key]: RefMapExtensions, | ||
} | ||
const NetNew = [ | ||
GlobalFunctionsAndProps, | ||
DescriptorExtensions, | ||
AsyncIterableExtensions, | ||
AsyncIteratorExtensions, | ||
IterableExtensions, | ||
IteratorExtensions, | ||
RefSetExtensions, | ||
] | ||
const Controls = {} | ||
export function enableAll(owners) { | ||
const list = owners || Owners | ||
Object.assign(Controls, { | ||
enableAll() { | ||
Controls.enablePatches() | ||
Controls.enableExtensions() | ||
}, | ||
if (!list) { | ||
throw new Error('Unable to enable features without owners list') | ||
} | ||
enablePatches() { | ||
Patches.forEach((extension) => { extension.apply() }) | ||
}, | ||
list.forEach(owner => { | ||
Patch.enableFor(owner) | ||
}) | ||
enableExtensions() { | ||
Object.values(Extensions).forEach((extension) => { extension.apply() }) | ||
}, | ||
enableNetNew() | ||
} | ||
disableAll(owners) { | ||
Controls.disablePatches() | ||
Controls.disableExtensions() | ||
}, | ||
export function enableNetNew() { | ||
NetNew.forEach(extension => { extension.apply() }) | ||
} | ||
disablePatches() { | ||
Patches.forEach((extension) => { extension.revert() }) | ||
}, | ||
export function disableAll(owners) { | ||
const list = owners || Owners | ||
disableExtensions() { | ||
Object.values(Extensions).forEach((extension) => { extension.revert() }) | ||
}, | ||
}) | ||
if (!list) { | ||
throw new Error('Unable to disable features without owners list') | ||
} | ||
list.forEach(owner => { | ||
Patch.disableFor(owner) | ||
}) | ||
disableNetNew() | ||
} | ||
export function disableNetNew() { | ||
NetNew.forEach(extension => { extension.revert() }) | ||
} | ||
export const all = (() => { | ||
let extensions = [ | ||
ObjectExtensions, | ||
FunctionExtensions, | ||
ReflectExtensions, | ||
StringExtensions, | ||
SymbolExtensions, | ||
ArrayPrototypeExtensions, | ||
GlobalFunctionsAndProps, | ||
DescriptorExtensions, | ||
const extensions = [ | ||
...Array.from(Patches.values()), | ||
...Array.from(Object.values(Extensions)), | ||
] | ||
@@ -97,3 +87,9 @@ | ||
Reflect.ownKeys(extension.patchEntries).reduce((_, key) => { | ||
accumulator[key] = extension.patchEntries[key].computed | ||
const entry = extension.patchEntries[key] | ||
if (entry.isAccessor) | ||
accumulator[key] = new Descriptor(entry.descriptor) | ||
else | ||
accumulator[key] = entry.computed | ||
return accumulator | ||
@@ -108,18 +104,15 @@ }, accumulator) | ||
const results = { | ||
...Controls, | ||
extensions: Extensions, | ||
patches: Patches, | ||
all, | ||
} | ||
export default results | ||
export { | ||
ObjectExtensions, | ||
FunctionExtensions, | ||
ReflectExtensions, | ||
StringExtensions, | ||
SymbolExtensions, | ||
ArrayPrototypeExtensions, | ||
GlobalFunctionsAndProps, | ||
DescriptorExtensions, | ||
AsyncIterableExtensions, | ||
AsyncIteratorExtensions, | ||
IterableExtensions, | ||
IteratorExtensions, | ||
RefSetExtensions, | ||
} | ||
Extensions, | ||
Patches, | ||
Controls, | ||
} |
import { Patch } from '@nejs/extension'; | ||
/** | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. It | ||
* adds utility methods to the `Object` class without modifying the global namespace | ||
* directly. This patch includes methods for key validation, object type checking, | ||
* and retrieving the string tag of an object. These methods are useful for | ||
* enhancing the capabilities of the standard `Object` class with additional | ||
* utility functions. | ||
* `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. | ||
* It adds utility methods to the `Object` class without modifying the global | ||
* namespace directly. This patch includes methods for key validation, object | ||
* type checking, and retrieving the string tag of an object. These methods | ||
* are useful for enhancing the capabilities of the standard `Object` class | ||
* with additional utility functions. | ||
*/ | ||
export const ObjectExtensions = new Patch(Object, { | ||
/** | ||
* The function checks if a value is either `undefined` or `null`. | ||
* | ||
* @param {any} value - The parameter "value" is a variable that can hold | ||
* any value. | ||
* @returns {boolean} `true` if the value is either `undefined` or `null`, | ||
* and `false` otherwise. | ||
*/ | ||
isNullDefined(value) { | ||
return value === undefined || value === null | ||
}, | ||
/** | ||
* Checks to see if the supplied `value` is both an object, and has the | ||
@@ -25,11 +37,15 @@ * appropriate symbol defined. | ||
/** | ||
* Retrieves the string tag of an object. The string tag is a representation of | ||
* the object's type, as defined by its `Object.prototype.toString` method. This | ||
* utility method is helpful for getting a more descriptive type of an object than | ||
* what is returned by the `typeof` operator, especially for custom objects. | ||
* Retrieves the string tag of an object. The string tag is a representation | ||
* of the object's type, as defined by its `Object.prototype.toString` | ||
* method. This utility method is helpful for getting a more descriptive | ||
* type of an object than what is returned by the `typeof` operator, | ||
* especially for custom objects. | ||
* | ||
* @param {*} value - The object whose string tag is to be retrieved. | ||
* @param {boolean} strict - if this is set to true, undefined will be | ||
* returned whenever a supplied object does not have a | ||
* `Symbol.toStringTag` defined, period. if false, the default, | ||
* @returns {string} - The string tag of the object, indicating its type. | ||
*/ | ||
getStringTag(value) { | ||
getStringTag(value, strict = false) { | ||
if (Object.hasStringTag(value)) { | ||
@@ -39,2 +55,6 @@ return value[Symbol.toStringTag] | ||
if (strict) { | ||
return undefined | ||
} | ||
if (value && (typeof value === 'function')) { | ||
@@ -59,9 +79,9 @@ return value.name | ||
* @param {object} [owner=globalThis] - The object in which to look up the | ||
* constructor corresponding to the string tag. Defaults to `globalThis`, which | ||
* covers global constructors like `Array`, `Object`, etc. | ||
* @returns {Function|object|null|undefined} - Returns the constructor or type | ||
* of the value based on its string tag. For 'Null' and 'Undefined', it returns | ||
* `null` and `undefined`, respectively. For other types, it returns the | ||
* corresponding constructor (e.g., `Array` for arrays) if available in the | ||
* `owner` object. | ||
* constructor corresponding to the string tag. Defaults to `globalThis`, | ||
* which covers global constructors like `Array`, `Object`, etc. | ||
* @returns {Function|object|null|undefined} - Returns the constructor or | ||
* type of the value based on its string tag. For 'Null' and 'Undefined', | ||
* it returns `null` and `undefined`, respectively. For other types, it | ||
* returns the corresponding constructor (e.g., `Array` for arrays) if | ||
* available in the `owner` object. | ||
*/ | ||
@@ -80,9 +100,10 @@ getType(value, owner = globalThis) { | ||
/** | ||
* Determines if the provided value is an object. This method checks whether the | ||
* value is an instance of `Object` or if its type is 'object'. It's a utility | ||
* method for type-checking, ensuring that a value is an object before performing | ||
* operations that are specific to objects. | ||
* Determines if the provided value is an object. This method checks whether | ||
* the value is an instance of `Object` or if its type is 'object'. It's a | ||
* utility method for type-checking, ensuring that a value is an object | ||
* before performing operations that are specific to objects. | ||
* | ||
* @param {*} value - The value to be checked. | ||
* @returns {boolean} - Returns `true` if the value is an object, otherwise `false`. | ||
* @returns {boolean} - Returns `true` if the value is an object, | ||
* otherwise `false`. | ||
*/ | ||
@@ -122,9 +143,10 @@ isObject(value) { | ||
/** | ||
* Checks if the given value is a valid key for an object. In JavaScript, a valid | ||
* key can be either a string or a symbol. This method is useful for validating | ||
* object keys before using them in operations like setting or getting object properties. | ||
* Checks if the given value is a valid key for an object. In JavaScript, a | ||
* valid key can be either a string or a symbol. This method is useful for | ||
* validating object keys before using them in operations like setting or | ||
* getting object properties. | ||
* | ||
* @param {*} value - The value to be checked. | ||
* @returns {boolean} - Returns `true` if the value is a valid object key (string or symbol), | ||
* otherwise `false`. | ||
* @returns {boolean} - Returns `true` if the value is a valid object key | ||
* (string or symbol), otherwise `false`. | ||
*/ | ||
@@ -142,14 +164,21 @@ isValidKey(value) { | ||
* final reduced object | ||
* @param {boolean} [bindAccessors = true] if this value is true | ||
* then any accessors from the source object will continue to have their | ||
* `this` value bound to the source. If the getter or setter on that object | ||
* is defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols specified | ||
* in the `keys` parameter. | ||
* @param {boolean} [bindAccessors = true] if this value is true then any | ||
* accessors from the source object will continue to have their `this` | ||
* value bound to the source. If the getter or setter on that object is | ||
* defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols | ||
* specified in the `keys` parameter. | ||
*/ | ||
stripTo(object, keys, bindAccessors = true) { | ||
const result = {} | ||
if (!object || typeof object !== 'object') { | ||
throw new TypeError( | ||
'Object.stripTo requires an object to strip. Received', | ||
object | ||
); | ||
} | ||
const result = {}; | ||
if (!Array.isArray(keys)) { | ||
return result | ||
return result; | ||
} | ||
@@ -159,19 +188,41 @@ | ||
if (Reflect.has(object, key)) { | ||
const descriptor = Object.getOwnPropertyDescriptor(object, key) | ||
if (Reflect.has(descriptor, 'get') || Reflect.has(descriptor, 'set')) { | ||
const originalDescriptor = Object.getOwnPropertyDescriptor(object, key); | ||
const descriptor = { ...originalDescriptor }; | ||
if ( | ||
typeof descriptor.get === 'function' || | ||
typeof descriptor.set === 'function' | ||
) { | ||
if (bindAccessors) { | ||
descriptor.get = descriptor?.get?.bind(object) | ||
descriptor.set = descriptor?.set?.bind(object) | ||
descriptor.get = descriptor.get?.bind(object); | ||
descriptor.set = descriptor.set?.bind(object); | ||
} | ||
Object.defineProperty(result, descriptor) | ||
} | ||
else { | ||
Object.defineProperty(result, descriptor) | ||
} | ||
Object.defineProperty(result, key, descriptor); | ||
} | ||
} | ||
return result | ||
return result; | ||
}, | ||
}); | ||
}); | ||
export const ObjectPrototypeExtensions = new Patch(Object.prototype, { | ||
/** | ||
* Strips an object down to only the keys specified. Optionally, any | ||
* accessors can be made to retain their context on the source object. | ||
* This is a passthrough to the static {@link Object.stripTo} function | ||
* | ||
* @param {Array<string|symbol>} keys the keys that should appear in the | ||
* final reduced object | ||
* @param {boolean} [bindAccessors = true] if this value is true then any | ||
* accessors from the source object will continue to have their `this` | ||
* value bound to the source. If the getter or setter on that object is | ||
* defined using an arrow function, this will not work as intended. | ||
* @returns {object} an object containing only the keys and symbols | ||
* specified in the `keys` parameter. | ||
*/ | ||
stripTo(keys, bindAccessors = true) { | ||
return Object.stripTo(this, keys, bindAccessors) | ||
} | ||
}) |
import { Patch } from '@nejs/extension' | ||
import { ObjectExtensions } from './objectextensions.js' | ||
const { isObject } = ObjectExtensions.patches | ||
/** | ||
@@ -34,16 +36,18 @@ * The `ReflectExtensions` class is a patch applied to the built-in JavaScript | ||
/** | ||
* Fetches all descriptors of an object, including those mapped to a | ||
* symbol descriptor value. | ||
* | ||
* @param {object} object the object from whose descriptors need to be | ||
* retrieved. | ||
* @returns {object} with keys mapped to object descriptors | ||
* @throws {TypeError} if the supplied `object` is null or not an object | ||
* a TypeError exception will be thrown | ||
*/ | ||
ownDescriptors(object) { | ||
const result = {} | ||
const revertOnDone = () => revertOnDone.doIt ? ObjectExtensions.revert() : '' | ||
revertOnDone.doIt = false | ||
if (!Object.isObject) { | ||
revertOnDone.doIt = true | ||
ObjectExtensions.apply() | ||
if (!isObject(object)) { | ||
throw new TypeError('The supplied object must be non-null and an object') | ||
} | ||
if (!Object.isObject(object)) { | ||
revertOnDone() | ||
return {} | ||
} | ||
const result = {} | ||
@@ -56,4 +60,2 @@ const keys = Reflect.ownKeys(object) | ||
revertOnDone() | ||
return result | ||
@@ -74,3 +76,3 @@ }, | ||
hasSome(object, ...keys) { | ||
return Object.isObject(object) && (keys.flat(Infinity) | ||
return isObject(object) && (keys.flat(Infinity) | ||
.map(key => Reflect.has(object, key)) | ||
@@ -77,0 +79,0 @@ .some(has => has) |
@@ -63,3 +63,4 @@ import { Patch } from '@nejs/extension'; | ||
* `Symbol('name')` | ||
* @returns | ||
* @returns true if the `value` in question is both a `symbol` and has | ||
* returns `undefined` if passed to `Symbol.keyFor` | ||
*/ | ||
@@ -66,0 +67,0 @@ isNonRegistered(value, allowOnlySymbols = false) { |
@@ -1,2 +0,2 @@ | ||
const { enableAll, disableAll } = require('../dist/cjs/index.js') | ||
const { Controls: { enableAll, disableAll } } = require('../dist/cjs/index.js') | ||
@@ -3,0 +3,0 @@ describe('Enabled extensions', () => { |
@@ -18,5 +18,6 @@ { | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"strict": true, | ||
"traceResolution": false, | ||
"types": ["node", "jest"] | ||
"types": ["jest"] | ||
}, | ||
@@ -23,0 +24,0 @@ "compileOnSave": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
2661717
156
12754
2080
15
2
90
1
+ Added@nejs/extension@2.20.0(transitive)
- Removed@nejs/extension@1.3.0(transitive)
Updated@nejs/extension@^2.1.0