From 5fd40588188f24cc7ce5393d0c4515be5df87961 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 | 17 +++++++++++++++++ 2 files changed, 20 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..5da4feb 100644 --- a/test/result-test.js +++ b/test/result-test.js @@ -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(λ)', () => {