dino-core
Version:
A dependency injection framework for NodeJS applications
52 lines • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValueObject = void 0;
const main_1 = require("../../main");
const Archetype_1 = require("./Archetype");
/**
* Define a value object in the common sense of DDD (Domain-Driven Design).
* The properties passed to the constructor will be set with the provided value
* and will only be accessible for reading.
*/
class ValueObject {
constructor(props) {
const ps = props ?? {};
for (const key in ps) {
Object.defineProperty(this, key, {
value: ps[key],
writable: false,
enumerable: true,
configurable: true
});
}
}
/**
* Compare two value objects for equality, the basic implementation is as following:
* > if other object is not defined return false
* > if this object and other object are not of the same type then return false
* > if any of this object properties is not same as the same property on other object then return false
* > otherwise return true
* @param other the other archetype to compare
* @returns true if the two value objects are same, false otherwise
*/
equals(other) {
if (!main_1.ObjectHelper.isDefined(other)) {
return false;
}
if (!main_1.ObjectHelper.instanceOf(other, this.constructor)) {
return false;
}
for (const key of Object.getOwnPropertyNames(this)) {
if (this[key] !== other[key]) {
return false;
}
}
return true;
}
static create(props) {
// eslint-disable-next-line no-new
return (0, Archetype_1.wrapWithPropertyAccessorHandler)(new this(props));
}
}
exports.ValueObject = ValueObject;
//# sourceMappingURL=ValueObject.js.map