Skip to content

Instantly share code, notes, and snippets.

@gcollazo
Created October 31, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcollazo/1599a623baf6fed164e23fe78ab312a4 to your computer and use it in GitHub Desktop.
Save gcollazo/1599a623baf6fed164e23fe78ab312a4 to your computer and use it in GitHub Desktop.
export default class ValueObject {
private readonly value: string;
constructor(value: string) {
if (!value) {
throw new ValidationError("ValueObject value cannot be empty");
}
this.value = value;
}
map(fn: (value: string) => string): ValueObject {
return new ValueObject(fn(this.value));
}
equals(otherValue: ValueObject): boolean {
return otherValue.value === this.value;
}
valueOf(): string {
return this.value;
}
static create(value: string): Result<ValueObject, BaseError> {
try {
let val = new ValueObject(value);
return ok<ValueObject, BaseError>(val);
} catch (error) {
return err<ValueObject, BaseError>(error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment