UNPKG

@yamamotok/dataobject

Version:

Decorator based JSON serializer and deserializer.

57 lines 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToPlain = void 0; const toPlainValueTransform_1 = require("./transformer/toPlainValueTransform"); const inContext_1 = require("./utils/inContext"); const Decorator_1 = require("./Decorator"); class ToPlain { /** * Create toPlain() method. * @param ctor the class * @param toPlainOptions several options */ static createToPlain(ctor, toPlainOptions) { return (obj, _context) => { const properties = Decorator_1.Decorator.getPropertyMap(obj); if (!properties) { return {}; } let ret = {}; properties.forEach((options, _key) => { const context = _context || 'toPlain'; // Skip property which is out of context if (!(0, inContext_1.inContext)(context, options === null || options === void 0 ? void 0 : options.context)) { return; // skip, out of context } const key = _key; const objValue = obj[key]; // `undefined` will not be processed unless `omitUndefined` is true if (objValue === undefined && (toPlainOptions === null || toPlainOptions === void 0 ? void 0 : toPlainOptions.omitUndefined) !== false) { return; // skip, because the value is undefined } const transformed = (0, toPlainValueTransform_1.toPlainValueTransform)({ key, sourceValue: objValue, context, options, }); // Again, `undefined` will not be processed unless `omitUndefined` is true if (transformed === undefined && (toPlainOptions === null || toPlainOptions === void 0 ? void 0 : toPlainOptions.omitUndefined) !== false) { return; // skip, because the transformed value is undefined. } // Spread value if the property has been decorated with `spread()` if (typeof transformed === 'object' && (options === null || options === void 0 ? void 0 : options.spread) && (0, inContext_1.inContext)(context, options.spread.context)) { ret = Object.assign(Object.assign({}, transformed), ret); } else { ret[key] = transformed; } }); return ret; }; } } exports.ToPlain = ToPlain; //# sourceMappingURL=ToPlain.js.map