Skip to content

Commit

Permalink
Add map() methods for Result instances
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed May 17, 2019
1 parent b8b2ea6 commit d7b9e8b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/json-decoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ const expectOkWithValue = <a>(result: Result<a>, expectedValue: a) =>
expect(result)
.to.be.an.instanceof(Ok)
.and.to.deep.equal({
value: expectedValue
value: expectedValue,
map: Ok.prototype.map
});
const expectErr = <a>(result: Result<a>) =>
expect(result).to.be.an.instanceof(Err);
const expectErrWithMsg = <a>(result: Result<a>, expectedErrorMsg: string) =>
expect(result)
.to.be.an.instanceof(Err)
.and.to.deep.equal({ error: expectedErrorMsg });
.and.to.deep.equal({ error: expectedErrorMsg, map: Err.prototype.map });

// Tests
describe('json-decoder', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
export class Ok<a> {
constructor(readonly value: a) {}

map<b>(fn: (a: a) => b): Result<b> {
return ok(fn(this.value));
}
}
export class Err {
constructor(readonly error: string) {}

map<b>(fn: (a: any) => b): Result<b> {
return this;
}
}
export type Result<a> = Ok<a> | Err;

Expand Down

0 comments on commit d7b9e8b

Please sign in to comment.