@aedart/support
Version:
The Ion support package
84 lines (75 loc) • 1.96 kB
JavaScript
import { DEPENDENCIES } from '@aedart/contracts/container';
import { targetMeta, getTargetMeta, hasTargetMeta } from '@aedart/support/meta';
/**
* @aedart/support
*
* BSD-3-Clause, Copyright (c) 2023-present Alin Eugen Deac <aedart@gmail.com>.
*/
/**
* Define the dependencies that a target requires
*
* **Note**: _Method is intended to be used as a class or method decorator!_
*
* @example
* ```js
* @dependencies('RockService', 'apiConnection')
* class Radio {
*
* @dependencies(RockSong)
* play(song)
* }
* ```
*
* @param {...Identifier[]} identifiers
*
* @return {ClassDecorator | ClassMethodDecorator}
*/
function dependencies(...identifiers) {
return targetMeta(DEPENDENCIES, identifiers);
}
/**
* Alias for [dependencies()]{@link import('@aedart/support/container').dependencies}
*
* @param {...Identifier[]} identifiers
*
* @return {ClassDecorator | ClassMethodDecorator}
*/
function dependsOn(...identifiers) {
return dependencies(...identifiers);
}
/**
* Returns the defined dependencies for given target
*
* @param {object} target
*
* @return {Identifier[]} Empty identifiers list, if none defined for target
*/
function getDependencies(target) {
return getTargetMeta(target, DEPENDENCIES, []);
}
/**
* Determine if target has dependencies defined
*
* @see dependencies
*
* @param {object} target
*
* @return {boolean}
*/
function hasDependencies(target) {
return hasTargetMeta(target, DEPENDENCIES);
}
/**
* Determine if value is of the type [Identifier]{@link import('@aedart/contracts/container').Identifier}.
*
* @param {unknown} value
*
* @return {boolean}
*/
function isBindingIdentifier(value) {
if (value === undefined || value === null) {
return false;
}
return ['string', 'number', 'symbol', 'object', 'function'].includes(typeof value);
}
export { dependencies, dependsOn, getDependencies, hasDependencies, isBindingIdentifier };