Skip to content

Commit

Permalink
added check.promise method
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Mar 26, 2015
1 parent 99582e3 commit ca09f79
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# check-more-types v1.4.0
# check-more-types v1.5.0

> Additional type checks for [check-types.js](https://github.com/philbooth/check-types.js)
Expand Down Expand Up @@ -90,6 +90,7 @@ for advice and examples.
* [check.defend in module pattern](#checkdefend-in-module-pattern)
* [Safe callback execution](#safe-callback-execution)
* [check.then](#checkthen)
* [check.promise](#checkpromise)


#### check.defined
Expand Down Expand Up @@ -610,6 +611,10 @@ exception. For these cases, there is `check.then`

----

#### check.promise

Returns true if given object has promise methods (`.then`, etc)

### 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": "1.4.0",
"version": "1.5.0",
"homepage": "https://github.com/kensho/check-more-types",
"license": "MIT",
"ignore": [
Expand Down
22 changes: 21 additions & 1 deletion check-more-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,25 @@
};
}

var promiseSchema = {
then: check.fn
};

// work around reserved keywords checks
promiseSchema['catch'] = check.fn;
promiseSchema['finally'] = check.fn;

var hasPromiseApi = schema.bind(null, promiseSchema);

/**
Returns true if argument implements promise api (.then, .catch, .finally)
@method promise
*/
function isPromise(p) {
return typeof p === 'object' &&
hasPromiseApi(p);
}

// new predicates to be added to check object. Use object to preserve names
var predicates = {
defined: defined,
Expand Down Expand Up @@ -482,7 +501,8 @@
git: git,
arrayOf: arrayOf,
badItems: badItems,
oneOf: oneOf
oneOf: oneOf,
promise: isPromise
};

Object.keys(predicates).forEach(function (name) {
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.

6 changes: 5 additions & 1 deletion docs/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,8 @@ exception. For these cases, there is `check.then`
onlyAddTo10(1, 2); // undefined
// sum is never called because isSum10 condition is false

----
----

### check.promise

Returns true if given object has promise methods (`.then`, etc)
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": "1.4.0",
"version": "1.5.0",
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"bugs": {
"url": "https://github.com/kensho/check-more-types/issues"
Expand Down
15 changes: 15 additions & 0 deletions test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ describe('check-more-types', function () {
la(check.fn(check.lowerCase));
});

describe('check/promise', function () {
it('checks objects api', function () {
var p = {
then: function () {},
catch: function () {},
finally: function () {}
};
la(check.promise(p));
la(!check.promise({}));
la(!check.promise('foo'));
la(!check.promise());
});

});

describe('check/oneOf', function () {
it('validates color', function () {
var colors = ['red', 'green', 'blue'];
Expand Down

0 comments on commit ca09f79

Please sign in to comment.