check-types
Advanced tools
Comparing version 1.4.0 to 2.0.0
{ | ||
"name": "check-types", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"main": "src/check-types.min.js", | ||
@@ -8,2 +8,4 @@ "ignore": [ | ||
"component.json", | ||
"HISTORY.md", | ||
"README.md", | ||
"config", | ||
@@ -10,0 +12,0 @@ "node_modules", |
{ | ||
"name": "check-types", | ||
"version": "1.4.0", | ||
"description": "A tiny JavaScript library for checking arguments and throwing exceptions.", | ||
"version": "2.0.0", | ||
"description": "A tiny library for asserting types and values.", | ||
"repo": "philbooth/check-types.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
# History | ||
## 2.0 | ||
* Breaking changes: | ||
* Drop ES3 support. | ||
* Rename `verify` => `assert`. | ||
* Remame `nulled` => `null`. | ||
* Switch `defined` to `undefined` for consistency with `null`. | ||
* Tightened implementation of `webUrl` to reject more cases. | ||
* Drop `gitUrl`. (sorry @galniv!) | ||
* Drop `email`. (sorry @rodrigo!) | ||
* Drop `floatNumber`. (sorry @rodrigo!) | ||
* Rename `oddNumber`, `evenNumber`, `positiveNumber`, `negativeNumber`, `intNumber` => `odd`, `even`, `positive`, `negative`, `integer`. | ||
* Rename `bool` => `boolean`. | ||
* Rename `every` => `all`. | ||
* Add predicate `assigned`. | ||
* Add `apply` batch operation. | ||
* Delete superfluous unit tests. | ||
## 1.4 | ||
@@ -4,0 +22,0 @@ |
{ | ||
"name": "check-types", | ||
"version": "1.4.0", | ||
"description": "A tiny library for checking arguments and throwing exceptions.", | ||
"version": "2.0.0", | ||
"description": "A tiny library for asserting types and values.", | ||
"homepage": "https://github.com/philbooth/check-types.js", | ||
@@ -15,16 +15,26 @@ "bugs": "https://github.com/philbooth/check-types.js/issues", | ||
"keywords": [ | ||
"type", | ||
"types", | ||
"type-check", | ||
"type-checking", | ||
"duck-typing", | ||
"arguments", | ||
"parameters" | ||
"parameters", | ||
"values", | ||
"data", | ||
"contract", | ||
"assert", | ||
"check", | ||
"verify", | ||
"safe", | ||
"safety" | ||
], | ||
"devDependencies": { | ||
"jshint": "2.5.x", | ||
"mocha": "1.19.x", | ||
"chai": "1.9.x", | ||
"mocha": "2.0.x", | ||
"chai": "1.10.x", | ||
"uglify-js": "2.4.x" | ||
}, | ||
"scripts": { | ||
"lint": "jshint ./src/check-types.js --config config/jshint.json", | ||
"lint": "jshint ./src/check-types.js", | ||
"test": "mocha --ui tdd --reporter spec --colors ./test/check-types.js", | ||
@@ -31,0 +41,0 @@ "minify": "uglifyjs ./src/check-types.js --compress --mangle --output ./src/check-types.min.js" |
253
README.md
@@ -6,4 +6,4 @@ # check-types.js | ||
A tiny JavaScript library | ||
for checking arguments | ||
and throwing exceptions. | ||
for asserting types | ||
and values. | ||
@@ -26,2 +26,4 @@ * [Why would I want that?](#why-would-i-want-that) | ||
* [Some examples](#some-examples) | ||
* [Where can I use it?](#where-can-i-use-it) | ||
* [What changed from 1.x to 2.x?](#what-changed-from-0x-to-1x) | ||
* [What changed from 0.x to 1.x?](#what-changed-from-0x-to-1x) | ||
@@ -35,4 +37,4 @@ * [How do I set up the build environment?](#how-do-i-set-up-the-build-environment) | ||
in your functions | ||
for checking arguments | ||
and throwing exceptions | ||
to check arguments | ||
and throw exceptions | ||
is a task that | ||
@@ -51,20 +53,23 @@ swiftly becomes tiresome | ||
14.4 kb unminified with comments, 3.2 kb minified, 1.2 kb minified + gzipped. | ||
13 kb unminified with comments, 3.3 kb minified, 1.2 kb minified + gzipped. | ||
## How do I install it? | ||
Any of the following will do: | ||
If you're using npm: | ||
``` | ||
npm install check-types | ||
npm install check-types --save | ||
``` | ||
jam install check-types | ||
Or if you just want the git repo: | ||
bower install check-types | ||
component install philbooth/check-types.js | ||
``` | ||
git clone git@github.com:philbooth/check-types.js.git | ||
``` | ||
If you're into | ||
other package managers, | ||
it is also available | ||
from Bower, Component and Jam. | ||
## How do I use it? | ||
@@ -88,15 +93,4 @@ | ||
the AMD-style format | ||
preferred by [Require.js][require]: | ||
preferred by [Require.js][require]. | ||
```javascript | ||
require.config({ | ||
paths: { | ||
check: 'check-types.js/src/check-types' | ||
} | ||
}); | ||
require([ 'check' ], function (check) { | ||
}); | ||
``` | ||
If you are | ||
@@ -107,4 +101,3 @@ including check-types.js | ||
are detected, | ||
check-types.js will just export its interface globally | ||
as `check`. | ||
it will export the interface globally as `check`. | ||
@@ -139,4 +132,4 @@ ### Calling the exported functions | ||
* `check.verify.xxx(thing, message)`: | ||
The verify modifier | ||
* `check.assert.xxx(thing, message)`: | ||
The assert modifier | ||
calls the equivalent predicate | ||
@@ -148,11 +141,13 @@ and throws an `Error` | ||
using the form | ||
`check.verify.maybe.xxx(thing, message)` | ||
`check.assert.maybe.xxx(thing, message)` | ||
or | ||
`check.verify.not.xxx(thing, message)` | ||
`check.assert.not.xxx(thing, message)` | ||
respectively. | ||
Additionally, there are some batch operations | ||
that allow you to test maps | ||
of many predicates at once. | ||
that allow you to apply predicates | ||
across multiple values | ||
inside arrays or objects. | ||
These are implemented by | ||
`check.apply`, | ||
`check.map`, | ||
@@ -179,12 +174,2 @@ `check.any` and | ||
* `check.gitUrl(thing)`: | ||
Returns `true` | ||
if `thing` is a git+ssh, git+http or git+https URL, | ||
`false` otherwise. | ||
* `check.email(thing)`: | ||
Returns `true` | ||
if `thing` seems like a valid email address, | ||
`false` otherwise. | ||
* `check.length(thing, value)`: | ||
@@ -200,3 +185,3 @@ Returns `true` | ||
Returns `true` | ||
if `thing` is a real number, | ||
if `thing` is a number, | ||
`false` otherwise. | ||
@@ -207,5 +192,5 @@ Note that | ||
`Number.NEGATIVE_INFINITY` | ||
are not real numbers. | ||
are not considered numbers here. | ||
* `check.positiveNumber(thing)`: | ||
* `check.positive(thing)`: | ||
Returns `true` if `thing` is a number | ||
@@ -215,3 +200,3 @@ greater than zero, | ||
* `check.negativeNumber(thing)`: | ||
* `check.negative(thing)`: | ||
Returns `true` | ||
@@ -222,3 +207,3 @@ if `thing` is a number | ||
* `check.oddNumber(thing)`: | ||
* `check.odd(thing)`: | ||
Returns `true` | ||
@@ -228,3 +213,3 @@ if `thing` is an odd number, | ||
* `check.evenNumber(thing)`: | ||
* `check.even(thing)`: | ||
Returns `true` | ||
@@ -234,3 +219,3 @@ if `thing` is an even number, | ||
* `check.intNumber(thing)`: | ||
* `check.integer(thing)`: | ||
Returns `true` | ||
@@ -240,10 +225,5 @@ if `thing` is an integer, | ||
* `check.floatNumber(thing)`: | ||
Returns `true` | ||
if `thing` is a floating-point number, | ||
`false` otherwise. | ||
#### Function functions | ||
* `check.fn(thing)`: | ||
* `check.function(thing)`: | ||
Returns `true` | ||
@@ -280,12 +260,2 @@ if `thing` is a function, | ||
* `check.nulled(thing)`: | ||
Returns `true` | ||
if `thing` is `null`, | ||
`false` otherwise. | ||
* `check.defined(thing)`: | ||
Returns `true` | ||
if `thing` is not `undefined`, | ||
`false` otherwise. | ||
* `check.emptyObject(thing)`: | ||
@@ -309,5 +279,21 @@ Returns `true` | ||
* `check.null(thing)`: | ||
Returns `true` | ||
if `thing` is `null`, | ||
`false` otherwise. | ||
* `check.undefined(thing)`: | ||
Returns `true` | ||
if `thing` is `undefined`, | ||
`false` otherwise. | ||
* `check.assigned(thing)`: | ||
Returns `true` | ||
if `thing` is not | ||
`null` or `undefined`, | ||
`false` otherwise. | ||
#### Boolean functions | ||
* `check.bool(thing)`: | ||
* `check.boolean(thing)`: | ||
Returns `true` | ||
@@ -319,2 +305,6 @@ if `thing` is a boolean, | ||
* `check.not.xxx(...)`: | ||
Returns the negation | ||
of the predicate. | ||
* `check.maybe.xxx(...)`: | ||
@@ -327,3 +317,3 @@ Returns `true` | ||
* `check.verify.xxx(...)` / `check.verify.maybe.xxx(...)`: | ||
* `check.assert.xxx(...)` / `check.assert.maybe.xxx(...)`: | ||
Throws an `Error` | ||
@@ -337,19 +327,28 @@ if the predicate returns false. | ||
* `check.map(things, functions)`: | ||
Maps each predicate from the `functions` object | ||
to the corresponding value from `things`, | ||
returning the hash of results. | ||
Similar to `like` | ||
but using functions instead of values. | ||
* `check.apply(things, predicates)`: | ||
Applies each value from the array of things | ||
to the corresponding predicate | ||
and returns the array of results. | ||
Passing a single predicate | ||
instead of an array of them | ||
applies all of the values | ||
to that predicate. | ||
* `check.map(things, predicates)`: | ||
Maps each value from the data | ||
to the corresponding predicate | ||
and returns a results object. | ||
Supports nested objects. | ||
* `check.every(predicateResults)`: | ||
* `check.all(results)`: | ||
Returns `true` | ||
if all properties of the `predicateResults` object are `true`, | ||
`false` otherwise. | ||
if all the result values are true | ||
in an array (returned from `apply`) | ||
or object (returned from `map`). | ||
* `check.any(predicateResults)`: | ||
Returns `true` | ||
is any property of the `predicateResults` object is `true`, | ||
`false` otherwise. | ||
if any result value is true | ||
in an array (returned from `apply`) | ||
or object (returned from `map`). | ||
@@ -359,3 +358,3 @@ #### Some examples | ||
```javascript | ||
check.object(0); | ||
check.even(3); | ||
// Returns false | ||
@@ -365,3 +364,3 @@ ``` | ||
```javascript | ||
check.maybe.object(null); | ||
check.maybe.even(null); | ||
// Returns true | ||
@@ -371,3 +370,3 @@ ``` | ||
```javascript | ||
check.not.object(0); | ||
check.not.even(3); | ||
// Returns true | ||
@@ -377,3 +376,3 @@ ``` | ||
```javascript | ||
check.verify.like({}, { foo: 'bar' }, 'Invalid object'); | ||
check.assert.like({ foo: 'bar' }, { baz: 'qux' }, 'Invalid object'); | ||
// Throws new Error('Invalid object') | ||
@@ -383,3 +382,3 @@ ``` | ||
```javascript | ||
check.verify.maybe.like(undefined, { foo: 'bar' }, 'Invalid object'); | ||
check.assert.maybe.like(undefined, { foo: 'bar' }, 'Invalid object'); | ||
// Doesn't throw | ||
@@ -389,3 +388,3 @@ ``` | ||
```javascript | ||
check.verify.not.like({}, { foo: 'bar' }, 'Invalid object'); | ||
check.assert.not.like({ foo: 'bar' }, { baz: 'qux' }, 'Invalid object'); | ||
// Doesn't throw | ||
@@ -395,12 +394,13 @@ ``` | ||
```javascript | ||
check.apply([ 'foo', 'bar', '' ], check.unemptyString); | ||
// Returns false | ||
``` | ||
```javascript | ||
check.map({ | ||
foo: 2, | ||
bar: { | ||
baz: 'qux' | ||
} | ||
bar: { baz: 'qux' } | ||
}, { | ||
foo: check.oddNumber, | ||
bar: { | ||
baz: check.unemptyString | ||
} | ||
foo: check.odd, | ||
bar: { baz: check.unemptyString } | ||
}); | ||
@@ -411,12 +411,9 @@ // Returns { foo: false, bar: { baz: true } } | ||
```javascript | ||
check.every( | ||
check.map({ | ||
foo: 0, | ||
bar: '' | ||
}, { | ||
foo: check.number, | ||
bar: check.unemptyString | ||
}) | ||
check.all( | ||
check.map( | ||
{ foo: 0, bar: '' }, | ||
{ foo: check.number, bar: check.string } | ||
); | ||
); | ||
// Returns false | ||
// Returns true | ||
``` | ||
@@ -426,9 +423,6 @@ | ||
check.any( | ||
check.map({ | ||
foo: 0, | ||
bar: '' | ||
}, { | ||
foo: check.number, | ||
bar: check.unemptyString | ||
}) | ||
check.apply( | ||
[ 1, 2, 3, '' ], | ||
check.string | ||
) | ||
); | ||
@@ -438,2 +432,46 @@ // Returns true | ||
## Where can I use it? | ||
As of version 2.0, | ||
this library no longer supports ES3. | ||
That means you can't use it | ||
in IE 7 or 8. | ||
Everywhere else should be fine. | ||
If those versions of IE | ||
are important to you, | ||
there is hope! | ||
The 1.x versions | ||
all support old IE | ||
and any future 1.x versions | ||
will adhere to that. | ||
See the [releases] | ||
for more information. | ||
## What changed from 1.x to 2.x? | ||
Breaking changes | ||
were made to the API | ||
in version 2.0.0. | ||
Specifically: | ||
* Support for ES3 was dropped | ||
* The predicates `gitUrl`, `email` and `floatNumber` were removed. | ||
* `verify` was renamed to `assert`. | ||
* `nulled` was renamed to `null`. | ||
* `oddNumber` was renamed to `odd`. | ||
* `evenNumber` was renamed to `even`. | ||
* `positiveNumber` was renamed to `positive`. | ||
* `negativeNumber` was renamed to `negative`. | ||
* `intNumber` was renamed to `integer`. | ||
* `defined` was swapped to become `undefined`. | ||
* `webUrl` was tightened to reject more cases. | ||
* The `assigned` predicate and the `apply` batch operation were added. | ||
See the [history] | ||
for more details. | ||
## What changed from 0.x to 1.x? | ||
@@ -485,2 +523,3 @@ | ||
[require]: http://requirejs.org/ | ||
[releases]: https://github.com/philbooth/check-types.js/releases | ||
[history]: https://github.com/philbooth/check-types.js/blob/master/HISTORY.md | ||
@@ -487,0 +526,0 @@ [npm]: https://npmjs.org/ |
@@ -11,30 +11,4 @@ /** | ||
var messages, predicates, functions, verify, maybe, not; | ||
var messages, predicates, functions, assert, maybe, not; | ||
predicates = { | ||
like: like, | ||
instance: instance, | ||
emptyObject: emptyObject, | ||
nulled: nulled, | ||
defined: defined, | ||
object: object, | ||
length: length, | ||
array: array, | ||
date: date, | ||
fn: fn, | ||
webUrl: webUrl, | ||
gitUrl: gitUrl, | ||
email: email, | ||
unemptyString: unemptyString, | ||
string: string, | ||
evenNumber: evenNumber, | ||
oddNumber: oddNumber, | ||
positiveNumber: positiveNumber, | ||
negativeNumber: negativeNumber, | ||
intNumber : intNumber, | ||
floatNumber : floatNumber, | ||
number: number, | ||
bool: bool | ||
}; | ||
messages = { | ||
@@ -44,5 +18,6 @@ like: 'Invalid type', | ||
emptyObject: 'Invalid object', | ||
nulled: 'Not null', | ||
defined: 'Not defined', | ||
object: 'Invalid object', | ||
assigned: 'Invalid value', | ||
undefined: 'Invalid value', | ||
null: 'Invalid value', | ||
length: 'Invalid length', | ||
@@ -53,19 +28,41 @@ array: 'Invalid array', | ||
webUrl: 'Invalid URL', | ||
gitUrl: 'Invalid git URL', | ||
email: 'Invalid email', | ||
unemptyString: 'Invalid string', | ||
string: 'Invalid string', | ||
evenNumber: 'Invalid number', | ||
oddNumber: 'Invalid number', | ||
positiveNumber: 'Invalid number', | ||
negativeNumber: 'Invalid number', | ||
intNumber: 'Invalid number', | ||
floatNumber: 'Invalid number', | ||
odd: 'Invalid number', | ||
even: 'Invalid number', | ||
positive: 'Invalid number', | ||
negative: 'Invalid number', | ||
integer: 'Invalid number', | ||
number: 'Invalid number', | ||
bool: 'Invalid boolean' | ||
boolean: 'Invalid boolean' | ||
}; | ||
predicates = { | ||
like: like, | ||
instance: instance, | ||
emptyObject: emptyObject, | ||
object: object, | ||
assigned: assigned, | ||
undefined: isUndefined, | ||
null: isNull, | ||
length: length, | ||
array: array, | ||
date: date, | ||
function: isFunction, | ||
webUrl: webUrl, | ||
unemptyString: unemptyString, | ||
string: string, | ||
odd: odd, | ||
even: even, | ||
positive: positive, | ||
negative: negative, | ||
integer : integer, | ||
number: number, | ||
boolean: boolean | ||
}; | ||
functions = { | ||
apply: apply, | ||
map: map, | ||
every: every, | ||
all: all, | ||
any: any | ||
@@ -75,10 +72,10 @@ }; | ||
functions = mixin(functions, predicates); | ||
verify = createModifiedPredicates(verifyModifier); | ||
assert = createModifiedPredicates(assertModifier); | ||
maybe = createModifiedPredicates(maybeModifier); | ||
not = createModifiedPredicates(notModifier); | ||
verify.maybe = createModifiedFunctions(verifyModifier, maybe); | ||
verify.not = createModifiedFunctions(verifyModifier, not); | ||
assert.maybe = createModifiedFunctions(assertModifier, maybe); | ||
assert.not = createModifiedFunctions(assertModifier, not); | ||
exportFunctions(mixin(functions, { | ||
verify: verify, | ||
assert: assert, | ||
maybe: maybe, | ||
@@ -97,20 +94,16 @@ not: not | ||
* | ||
* @param thing {object} The object to test. | ||
* @param duck {object} The archetypal object, or | ||
* 'duck', that the test is | ||
* against. | ||
*/ | ||
function like (thing, duck) { | ||
function like (data, duck) { | ||
var name; | ||
verify.object(thing); | ||
verify.object(duck); | ||
assert.object(data); | ||
assert.object(duck); | ||
for (name in duck) { | ||
if (duck.hasOwnProperty(name)) { | ||
if (thing.hasOwnProperty(name) === false || typeof thing[name] !== typeof duck[name]) { | ||
if (data.hasOwnProperty(name) === false || typeof data[name] !== typeof duck[name]) { | ||
return false; | ||
} | ||
if (object(thing[name]) && like(thing[name], duck[name]) === false) { | ||
if (object(data[name]) && like(data[name], duck[name]) === false) { | ||
return false; | ||
@@ -130,12 +123,5 @@ } | ||
* | ||
* @param thing {object} The object to test. | ||
* @param prototype {function} The prototype that the | ||
* test is against. | ||
*/ | ||
function instance (thing, prototype) { | ||
if (!defined(thing) || nulled(thing)) { | ||
return false; | ||
} | ||
if (fn(prototype) && thing instanceof prototype) { | ||
function instance (data, prototype) { | ||
if (data && isFunction(prototype) && data instanceof prototype) { | ||
return true; | ||
@@ -150,143 +136,109 @@ } | ||
* | ||
* Returns `true` if something is an empty, non-null, | ||
* non-array object, `false` otherwise. | ||
* Returns `true` if something is an empty object, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function emptyObject (thing) { | ||
var property; | ||
if (object(thing)) { | ||
for (property in thing) { | ||
if (thing.hasOwnProperty(property)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
function emptyObject (data) { | ||
return object(data) && Object.keys(data).length === 0; | ||
} | ||
/** | ||
* Public function `nulled`. | ||
* Public function `object`. | ||
* | ||
* Returns `true` if something is null, | ||
* Returns `true` if something is a plain-old JS object, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function nulled (thing) { | ||
return thing === null; | ||
function object (data) { | ||
return assigned(data) && Object.prototype.toString.call(data) === '[object Object]'; | ||
} | ||
/** | ||
* Public function `defined`. | ||
* Public function `assigned`. | ||
* | ||
* Returns `true` if something is not undefined, | ||
* Returns `true` if something is not null or undefined, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function defined (thing) { | ||
return thing !== void 0; | ||
function assigned (data) { | ||
return !isUndefined(data) && !isNull(data); | ||
} | ||
/** | ||
* Public function `object`. | ||
* Public function `undefined`. | ||
* | ||
* Returns `true` if something is a non-null, non-array, | ||
* non-date object, `false` otherwise. | ||
* Returns `true` if something is undefined, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function object (thing) { | ||
return typeof thing === 'object' && !nulled(thing) && !array(thing) && !date(thing); | ||
function isUndefined (data) { | ||
return data === undefined; | ||
} | ||
/** | ||
* Public function `length`. | ||
* Public function `null`. | ||
* | ||
* Returns `true` if something is has a length property | ||
* that equals `value`, `false` otherwise. | ||
* Returns `true` if something is null, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
* @param value The required length to test against. | ||
*/ | ||
function length (thing, value) { | ||
return thing && thing.length === value; | ||
function isNull (data) { | ||
return data === null; | ||
} | ||
/** | ||
* Public function `array`. | ||
* Public function `length`. | ||
* | ||
* Returns `true` something is an array, `false` otherwise. | ||
* Returns `true` if something is has a length property | ||
* that equals `value`, `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function array (thing) { | ||
if (Array.isArray) { | ||
return Array.isArray(thing); | ||
} | ||
function length (data, value) { | ||
assert.number(value); | ||
return Object.prototype.toString.call(thing) === '[object Array]'; | ||
return assigned(data) && data.length === value; | ||
} | ||
/** | ||
* Public function `date`. | ||
* Public function `array`. | ||
* | ||
* Returns `true` something is a date, `false` otherwise. | ||
* Returns `true` something is an array, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function date (thing) { | ||
return Object.prototype.toString.call(thing) === '[object Date]'; | ||
function array (data) { | ||
return Array.isArray(data); | ||
} | ||
/** | ||
* Public function `fn`. | ||
* Public function `date`. | ||
* | ||
* Returns `true` if something is function, `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function fn (thing) { | ||
return typeof thing === 'function'; | ||
} | ||
/** | ||
* Public function `webUrl`. | ||
* | ||
* Returns `true` if something is an HTTP or HTTPS URL, | ||
* Returns `true` something is a date, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function webUrl (thing) { | ||
return unemptyString(thing) && /^https?:\/\/.+/.test(thing); | ||
function date (data) { | ||
return Object.prototype.toString.call(data) === '[object Date]'; | ||
} | ||
/** | ||
* Public function `gitUrl`. | ||
* Public function `function`. | ||
* | ||
* Returns `true` if something is a git+ssh, git+http or git+https URL, | ||
* Returns `true` if something is function, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function gitUrl (thing) { | ||
return unemptyString(thing) && /^git\+(ssh|https?):\/\/.+/.test(thing); | ||
function isFunction (data) { | ||
return typeof data === 'function'; | ||
} | ||
/** | ||
* Public function `email`. | ||
* Public function `webUrl`. | ||
* | ||
* Returns `true` if something seems like a valid email address, | ||
* Returns `true` if something is an HTTP or HTTPS URL, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function email (thing) { | ||
return unemptyString(thing) && /\S+@\S+/.test(thing); | ||
function webUrl (data) { | ||
return unemptyString(data) && /^(https?:)?\/\/([\w-\.~:@]+)(\/[\w-\.~\/\?#\[\]&\(\)\*\+,;=%]*)?$/.test(data); | ||
} | ||
@@ -300,6 +252,5 @@ | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function unemptyString (thing) { | ||
return string(thing) && thing !== ''; | ||
function unemptyString (data) { | ||
return string(data) && data !== ''; | ||
} | ||
@@ -312,10 +263,9 @@ | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function string (thing) { | ||
return typeof thing === 'string'; | ||
function string (data) { | ||
return typeof data === 'string'; | ||
} | ||
/** | ||
* Public function `oddNumber`. | ||
* Public function `odd`. | ||
* | ||
@@ -325,10 +275,9 @@ * Returns `true` if something is an odd number, | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function oddNumber (thing) { | ||
return number(thing) && (thing % 2 === 1 || thing % 2 === -1); | ||
function odd (data) { | ||
return integer(data) && !even(data); | ||
} | ||
/** | ||
* Public function `evenNumber`. | ||
* Public function `even`. | ||
* | ||
@@ -338,81 +287,88 @@ * Returns `true` if something is an even number, | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function evenNumber (thing) { | ||
return number(thing) && thing % 2 === 0; | ||
function even (data) { | ||
return number(data) && data % 2 === 0; | ||
} | ||
/** | ||
* Public function `intNumber`. | ||
* Public function `integer`. | ||
* | ||
* Returns `true` if something is an integer number, | ||
* Returns `true` if something is an integer, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function intNumber (thing) { | ||
return number(thing) && thing % 1 === 0; | ||
function integer (data) { | ||
return number(data) && data % 1 === 0; | ||
} | ||
/** | ||
* Public function `floatNumber`. | ||
* Public function `positive`. | ||
* | ||
* Returns `true` if something is a float number, | ||
* Returns `true` if something is a positive number, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function floatNumber (thing) { | ||
return number(thing) && thing % 1 !== 0; | ||
function positive (data) { | ||
return number(data) && data > 0; | ||
} | ||
/** | ||
* Public function `positiveNumber`. | ||
* Public function `negative`. | ||
* | ||
* Returns `true` if something is a positive number, | ||
* Returns `true` if something is a negative number, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
* @param data The thing to test. | ||
*/ | ||
function positiveNumber (thing) { | ||
return number(thing) && thing > 0; | ||
function negative (data) { | ||
return number(data) && data < 0; | ||
} | ||
/** | ||
* Public function `negativeNumber`. | ||
* Public function `number`. | ||
* | ||
* Returns `true` if something is a positive number, | ||
* Returns `true` if data is a number, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function negativeNumber (thing) { | ||
return number(thing) && thing < 0; | ||
function number (data) { | ||
return typeof data === 'number' && isNaN(data) === false && | ||
data !== Number.POSITIVE_INFINITY && | ||
data !== Number.NEGATIVE_INFINITY; | ||
} | ||
/** | ||
* Public function `number`. | ||
* Public function `boolean`. | ||
* | ||
* Returns `true` if something is a real number, | ||
* Returns `true` if data is a boolean value, | ||
* `false` otherwise. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function number (thing) { | ||
return typeof thing === 'number' && | ||
isNaN(thing) === false && | ||
thing !== Number.POSITIVE_INFINITY && | ||
thing !== Number.NEGATIVE_INFINITY; | ||
function boolean (data) { | ||
return data === false || data === true; | ||
} | ||
/** | ||
* Public function `bool`. | ||
* Public function `apply`. | ||
* | ||
* Returns `true` if something is a bool, | ||
* `false` otherwise. | ||
* Maps each value from the data to the corresponding predicate and returns | ||
* the result array. If the same function is to be applied across all of the | ||
* data, a single predicate function may be passed in. | ||
* | ||
* @param thing The thing to test. | ||
*/ | ||
function bool (thing) { | ||
return thing === false || thing === true; | ||
function apply (data, predicates) { | ||
assert.array(data); | ||
if (isFunction(predicates)) { | ||
return data.map(function (value) { | ||
return predicates(value); | ||
}); | ||
} | ||
assert.array(predicates); | ||
assert.length(data, predicates.length); | ||
return data.map(function (value, index) { | ||
return predicates[index](value); | ||
}); | ||
} | ||
@@ -423,27 +379,24 @@ | ||
* | ||
* Returns the results hash of mapping each predicate to the | ||
* corresponding thing's property. Similar to `like` but | ||
* with functions instead of values. | ||
* Maps each value from the data to the corresponding predicate and returns | ||
* the result object. Supports nested objects. | ||
* | ||
* @param things {object} The things to test. | ||
* @param predicates {object} The map of functions to call against | ||
* the corresponding properties from `things`. | ||
*/ | ||
function map (things, predicates) { | ||
var property, result = {}, predicate; | ||
function map (data, predicates) { | ||
var result = {}, keys; | ||
verify.object(things); | ||
verify.object(predicates); | ||
assert.object(data); | ||
assert.object(predicates); | ||
for (property in predicates) { | ||
if (predicates.hasOwnProperty(property)) { | ||
predicate = predicates[property]; | ||
keys = Object.keys(predicates); | ||
assert.length(Object.keys(data), keys.length); | ||
if (fn(predicate)) { | ||
result[property] = predicate(things[property]); | ||
} else if (object(predicate)) { | ||
result[property] = map(things[property], predicate); | ||
} | ||
keys.forEach(function (key) { | ||
var predicate = predicates[key]; | ||
if (isFunction(predicate)) { | ||
result[key] = predicate(data[key]); | ||
} else if (object(predicate)) { | ||
result[key] = map(data[key], predicate); | ||
} | ||
} | ||
}); | ||
@@ -454,27 +407,49 @@ return result; | ||
/** | ||
* Public function `every` | ||
* Public function `all` | ||
* | ||
* Returns the conjunction of all booleans in a hash. | ||
* Check that all boolean values are true | ||
* in an array (returned from `apply`) | ||
* or object (returned from `map`). | ||
* | ||
* @param predicateResults {object} The hash of evaluated predicates. | ||
*/ | ||
function every (predicateResults) { | ||
var property, value; | ||
function all (data) { | ||
if (array(data)) { | ||
return testArray(data, false); | ||
} | ||
verify.object(predicateResults); | ||
assert.object(data); | ||
for (property in predicateResults) { | ||
if (predicateResults.hasOwnProperty(property)) { | ||
value = predicateResults[property]; | ||
return testObject(data, false); | ||
} | ||
if (object(value) && every(value) === false) { | ||
return false; | ||
function testArray (data, result) { | ||
var i; | ||
for (i = 0; i < data.length; i += 1) { | ||
if (data[i] === result) { | ||
return result; | ||
} | ||
} | ||
return !result; | ||
} | ||
function testObject (data, result) { | ||
var key, value; | ||
for (key in data) { | ||
if (data.hasOwnProperty(key)) { | ||
value = data[key]; | ||
if (object(value) && testObject(value, result) === result) { | ||
return result; | ||
} | ||
if (value === false) { | ||
return false; | ||
if (value === result) { | ||
return result; | ||
} | ||
} | ||
} | ||
return true; | ||
return !result; | ||
} | ||
@@ -485,37 +460,22 @@ | ||
* | ||
* Returns the disjunction of all booleans in a hash. | ||
* Check that at least one boolean value is true | ||
* in an array (returned from `apply`) | ||
* or object (returned from `map`). | ||
* | ||
* @param predicateResults {object} The hash of evaluated predicates. | ||
*/ | ||
function any (predicateResults) { | ||
var property, value; | ||
function any (data) { | ||
if (array(data)) { | ||
return testArray(data, true); | ||
} | ||
verify.object(predicateResults); | ||
assert.object(data); | ||
for (property in predicateResults) { | ||
if (predicateResults.hasOwnProperty(property)) { | ||
value = predicateResults[property]; | ||
if (object(value) && any(value)) { | ||
return true; | ||
} | ||
if (value === true) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
return testObject(data, true); | ||
} | ||
function mixin (target, source) { | ||
var property; | ||
Object.keys(source).forEach(function (key) { | ||
target[key] = source[key]; | ||
}); | ||
for (property in source) { | ||
if (source.hasOwnProperty(property)) { | ||
target[property] = source[property]; | ||
} | ||
} | ||
return target; | ||
@@ -525,7 +485,7 @@ } | ||
/** | ||
* Public modifier `verify`. | ||
* Public modifier `assert`. | ||
* | ||
* Throws if `predicate` returns `false`. | ||
*/ | ||
function verifyModifier (predicate, defaultMessage) { | ||
function assertModifier (predicate, defaultMessage) { | ||
return function () { | ||
@@ -549,3 +509,3 @@ var message; | ||
return function () { | ||
if (!defined(arguments[0]) || nulled(arguments[0])) { | ||
if (!assigned(arguments[0])) { | ||
return true; | ||
@@ -574,9 +534,7 @@ } | ||
function createModifiedFunctions (modifier, functions) { | ||
var name, result = {}; | ||
var result = {}; | ||
for (name in functions) { | ||
if (functions.hasOwnProperty(name)) { | ||
result[name] = modifier(functions[name], messages[name]); | ||
} | ||
} | ||
Object.keys(functions).forEach(function (key) { | ||
result[key] = modifier(functions[key], messages[key]); | ||
}); | ||
@@ -583,0 +541,0 @@ return result; |
@@ -1,1 +0,1 @@ | ||
!function(n){"use strict";function r(n,t){var e;Y.object(n),Y.object(t);for(e in t)if(t.hasOwnProperty(e)){if(n.hasOwnProperty(e)===!1||typeof n[e]!=typeof t[e])return!1;if(o(n[e])&&r(n[e],t[e])===!1)return!1}return!0}function t(n,r){return!i(n)||u(n)?!1:c(r)&&n instanceof r?!0:!1}function e(n){var r;if(o(n)){for(r in n)if(n.hasOwnProperty(r))return!1;return!0}return!1}function u(n){return null===n}function i(n){return void 0!==n}function o(n){return"object"==typeof n&&!u(n)&&!f(n)&&!l(n)}function a(n,r){return n&&n.length===r}function f(n){return Array.isArray?Array.isArray(n):"[object Array]"===Object.prototype.toString.call(n)}function l(n){return"[object Date]"===Object.prototype.toString.call(n)}function c(n){return"function"==typeof n}function b(n){return s(n)&&/^https?:\/\/.+/.test(n)}function d(n){return s(n)&&/^git\+(ssh|https?):\/\/.+/.test(n)}function m(n){return s(n)&&/\S+@\S+/.test(n)}function s(n){return y(n)&&""!==n}function y(n){return"string"==typeof n}function p(n){return j(n)&&(1===n%2||-1===n%2)}function v(n){return j(n)&&0===n%2}function I(n){return j(n)&&0===n%1}function g(n){return j(n)&&0!==n%1}function N(n){return j(n)&&n>0}function h(n){return j(n)&&0>n}function j(n){return"number"==typeof n&&isNaN(n)===!1&&n!==Number.POSITIVE_INFINITY&&n!==Number.NEGATIVE_INFINITY}function O(n){return n===!1||n===!0}function w(n,r){var t,e,u={};Y.object(n),Y.object(r);for(t in r)r.hasOwnProperty(t)&&(e=r[t],c(e)?u[t]=e(n[t]):o(e)&&(u[t]=w(n[t],e)));return u}function P(n){var r,t;Y.object(n);for(r in n)if(n.hasOwnProperty(r)){if(t=n[r],o(t)&&P(t)===!1)return!1;if(t===!1)return!1}return!0}function S(n){var r,t;Y.object(n);for(r in n)if(n.hasOwnProperty(r)){if(t=n[r],o(t)&&S(t))return!0;if(t===!0)return!0}return!1}function A(n,r){var t;for(t in r)r.hasOwnProperty(t)&&(n[t]=r[t]);return n}function U(n,r){return function(){var t;if(n.apply(null,arguments)===!1)throw t=arguments[arguments.length-1],new Error(s(t)?t:r)}}function E(n){return function(){return!i(arguments[0])||u(arguments[0])?!0:n.apply(null,arguments)}}function T(n){return function(){return!n.apply(null,arguments)}}function k(n){return x(n,R)}function x(n,r){var t,e={};for(t in r)r.hasOwnProperty(t)&&(e[t]=n(r[t],L[t]));return e}function F(r){"function"==typeof define&&define.amd?define(function(){return r}):"undefined"!=typeof module&&null!==module&&module.exports?module.exports=r:n.check=r}var L,R,V,Y,_,D;R={like:r,instance:t,emptyObject:e,nulled:u,defined:i,object:o,length:a,array:f,date:l,fn:c,webUrl:b,gitUrl:d,email:m,unemptyString:s,string:y,evenNumber:v,oddNumber:p,positiveNumber:N,negativeNumber:h,intNumber:I,floatNumber:g,number:j,bool:O},L={like:"Invalid type",instance:"Invalid type",emptyObject:"Invalid object",nulled:"Not null",defined:"Not defined",object:"Invalid object",length:"Invalid length",array:"Invalid array",date:"Invalid date",fn:"Invalid function",webUrl:"Invalid URL",gitUrl:"Invalid git URL",email:"Invalid email",unemptyString:"Invalid string",string:"Invalid string",evenNumber:"Invalid number",oddNumber:"Invalid number",positiveNumber:"Invalid number",negativeNumber:"Invalid number",intNumber:"Invalid number",floatNumber:"Invalid number",number:"Invalid number",bool:"Invalid boolean"},V={map:w,every:P,any:S},V=A(V,R),Y=k(U),_=k(E),D=k(T),Y.maybe=x(U,_),Y.not=x(U,D),F(A(V,{verify:Y,maybe:_,not:D}))}(this); | ||
!function(n){"use strict";function t(n,e){var r;G.object(n),G.object(e);for(r in e)if(e.hasOwnProperty(r)){if(n.hasOwnProperty(r)===!1||typeof n[r]!=typeof e[r])return!1;if(u(n[r])&&t(n[r],e[r])===!1)return!1}return!0}function e(n,t){return n&&d(t)&&n instanceof t?!0:!1}function r(n){return u(n)&&0===Object.keys(n).length}function u(n){return i(n)&&"[object Object]"===Object.prototype.toString.call(n)}function i(n){return!o(n)&&!a(n)}function o(n){return void 0===n}function a(n){return null===n}function c(n,t){return G.number(t),i(n)&&n.length===t}function f(n){return Array.isArray(n)}function l(n){return"[object Date]"===Object.prototype.toString.call(n)}function d(n){return"function"==typeof n}function b(n){return s(n)&&/^(https?:)?\/\/([\w-\.~:@]+)(\/[\w-\.~\/\?#\[\]&\(\)\*\+,;=%]*)?$/.test(n)}function s(n){return v(n)&&""!==n}function v(n){return"string"==typeof n}function y(n){return g(n)&&!p(n)}function p(n){return j(n)&&n%2===0}function g(n){return j(n)&&n%1===0}function m(n){return j(n)&&n>0}function I(n){return j(n)&&0>n}function j(n){return"number"==typeof n&&isNaN(n)===!1&&n!==Number.POSITIVE_INFINITY&&n!==Number.NEGATIVE_INFINITY}function h(n){return n===!1||n===!0}function O(n,t){return G.array(n),d(t)?n.map(function(n){return t(n)}):(G.array(t),G.length(n,t.length),n.map(function(n,e){return t[e](n)}))}function w(n,t){var e,r={};return G.object(n),G.object(t),e=Object.keys(t),G.length(Object.keys(n),e.length),e.forEach(function(e){var i=t[e];d(i)?r[e]=i(n[e]):u(i)&&(r[e]=w(n[e],i))}),r}function N(n){return f(n)?k(n,!1):(G.object(n),E(n,!1))}function k(n,t){var e;for(e=0;e<n.length;e+=1)if(n[e]===t)return t;return!t}function E(n,t){var e,r;for(e in n)if(n.hasOwnProperty(e)){if(r=n[e],u(r)&&E(r,t)===t)return t;if(r===t)return t}return!t}function S(n){return f(n)?k(n,!0):(G.object(n),E(n,!0))}function P(n,t){return Object.keys(t).forEach(function(e){n[e]=t[e]}),n}function T(n,t){return function(){var e;if(n.apply(null,arguments)===!1)throw e=arguments[arguments.length-1],new Error(s(e)?e:t)}}function A(n){return function(){return i(arguments[0])?n.apply(null,arguments):!0}}function U(n){return function(){return!n.apply(null,arguments)}}function x(n){return F(n,_)}function F(n,t){var e={};return Object.keys(t).forEach(function(r){e[r]=n(t[r],Y[r])}),e}function V(t){"function"==typeof define&&define.amd?define(function(){return t}):"undefined"!=typeof module&&null!==module&&module.exports?module.exports=t:n.check=t}var Y,_,D,G,L,R;Y={like:"Invalid type",instance:"Invalid type",emptyObject:"Invalid object",object:"Invalid object",assigned:"Invalid value",undefined:"Invalid value","null":"Invalid value",length:"Invalid length",array:"Invalid array",date:"Invalid date",fn:"Invalid function",webUrl:"Invalid URL",unemptyString:"Invalid string",string:"Invalid string",odd:"Invalid number",even:"Invalid number",positive:"Invalid number",negative:"Invalid number",integer:"Invalid number",number:"Invalid number","boolean":"Invalid boolean"},_={like:t,instance:e,emptyObject:r,object:u,assigned:i,undefined:o,"null":a,length:c,array:f,date:l,"function":d,webUrl:b,unemptyString:s,string:v,odd:y,even:p,positive:m,negative:I,integer:g,number:j,"boolean":h},D={apply:O,map:w,all:N,any:S},D=P(D,_),G=x(T),L=x(A),R=x(U),G.maybe=F(T,L),G.not=F(T,R),V(P(D,{assert:G,maybe:L,not:R}))}(this); |
@@ -49,3 +49,3 @@ /*globals require, chai */ | ||
test('like with two object arguments does not throw', function () { | ||
test('like with two empty objects does not throw', function () { | ||
assert.doesNotThrow(function () { | ||
@@ -56,10 +56,4 @@ check.like({}, {}); | ||
test('like with function first argument throws', function () { | ||
test('like with null, empty object throws', function () { | ||
assert.throws(function () { | ||
check.like(function () {}, {}); | ||
}); | ||
}); | ||
test('like with null first argument throws', function () { | ||
assert.throws(function () { | ||
check.like(null, {}); | ||
@@ -69,9 +63,9 @@ }); | ||
test('like with function second argument throws', function () { | ||
test('like with empty function, empty object throws', function () { | ||
assert.throws(function () { | ||
check.like({}, function () {}); | ||
check.like(function () {}, {}); | ||
}); | ||
}); | ||
test('like with null second argument throws', function () { | ||
test('like with empty object, null throws', function () { | ||
assert.throws(function () { | ||
@@ -86,20 +80,20 @@ check.like({}, null); | ||
test('like with foo bar properties returns false', function () { | ||
test('like with different named properties returns false', function () { | ||
assert.isFalse(check.like({ foo: {} }, { bar: {} })); | ||
}); | ||
test('like with foo foo properties returns true', function () { | ||
test('like with same named properties returns true', function () { | ||
assert.isTrue(check.like({ foo: {} }, { foo: {} })); | ||
}); | ||
test('like with bar baz second properties returns false', function () { | ||
test('like with different named second properties returns false', function () { | ||
assert.isFalse(check.like({ foo: {}, bar: {} }, { foo: {}, baz: {} })); | ||
}); | ||
test('like with bar bar second properties returns true', function () { | ||
assert.isTrue(check.like({ foo: (function () {}), bar: {} }, { foo: (function () {}), bar: {} })); | ||
test('like with same named second properties returns true', function () { | ||
assert.isTrue(check.like({ foo: function () {}, bar: {} }, { foo: function () {}, bar: {} })); | ||
}); | ||
test('like with differently typed bar bar second properties returns false', function () { | ||
assert.isFalse(check.like({ foo: (function () {}), bar: {} }, { foo: (function () {}), bar: (function () {}) })); | ||
test('like with differently typed second properties returns false', function () { | ||
assert.isFalse(check.like({ foo: function () {}, bar: {} }, { foo: function () {}, bar: function () {} })); | ||
}); | ||
@@ -109,6 +103,6 @@ | ||
assert.isFalse(check.like({ foo: { bar: { qux: 'string' }, baz: 23 }}, | ||
{ foo: { bar: { qux: 123 }, baz: 66 }})); | ||
{ foo: { bar: { qux: 123 }, baz: 66 }})); | ||
}); | ||
test('like with alike nested objects returns true', function() { | ||
test('like with similar nested objects returns true', function() { | ||
assert.isTrue(check.like({ foo: { bar: { qux: 'string' }, baz: 23 }}, | ||
@@ -146,3 +140,3 @@ { foo: { bar: { qux: 'other' }, baz: 66 }})); | ||
test('instance with Object and object returns false', function () { | ||
test('instance with swapped arguments returns false', function () { | ||
assert.isFalse(check.instance(Object, {})); | ||
@@ -171,46 +165,62 @@ }); | ||
test('nulled function is defined', function () { | ||
assert.isFunction(check.nulled); | ||
test('null function is defined', function () { | ||
assert.isFunction(check.null); | ||
}); | ||
test('nulled with null returns true', function () { | ||
assert.isTrue(check.nulled(null)); | ||
test('null with null returns true', function () { | ||
assert.isTrue(check.null(null)); | ||
}); | ||
test('nulled with string returns false', function () { | ||
assert.isFalse(check.nulled('null')); | ||
test('null with empty object returns false', function () { | ||
assert.isFalse(check.null({})); | ||
}); | ||
test('nulled with empty string returns false', function () { | ||
assert.isFalse(check.nulled('')); | ||
test('null with undefined returns false', function () { | ||
assert.isFalse(check.null(undefined)); | ||
}); | ||
test('nulled with undefined returns false', function () { | ||
assert.isFalse(check.nulled(undefined)); | ||
test('undefined function is defined', function () { | ||
assert.isFunction(check.undefined); | ||
}); | ||
test('nulled with object returns false', function () { | ||
assert.isFalse(check.nulled({})); | ||
test('undefined with undefined returns true', function () { | ||
assert.isTrue(check.undefined(undefined)); | ||
}); | ||
test('defined function is defined', function () { | ||
assert.isFunction(check.defined); | ||
test('undefined with null returns false', function () { | ||
assert.isFalse(check.undefined(null)); | ||
}); | ||
test('defined with undefined returns false', function () { | ||
assert.isFalse(check.defined(undefined)); | ||
test('undefined with empty object returns false', function () { | ||
assert.isFalse(check.undefined({})); | ||
}); | ||
test('defined with string returns true', function () { | ||
assert.isTrue(check.defined('undefined')); | ||
test('undefined with false returns false', function () { | ||
assert.isFalse(check.undefined(false)); | ||
}); | ||
test('defined with object returns true', function () { | ||
assert.isTrue(check.defined({})); | ||
test('assigned function is defined', function () { | ||
assert.isFunction(check.assigned); | ||
}); | ||
test('defined with null returns true', function () { | ||
assert.isTrue(check.defined(null)); | ||
test('assigned with null returns false', function () { | ||
assert.isFalse(check.assigned(null)); | ||
}); | ||
test('assigned with undefined returns false', function () { | ||
assert.isFalse(check.assigned(undefined)); | ||
}); | ||
test('assigned with empty object returns true', function () { | ||
assert.isTrue(check.assigned({})); | ||
}); | ||
test('assigned with empty string returns true', function () { | ||
assert.isTrue(check.assigned('')); | ||
}); | ||
test('assigned with false returns true', function () { | ||
assert.isTrue(check.assigned(false)); | ||
}); | ||
test('object function is defined', function () { | ||
@@ -228,6 +238,2 @@ assert.isFunction(check.object); | ||
test('object with string returns false', function () { | ||
assert.isFalse(check.object('{}')); | ||
}); | ||
test('object with array returns false', function () { | ||
@@ -237,2 +243,6 @@ assert.isFalse(check.object([])); | ||
test('object with string returns false', function () { | ||
assert.isFalse(check.object('')); | ||
}); | ||
test('object with date returns false', function () { | ||
@@ -246,10 +256,26 @@ assert.isFalse(check.object(new Date())); | ||
test('length with matching undefined length returns true', function () { | ||
assert.isTrue(check.length({})); | ||
test('length without length argument throws', function () { | ||
assert.throws(function () { | ||
check.length({}); | ||
}); | ||
}); | ||
test('length with contrasting undefined length returns false', function () { | ||
assert.isFalse(check.length({}, 7)); | ||
test('length with length argument does not throw', function () { | ||
assert.doesNotThrow(function () { | ||
check.length({}, 5); | ||
}); | ||
}); | ||
test('length with zero on empty array returns true', function () { | ||
assert.isTrue(check.length([], 0)); | ||
}); | ||
test('length with zero on empty string returns true', function () { | ||
assert.isTrue(check.length('', 0)); | ||
}); | ||
test('length with zero on empty object returns false', function () { | ||
assert.isFalse(check.length({}, 0)); | ||
}); | ||
test('length with matching length on array returns true', function () { | ||
@@ -263,2 +289,10 @@ assert.isTrue(check.length([ 'foo', 'bar' ], 2)); | ||
test('length with matching length on string returns true', function () { | ||
assert.isTrue(check.length('foo', 3)); | ||
}); | ||
test('length with contrasting length on string returns false', function () { | ||
assert.isFalse(check.length('foobar', 3)); | ||
}); | ||
test('length with matching length on object returns true', function () { | ||
@@ -269,3 +303,3 @@ assert.isTrue(check.length({ length: 1 }, 1)); | ||
test('length with contrasting length on object returns false', function () { | ||
assert.isFalse(check.length({ length: 1 }, 2)); | ||
assert.isFalse(check.length({ length: 2 }, 1)); | ||
}); | ||
@@ -281,8 +315,4 @@ | ||
test('array with null returns false', function () { | ||
assert.isFalse(check.array(null)); | ||
}); | ||
test('array with string returns false', function () { | ||
assert.isFalse(check.array('[]')); | ||
assert.isFalse(check.array('')); | ||
}); | ||
@@ -294,2 +324,6 @@ | ||
test('array with arguments object returns false', function () { | ||
assert.isFalse(check.array(arguments)); | ||
}); | ||
test('date function is defined', function () { | ||
@@ -307,30 +341,14 @@ assert.isFunction(check.date); | ||
test('date with null returns false', function () { | ||
assert.isFalse(check.date(null)); | ||
test('function function is defined', function () { | ||
assert.isFunction(check.function); | ||
}); | ||
test('date with string returns false', function () { | ||
assert.isFalse(check.date('new Date()')); | ||
test('function with function returns true', function () { | ||
assert.isTrue(check.function(function () {})); | ||
}); | ||
test('fn function is defined', function () { | ||
assert.isFunction(check.fn); | ||
test('function with object returns false', function () { | ||
assert.isFalse(check.function({})); | ||
}); | ||
test('fn with function returns true', function () { | ||
assert.isTrue(check.fn(function () {})); | ||
}); | ||
test('fn with null returns false', function () { | ||
assert.isFalse(check.fn(null)); | ||
}); | ||
test('fn with string returns false', function () { | ||
assert.isFalse(check.fn('function () {}')); | ||
}); | ||
test('fn with object returns false', function () { | ||
assert.isFalse(check.fn({})); | ||
}); | ||
test('webUrl function is defined', function () { | ||
@@ -340,18 +358,14 @@ assert.isFunction(check.webUrl); | ||
test('webUrl with https: URL returns true', function () { | ||
assert.isTrue(check.webUrl('https://example.com/')); | ||
test('webUrl with http scheme returns true', function () { | ||
assert.isTrue(check.webUrl('http://127.0.0.1:8080/')); | ||
}); | ||
test('webUrl with ftp: URL returns false', function () { | ||
test('webUrl with ftp scheme returns false', function () { | ||
assert.isFalse(check.webUrl('ftp://example.com/')); | ||
}); | ||
test('webUrl with http: URL returns true', function () { | ||
assert.isTrue(check.webUrl('http://127.0.0.1:8080/')); | ||
test('webUrl with https scheme returns true', function () { | ||
assert.isTrue(check.webUrl('https://example.com/')); | ||
}); | ||
test('webUrl with protocol-relative URL returns false', function () { | ||
assert.isFalse(check.webUrl('//example.com/')); | ||
}); | ||
test('webUrl with httpss scheme returns false', function () { | ||
@@ -361,82 +375,36 @@ assert.isFalse(check.webUrl('httpss://')); | ||
test('webUrl without domain returns false', function () { | ||
assert.isFalse(check.webUrl('http://')); | ||
}); | ||
test('webUrl with object returns false', function () { | ||
assert.isFalse(check.webUrl({ toString: function () { return 'https://example.com/'; } })); | ||
assert.isFalse( | ||
check.webUrl({ | ||
toString: function () { | ||
return 'https://example.com/'; | ||
} | ||
}) | ||
); | ||
}); | ||
test('webUrl with number returns false', function () { | ||
assert.isFalse(check.webUrl(42)); | ||
test('webUrl with no scheme returns true', function () { | ||
assert.isTrue(check.webUrl('//example.com/')); | ||
}); | ||
test('gitUrl function is defined', function () { | ||
assert.isFunction(check.gitUrl); | ||
test('webUrl without domain returns false', function () { | ||
assert.isFalse(check.webUrl('http:///')); | ||
}); | ||
test('gitUrl with git+ssh: URL returns true', function () { | ||
assert.isTrue(check.gitUrl('git+ssh://example.com/')); | ||
test('webUrl with single-word domain returns true', function () { | ||
assert.isTrue(check.webUrl('http://ws/')); | ||
}); | ||
test('gitUrl with git+http: URL returns true', function () { | ||
assert.isTrue(check.gitUrl('git+http://example.com/')); | ||
test('webUrl without path returns true', function () { | ||
assert.isTrue(check.webUrl('http://example.com')); | ||
}); | ||
test('gitUrl with git+https: URL returns true', function () { | ||
assert.isTrue(check.gitUrl('git+https://example.com/')); | ||
test('webUrl with bad character returns false', function () { | ||
assert.isFalse(check.webUrl('http://example.com/`')); | ||
}); | ||
test('gitUrl with http: URL returns false', function () { | ||
assert.isFalse(check.gitUrl('http://example.com/')); | ||
test('webUrl with percent-encoding returns true', function () { | ||
assert.isTrue(check.webUrl('http://example.com/%20')); | ||
}); | ||
test('gitUrl with protocol-relative URL returns false', function () { | ||
assert.isFalse(check.gitUrl('//example.com/')); | ||
}); | ||
test('gitUrl with httpss scheme returns false', function () { | ||
assert.isFalse(check.gitUrl('git+httpss://')); | ||
}); | ||
test('gitUrl without domain returns false', function () { | ||
assert.isFalse(check.gitUrl('git+ssh://')); | ||
}); | ||
test('gitUrl with object returns false', function () { | ||
assert.isFalse(check.gitUrl({ toString: function () { return 'git+ssh://example.com/'; } })); | ||
}); | ||
test('gitUrl with number returns false', function () { | ||
assert.isFalse(check.gitUrl(42)); | ||
}); | ||
test('email function is defined', function () { | ||
assert.isFunction(check.email); | ||
}); | ||
test('email with user@host.tld returns true', function () { | ||
assert.isTrue(check.email('user@host.tld')); | ||
}); | ||
test('email with user.other@host.tld returns true', function () { | ||
assert.isTrue(check.email('user.other@host.tld')); | ||
}); | ||
test('email with user.other@sub.host.tld returns true', function () { | ||
assert.isTrue(check.email('user.other@sub.host.tld')); | ||
}); | ||
test('email with @ returns false', function () { | ||
assert.isFalse(check.email('@')); | ||
}); | ||
test('email with object returns false', function () { | ||
assert.isFalse(check.email({ toString: function () { return 'user@host.tld'; } })); | ||
}); | ||
test('email with number returns false', function () { | ||
assert.isFalse(check.email(42)); | ||
}); | ||
test('unemptyString function is defined', function () { | ||
@@ -446,10 +414,6 @@ assert.isFunction(check.unemptyString); | ||
test('unemptyString with string foo returns true', function () { | ||
test('unemptyString with unempty string returns true', function () { | ||
assert.isTrue(check.unemptyString('foo')); | ||
}); | ||
test('unemptyString with null returns false', function () { | ||
assert.isFalse(check.unemptyString(null)); | ||
}); | ||
test('unemptyString with empty string returns false', function () { | ||
@@ -459,8 +423,14 @@ assert.isFalse(check.unemptyString('')); | ||
test('unemptyString with object returns false', function () { | ||
assert.isFalse(check.unemptyString({})); | ||
test('unemptyString with alternative unempty string returns true', function () { | ||
assert.isTrue(check.unemptyString('bar')); | ||
}); | ||
test('unemptyString with string bar returns true', function () { | ||
assert.isTrue(check.unemptyString('bar')); | ||
test('unemptyString with object returns false', function () { | ||
assert.isFalse( | ||
check.unemptyString({ | ||
toString: function () { | ||
return 'foo'; | ||
} | ||
}) | ||
); | ||
}); | ||
@@ -472,6 +442,2 @@ | ||
test('string with string foo returns true', function () { | ||
assert.isTrue(check.string('foo')); | ||
}); | ||
test('string with empty string returns true', function () { | ||
@@ -482,181 +448,163 @@ assert.isTrue(check.string('')); | ||
test('string with object returns false', function () { | ||
assert.isFalse(check.string({})); | ||
assert.isFalse( | ||
check.string({ | ||
toString: function () { | ||
return ''; | ||
} | ||
}) | ||
); | ||
}); | ||
test('oddNumber function is defined', function () { | ||
assert.isFunction(check.oddNumber); | ||
test('odd function is defined', function () { | ||
assert.isFunction(check.odd); | ||
}); | ||
test('oddNumber with odd number returns true', function () { | ||
assert.isTrue(check.oddNumber(1)); | ||
test('odd with odd number returns true', function () { | ||
assert.isTrue(check.odd(1)); | ||
}); | ||
test('oddNumber with even number returns false', function () { | ||
assert.isFalse(check.oddNumber(2)); | ||
test('odd with even number returns false', function () { | ||
assert.isFalse(check.odd(2)); | ||
}); | ||
test('oddNumber with negative odd number returns true', function () { | ||
assert.isTrue(check.oddNumber(-3)); | ||
test('odd with negative odd number returns true', function () { | ||
assert.isTrue(check.odd(-3)); | ||
}); | ||
test('oddNumber with negative even number returns false', function () { | ||
assert.isFalse(check.oddNumber(-4)); | ||
test('odd with negative even number returns false', function () { | ||
assert.isFalse(check.odd(-4)); | ||
}); | ||
test('oddNumber with floating point number returns false', function () { | ||
assert.isFalse(check.evenNumber(5.5)); | ||
test('odd with floating point number returns false', function () { | ||
assert.isFalse(check.odd(5.5)); | ||
}); | ||
test('oddNumber with string returns false', function () { | ||
assert.isFalse(check.oddNumber('1')); | ||
test('odd with string returns false', function () { | ||
assert.isFalse(check.odd('1')); | ||
}); | ||
test('evenNumber function is defined', function () { | ||
assert.isFunction(check.evenNumber); | ||
test('even function is defined', function () { | ||
assert.isFunction(check.even); | ||
}); | ||
test('evenNumber with even number returns true', function () { | ||
assert.isTrue(check.evenNumber(2)); | ||
test('even with even number returns true', function () { | ||
assert.isTrue(check.even(2)); | ||
}); | ||
test('evenNumber with odd number returns false', function () { | ||
assert.isFalse(check.evenNumber(3)); | ||
test('even with odd number returns false', function () { | ||
assert.isFalse(check.even(3)); | ||
}); | ||
test('evenNumber with negative even number returns true', function () { | ||
assert.isTrue(check.evenNumber(-4)); | ||
test('even with negative even number returns true', function () { | ||
assert.isTrue(check.even(-4)); | ||
}); | ||
test('evenNumber with negative odd number returns false', function () { | ||
assert.isFalse(check.evenNumber(-5)); | ||
test('even with negative odd number returns false', function () { | ||
assert.isFalse(check.even(-5)); | ||
}); | ||
test('evenNumber with floating point number returns false', function () { | ||
assert.isFalse(check.evenNumber(2.4)); | ||
test('even with floating point number returns false', function () { | ||
assert.isFalse(check.even(2.4)); | ||
}); | ||
test('evenNumber with string returns false', function () { | ||
assert.isFalse(check.evenNumber('2')); | ||
test('even with string returns false', function () { | ||
assert.isFalse(check.even('2')); | ||
}); | ||
test('positiveNumber function is defined', function () { | ||
assert.isFunction(check.positiveNumber); | ||
test('positive function is defined', function () { | ||
assert.isFunction(check.positive); | ||
}); | ||
test('positiveNumber with positive integer returns true', function () { | ||
assert.isTrue(check.positiveNumber(1)); | ||
test('positive with positive integer returns true', function () { | ||
assert.isTrue(check.positive(1)); | ||
}); | ||
test('positiveNumber with negative integer returns false', function () { | ||
assert.isFalse(check.positiveNumber(-1)); | ||
test('positive with negative integer returns false', function () { | ||
assert.isFalse(check.positive(-1)); | ||
}); | ||
test('positiveNumber with positive fraction returns true', function () { | ||
assert.isTrue(check.positiveNumber(1/2)); | ||
test('positive with positive fraction returns true', function () { | ||
assert.isTrue(check.positive(1/2)); | ||
}); | ||
test('positiveNumber with negative fraction returns false', function () { | ||
assert.isFalse(check.positiveNumber(-1/2)); | ||
test('positive with negative fraction returns false', function () { | ||
assert.isFalse(check.positive(-1/2)); | ||
}); | ||
test('positiveNumber with positive infinity returns false', function () { | ||
assert.isFalse(check.positiveNumber(Number.POSITIVE_INFINITY)); | ||
test('positive with positive infinity returns false', function () { | ||
assert.isFalse(check.positive(Number.POSITIVE_INFINITY)); | ||
}); | ||
test('positiveNumber with negative infinity returns false', function () { | ||
assert.isFalse(check.positiveNumber(Number.NEGATIVE_INFINITY)); | ||
test('positive with NaN returns false', function () { | ||
assert.isFalse(check.positive(NaN)); | ||
}); | ||
test('positiveNumber with NaN returns false', function () { | ||
assert.isFalse(check.positiveNumber(NaN)); | ||
test('positive with string returns false', function () { | ||
assert.isFalse(check.positive('1')); | ||
}); | ||
test('positiveNumber with string returns false', function () { | ||
assert.isFalse(check.positiveNumber('1')); | ||
test('negative function is defined', function () { | ||
assert.isFunction(check.negative); | ||
}); | ||
test('negativeNumber function is defined', function () { | ||
assert.isFunction(check.negativeNumber); | ||
test('negative with positive integer returns false', function () { | ||
assert.isFalse(check.negative(1)); | ||
}); | ||
test('negativeNumber with positive integer returns false', function () { | ||
assert.isFalse(check.negativeNumber(1)); | ||
test('negative with negative integer returns true', function () { | ||
assert.isTrue(check.negative(-1)); | ||
}); | ||
test('negativeNumber with negative integer returns true', function () { | ||
assert.isTrue(check.negativeNumber(-1)); | ||
test('negative with positive fraction returns false', function () { | ||
assert.isFalse(check.negative(1/2)); | ||
}); | ||
test('negativeNumber with positive fraction returns false', function () { | ||
assert.isFalse(check.negativeNumber(1/2)); | ||
test('negative with negative fraction returns true', function () { | ||
assert.isTrue(check.negative(-1/2)); | ||
}); | ||
test('negativeNumber with negative fraction returns true', function () { | ||
assert.isTrue(check.negativeNumber(-1/2)); | ||
test('negative with negative infinity returns false', function () { | ||
assert.isFalse(check.negative(Number.NEGATIVE_INFINITY)); | ||
}); | ||
test('negativeNumber with positive infinity returns false', function () { | ||
assert.isFalse(check.negativeNumber(Number.POSITIVE_INFINITY)); | ||
test('negative with NaN returns false', function () { | ||
assert.isFalse(check.negative(NaN)); | ||
}); | ||
test('negativeNumber with negative infinity returns false', function () { | ||
assert.isFalse(check.negativeNumber(Number.NEGATIVE_INFINITY)); | ||
test('negative with string returns false', function () { | ||
assert.isFalse(check.negative('-1')); | ||
}); | ||
test('negativeNumber with NaN returns false', function () { | ||
assert.isFalse(check.negativeNumber(NaN)); | ||
test('integer function is defined', function () { | ||
assert.isFunction(check.integer); | ||
}); | ||
test('negativeNumber with string returns false', function () { | ||
assert.isFalse(check.negativeNumber('-1')); | ||
test('integer with positive integer returns true', function () { | ||
assert.isTrue(check.integer(1)); | ||
}); | ||
test('intNumber function is defined', function () { | ||
assert.isFunction(check.intNumber); | ||
test('integer with positive floating point number returns false', function () { | ||
assert.isFalse(check.integer(0.1)); | ||
}); | ||
test('intNumber with integer returns true', function () { | ||
assert.isTrue(check.intNumber(1)); | ||
test('integer with negative integer returns true', function () { | ||
assert.isTrue(check.integer(-2)); | ||
}); | ||
test('intNumber with floating point number returns false', function () { | ||
assert.isFalse(check.intNumber(0.5)); | ||
test('integer with negative floating point number returns false', function () { | ||
assert.isFalse(check.integer(-0.2)); | ||
}); | ||
test('intNumber with infinity returns false', function () { | ||
assert.isFalse(check.intNumber(Infinity)); | ||
test('integer with infinity returns false', function () { | ||
assert.isFalse(check.integer(Infinity)); | ||
}); | ||
test('intNumber with NaN returns false', function () { | ||
assert.isFalse(check.intNumber(NaN)); | ||
test('integer with NaN returns false', function () { | ||
assert.isFalse(check.integer(NaN)); | ||
}); | ||
test('intNumber with string returns false', function () { | ||
assert.isFalse(check.intNumber('1')); | ||
test('integer with string returns false', function () { | ||
assert.isFalse(check.integer('1')); | ||
}); | ||
test('floatNumber function is defined', function () { | ||
assert.isFunction(check.floatNumber); | ||
}); | ||
test('floatNumber with integer returns false', function () { | ||
assert.isFalse(check.floatNumber(2)); | ||
}); | ||
test('floatNumber with float returns true', function () { | ||
assert.isTrue(check.floatNumber(1.1)); | ||
}); | ||
test('floatNumber with infinity returns false', function () { | ||
assert.isFalse(check.floatNumber(Infinity)); | ||
}); | ||
test('floatNumber with NaN returns false', function () { | ||
assert.isFalse(check.floatNumber(NaN)); | ||
}); | ||
test('floatNumber with string returns false', function () { | ||
assert.isFalse(check.floatNumber('1.1')); | ||
}); | ||
test('number function is defined', function () { | ||
@@ -694,6 +642,2 @@ assert.isFunction(check.number); | ||
test('number with object returns false', function () { | ||
assert.isFalse(check.number({})); | ||
}); | ||
test('number with string returns false', function () { | ||
@@ -703,26 +647,87 @@ assert.isFalse(check.number('1')); | ||
test('bool function is defined', function () { | ||
assert.isFunction(check.bool); | ||
test('boolean function is defined', function () { | ||
assert.isFunction(check.boolean); | ||
}); | ||
test('bool with true boolean returns true', function () { | ||
assert.isTrue(check.bool(true)); | ||
test('boolean with true returns true', function () { | ||
assert.isTrue(check.boolean(true)); | ||
}); | ||
test('bool with false boolean returns true', function () { | ||
assert.isTrue(check.bool(false)); | ||
test('boolean with false returns true', function () { | ||
assert.isTrue(check.boolean(false)); | ||
}); | ||
test('bool with positive number returns false', function () { | ||
assert.isFalse(check.bool(1)); | ||
test('boolean with one returns false', function () { | ||
assert.isFalse(check.boolean(1)); | ||
}); | ||
test('bool with object returns false', function () { | ||
assert.isFalse(check.bool({})); | ||
test('boolean with unempty string returns false', function () { | ||
assert.isFalse(check.boolean('1')); | ||
}); | ||
test('bool with string returns false', function () { | ||
assert.isFalse(check.bool('false')); | ||
test('apply function is defined', function () { | ||
assert.isFunction(check.apply); | ||
}); | ||
test('apply with non-array data throws', function() { | ||
assert.throws(function () { | ||
check.apply({}, []); | ||
}); | ||
}); | ||
test('apply with non-array predicates throws', function() { | ||
assert.throws(function() { | ||
check.apply([], {}); | ||
}); | ||
}); | ||
test('apply with array data and predicates does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
check.apply([], []); | ||
}); | ||
}); | ||
test('apply with one predicate does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
check.apply([ '', '', ''], check.string); | ||
}); | ||
}); | ||
test('apply with insufficient data throws', function() { | ||
assert.throws(function() { | ||
check.apply([ '' ], [ check.string, check.string ]); | ||
}); | ||
}); | ||
test('apply with insufficient predicates throws', function() { | ||
assert.throws(function() { | ||
check.apply([ '', '', '' ], [ check.string, check.string ]); | ||
}); | ||
}); | ||
test('apply returns the correct results', function() { | ||
var result = | ||
check.apply( | ||
[ '', 0, '', 0 ], | ||
[ check.string, check.string, check.number, check.number ] | ||
); | ||
assert.lengthOf(result, 4); | ||
assert.isTrue(result[0]); | ||
assert.isFalse(result[1]); | ||
assert.isFalse(result[2]); | ||
assert.isTrue(result[3]); | ||
}); | ||
test('apply with assertion does not throw with valid data', function() { | ||
assert.doesNotThrow(function() { | ||
check.apply([ 'foo' ], check.assert.string); | ||
}); | ||
}); | ||
test('apply with assertion throws with invalid data', function() { | ||
assert.throws(function() { | ||
check.apply([ 'foo', 0 ], check.assert.string); | ||
}); | ||
}); | ||
test('map function is defined', function () { | ||
@@ -732,79 +737,90 @@ assert.isFunction(check.map); | ||
test('map with invalid object throws', function() { | ||
test('map with non-object data throws', function() { | ||
assert.throws(function () { | ||
check.map(null, { foo: check.string }); | ||
check.map([], {}); | ||
}); | ||
}); | ||
test('map with invalid predicates throws', function() { | ||
test('map with non-object predicates throws', function() { | ||
assert.throws(function() { | ||
check.map({ foo: 'test' }, null); | ||
check.map({}, function () {}); | ||
}); | ||
}); | ||
test('map with valid object and predicates does not throw', function() { | ||
test('map with object data and predicates does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
check.map({ foo: 'test' }, { foo: check.string }); | ||
check.map({}, {}); | ||
}); | ||
}); | ||
test('map with valid object and predicates returns the predicates results', function() { | ||
var result = check.map({ foo: 'test', bar: 33 }, | ||
{ foo: check.string, | ||
bar: check.evenNumber }); | ||
assert.deepEqual(result, { foo: true, bar: false }); | ||
test('map with insufficient data throws', function() { | ||
assert.throws(function() { | ||
check.map({ foo: '' }, { foo: check.string, bar: check.string }); | ||
}); | ||
}); | ||
test('map with undefined properties in thing still calls corresponding predicate', function() { | ||
test('map with insufficient predicates throws', function() { | ||
assert.throws(function() { | ||
check.map({ bar: 33 }, { foo: check.verify.string }); | ||
check.map({ foo: '' }, {}); | ||
}); | ||
}); | ||
test('map with nested objects and predicates returns the predicates results', function() { | ||
var result = check.map({ foo: { bar: 20 } }, | ||
{ foo: { bar: check.evenNumber } }); | ||
assert.deepEqual(result, { foo: { bar: true } }); | ||
test('map returns the correct results', function() { | ||
var result = | ||
check.map( | ||
{ foo: '', bar: 0, baz: { qux: 0 } }, | ||
{ foo: check.string, bar: check.string, baz: { qux: check.number } } | ||
); | ||
assert.lengthOf(Object.keys(result), 3); | ||
assert.isTrue(result.foo); | ||
assert.isFalse(result.bar); | ||
assert.isObject(result.baz); | ||
assert.lengthOf(Object.keys(result.baz), 1); | ||
assert.isTrue(result.baz.qux); | ||
}); | ||
test('map with verifier functions does not throw when valid', function() { | ||
test('map with assertion does not throw with valid data', function() { | ||
assert.doesNotThrow(function() { | ||
check.map({ foo: 'bar', baz: 123 }, | ||
{ foo: check.verify.string, | ||
baz: check.verify.number }); | ||
check.map({ foo: 'bar' }, { foo: check.assert.string }); | ||
}); | ||
}); | ||
test('map with verifier functions throws when invalid', function() { | ||
test('map with assertion throws with invalid data', function() { | ||
assert.throws(function() { | ||
check.map({ foo: 'bar', baz: 123 }, | ||
{ foo: check.verify.number }); | ||
check.map({ foo: 'foo', bar: 0 }, check.assert.string); | ||
}); | ||
}); | ||
test('every function is defined', function () { | ||
assert.isFunction(check.every); | ||
test('all function is defined', function () { | ||
assert.isFunction(check.all); | ||
}); | ||
test('every with invalid object throws', function() { | ||
test('all with invalid data throws', function() { | ||
assert.throws(function() { | ||
check.every(null); | ||
check.all('foo'); | ||
}); | ||
}); | ||
test('all with object data does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
check.all({ foo: true }); | ||
}); | ||
}); | ||
test('every with valid object does not throw', function() { | ||
test('all with array data does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
check.every({ foo: true }); | ||
check.all([ true ]); | ||
}); | ||
}); | ||
test('every with valid object evaluates the conjunction of all values', function() { | ||
assert.isTrue(check.every({ foo: true, bar: true, baz: true })); | ||
assert.isFalse(check.every({ foo: true, bar: true, baz: false })); | ||
test('all returns true when data is all true', function() { | ||
assert.isTrue(check.all({ foo: true, bar: true, baz: true, qux: true })); | ||
assert.isTrue(check.all([ true, true, true, true ])); | ||
assert.isTrue(check.all({ foo: { bar: { baz: { qux: true }}}})); | ||
}); | ||
test('every with nested objects evaluates the conjunction of all values', function() { | ||
assert.isTrue(check.every({ foo: true, bar: { baz: true } })); | ||
assert.isFalse(check.every({ foo: { bar : { baz : false }, bat: true } })); | ||
test('all returns false when some data is not true', function() { | ||
assert.isFalse(check.all({ foo: true, bar: true, baz: true, qux: false })); | ||
assert.isFalse(check.all([ true, true, false, true ])); | ||
assert.isFalse(check.all({ foo: { bar: { baz: false }, qux: true } })); | ||
}); | ||
@@ -816,9 +832,9 @@ | ||
test('any with invalid object throws', function() { | ||
test('any with invalid data throws', function() { | ||
assert.throws(function() { | ||
check.any(null); | ||
check.any('foo'); | ||
}); | ||
}); | ||
test('any with valid object does not throw', function() { | ||
test('any with object data does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
@@ -829,72 +845,88 @@ check.any({ foo: true }); | ||
test('any with valid object evaluates the disjunction of all values', function() { | ||
assert.isTrue(check.any({ foo: false, bar: true, baz: false })); | ||
test('any with array data does not throw', function() { | ||
assert.doesNotThrow(function() { | ||
check.any([ true ]); | ||
}); | ||
}); | ||
test('any returns true when some data is true', function() { | ||
assert.isTrue(check.any({ foo: false, bar: true })); | ||
assert.isTrue(check.any([ false, true ])); | ||
assert.isTrue(check.any({ foo: { bar: true }})); | ||
}); | ||
test('any returns false when all data is not true', function() { | ||
assert.isFalse(check.any({ foo: false, bar: false })); | ||
assert.isFalse(check.any([ false, false ])); | ||
assert.isFalse(check.any({ foo: { bar: false }})); | ||
}); | ||
test('any with nested objects evaluates the disjunction of all values', function() { | ||
assert.isTrue(check.any({ foo: { bar: false, baz: true }, bat: false })); | ||
assert.isFalse(check.any({ foo: { bar: false, baz: false }, bat: false })); | ||
test('assert modifier is defined', function() { | ||
assert.isObject(check.assert); | ||
}); | ||
test('verify modifier is defined', function() { | ||
assert.isObject(check.verify); | ||
test('assert modifier is applied to predicates', function () { | ||
assert.isFunction(check.assert.like); | ||
assert.isFunction(check.assert.instance); | ||
assert.isFunction(check.assert.emptyObject); | ||
assert.isFunction(check.assert.object); | ||
assert.isFunction(check.assert.null); | ||
assert.isFunction(check.assert.undefined); | ||
assert.isFunction(check.assert.assigned); | ||
assert.isFunction(check.assert.length); | ||
assert.isFunction(check.assert.array); | ||
assert.isFunction(check.assert.date); | ||
assert.isFunction(check.assert.function); | ||
assert.isFunction(check.assert.webUrl); | ||
assert.isFunction(check.assert.unemptyString); | ||
assert.isFunction(check.assert.string); | ||
assert.isFunction(check.assert.odd); | ||
assert.isFunction(check.assert.even); | ||
assert.isFunction(check.assert.positive); | ||
assert.isFunction(check.assert.negative); | ||
assert.isFunction(check.assert.integer); | ||
assert.isFunction(check.assert.number); | ||
assert.isFunction(check.assert.boolean); | ||
}); | ||
test('verify modifier is applied to predicates', function () { | ||
assert.isFunction(check.verify.like); | ||
assert.isFunction(check.verify.instance); | ||
assert.isFunction(check.verify.emptyObject); | ||
assert.isFunction(check.verify.object); | ||
assert.isFunction(check.verify.length); | ||
assert.isFunction(check.verify.array); | ||
assert.isFunction(check.verify.date); | ||
assert.isFunction(check.verify.fn); | ||
assert.isFunction(check.verify.webUrl); | ||
assert.isFunction(check.verify.unemptyString); | ||
assert.isFunction(check.verify.string); | ||
assert.isFunction(check.verify.evenNumber); | ||
assert.isFunction(check.verify.oddNumber); | ||
assert.isFunction(check.verify.positiveNumber); | ||
assert.isFunction(check.verify.negativeNumber); | ||
assert.isFunction(check.verify.number); | ||
assert.isFunction(check.verify.gitUrl); | ||
assert.isFunction(check.verify.nulled); | ||
assert.isFunction(check.verify.defined); | ||
test('assert modifier is not applied to batch operations', function () { | ||
assert.isUndefined(check.assert.map); | ||
assert.isUndefined(check.assert.apply); | ||
assert.isUndefined(check.assert.all); | ||
assert.isUndefined(check.assert.any); | ||
}); | ||
test('verify modifier is not applied to batch operations', function () { | ||
assert.isUndefined(check.verify.map); | ||
assert.isUndefined(check.verify.every); | ||
assert.isUndefined(check.verify.any); | ||
test('assert modifier is not applied to itself', function () { | ||
assert.isUndefined(check.assert.assert); | ||
}); | ||
test('verify modifier is not applied to itself', function () { | ||
assert.isUndefined(check.verify.verify); | ||
test('assert modifier is applied to maybe', function () { | ||
assert.isObject(check.assert.maybe); | ||
assert.strictEqual(Object.keys(check.assert.maybe).length, 21); | ||
}); | ||
test('verify modifier is applied to maybe', function () { | ||
assert.isObject(check.verify.maybe); | ||
assert.strictEqual(Object.keys(check.verify.maybe).length, 23); | ||
test('assert modifier is applied to not', function () { | ||
assert.isObject(check.assert.not); | ||
assert.strictEqual(Object.keys(check.assert.not).length, 21); | ||
}); | ||
test('verify modifier has correct number of keys', function () { | ||
assert.strictEqual(Object.keys(check.verify).length, 25); | ||
test('assert modifier has correct number of keys', function () { | ||
assert.strictEqual(Object.keys(check.assert).length, 23); | ||
}); | ||
test('verify modifier throws when value is wrong', function () { | ||
test('assert modifier throws when value is wrong', function () { | ||
assert.throws(function () { | ||
check.verify.unemptyString(''); | ||
check.assert.unemptyString(''); | ||
}); | ||
}); | ||
test('verify modifier does not throw when value is correct', function () { | ||
test('assert modifier does not throw when value is correct', function () { | ||
assert.doesNotThrow(function () { | ||
check.verify.unemptyString(' '); | ||
check.assert.unemptyString(' '); | ||
}); | ||
}); | ||
test('verify modifier throws Error instance', function () { | ||
test('assert modifier throws Error instance', function () { | ||
try { | ||
check.verify.unemptyString(''); | ||
check.assert.unemptyString(''); | ||
} catch (error) { | ||
@@ -905,5 +937,5 @@ assert.instanceOf(error, Error); | ||
test('verify modifier sets default message on Error instance', function () { | ||
test('assert modifier sets default message on Error instance', function () { | ||
try { | ||
check.verify.unemptyString(''); | ||
check.assert.unemptyString(''); | ||
} catch (error) { | ||
@@ -914,5 +946,5 @@ assert.strictEqual(error.message, 'Invalid string'); | ||
test('verify modifer sets message on Error instance', function () { | ||
test('assert modifer sets message on Error instance', function () { | ||
try { | ||
check.verify.unemptyString('', 'foo bar'); | ||
check.assert.unemptyString('', 'foo bar'); | ||
} catch (error) { | ||
@@ -923,5 +955,5 @@ assert.strictEqual(error.message, 'foo bar'); | ||
test('verify modifiers prohibits empty error messages', function () { | ||
test('assert modifier prohibits empty error messages', function () { | ||
try { | ||
check.verify.unemptyString('', ''); | ||
check.assert.unemptyString('', ''); | ||
} catch (error) { | ||
@@ -940,8 +972,8 @@ assert.strictEqual(error.message, 'Invalid string'); | ||
test('maybe modifier is not applied to verify', function () { | ||
assert.isUndefined(check.maybe.verify); | ||
test('maybe modifier is not applied to assert', function () { | ||
assert.isUndefined(check.maybe.assert); | ||
}); | ||
test('maybe modifier has correct number of keys', function () { | ||
assert.strictEqual(Object.keys(check.maybe).length, 23); | ||
assert.strictEqual(Object.keys(check.maybe).length, 21); | ||
}); | ||
@@ -958,4 +990,4 @@ | ||
test('maybe modifier returns predicate result on value', function() { | ||
assert.isFalse(check.maybe.oddNumber(2)); | ||
assert.isTrue(check.maybe.oddNumber(1)); | ||
assert.isFalse(check.maybe.odd(2)); | ||
assert.isTrue(check.maybe.odd(1)); | ||
}); | ||
@@ -971,8 +1003,8 @@ | ||
test('not modifier is not applied to verify', function () { | ||
assert.isUndefined(check.not.verify); | ||
test('not modifier is not applied to assert', function () { | ||
assert.isUndefined(check.not.assert); | ||
}); | ||
test('not modifier has correct number of keys', function () { | ||
assert.strictEqual(Object.keys(check.not).length, 23); | ||
assert.strictEqual(Object.keys(check.not).length, 21); | ||
}); | ||
@@ -988,23 +1020,23 @@ | ||
test('verify modifier with maybe does not throw when value is correct', function() { | ||
test('assert modifier with maybe does not throw when value is correct', function() { | ||
assert.doesNotThrow(function() { | ||
check.verify.maybe.positiveNumber(1); | ||
check.assert.maybe.positive(1); | ||
}); | ||
}); | ||
test('verify modifier with maybe throws when value is wrong', function() { | ||
test('assert modifier with maybe throws when value is wrong', function() { | ||
assert.throws(function() { | ||
check.verify.maybe.positiveNumber(-1); | ||
check.assert.maybe.positive(-1); | ||
}); | ||
}); | ||
test('verify modifier with not throws when value is correct', function() { | ||
test('assert modifier with not throws when value is correct', function() { | ||
assert.throws(function() { | ||
check.verify.not.negativeNumber(-1); | ||
check.assert.not.negative(-1); | ||
}); | ||
}); | ||
test('verify modifier with not doesNotThrow when value is wrong', function() { | ||
test('assert modifier with not does not throw when value is wrong', function() { | ||
assert.doesNotThrow(function() { | ||
check.verify.not.negativeNumber(1); | ||
check.assert.not.negative(1); | ||
}); | ||
@@ -1014,5 +1046,6 @@ }); | ||
test('maybe modifier with falsey values evaluates predicate', function() { | ||
assert.isFalse(check.maybe.positiveNumber(0)); | ||
assert.isFalse(check.maybe.positive(0)); | ||
}); | ||
}); | ||
}(typeof require === 'function' ? require : undefined)); | ||
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
501
68942
1305