UNPKG

@yamamotok/dataobject

Version:

Decorator based JSON serializer and deserializer.

63 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Decorator = void 0; class Decorator { static getPropertyMap(obj) { return Reflect.getMetadata(Decorator.PropertiesMetadataKey, obj); } static propertyMap(target) { const object = target; let map; if (Reflect.hasOwnMetadata(Decorator.PropertiesMetadataKey, object)) { map = Reflect.getMetadata(Decorator.PropertiesMetadataKey, object); } else { map = new Map(); Reflect.defineMetadata(Decorator.PropertiesMetadataKey, map, object); } return map; } static property(options) { return (target, propertyKey) => { const map = Decorator.propertyMap(target); const typeInfo = Reflect.getMetadata('design:type', target, propertyKey); map.set(propertyKey, Object.assign(Object.assign(Object.assign({}, map.get(propertyKey)), options), { typeInfo })); }; } static required() { return (target, propertyKey) => { const map = Decorator.propertyMap(target); map.set(propertyKey, Object.assign(Object.assign({}, map.get(propertyKey)), { required: true })); }; } static spread(context) { return (target, propertyKey) => { const map = Decorator.propertyMap(target); map.set(propertyKey, Object.assign(Object.assign({}, map.get(propertyKey)), { spread: { context } })); }; } static context(...contextNames) { return (target, propertyKey) => { if (contextNames.length < 1) { return; } const map = Decorator.propertyMap(target); map.set(propertyKey, Object.assign(Object.assign({}, map.get(propertyKey)), { context: contextNames })); }; } static validator(validator, options) { return (target, propertyKey) => { const map = Decorator.propertyMap(target); map.set(propertyKey, Object.assign(Object.assign({}, map.get(propertyKey)), { validator: { validator, options } })); }; } static validatorS(validator, options) { return (target, propertyKey) => { const map = Decorator.propertyMap(target); map.set(propertyKey, Object.assign(Object.assign({}, map.get(propertyKey)), { validator: { validator, options: Object.assign(Object.assign({}, options), { asString: true }) } })); }; } } exports.Decorator = Decorator; Decorator.PropertiesMetadataKey = Symbol('custom:domainobject-properties'); //# sourceMappingURL=Decorator.js.map