check-more-types
Advanced tools
Comparing version 0.1.3 to 0.2.0
{ | ||
"name": "check-more-types", | ||
"main": "check-more-types.js", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/kensho/check-more-types", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -17,88 +17,72 @@ (function checkMoreTypes(check) { | ||
if (!check.defined) { | ||
/** | ||
Checks if argument is defined or not | ||
// new predicates to be added to check object | ||
var predicates = [defined, bit, bool, has, lowerCase, unemptyArray, | ||
arrayOfStrings, arrayOfArraysOfStrings, all, raises]; | ||
@method defined | ||
*/ | ||
check.defined = function (value) { | ||
return typeof value !== 'undefined'; | ||
}; | ||
/** | ||
Checks if argument is defined or not | ||
@method defined | ||
*/ | ||
function defined(value) { | ||
return typeof value !== 'undefined'; | ||
} | ||
if (!check.bit) { | ||
/** | ||
Checks if given value is 0 or 1 | ||
/** | ||
Checks if given value is 0 or 1 | ||
@method bit | ||
*/ | ||
check.bit = function (value) { | ||
return value === 0 || value === 1; | ||
}; | ||
@method bit | ||
*/ | ||
function bit(value) { | ||
return value === 0 || value === 1; | ||
} | ||
if (!check.bool) { | ||
/** | ||
Checks if given value is true of false | ||
/** | ||
Checks if given value is true of false | ||
@method bool | ||
*/ | ||
check.bool = function (value) { | ||
return typeof value === 'boolean'; | ||
}; | ||
@method bool | ||
*/ | ||
function bool(value) { | ||
return typeof value === 'boolean'; | ||
} | ||
if (!check.has) { | ||
/** | ||
Checks if given object has a property | ||
@method has | ||
*/ | ||
check.has = function (o, property) { | ||
return Boolean(o && property && | ||
typeof property === 'string' && | ||
typeof o[property] !== 'undefined'); | ||
}; | ||
/** | ||
Checks if given object has a property | ||
@method has | ||
*/ | ||
function has(o, property) { | ||
return Boolean(o && property && | ||
typeof property === 'string' && | ||
typeof o[property] !== 'undefined'); | ||
} | ||
if (!check.lowerCase) { | ||
/** | ||
Checks if given string is already in lower case | ||
@method lowerCase | ||
*/ | ||
check.lowerCase = function (str) { | ||
return check.string(str) && | ||
str.toLowerCase() === str; | ||
}; | ||
/** | ||
Checks if given string is already in lower case | ||
@method lowerCase | ||
*/ | ||
function lowerCase(str) { | ||
return check.string(str) && | ||
str.toLowerCase() === str; | ||
} | ||
if(!check.unemptyArray) { | ||
/** | ||
Returns true if the argument is an array with at least one value | ||
@method unemptyArray | ||
*/ | ||
check.unemptyArray = function (a) { | ||
return check.array(a) && a.length > 0; | ||
}; | ||
/** | ||
Returns true if the argument is an array with at least one value | ||
@method unemptyArray | ||
*/ | ||
function unemptyArray(a) { | ||
return check.array(a) && a.length > 0; | ||
} | ||
if (!check.arrayOfStrings) { | ||
/** | ||
Returns true if given array only has strings | ||
@method arrayOfStrings | ||
@param a Array to check | ||
@param checkLowerCase Checks if all strings are lowercase | ||
*/ | ||
check.arrayOfStrings = function (a, checkLowerCase) { | ||
var v = check.array(a) && a.every(check.string); | ||
if (v && check.bool(checkLowerCase) && checkLowerCase) { | ||
return a.every(check.lowerCase); | ||
} | ||
return v; | ||
}; | ||
/** | ||
Returns true if given array only has strings | ||
@method arrayOfStrings | ||
@param a Array to check | ||
@param checkLowerCase Checks if all strings are lowercase | ||
*/ | ||
function arrayOfStrings(a, checkLowerCase) { | ||
var v = check.array(a) && a.every(check.string); | ||
if (v && check.bool(checkLowerCase) && checkLowerCase) { | ||
return a.every(check.lowerCase); | ||
} | ||
return v; | ||
} | ||
@@ -117,15 +101,12 @@ | ||
if (!check.arrayOfArraysOfStrings) { | ||
/** | ||
Returns true if given argument is array of arrays of strings | ||
@method arrayOfArraysOfStrings | ||
@param a Array to check | ||
@param checkLowerCase Checks if all strings are lowercase | ||
*/ | ||
check.arrayOfArraysOfStrings = function (a, checkLowerCase) { | ||
return check.array(a) && a.every(function (arr) { | ||
return check.arrayOfStrings(arr, checkLowerCase); | ||
}); | ||
}; | ||
/** | ||
Returns true if given argument is array of arrays of strings | ||
@method arrayOfArraysOfStrings | ||
@param a Array to check | ||
@param checkLowerCase Checks if all strings are lowercase | ||
*/ | ||
function arrayOfArraysOfStrings(a, checkLowerCase) { | ||
return check.array(a) && a.every(function (arr) { | ||
return check.arrayOfStrings(arr, checkLowerCase); | ||
}); | ||
} | ||
@@ -146,21 +127,18 @@ | ||
if (!check.all) { | ||
/** | ||
Checks if object passes all rules in predicates. | ||
/** | ||
Checks if object passes all rules in predicates. | ||
@method all | ||
@param {object} object object to check | ||
@param {object} predicates rules to check. Usually one per property. | ||
@public | ||
@returns true or false | ||
*/ | ||
check.all = function all(obj, predicates) { | ||
check.verify.object(obj, 'missing object to check'); | ||
check.verify.object(predicates, 'missing predicates object'); | ||
Object.keys(predicates).forEach(function (property) { | ||
check.verify.fn(predicates[property], 'not a predicate function for ' + property); | ||
}); | ||
return check.every(check.map(obj, predicates)); | ||
}; | ||
@method all | ||
@param {object} object object to check | ||
@param {object} predicates rules to check. Usually one per property. | ||
@public | ||
@returns true or false | ||
*/ | ||
function all(obj, predicates) { | ||
check.verify.object(obj, 'missing object to check'); | ||
check.verify.object(predicates, 'missing predicates object'); | ||
Object.keys(predicates).forEach(function (property) { | ||
check.verify.fn(predicates[property], 'not a predicate function for ' + property); | ||
}); | ||
return check.every(check.map(obj, predicates)); | ||
} | ||
@@ -191,26 +169,61 @@ | ||
if (!check.raises) { | ||
/** Checks if given function raises an error | ||
/** Checks if given function raises an error | ||
@method raises | ||
*/ | ||
check.raises = function (fn, errorValidator) { | ||
check.verify.fn(fn, 'expected function that raises'); | ||
try { | ||
fn(); | ||
} catch (err) { | ||
if (typeof errorValidator === 'undefined') { | ||
return true; | ||
} | ||
if (typeof errorValidator === 'function') { | ||
return errorValidator(err); | ||
} | ||
return false; | ||
@method raises | ||
*/ | ||
function raises(fn, errorValidator) { | ||
check.verify.fn(fn, 'expected function that raises'); | ||
try { | ||
fn(); | ||
} catch (err) { | ||
if (typeof errorValidator === 'undefined') { | ||
return true; | ||
} | ||
// error has not been raised | ||
if (typeof errorValidator === 'function') { | ||
return errorValidator(err); | ||
} | ||
return false; | ||
} | ||
// error has not been raised | ||
return false; | ||
} | ||
function registerPredicate(fn) { | ||
check.verify.fn(fn, 'expected predicate function'); | ||
check.verify.unemptyString(fn.name, 'predicate function missing name'); | ||
if (!check[fn.name]) { | ||
check[fn.name] = fn; | ||
} | ||
} | ||
predicates.forEach(registerPredicate); | ||
/** | ||
* Public modifier `maybe`. | ||
* | ||
* Returns `true` if `predicate` is `null` or `undefined`, | ||
* otherwise propagates the return value from `predicate`. | ||
* copied from check-types.js | ||
*/ | ||
function maybeModifier (predicate) { | ||
return function () { | ||
if (!check.defined(arguments[0]) || check.nulled(arguments[0])) { | ||
return true; | ||
} | ||
return predicate.apply(null, arguments); | ||
}; | ||
} | ||
function registerMaybePredicate(fn) { | ||
check.verify.object(check.maybe, 'missing check.maybe object'); | ||
check.verify.fn(fn, 'expected predicate function'); | ||
check.verify.unemptyString(fn.name, 'predicate function missing name'); | ||
if (!check.maybe[fn.name]) { | ||
check.maybe[fn.name] = maybeModifier(fn); | ||
} | ||
} | ||
predicates.forEach(registerMaybePredicate); | ||
}(typeof window === 'object' ? window.check : global.check)); |
@@ -131,2 +131,18 @@ # API | ||
--- | ||
Every predicate function is also added to `check.maybe` object. | ||
The `maybe` predicate passes if the argument is null or undefined, | ||
or the predicate returns true. | ||
### check.maybe | ||
check.maybe.bool(); // true | ||
check.maybe.bool('true'); // false | ||
var empty; | ||
check.maybe.lowerCase(empty); // true | ||
check.maybe.unemptyArray(); // true | ||
check.maybe.unemptyArray([]); // false | ||
check.maybe.unemptyArray(['foo', 'bar']); // true | ||
--- |
{ | ||
"name": "check-more-types", | ||
"description": "Additional type checks for https://github.com/philbooth/check-types.js", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -1,2 +0,2 @@ | ||
# check-more-types v0.1.3 | ||
# check-more-types v0.2.0 | ||
@@ -167,3 +167,19 @@ > Additional type checks for [check-types.js](https://github.com/philbooth/check-types.js) | ||
Every predicate function is also added to `check.maybe` object. | ||
The `maybe` predicate passes if the argument is null or undefined, | ||
or the predicate returns true. | ||
#### check.maybe | ||
check.maybe.bool(); // true | ||
check.maybe.bool('true'); // false | ||
var empty; | ||
check.maybe.lowerCase(empty); // true | ||
check.maybe.unemptyArray(); // true | ||
check.maybe.unemptyArray([]); // false | ||
check.maybe.unemptyArray(['foo', 'bar']); // true | ||
--- | ||
### Small print | ||
@@ -170,0 +186,0 @@ |
@@ -480,2 +480,35 @@ require('lazy-ass'); | ||
}); | ||
describe('maybe modifier', function () { | ||
it('default maybe from check-types.js', function () { | ||
la(check.object(check.maybe), 'check.maybe is an object'); | ||
la(check.fn(check.maybe.fn), 'check.maybe.fn function'); | ||
la(check.maybe.fn(), 'undefined is maybe a function'); | ||
la(check.maybe.fn(null), 'null is maybe a function'); | ||
}); | ||
it('check.maybe.bit', function () { | ||
la(check.fn(check.maybe.bit), 'check.maybe.bit function'); | ||
la(check.maybe.bit(1)); | ||
la(check.maybe.bit()); | ||
la(check.maybe.bit(null)); | ||
la(!check.maybe.bit(4)); | ||
}); | ||
it('check.maybe other functions', function () { | ||
la(check.maybe.bool()); | ||
la(!check.maybe.bool('true')); | ||
}); | ||
it('check.maybe', function () { | ||
la(check.maybe.bool(), 'undefined is maybe bool'); | ||
la(!check.maybe.bool('true')); | ||
var empty; | ||
la(check.maybe.lowerCase(empty)); | ||
la(check.maybe.unemptyArray()); | ||
la(!check.maybe.unemptyArray([])); | ||
la(check.maybe.unemptyArray(['foo', 'bar'])); | ||
}); | ||
}); | ||
}); |
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
36862
712
221