UNPKG

@kingdom-sdk/core

Version:

Core module to design DDD applications in TypeScript

40 lines (30 loc) 842 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValueObject = void 0; var _objectHash = _interopRequireDefault(require("object-hash")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Represent a transient value. */ class ValueObject { constructor(props) { this.props = Object.freeze(props); } toString() { const classname = this.constructor.name; const pairs = Object.entries(this.props).map(item => `${item[0]}="${item[1]}"`).join(', '); return `${classname}(${pairs})`; } toHash() { return (0, _objectHash.default)(this.props); } equals(other) { if (!(other instanceof ValueObject)) { return false; } return this.toHash() === other.toHash(); } } exports.ValueObject = ValueObject;