Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

custom-ability

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

custom-ability - npm Package Compare versions

Comparing version 2.0.0-alpha.4 to 2.0.0-alpha.5

87

lib/custom-ability.d.ts

@@ -1,18 +0,3 @@

/**
* A symbol used to mark a class's abilities
*
* @constant
* @type {Symbol}
*/
export declare const abilitiesSym = "$abilities";
/**
* A symbol used to mark a class's additional ability whether injected
*
* @constant
* @type {Symbol}
*/
export declare const abilitiesOptSym = "$abilitiesOpt";
/**
* The additional injection mode
*/
export declare const AdditionalInjectionMode: {

@@ -22,95 +7,23 @@ all: number;

};
/**
* The Ability Options
*/
export interface AbilityOptions {
/**
* An optional id for AdditionalAbility option
*/
id?: string;
/**
* The additional injection mode
*/
mode?: number;
/**
* An optional list of method names to include.
*/
include?: string | string[];
/**
* An optional list of method names to exclude.
*/
exclude?: string | string[];
/**
* An optional object mapping method names to functions to be added to the target class.
*/
methods?: Record<string, Function>;
/**
* An optional object mapping method names to static functions to be added to the target class.
*/
classMethods?: Record<string, Function>;
}
/**
* An additional ability
*
*/
export interface AdditionalAbility {
/**
* the AdditionalAbilityOptions ID
*/
id?: string;
/**
* the Additional Injection Mode
*/
mode?: number;
/**
* the list of required methods
*/
required?: string[];
/**
* Returns the additional ability options if they exist
*
* @param options the ability Options
* @returns the Additional Ability options if exists
*/
getOpts: (options?: AbilityOptions) => AbilityOptions | undefined;
}
/**
* An object mapping target ability names to additional abilities
*
*/
export interface AdditionalAbilities {
[key: string]: AdditionalAbility | Array<AdditionalAbility>;
}
/**
* The ability injector options
*
*/
export interface AbilityInjectorOptions {
/**
* The optional depends abilities which can work together
*/
depends?: AdditionalAbilities;
}
/**
* A function that adds(injects) the ability of a specified ability class to a target class.
*
* Note: Maybe the ability will be injected into the inheritance class.
*
* @param {Function} targetClass - The target class to which the ability will be added.
* @param {AbilityOptions} [options] - An optional ability configuration object.
* @returns {Function} - An injected target class that takes a class and adds the ability to it using the specified
* options.
*/
export type AbilityFn = (targetClass: Function, options?: AbilityOptions) => Function;
/**
* Creates a function that adds(injects) the ability to the target class based on the ability class.
*
* @param abilityClass The ability class to inject into the target class.
* @param isGetClassFunc An optional parameter that indicates whether abilityClass should be invoked
* with aClass and aOptions to get the actual ability class. defaults to false
* @param injectorOpts An optional injector options object
* @returns Another function that accepts the target class and options to include or exclude specific
* properties and methods.
* The returned function injects the abilities into the target class and returns the modified class.
*/
export declare function createAbilityInjector(abilityClass: Function, isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn;

@@ -117,0 +30,0 @@ export declare function createAbilityInjector(abilityClass: Function, aCoreMethod?: string | string[], isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn;

@@ -19,30 +19,5 @@ "use strict";

const skipProtoNames = ['constructor', '__proto__'];
/**
* A symbol used to mark a class's abilities
*
* @constant
* @type {Symbol}
*/
exports.abilitiesSym = '$abilities';
/**
* A symbol used to mark a class's additional ability whether injected
*
* @constant
* @type {Symbol}
*/
exports.abilitiesOptSym = '$abilitiesOpt';
/**
* The additional injection mode
*/
exports.AdditionalInjectionMode = { all: 0, target: 1 };
/**
* Inject non-enumerable members of the aObject into aTargetClass
*
* @internal
* @param aTargetClass the target class
* @param aObject the non-enumerable members of the object will be injected into aTargetClass
* @param filter It'll be injected only when filter callback function return true, if exists
* @param isStatic Whether the members to be injected are static
* @returns The names of members that have been injected
*/
function injectMembersFromNonEnum(aTargetClass, aObject, filter, isStatic) {

@@ -60,3 +35,2 @@ const nonEnumNames = (0, get_non_enumerable_names_1.getNonEnumerableNames)(aObject);

const is$ = name[0] === '$';
// get rid of the first char '$'
if (is$) {

@@ -98,17 +72,2 @@ name = name.substring(1);

;
/**
* Creates a function that adds(injects) the ability to the target class based on the ability class.
*
* @param abilityClass The ability class to inject into the target class.
* @param aCoreMethod An optional parameter that specifies the core methods that the ability class must have.
* This is a minimum set of methods required for the ability to be considered injected.
* Core methods are defined in the ability class, and can be static or instance methods.
* If a core method is a static method, it must be prefixed with the "@" symbol.
* @param isGetClassFunc An optional parameter that indicates whether abilityClass should be invoked
* with aClass and aOptions to get the actual ability class. defaults to false
* @param injectorOpts An optional injector options object
* @returns Another function that accepts the target class and options to include or exclude specific
* properties and methods.
* The returned function injects the abilities into the target class and returns the modified class.
*/
function createAbilityInjector(abilityClass, aCoreMethod, isGetClassFunc, injectorOpts) {

@@ -143,3 +102,2 @@ if (typeof aCoreMethod === 'boolean') {

let vHasCoreMethod = (0, array_1.default)(aCoreMethod) ? aCoreMethod[0] : aCoreMethod;
// TODO: Check the core method on the target class or the inheritance?
if (vHasCoreMethod) {

@@ -186,3 +144,2 @@ if (vHasCoreMethod[0] !== '@') {

const vInjectedOnParent = (0, injected_on_parent_1.default)(aClass, vName);
// return the injected class if it has already been injected on parent
if (vInjectedOnParent) {

@@ -202,9 +159,6 @@ return vInjectedOnParent;

if (!vHasIncludeOptions) {
// inject the static methods
let vExcludes = injectMembersFromNonEnum(aClass, AbilityClass, null, true);
// inject the enumerable members
(0, extend_1.default)(aClass, AbilityClass, function (k) {
return (vExcludes.indexOf(k) === -1);
});
// inject the methods
vExcludes = injectMembersFromNonEnum(vClassPrototype, AbilityClass.prototype);

@@ -217,3 +171,2 @@ (0, extend_1.default)(vClassPrototype, AbilityClass.prototype, function (k) {

let vExcludes = injectMembersFromNonEnum(aClass, AbilityClass, vFilterMembers, true);
// inject the enumerable members
(0, extend_1.default)(aClass, AbilityClass, function (k) {

@@ -241,3 +194,2 @@ return (vExcludes.indexOf(k) === -1 && vFilterMembers('@' + k));

}
// Apply optional dependencies
if (vDepends) {

@@ -323,3 +275,2 @@ Object.keys(vDepends).forEach(function (name) {

if (vTargets.length) {
// apply additional ability from parent to child
for (let i = vTargets.length - 1; i >= 0; i--) {

@@ -340,9 +291,2 @@ const item = vTargets[i];

;
/**
* Returns the additional ability options for a specified ability class
*
* @param aClass - The class to which the additional ability options belong
* @param aName - The name of the ability
* @returns The additional ability options
*/
function getAdditionalAbilityOptions(aClass, aName) {

@@ -363,10 +307,2 @@ const $abilities = aClass.prototype[exports.abilitiesSym];

}
/**
* Adds an additional ability to a class based on the provided options
*
* @param {Function} aClass - The target class to which the additional ability will be added
* @param {string} aName - The name of the additional ability
* @param {AbilityOptions} aOptions - The options that describe which methods to inject
* @param {Function} [fromClass] - The class from which the additional ability is being applied
*/
function applyAdditionalAbility(aClass, aName, aOptions, fromClass) {

@@ -387,9 +323,2 @@ if (aOptions != null) {

}
/**
* Pushes an array of items into a destination array, but only if the items are not already in the destination array
*
* @param dest - The destination array
* @param src - The source array or item
* @returns The destination array with the new items added
*/
function arrayPushOnly(dest, src) {

@@ -406,8 +335,2 @@ if (src !== undefined) {

}
/**
* Returns an array of all members of a class, including static and prototype members
*
* @param aClass - The class to get the members from
* @returns An array of member names
*/
function getMembers(aClass) {

@@ -420,12 +343,2 @@ let result = (0, get_non_enumerable_names_1.getNonEnumerableNames)(aClass).filter(n => !skipStaticNames.includes(n)).map(name => '@' + name);

}
/**
* Determines whether to include a member based on the provided options
*
* @private
* @param {string} k - The name of the member
* @param {string|string[]} aIncludes - The names of members to include
* @param {string|string[]} aExcludes - The names of members to exclude
* @param {boolean} [aIsStatic] - Whether the member is a static member
* @returns {boolean} - Whether to include the member
*/
function filter(k, aIncludes, aExcludes, aIsStatic) {

@@ -457,12 +370,2 @@ if (aIsStatic) {

;
/**
* Returns an object containing only the members that pass the filter
*
* @function
* @private
* @param {Object} obj - The object to filter
* @param {AbilityOptions} aOptions - The options that describe which members to include
* @param {boolean} [isStatic] - Whether the members are static members
* @returns {Object} - An object containing only the members that pass the filter
*/
function getFilteredMembers(obj, aOptions, isStatic) {

@@ -477,19 +380,1 @@ const result = {};

}
/*
function cloneObj(src: object, maxDeep = 5) {
if (!src) {return src};
const result = {}
Object.keys(src).forEach(key => {
const value = src[key]
if (Array.isArray(value)) {
result[key] = value.slice()
} else if (maxDeep > 0 && value instanceof Object) {
--maxDeep
result[key] = cloneObj(value, maxDeep)
} else {
result[key] = src[key]
}
})
return result
}
*/

@@ -1,18 +0,3 @@

/**
* A symbol used to mark a class's abilities
*
* @constant
* @type {Symbol}
*/
export declare const abilitiesSym = "$abilities";
/**
* A symbol used to mark a class's additional ability whether injected
*
* @constant
* @type {Symbol}
*/
export declare const abilitiesOptSym = "$abilitiesOpt";
/**
* The additional injection mode
*/
export declare const AdditionalInjectionMode: {

@@ -22,95 +7,23 @@ all: number;

};
/**
* The Ability Options
*/
export interface AbilityOptions {
/**
* An optional id for AdditionalAbility option
*/
id?: string;
/**
* The additional injection mode
*/
mode?: number;
/**
* An optional list of method names to include.
*/
include?: string | string[];
/**
* An optional list of method names to exclude.
*/
exclude?: string | string[];
/**
* An optional object mapping method names to functions to be added to the target class.
*/
methods?: Record<string, Function>;
/**
* An optional object mapping method names to static functions to be added to the target class.
*/
classMethods?: Record<string, Function>;
}
/**
* An additional ability
*
*/
export interface AdditionalAbility {
/**
* the AdditionalAbilityOptions ID
*/
id?: string;
/**
* the Additional Injection Mode
*/
mode?: number;
/**
* the list of required methods
*/
required?: string[];
/**
* Returns the additional ability options if they exist
*
* @param options the ability Options
* @returns the Additional Ability options if exists
*/
getOpts: (options?: AbilityOptions) => AbilityOptions | undefined;
}
/**
* An object mapping target ability names to additional abilities
*
*/
export interface AdditionalAbilities {
[key: string]: AdditionalAbility | Array<AdditionalAbility>;
}
/**
* The ability injector options
*
*/
export interface AbilityInjectorOptions {
/**
* The optional depends abilities which can work together
*/
depends?: AdditionalAbilities;
}
/**
* A function that adds(injects) the ability of a specified ability class to a target class.
*
* Note: Maybe the ability will be injected into the inheritance class.
*
* @param {Function} targetClass - The target class to which the ability will be added.
* @param {AbilityOptions} [options] - An optional ability configuration object.
* @returns {Function} - An injected target class that takes a class and adds the ability to it using the specified
* options.
*/
export type AbilityFn = (targetClass: Function, options?: AbilityOptions) => Function;
/**
* Creates a function that adds(injects) the ability to the target class based on the ability class.
*
* @param abilityClass The ability class to inject into the target class.
* @param isGetClassFunc An optional parameter that indicates whether abilityClass should be invoked
* with aClass and aOptions to get the actual ability class. defaults to false
* @param injectorOpts An optional injector options object
* @returns Another function that accepts the target class and options to include or exclude specific
* properties and methods.
* The returned function injects the abilities into the target class and returns the modified class.
*/
export declare function createAbilityInjector(abilityClass: Function, isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn;

@@ -117,0 +30,0 @@ export declare function createAbilityInjector(abilityClass: Function, aCoreMethod?: string | string[], isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn;

@@ -13,30 +13,5 @@ import isArray from 'util-ex/lib/is/type/array';

const skipProtoNames = ['constructor', '__proto__'];
/**
* A symbol used to mark a class's abilities
*
* @constant
* @type {Symbol}
*/
export const abilitiesSym = '$abilities';
/**
* A symbol used to mark a class's additional ability whether injected
*
* @constant
* @type {Symbol}
*/
export const abilitiesOptSym = '$abilitiesOpt';
/**
* The additional injection mode
*/
export const AdditionalInjectionMode = { all: 0, target: 1 };
/**
* Inject non-enumerable members of the aObject into aTargetClass
*
* @internal
* @param aTargetClass the target class
* @param aObject the non-enumerable members of the object will be injected into aTargetClass
* @param filter It'll be injected only when filter callback function return true, if exists
* @param isStatic Whether the members to be injected are static
* @returns The names of members that have been injected
*/
function injectMembersFromNonEnum(aTargetClass, aObject, filter, isStatic) {

@@ -54,3 +29,2 @@ const nonEnumNames = getNonEnumNames(aObject);

const is$ = name[0] === '$';
// get rid of the first char '$'
if (is$) {

@@ -92,17 +66,2 @@ name = name.substring(1);

;
/**
* Creates a function that adds(injects) the ability to the target class based on the ability class.
*
* @param abilityClass The ability class to inject into the target class.
* @param aCoreMethod An optional parameter that specifies the core methods that the ability class must have.
* This is a minimum set of methods required for the ability to be considered injected.
* Core methods are defined in the ability class, and can be static or instance methods.
* If a core method is a static method, it must be prefixed with the "@" symbol.
* @param isGetClassFunc An optional parameter that indicates whether abilityClass should be invoked
* with aClass and aOptions to get the actual ability class. defaults to false
* @param injectorOpts An optional injector options object
* @returns Another function that accepts the target class and options to include or exclude specific
* properties and methods.
* The returned function injects the abilities into the target class and returns the modified class.
*/
export function createAbilityInjector(abilityClass, aCoreMethod, isGetClassFunc, injectorOpts) {

@@ -137,3 +96,2 @@ if (typeof aCoreMethod === 'boolean') {

let vHasCoreMethod = isArray(aCoreMethod) ? aCoreMethod[0] : aCoreMethod;
// TODO: Check the core method on the target class or the inheritance?
if (vHasCoreMethod) {

@@ -180,3 +138,2 @@ if (vHasCoreMethod[0] !== '@') {

const vInjectedOnParent = isInjectedOnParent(aClass, vName);
// return the injected class if it has already been injected on parent
if (vInjectedOnParent) {

@@ -196,9 +153,6 @@ return vInjectedOnParent;

if (!vHasIncludeOptions) {
// inject the static methods
let vExcludes = injectMembersFromNonEnum(aClass, AbilityClass, null, true);
// inject the enumerable members
extendFilter(aClass, AbilityClass, function (k) {
return (vExcludes.indexOf(k) === -1);
});
// inject the methods
vExcludes = injectMembersFromNonEnum(vClassPrototype, AbilityClass.prototype);

@@ -211,3 +165,2 @@ extendFilter(vClassPrototype, AbilityClass.prototype, function (k) {

let vExcludes = injectMembersFromNonEnum(aClass, AbilityClass, vFilterMembers, true);
// inject the enumerable members
extendFilter(aClass, AbilityClass, function (k) {

@@ -235,3 +188,2 @@ return (vExcludes.indexOf(k) === -1 && vFilterMembers('@' + k));

}
// Apply optional dependencies
if (vDepends) {

@@ -317,3 +269,2 @@ Object.keys(vDepends).forEach(function (name) {

if (vTargets.length) {
// apply additional ability from parent to child
for (let i = vTargets.length - 1; i >= 0; i--) {

@@ -333,9 +284,2 @@ const item = vTargets[i];

;
/**
* Returns the additional ability options for a specified ability class
*
* @param aClass - The class to which the additional ability options belong
* @param aName - The name of the ability
* @returns The additional ability options
*/
function getAdditionalAbilityOptions(aClass, aName) {

@@ -356,10 +300,2 @@ const $abilities = aClass.prototype[abilitiesSym];

}
/**
* Adds an additional ability to a class based on the provided options
*
* @param {Function} aClass - The target class to which the additional ability will be added
* @param {string} aName - The name of the additional ability
* @param {AbilityOptions} aOptions - The options that describe which methods to inject
* @param {Function} [fromClass] - The class from which the additional ability is being applied
*/
function applyAdditionalAbility(aClass, aName, aOptions, fromClass) {

@@ -380,9 +316,2 @@ if (aOptions != null) {

}
/**
* Pushes an array of items into a destination array, but only if the items are not already in the destination array
*
* @param dest - The destination array
* @param src - The source array or item
* @returns The destination array with the new items added
*/
function arrayPushOnly(dest, src) {

@@ -399,8 +328,2 @@ if (src !== undefined) {

}
/**
* Returns an array of all members of a class, including static and prototype members
*
* @param aClass - The class to get the members from
* @returns An array of member names
*/
function getMembers(aClass) {

@@ -413,12 +336,2 @@ let result = getNonEnumNames(aClass).filter(n => !skipStaticNames.includes(n)).map(name => '@' + name);

}
/**
* Determines whether to include a member based on the provided options
*
* @private
* @param {string} k - The name of the member
* @param {string|string[]} aIncludes - The names of members to include
* @param {string|string[]} aExcludes - The names of members to exclude
* @param {boolean} [aIsStatic] - Whether the member is a static member
* @returns {boolean} - Whether to include the member
*/
function filter(k, aIncludes, aExcludes, aIsStatic) {

@@ -450,12 +363,2 @@ if (aIsStatic) {

;
/**
* Returns an object containing only the members that pass the filter
*
* @function
* @private
* @param {Object} obj - The object to filter
* @param {AbilityOptions} aOptions - The options that describe which members to include
* @param {boolean} [isStatic] - Whether the members are static members
* @returns {Object} - An object containing only the members that pass the filter
*/
function getFilteredMembers(obj, aOptions, isStatic) {

@@ -470,19 +373,1 @@ const result = {};

}
/*
function cloneObj(src: object, maxDeep = 5) {
if (!src) {return src};
const result = {}
Object.keys(src).forEach(key => {
const value = src[key]
if (Array.isArray(value)) {
result[key] = value.slice()
} else if (maxDeep > 0 && value instanceof Object) {
--maxDeep
result[key] = cloneObj(value, maxDeep)
} else {
result[key] = src[key]
}
})
return result
}
*/

1

lib/esm/index.js

@@ -0,1 +1,2 @@

// @sourceType: module
import { createAbilityInjector } from './custom-ability';

@@ -2,0 +3,0 @@ export * from './custom-ability';

{
"name": "custom-ability",
"version": "2.0.0-alpha.4",
"version": "2.0.0-alpha.5",
"description": "make custom ability more easy. generate the ability which can be added to any class directly.",

@@ -22,3 +22,3 @@ "homepage": "https://github.com/snowyu/custom-ability.js",

"build.ts.cjs": "tsc --module commonjs -outDir lib",
"build.ts.mjs": "tsc --moduleResolution nodenext --module es2022 -outDir lib/esm",
"build.ts.mjs": "tsc --moduleResolution nodenext --module es2022 -outDir lib/esm;node script/prepend-module.js lib/esm/index.js",
"clean": "rm -fr web docs lib",

@@ -40,4 +40,4 @@ "clean.doc": "rm -fr web docs",

"dependencies": {
"inherits-ex": "^2.1.0-alpha.11",
"util-ex": "^2.0.0-alpha.8"
"inherits-ex": "^2.1.0-alpha.12",
"util-ex": "^2.0.0-alpha.9"
},

@@ -44,0 +44,0 @@ "devDependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc