Skip to content

Commit

Permalink
fix(Result): Result.recoverWhen should execute the predicate before e…
Browse files Browse the repository at this point in the history
…xecuting the lambda
  • Loading branch information
Florian committed Nov 11, 2019
1 parent c29161a commit 5fd4058
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions source/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ Object.assign(Error.prototype, {

recoverWhen(predicate, λ) {
return Ok(this.error)
.map(λ)
.filter(predicate);
.filter(predicate)
.mapError(constant(this.error))
.map(λ);
},

merge() {
Expand Down
17 changes: 17 additions & 0 deletions test/result-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ describe('The Result type', () => {

expect(transformed.merge()).to.equal(value);
});

it('should not call the lambda if the predicate evaluates to false', () => {
let lambdaCalled = false;

const value = 10;

const error = Error(value);

const transformed = error.recoverWhen(constant(false), () => {
lambdaCalled = true;
});

console.log(transformed);

expect(lambdaCalled).to.equal(false);
expect(transformed.merge()).to.equal(value);
});
});

describe('flatMap(λ)', () => {
Expand Down

0 comments on commit 5fd4058

Please sign in to comment.