From 192c34be501599f908937c06f12599f327c6d37a Mon Sep 17 00:00:00 2001 From: Aldwin Vlasblom Date: Sat, 6 May 2017 13:27:49 +0200 Subject: [PATCH] Add fast failure to encase and fromPromise Closes #91 --- src/encase.js | 12 +++++------- src/from-promise.js | 12 +++++------- test/5.encase.test.js | 6 +++--- test/5.from-promise.test.js | 6 +++--- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/encase.js b/src/encase.js index 15bf2efd..d5e14987 100644 --- a/src/encase.js +++ b/src/encase.js @@ -56,28 +56,26 @@ export function attempt(f){ } export function encase(f, x){ - if(arguments.length === 1) return partial1(encase, f); if(!isFunction(f)) invalidArgument('Future.encase', 0, 'be a function', f); + if(arguments.length === 1) return partial1(encase, f); return new Encase(f, x); } export function encase2(f, x, y){ + if(!isFunction(f)) invalidArgument('Future.encase2', 0, 'be a function', f); switch(arguments.length){ case 1: return partial1(encase2, f); case 2: return partial2(encase2, f, x); - default: - if(!isFunction(f)) invalidArgument('Future.encase2', 0, 'be a function', f); - return new Encase(f, x, y); + default: return new Encase(f, x, y); } } export function encase3(f, x, y, z){ + if(!isFunction(f)) invalidArgument('Future.encase3', 0, 'be a function', f); switch(arguments.length){ case 1: return partial1(encase3, f); case 2: return partial2(encase3, f, x); case 3: return partial3(encase3, f, x, y); - default: - if(!isFunction(f)) invalidArgument('Future.encase3', 0, 'be a function', f); - return new Encase(f, x, y, z); + default: return new Encase(f, x, y, z); } } diff --git a/src/from-promise.js b/src/from-promise.js index 2daeb3a7..c81ff1ef 100644 --- a/src/from-promise.js +++ b/src/from-promise.js @@ -51,28 +51,26 @@ FromPromise.prototype.toString = function FromPromise$toString(){ }; export function fromPromise(f, x){ - if(arguments.length === 1) return partial1(fromPromise, f); if(!isFunction(f)) invalidArgument('Future.fromPromise', 0, 'be a function', f); + if(arguments.length === 1) return partial1(fromPromise, f); return new FromPromise(f, x); } export function fromPromise2(f, x, y){ + if(!isFunction(f)) invalidArgument('Future.fromPromise2', 0, 'be a function', f); switch(arguments.length){ case 1: return partial1(fromPromise2, f); case 2: return partial2(fromPromise2, f, x); - default: - if(!isFunction(f)) invalidArgument('Future.fromPromise2', 0, 'be a function', f); - return new FromPromise(f, x, y); + default: return new FromPromise(f, x, y); } } export function fromPromise3(f, x, y, z){ + if(!isFunction(f)) invalidArgument('Future.fromPromise3', 0, 'be a function', f); switch(arguments.length){ case 1: return partial1(fromPromise3, f); case 2: return partial2(fromPromise3, f, x); case 3: return partial3(fromPromise3, f, x, y); - default: - if(!isFunction(f)) invalidArgument('Future.fromPromise3', 0, 'be a function', f); - return new FromPromise(f, x, y, z); + default: return new FromPromise(f, x, y, z); } } diff --git a/test/5.encase.test.js b/test/5.encase.test.js index 42ff7872..a42ab51e 100644 --- a/test/5.encase.test.js +++ b/test/5.encase.test.js @@ -31,7 +31,7 @@ describe('encase()', () => { it('throws TypeError when not given a function', () => { const xs = [NaN, {}, [], 1, 'a', new Date, undefined, null]; - const fs = xs.map(x => () => encase(x)(1)); + const fs = xs.map(x => () => encase(x)); fs.forEach(f => expect(f).to.throw(TypeError, /Future/)); }); @@ -53,7 +53,7 @@ describe('encase2()', () => { it('throws TypeError when not given a function', () => { const xs = [NaN, {}, [], 1, 'a', new Date, undefined, null]; - const fs = xs.map(x => () => encase2(x)(1)(2)); + const fs = xs.map(x => () => encase2(x)); fs.forEach(f => expect(f).to.throw(TypeError, /Future/)); }); @@ -79,7 +79,7 @@ describe('encase3()', () => { it('throws TypeError when not given a function', () => { const xs = [NaN, {}, [], 1, 'a', new Date, undefined, null]; - const fs = xs.map(x => () => encase3(x)(1)(2)(3)); + const fs = xs.map(x => () => encase3(x)); fs.forEach(f => expect(f).to.throw(TypeError, /Future/)); }); diff --git a/test/5.from-promise.test.js b/test/5.from-promise.test.js index e71ec8e8..beedc030 100644 --- a/test/5.from-promise.test.js +++ b/test/5.from-promise.test.js @@ -17,7 +17,7 @@ describe('fromPromise()', () => { it('throws TypeError when not given a function', () => { const xs = [NaN, {}, [], 1, 'a', new Date, undefined, null]; - const fs = xs.map(x => () => fromPromise(x)(1)); + const fs = xs.map(x => () => fromPromise(x)); fs.forEach(f => expect(f).to.throw(TypeError, /Future/)); }); @@ -39,7 +39,7 @@ describe('fromPromise2()', () => { it('throws TypeError when not given a function', () => { const xs = [NaN, {}, [], 1, 'a', new Date, undefined, null]; - const fs = xs.map(x => () => fromPromise2(x)(1)(2)); + const fs = xs.map(x => () => fromPromise2(x)); fs.forEach(f => expect(f).to.throw(TypeError, /Future/)); }); @@ -65,7 +65,7 @@ describe('fromPromise3()', () => { it('throws TypeError when not given a function', () => { const xs = [NaN, {}, [], 1, 'a', new Date, undefined, null]; - const fs = xs.map(x => () => fromPromise3(x)(1)(2)(3)); + const fs = xs.map(x => () => fromPromise3(x)); fs.forEach(f => expect(f).to.throw(TypeError, /Future/)); });