check-more-types
Advanced tools
Comparing version 0.8.0 to 0.8.1
{ | ||
"name": "check-more-types", | ||
"main": "check-more-types.js", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"homepage": "https://github.com/kensho/check-more-types", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -7,20 +7,8 @@ # {%= name %} v{%= version %} | ||
### Install | ||
**node:** `npm install {%= name %} --save` | ||
global.check = require('check-types'); | ||
require('{%= name %}'); | ||
// patches global check object | ||
**browser** `bower install {%= name %} --save` | ||
<script src="check-types.js"></script> | ||
<script src="{%= name %}.js"></script> | ||
See [Readable conditions](http://bahmutov.calepin.co/readable-conditions-using-check-typesjs.html) | ||
for examples. | ||
for advice and examples. | ||
{%= _.doc("./docs/install.md") %} | ||
{%= _.doc("./docs/use-toc.md") %} | ||
{%= _.doc("./docs/use.md") %} | ||
{%= _.doc("./docs/footer.md") %} | ||
@@ -27,0 +15,0 @@ |
@@ -1,3 +0,1 @@ | ||
# API | ||
### check.defined | ||
@@ -111,2 +109,18 @@ | ||
### check.schema | ||
var obj = { | ||
foo: 'foo', | ||
bar: 'bar', | ||
baz: 'baz' | ||
}; | ||
var schema = { | ||
foo: check.unemptyString, | ||
bar: function(value) { | ||
return value === 'bar'; | ||
} | ||
}; | ||
check.schema(schema, obj); // true | ||
check.schema(schema, {}); // false | ||
`check.spec` is equivalent to `check.all` but with arguments reversed. | ||
@@ -203,13 +217,44 @@ This makes it very convenient to create new validator functions using partial | ||
### Defending a function | ||
### check.mixin(predicate, name) | ||
function isBar(a) { | ||
return a === 'bar'; | ||
} | ||
check.mixin(isBar, 'bar'); | ||
check.bar('bar'); // true | ||
check.bar('anything else'); // false | ||
// supports modifiers | ||
check.maybe.bar(); // true | ||
check.maybe.bar('bar'); // true | ||
check.not.bar('foo'); // true | ||
check.not.bar('bar'); // false | ||
Mixin will not override existing functions | ||
### check.mixin does not override | ||
function isFoo(a) { | ||
return a === 'foo'; | ||
} | ||
function isBar(a) { | ||
return a === 'bar'; | ||
} | ||
check.mixin(isFoo, 'isFoo'); | ||
check.isFoo; // isFoo | ||
check.mixin(isBar, 'isFoo'); | ||
check.isFoo; // isFoo | ||
## Defending a function | ||
Using *check-more-types* you can separate the inner function logic from checking input | ||
arguments. Instead of this | ||
:::javascript | ||
function add(a, b) { | ||
la(check.number(a), 'first argument should be a number', a); | ||
la(check.number(a), 'second argument should be a number', b); | ||
return a + b; | ||
} | ||
```js | ||
function add(a, b) { | ||
la(check.number(a), 'first argument should be a number', a); | ||
la(check.number(a), 'second argument should be a number', b); | ||
return a + b; | ||
} | ||
``` | ||
@@ -225,2 +270,3 @@ you can use `check.defend` function | ||
add('foo', 2); // 'foo2' | ||
// calling safeAdd('foo', 2) raises an exception | ||
check.raises(safeAdd.bind(null, 'foo', 2)); // true | ||
@@ -245,12 +291,21 @@ | ||
This works great when combined with JavaScript module pattern | ||
This works great when combined with JavaScript module pattern as in this example | ||
:::javascript | ||
var add = (function () { | ||
// inner private function without any argument checks | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
// return defended function | ||
return check.defend(add, check.number, check.number); | ||
}()); | ||
### check.defend in module pattern | ||
var add = (function() { | ||
// inner private function without any argument checks | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
// return defended function | ||
return check.defend(add, check.number, check.number); | ||
}()); | ||
add(2, 3); // 5 | ||
// trying to call with non-numbers raises an exception | ||
function callAddWithNonNumbers() { | ||
return add('foo', 'bar'); | ||
} | ||
check.raises(callAddWithNonNumbers); // true | ||
--- |
@@ -36,2 +36,13 @@ module.exports = function(grunt) { | ||
toc: { | ||
api: { | ||
options: { | ||
heading: '** API **\n' | ||
}, | ||
files: { | ||
'./docs/use-toc.md': './docs/use.md' | ||
} | ||
} | ||
}, | ||
readme: { | ||
@@ -83,5 +94,5 @@ options: { | ||
grunt.registerTask('test', ['mochaTest']); | ||
grunt.registerTask('doc', ['xplain', 'readme']); | ||
grunt.registerTask('doc', ['xplain', 'toc', 'readme']); | ||
grunt.registerTask('default', | ||
['nice-package', 'deps-ok', 'sync', 'jshint', 'test', 'uglify', 'doc']); | ||
}; |
{ | ||
"name": "check-more-types", | ||
"description": "Additional type checks for https://github.com/philbooth/check-types.js", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", | ||
@@ -20,2 +20,3 @@ "bugs": { | ||
"grunt-readme": "0.4.5", | ||
"grunt-toc": "0.1.0", | ||
"grunt-xplain": "0.2.4", | ||
@@ -22,0 +23,0 @@ "lazy-ass": "0.5.1", |
122
README.md
@@ -1,2 +0,2 @@ | ||
# check-more-types v0.8.0 | ||
# check-more-types v0.8.1 | ||
@@ -22,4 +22,7 @@ > Additional type checks for [check-types.js](https://github.com/philbooth/check-types.js) | ||
### Install | ||
See [Readable conditions](http://bahmutov.calepin.co/readable-conditions-using-check-typesjs.html) | ||
for advice and examples. | ||
## Install | ||
**node:** `npm install check-more-types --save` | ||
@@ -36,7 +39,28 @@ | ||
See [Readable conditions](http://bahmutov.calepin.co/readable-conditions-using-check-typesjs.html) | ||
for examples. | ||
## API | ||
** API ** | ||
* [check.bit](#checkbit) | ||
* [check.bool](#checkbool) | ||
* [check.unemptyArray](#checkunemptyarray) | ||
* [check.arrayOfStrings](#checkarrayofstrings) | ||
* [check.arrayOfArraysOfStrings](#checkarrayofarraysofstrings) | ||
* [check.lowerCase](#checklowercase) | ||
* [check.has(obj, property)](#checkhasobj-property) | ||
* [check.all](#checkall) | ||
* [check.schema](#checkschema) | ||
* [check.schema bind](#checkschema-bind) | ||
* [check.raises(fn, validator)](#checkraisesfn-validator) | ||
* [Modifiers](#modifiers) | ||
* [check.maybe](#checkmaybe) | ||
* [check.not](#checknot) | ||
* [check.verify](#checkverify) | ||
* [Adding your own predicates](#adding-your-own-predicates) | ||
* [check.mixin(predicate, name)](#checkmixinpredicate-name) | ||
* [check.mixin does not override](#checkmixin-does-not-override) | ||
* [Defending a function](#defending-a-function) | ||
* [check.defend(fn, predicates)](#checkdefendfn-predicates) | ||
* [protects optional arguments](#protects-optional-arguments) | ||
* [check.defend in module pattern](#checkdefend-in-module-pattern) | ||
#### check.defined | ||
@@ -150,2 +174,18 @@ | ||
#### check.schema | ||
var obj = { | ||
foo: 'foo', | ||
bar: 'bar', | ||
baz: 'baz' | ||
}; | ||
var schema = { | ||
foo: check.unemptyString, | ||
bar: function(value) { | ||
return value === 'bar'; | ||
} | ||
}; | ||
check.schema(schema, obj); // true | ||
check.schema(schema, {}); // false | ||
`check.spec` is equivalent to `check.all` but with arguments reversed. | ||
@@ -242,13 +282,44 @@ This makes it very convenient to create new validator functions using partial | ||
#### Defending a function | ||
#### check.mixin(predicate, name) | ||
function isBar(a) { | ||
return a === 'bar'; | ||
} | ||
check.mixin(isBar, 'bar'); | ||
check.bar('bar'); // true | ||
check.bar('anything else'); // false | ||
// supports modifiers | ||
check.maybe.bar(); // true | ||
check.maybe.bar('bar'); // true | ||
check.not.bar('foo'); // true | ||
check.not.bar('bar'); // false | ||
Mixin will not override existing functions | ||
#### check.mixin does not override | ||
function isFoo(a) { | ||
return a === 'foo'; | ||
} | ||
function isBar(a) { | ||
return a === 'bar'; | ||
} | ||
check.mixin(isFoo, 'isFoo'); | ||
check.isFoo; // isFoo | ||
check.mixin(isBar, 'isFoo'); | ||
check.isFoo; // isFoo | ||
### Defending a function | ||
Using *check-more-types* you can separate the inner function logic from checking input | ||
arguments. Instead of this | ||
:::javascript | ||
function add(a, b) { | ||
la(check.number(a), 'first argument should be a number', a); | ||
la(check.number(a), 'second argument should be a number', b); | ||
return a + b; | ||
} | ||
```js | ||
function add(a, b) { | ||
la(check.number(a), 'first argument should be a number', a); | ||
la(check.number(a), 'second argument should be a number', b); | ||
return a + b; | ||
} | ||
``` | ||
@@ -264,2 +335,3 @@ you can use `check.defend` function | ||
add('foo', 2); // 'foo2' | ||
// calling safeAdd('foo', 2) raises an exception | ||
check.raises(safeAdd.bind(null, 'foo', 2)); // true | ||
@@ -284,14 +356,22 @@ | ||
This works great when combined with JavaScript module pattern | ||
This works great when combined with JavaScript module pattern as in this example | ||
:::javascript | ||
var add = (function () { | ||
// inner private function without any argument checks | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
// return defended function | ||
return check.defend(add, check.number, check.number); | ||
#### check.defend in module pattern | ||
var add = (function() { | ||
// inner private function without any argument checks | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
// return defended function | ||
return check.defend(add, check.number, check.number); | ||
}()); | ||
add(2, 3); // 5 | ||
// trying to call with non-numbers raises an exception | ||
function callAddWithNonNumbers() { | ||
return add('foo', 'bar'); | ||
} | ||
check.raises(callAddWithNonNumbers); // true | ||
--- | ||
@@ -298,0 +378,0 @@ ### Small print |
@@ -266,4 +266,5 @@ describe('check-more-types', function () { | ||
describe('check.schema', function () { | ||
la(check.fn(check.schema)); | ||
it('check.schema', function () { | ||
la(check.fn(check.schema)); | ||
var obj = { | ||
@@ -281,2 +282,3 @@ foo: 'foo', | ||
la(check.schema(schema, obj)); | ||
la(!check.schema(schema, {})); | ||
}); | ||
@@ -599,3 +601,32 @@ | ||
la(check.bar === isBar, 'old check predicate remains'); | ||
delete check.foo; | ||
delete check.bar; | ||
}); | ||
it('check.mixin(predicate, name)', function () { | ||
function isBar(a) { | ||
return a === 'bar'; | ||
} | ||
check.mixin(isBar, 'bar'); | ||
la(check.bar('bar')); | ||
la(!check.bar('anything else')); | ||
// supports modifiers | ||
la(check.maybe.bar()); | ||
la(check.maybe.bar('bar')); | ||
la(check.not.bar('foo')); | ||
la(!check.not.bar('bar')); | ||
}); | ||
it('check.mixin does not override', function () { | ||
function isFoo(a) { | ||
return a === 'foo'; | ||
} | ||
function isBar(a) { | ||
return a === 'bar'; | ||
} | ||
check.mixin(isFoo, 'isFoo'); | ||
la(check.isFoo === isFoo, 'predicate has been registered'); | ||
check.mixin(isBar, 'isFoo'); | ||
la(check.isFoo === isFoo, 'old check predicate remains'); | ||
}); | ||
}); | ||
@@ -667,5 +698,23 @@ | ||
la(add('foo', 2) === 'foo2', 'adding string to number works just fine'); | ||
// calling safeAdd('foo', 2) raises an exception | ||
la(check.raises(safeAdd.bind(null, 'foo', 2))); | ||
}); | ||
it('check.defend in module pattern', function () { | ||
var add = (function () { | ||
// inner private function without any argument checks | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
// return defended function | ||
return check.defend(add, check.number, check.number); | ||
}()); | ||
la(add(2, 3) === 5); | ||
// trying to call with non-numbers raises an exception | ||
function callAddWithNonNumbers() { | ||
return add('foo', 'bar'); | ||
} | ||
la(check.raises(callAddWithNonNumbers)); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55559
18
959
409
16