check-more-types
Advanced tools
Comparing version 0.4.0 to 0.5.0
{ | ||
"name": "check-more-types", | ||
"main": "check-more-types.js", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"homepage": "https://github.com/kensho/check-more-types", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -89,13 +89,2 @@ (function checkMoreTypes(check) { | ||
if (!check.verify.arrayOfStrings) { | ||
check.verify.arrayOfStrings = function (a, msg) { | ||
check.verify.array(a, msg + '\nexpected an array, got ' + | ||
JSON.stringify(a, null, 2)); | ||
a.forEach(function (str, k) { | ||
check.verify.string(str, msg + '\nexpected string at position ' + k + ' got ' + | ||
JSON.stringify(str, null, 2)); | ||
}); | ||
}; | ||
} | ||
/** | ||
@@ -113,18 +102,10 @@ Returns true if given argument is array of arrays of strings | ||
if (!check.verify.arrayOfArraysOfStrings) { | ||
check.verify.arrayOfArraysOfStrings = function (a, msg) { | ||
check.verify.array(a, msg + '\nexpected a top level array, got ' + | ||
JSON.stringify(a, null, 2)); | ||
a.forEach(function (arr, k) { | ||
check.verify.arrayOfStrings(arr, | ||
msg + '\nexpected an array of strings at position ' + k + ' got ' + | ||
JSON.stringify(arr, null, 2)); | ||
}); | ||
}; | ||
} | ||
/** | ||
Checks if object passes all rules in predicates. | ||
check.all({ foo: 'foo' }, { foo: check.string }, 'wrong object'); | ||
This is a composition of check.every(check.map ...) calls | ||
https://github.com/philbooth/check-types.js#batch-operations | ||
@method all | ||
@@ -145,25 +126,2 @@ @param {object} object object to check | ||
if (!check.verify.all) { | ||
/* | ||
verify.all(object, predicates, message) | ||
object - object to verify | ||
predicates - properties to verify, each property should have a check string | ||
message - requires message to throw if any predicate is false | ||
verify.all({ foo: 'foo' }, { foo: check.string }, 'wrong object'); | ||
This is a composition of check.every(check.map ...) calls | ||
https://github.com/philbooth/check-types.js#batch-operations | ||
*/ | ||
check.verify.all = function (obj, predicates, message) { | ||
check.verify.unemptyString(message, 'missing error string'); | ||
check.verify.object(obj, 'missing object to check'); | ||
check.verify.object(predicates, 'missing predicates object'); | ||
if (!check.every(check.map(obj, predicates))) { | ||
throw new Error(message); | ||
} | ||
}; | ||
} | ||
/** Checks if given function raises an error | ||
@@ -235,5 +193,22 @@ | ||
/** | ||
* Public modifier `verify`. | ||
* | ||
* Throws if `predicate` returns `false`. | ||
* copied from check-types.js | ||
*/ | ||
function verifyModifier(predicate, defaultMessage) { | ||
return function () { | ||
var message; | ||
if (predicate.apply(null, arguments) === false) { | ||
message = arguments[arguments.length - 1]; | ||
throw new Error(check.unemptyString(message) ? message : defaultMessage); | ||
} | ||
}; | ||
} | ||
registerPredicate(check, fn.name, fn); | ||
registerPredicate(check.maybe, fn.name, maybeModifier(fn)); | ||
registerPredicate(check.not, fn.name, notModifier(fn)); | ||
registerPredicate(check.verify, fn.name, verifyModifier(fn, 'fn.name failed')); | ||
}; | ||
@@ -240,0 +215,0 @@ } |
@@ -133,3 +133,2 @@ # API | ||
Every predicate function is also added to `check.maybe` object. | ||
@@ -157,2 +156,20 @@ The `maybe` predicate passes if the argument is null or undefined, | ||
Every predicate can also throw an exception if it fails | ||
### check.verify | ||
check.verify.arrayOfStrings(['foo', 'bar']); | ||
check.verify.bit(1); | ||
function nonStrings() { | ||
check.verify.arrayOfStrings(['Foo', 1]); | ||
} | ||
check.raises(nonStrings); // true | ||
function nonLowerCase() { | ||
check.verify.lowerCase('Foo'); | ||
} | ||
check.raises(nonLowerCase); // true | ||
--- | ||
## Adding your own predicates | ||
@@ -159,0 +176,0 @@ |
{ | ||
"name": "check-more-types", | ||
"description": "Additional type checks for https://github.com/philbooth/check-types.js", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", | ||
@@ -10,3 +10,3 @@ "bugs": { | ||
"devDependencies": { | ||
"check-types": "1.3.1", | ||
"check-types": "1.3.2", | ||
"grunt-contrib-jshint": "0.10.0", | ||
@@ -13,0 +13,0 @@ "grunt-contrib-watch": "0.6.1", |
@@ -1,2 +0,2 @@ | ||
# check-more-types v0.4.0 | ||
# check-more-types v0.5.0 | ||
@@ -167,3 +167,2 @@ > Additional type checks for [check-types.js](https://github.com/philbooth/check-types.js) | ||
Every predicate function is also added to `check.maybe` object. | ||
@@ -191,2 +190,20 @@ The `maybe` predicate passes if the argument is null or undefined, | ||
Every predicate can also throw an exception if it fails | ||
#### check.verify | ||
check.verify.arrayOfStrings(['foo', 'bar']); | ||
check.verify.bit(1); | ||
function nonStrings() { | ||
check.verify.arrayOfStrings(['Foo', 1]); | ||
} | ||
check.raises(nonStrings); // true | ||
function nonLowerCase() { | ||
check.verify.lowerCase('Foo'); | ||
} | ||
check.raises(nonLowerCase); // true | ||
--- | ||
### Adding your own predicates | ||
@@ -193,0 +210,0 @@ |
@@ -180,6 +180,2 @@ require('lazy-ass'); | ||
})); | ||
la(check.raises(function () { | ||
check.verify.all({}, {}); | ||
})); | ||
}); | ||
@@ -559,2 +555,27 @@ | ||
}); | ||
describe('check.verify extras', function () { | ||
it('has extra methods', function () { | ||
la(check.object(check.verify)); | ||
la(check.fn(check.verify.lowerCase)); | ||
}); | ||
it('check.verify', function () { | ||
check.verify.arrayOfStrings(['foo', 'bar']); | ||
check.verify.bit(1); | ||
function nonStrings() { | ||
check.verify.arrayOfStrings(['Foo', 1]); | ||
} | ||
la(check.raises(nonStrings)); | ||
function nonLowerCase() { | ||
check.verify.lowerCase('Foo'); | ||
} | ||
la(check.raises(nonLowerCase)); | ||
}); | ||
}); | ||
}); |
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
40662
265
761