Created
October 31, 2020 19:35
-
-
Save gcollazo/1599a623baf6fed164e23fe78ab312a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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