UNPKG

@hiki9/rich-domain

Version:

Rich Domain is a library that provides a set of tools to help you build complex business logic in NodeJS using Domain Driven Design principles.

45 lines 2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutoMapperValueObject = void 0; const validator_1 = __importDefault(require("../../utils/validator")); const errors_1 = require("../errors"); const ids_1 = require("./ids"); class AutoMapperValueObject { valueObjectToObj(valueObject) { const value = valueObject['props']; if (!value || typeof value !== "object" || value === null || value instanceof Date) { return value; } const obj = Object.entries(value) .reduce((accumulator, [key, instance]) => { if (instance instanceof Array) { accumulator[key] = instance.map((item) => { if (validator_1.default.isEntity(item) || validator_1.default.isAggregate(item)) throw new errors_1.DomainError("Entity cannot be a value object children."); if (validator_1.default.isValueObject(item)) return item.toPrimitives(); else return item; }); } else { if (validator_1.default.isEntity(instance) || validator_1.default.isAggregate(instance)) throw new errors_1.DomainError("Entity cannot be a value object children."); if (validator_1.default.isValueObject(instance)) accumulator[key] = instance.toPrimitives(); else if ((instance) instanceof ids_1.Id) { accumulator[key] = instance.value; } else accumulator[key] = instance; } return accumulator; }, {}); return obj; } } exports.AutoMapperValueObject = AutoMapperValueObject; //# sourceMappingURL=auto-mapper-value-object.js.map