Skip to content

Commit

Permalink
1.0.0 added check.then
Browse files Browse the repository at this point in the history
  • Loading branch information
Gleb Bahmutov committed Sep 29, 2014
1 parent 280596c commit 943f19d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 8 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# check-more-types v0.9.9
# check-more-types v1.0.0

> Additional type checks for [check-types.js](https://github.com/philbooth/check-types.js)
Expand Down Expand Up @@ -80,6 +80,8 @@ for advice and examples.
* [protects optional arguments](#protects-optional-arguments)
* [check.defend with messages](#checkdefend-with-messages)
* [check.defend in module pattern](#checkdefend-in-module-pattern)
* [Safe callback execution](#safe-callback-execution)
* [check.then](#checkthen)


#### check.defined
Expand Down Expand Up @@ -506,6 +508,29 @@ This works great when combined with JavaScript module pattern as in this example

---

### Safe callback execution

Sometimes we want to execute a function depending on the condition, but without throwing an
exception. For these cases, there is `check.then`

#### check.then

function isSum10(a, b) {
return a + b === 10;
}

function sum(a, b) {
return a + b;
}
var onlyAddTo10 = check.then(isSum10, sum);
// isSum10 returns true for these arguments
// then sum is executed
onlyAddTo10(3, 7); // 10
onlyAddTo10(1, 2); // undefined
// sum is never called because isSum10 condition is false

----

### Small print

Author: Kensho © 2014
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "check-more-types",
"main": "check-more-types.js",
"version": "0.9.9",
"version": "1.0.0",
"homepage": "https://github.com/kensho/check-more-types",
"license": "MIT",
"ignore": [
Expand Down
4 changes: 3 additions & 1 deletion check-more-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@
*/
check.then = function then(condition, fn) {
return function () {
if (condition) {
var ok = typeof condition === 'function' ?
condition.apply(null, arguments) : condition;
if (ok) {
return fn.apply(null, arguments);
}
};
Expand Down
4 changes: 2 additions & 2 deletions check-more-types.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion docs/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,27 @@ This works great when combined with JavaScript module pattern as in this example
}
check.raises(callAddWithNonNumbers); // true

---
---

## Safe callback execution

Sometimes we want to execute a function depending on the condition, but without throwing an
exception. For these cases, there is `check.then`

### check.then

function isSum10(a, b) {
return a + b === 10;
}

function sum(a, b) {
return a + b;
}
var onlyAddTo10 = check.then(isSum10, sum);
// isSum10 returns true for these arguments
// then sum is executed
onlyAddTo10(3, 7); // 10
onlyAddTo10(1, 2); // undefined
// sum is never called because isSum10 condition is false

----
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "check-more-types",
"description": "Additional type checks for https://github.com/philbooth/check-types.js",
"version": "0.9.9",
"version": "1.0.0",
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"bugs": {
"url": "https://github.com/kensho/check-more-types/issues"
Expand Down
45 changes: 44 additions & 1 deletion test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('check-more-types', function () {
function doIt() { done = true; }

beforeEach(function () {
console.log('done = false');
done = false;
});

Expand All @@ -38,7 +37,51 @@ describe('check-more-types', function () {
});

it('can evaluate predicate function', function () {
function isTrue() { return true; }
var safeDo = check.then(isTrue, doIt);
safeDo();
la(done);
});

it('can evaluate predicate function (returns false)', function () {
function isFalse() { return false; }
var safeDo = check.then(isFalse, doIt);
safeDo();
la(!done);
});

it('can evaluate condition based on arguments', function () {
function is3(a) {
return a === 3;
}
var safeDo = check.then(is3, doIt);
safeDo();
la(!done, 'argument was not 3');

safeDo(3);
la(done, 'argument was 3');
});

it('handles multiple arguments', function () {
function sumIs10(a, b) { return a + b === 10; }
var safeDo = check.then(sumIs10, doIt);
safeDo(4, 6);
la(done, 'executed');
done = false;
safeDo(4, 4);
la(!done, 'sum was not 10');
});

it('check.then', function () {
function isSum10(a, b) { return a + b === 10; }
function sum(a, b) { return a + b; }
var onlyAddTo10 = check.then(isSum10, sum);
// isSum10 returns true for these arguments
// then sum is executed
la(onlyAddTo10(3, 7) === 10);

la(onlyAddTo10(1, 2) === undefined);
// sum is never called because isSum10 condition is false
});
});

Expand Down

0 comments on commit 943f19d

Please sign in to comment.