@abraham/reflection
Version:
Lightweight ES Module implementation of reflect-metadata
108 lines (104 loc) • 4.38 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@abraham/reflection"] = {}));
})(this, (function (exports) { 'use strict';
const Metadata = new WeakMap();
function decorateProperty(decorators, target, propertyKey, descriptor) {
decorators.reverse().forEach((decorator) => {
descriptor = decorator(target, propertyKey, descriptor) || descriptor;
});
return descriptor;
}
function decorateConstructor(decorators, target) {
decorators.reverse().forEach((decorator) => {
const decorated = decorator(target);
if (decorated) {
target = decorated;
}
});
return target;
}
function decorate(decorators, target, propertyKey, attributes) {
if (!Array.isArray(decorators) || decorators.length === 0) {
throw new TypeError();
}
if (propertyKey !== undefined) {
return decorateProperty(decorators, target, propertyKey, attributes);
}
if (typeof target === 'function') {
return decorateConstructor(decorators, target);
}
return;
}
function getMetadataMap(target, propertyKey) {
return Metadata.get(target) && Metadata.get(target).get(propertyKey);
}
function ordinaryGetOwnMetadata(metadataKey, target, propertyKey) {
if (target === undefined) {
throw new TypeError();
}
const metadataMap = getMetadataMap(target, propertyKey);
return metadataMap && metadataMap.get(metadataKey);
}
function createMetadataMap(target, propertyKey) {
const targetMetadata = Metadata.get(target) ||
new Map();
Metadata.set(target, targetMetadata);
const metadataMap = targetMetadata.get(propertyKey) || new Map();
targetMetadata.set(propertyKey, metadataMap);
return metadataMap;
}
function ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey) {
if (propertyKey && !['string', 'symbol'].includes(typeof propertyKey)) {
throw new TypeError();
}
(getMetadataMap(target, propertyKey) ||
createMetadataMap(target, propertyKey)).set(metadataKey, metadataValue);
}
function ordinaryGetMetadata(metadataKey, target, propertyKey) {
return ordinaryGetOwnMetadata(metadataKey, target, propertyKey)
? ordinaryGetOwnMetadata(metadataKey, target, propertyKey)
: Object.getPrototypeOf(target)
? ordinaryGetMetadata(metadataKey, Object.getPrototypeOf(target), propertyKey)
: undefined;
}
function metadata(metadataKey, metadataValue) {
return function decorator(target, propertyKey) {
ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
};
}
function getMetadata(metadataKey, target, propertyKey) {
return ordinaryGetMetadata(metadataKey, target, propertyKey);
}
function getOwnMetadata(metadataKey, target, propertyKey) {
return ordinaryGetOwnMetadata(metadataKey, target, propertyKey);
}
function hasOwnMetadata(metadataKey, target, propertyKey) {
return !!ordinaryGetOwnMetadata(metadataKey, target, propertyKey);
}
function hasMetadata(metadataKey, target, propertyKey) {
return !!ordinaryGetMetadata(metadataKey, target, propertyKey);
}
function defineMetadata(metadataKey, metadataValue, target, propertyKey) {
ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
}
const Reflection = {
decorate,
defineMetadata,
getMetadata,
getOwnMetadata,
hasMetadata,
hasOwnMetadata,
metadata,
};
Object.assign(Reflect, Reflection);
exports.Reflection = Reflection;
exports.decorate = decorate;
exports.defineMetadata = defineMetadata;
exports.getMetadata = getMetadata;
exports.getOwnMetadata = getOwnMetadata;
exports.hasMetadata = hasMetadata;
exports.hasOwnMetadata = hasOwnMetadata;
exports.metadata = metadata;
}));