ce-decorators
Version:
Custom Element decorators for typescript
118 lines (100 loc) • 3.82 kB
JavaScript
/**
* Copyright (c) 2018 Mathis Zeiher
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
const origReflect = window.Reflect || {}; // tslint:disable-line:no-any
// tslint:disable-line:no-any
const reflectMap = new Map();
function getOrCreateMetadataMap(map, property) {
let metadataMap = map.get(property);
if (!metadataMap) {
metadataMap = new Map();
map.set(property, metadataMap);
}
return metadataMap;
}
function getOrCreatePropMap(map, target) {
let propMap = map.get(target);
if (!propMap) {
propMap = new Map();
map.set(target, propMap);
}
return propMap;
}
function decorate(decorators, target, propertyKey, description) {
// tslint:disable-line:no-any
if (typeof origReflect === 'object' && typeof origReflect.decorate === 'function' && origReflect.decorate !== decorate) {
// tslint:disable-line:no-unsafe-any
origReflect.decorate(decorators, target, propertyKey, description); // tslint:disable-line:no-unsafe-any
}
const desciptor = decorators.reverse() // tslint:disable-line:no-any
.reduce((prevValue, currentValue) => currentValue(target, propertyKey, prevValue) || prevValue, description); // tslint:disable-line
return desciptor || description;
}
function metadata(metadataKey, metaDataValue) {
// tslint:disable-line:no-any
if (typeof origReflect === 'object' && typeof origReflect.metadata === 'function' && origReflect.metadata !== metadata) {
// tslint:disable-line:no-unsafe-any
return origReflect.metadata(metadataKey, metaDataValue); // tslint:disable-line:no-unsafe-any
} else {
return (target, property) => {
if (!property) {
property = '';
}
const propMap = getOrCreatePropMap(reflectMap, target);
getOrCreateMetadataMap(propMap, property).set(metadataKey, metaDataValue);
};
}
}
function getMetadata(metadataKey, target, propertyKey) {
// tslint:disable-line:no-any
if (typeof origReflect === 'object' && typeof origReflect.getMetadata === 'function' && origReflect.getMetadata !== getMetadata) {
// tslint:disable-line:no-unsafe-any
return origReflect.getMetadata(metadataKey, target, propertyKey); // tslint:disable-line:no-unsafe-any
} else {
return getOrCreateMetadataMap(getOrCreatePropMap(reflectMap, target), propertyKey).get(metadataKey);
}
}
if (!window.Reflect) {
// tslint:disable-line:no-any
window.Reflect = {
decorate,
getMetadata,
metadata
}; // tslint:disable-line:no-any
window.ReflectPoorlyFill = {
decorate,
getMetadata,
metadata
}; // tslint:disable-line:no-any
} else {
if (!window.Reflect.decorate) {
// tslint:disable-line
window.Reflect.decorate = decorate; // tslint:disable-line
}
if (!window.Reflect.getMetadata) {
// tslint:disable-line
window.Reflect.getMetadata = getMetadata; // tslint:disable-line
}
if (!window.Reflect.metadata) {
// tslint:disable-line
window.Reflect.metadata = metadata; // tslint:disable-line
}
window.ReflectPoorlyFill = {
decorate,
getMetadata,
metadata
}; // tslint:disable-line
}
//# sourceMappingURL=reflect.js.map