Skip to content

Commit

Permalink
Add fast failure to encase and fromPromise
Browse files Browse the repository at this point in the history
Closes #91
  • Loading branch information
Avaq committed May 6, 2017
1 parent fe577cc commit 192c34b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
12 changes: 5 additions & 7 deletions src/encase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
12 changes: 5 additions & 7 deletions src/from-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions test/5.encase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/));
});

Expand All @@ -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/));
});

Expand All @@ -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/));
});

Expand Down
6 changes: 3 additions & 3 deletions test/5.from-promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/));
});

Expand All @@ -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/));
});

Expand All @@ -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/));
});

Expand Down

0 comments on commit 192c34b

Please sign in to comment.