@synet/patterns
Version:
Robust, battle-tested collection of stable patterns used in Synet packages
36 lines (35 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Unit = void 0;
const result_1 = require("../patterns/result");
const value_object_1 = require("../patterns/value-object");
class Unit extends value_object_1.ValueObject {
constructor(props) {
super(props);
}
static create(props) {
const unit = new Unit(props);
return result_1.Result.success(unit);
}
static fromJSON(json) {
try {
const data = JSON.parse(json);
return Unit.create(data);
}
catch (error) {
return result_1.Result.fail(`Invalid JSON: ${error instanceof Error ? error.message : String(error)}`);
}
}
toJSON() {
try {
return JSON.stringify(this.props.data);
}
catch (error) {
throw new Error(`Failed to serialize unit to JSON: ${error instanceof Error ? error.message : String(error)}`);
}
}
get data() {
return this.props.data;
}
}
exports.Unit = Unit;