@cashfarm/plow
Version:
Library for validating input data and parameters
42 lines • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ConstructorParams = Symbol.for('plow:ValueObject.ConstructorParams');
const Constructor = Symbol.for('plow:ValueObject.Constructor');
/**
* Base class for ValueObject's
*
* A value object is an object whose identitiy is
* determined by it's properties values.
*
* Value objects MUST:
* - Be immutable
* - Be compared by value-equality
* @export
* @class ValueObject
*/
class ValueObject {
/**
* @param construct Your value object's constructor
* @param constructParams Name of the properties to pass to constructor IN ORDER
*/
constructor(construct, constructParams) {
this.construct = construct;
this[Constructor] = construct;
this[ConstructorParams] = constructParams;
}
equals(other) {
return !Object.keys(this).some(prop => {
// return true if prop is different
if (typeof this[prop].equals === 'function') {
return !this[prop].equals(other[prop]);
}
return this[prop] !== other[prop];
});
}
newInstanceWith(updatedProps) {
return new this[Constructor](...this[ConstructorParams]
.map(p => updatedProps.hasOwnProperty(p) ? updatedProps[p] : this[p]));
}
}
exports.ValueObject = ValueObject;
//# sourceMappingURL=valueObject.js.map