UNPKG

@yamamotok/dataobject

Version:

Decorator based JSON serializer and deserializer.

22 lines 932 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClassStrategy = void 0; /* eslint-disable @typescript-eslint/no-explicit-any */ const types_1 = require("../../types"); const getConstructor_1 = require("../../utils/getConstructor"); const hasToPlain_1 = require("../../utils/hasToPlain"); const Strategy_1 = require("../Strategy"); class ClassStrategy extends Strategy_1.Strategy { transform(opts) { const { sourceValue, context } = opts; const ctor = (0, getConstructor_1.getConstructor)(sourceValue); if (ctor && (0, hasToPlain_1.hasToPlain)(ctor)) { const ret = ctor.toPlain(sourceValue, context); ret[types_1.TYPE_ATTRIBUTE_NAME] = ctor.name; return new Strategy_1.Transformed(ret); } return undefined; } } exports.ClassStrategy = ClassStrategy; //# sourceMappingURL=ClassStrategy.js.map