From 5d3ffa6250274e123150663a270cc0add6c6ab57 Mon Sep 17 00:00:00 2001 From: Florian Date: Mon, 11 Nov 2019 18:57:36 -0500 Subject: [PATCH] fix(Result): Result.recoverWhen should execute the predicate before executing the lambda --- source/result.js | 5 +++-- test/result-test.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/source/result.js b/source/result.js index 6a0725d..99ba175 100644 --- a/source/result.js +++ b/source/result.js @@ -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() { diff --git a/test/result-test.js b/test/result-test.js index f1c6c34..e7988bf 100644 --- a/test/result-test.js +++ b/test/result-test.js @@ -307,6 +307,21 @@ 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; + }); + + expect(lambdaCalled).to.equal(false); + expect(transformed.merge()).to.equal(value); + }); }); describe('flatMap(λ)', () => {