check-types
Advanced tools
Comparing version 2.2.0 to 3.0.0
{ | ||
"name": "check-types", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"main": "src/check-types.min.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
{ | ||
"name": "check-types", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "A tiny library for asserting types and values.", | ||
@@ -5,0 +5,0 @@ "repo": "philbooth/check-types.js", |
# History | ||
## 3.0 | ||
* Breaking changes: | ||
* Fix errant check that property counts match in `map`. | ||
* Drop `webUrl`. (sorry @bahmutov!) | ||
* Implement `match` for general regex-matching. Possibly of interest to former users of `webUrl`, `gitUrl` and `email`. | ||
* Implement `contains`. | ||
* Implement `between`. | ||
* Implement `greater`. | ||
* Implement `less`. | ||
* Implement `zero`. | ||
* Implement `emptyArray`. | ||
* Implement `error`. | ||
* Rename `length` => `hasLength`. | ||
* Turn `assert`, `not` and `maybe` into standalone functions as well as modifiers. | ||
## 2.2 | ||
@@ -4,0 +20,0 @@ |
{ | ||
"name": "check-types", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "A tiny library for asserting types and values.", | ||
@@ -32,5 +32,5 @@ "homepage": "https://github.com/philbooth/check-types.js", | ||
"devDependencies": { | ||
"jshint": "2.5.x", | ||
"mocha": "2.0.x", | ||
"chai": "1.10.x", | ||
"jshint": "2.6.x", | ||
"mocha": "2.2.x", | ||
"chai": "2.1.x", | ||
"uglify-js": "2.4.x" | ||
@@ -37,0 +37,0 @@ }, |
139
README.md
@@ -20,2 +20,3 @@ # check-types.js | ||
* [Date functions](#date-functions) | ||
* [Error functions](#error-functions) | ||
* [Object functions](#object-functions) | ||
@@ -27,2 +28,3 @@ * [Boolean functions](#boolean-functions) | ||
* [Where can I use it?](#where-can-i-use-it) | ||
* [What changed from 2.x to 3.x?](#what-changed-from-2x-to-3x) | ||
* [What changed from 1.x to 2.x?](#what-changed-from-1x-to-2x) | ||
@@ -52,3 +54,3 @@ * [What changed from 0.x to 1.x?](#what-changed-from-0x-to-1x) | ||
14 kb unminified with comments, 3.7 kb minified, 1.4 kb minified + gzipped. | ||
17 kb unminified with comments, 4.3 kb minified, 1.5 kb minified + gzipped. | ||
@@ -119,11 +121,20 @@ ## How do I install it? | ||
The `not` modifier | ||
negates a predicate, | ||
negates predicates, | ||
returning `true` if the predicate returns `false` | ||
and `false` if the predicate returns `true`. | ||
It is also itself a function, | ||
which simply returns | ||
the negation of | ||
its argument. | ||
* `check.maybe.xxx(thing)`: | ||
The `maybe` modifier | ||
returns `true` if `thing` is `null` or `undefined`, | ||
otherwise it returns the result | ||
tweaks predicates to | ||
return `true` if `thing` is `null` or `undefined`, | ||
otherwise return the result | ||
of the predicate. | ||
It is also itself a function, | ||
which returns `true` | ||
when its argument is `null` or `undefined`, | ||
otherwise it returns its argument. | ||
@@ -138,6 +149,6 @@ * `check.either.xxx(thing).or.yyy(thang)`: | ||
The assert modifier | ||
calls the equivalent predicate | ||
and throws an `Error` | ||
if the result is `false`. | ||
It can also be applied | ||
changes predicates | ||
to throw when their result | ||
would otherwise be `false`. | ||
It can be applied | ||
to the `not`, `maybe` and `either` modifiers | ||
@@ -149,2 +160,5 @@ using the forms | ||
`check.assert.either.xxx(thing, message).or.yyy(thang)`. | ||
It is also itself a function, | ||
which simply throws | ||
when its argument is false. | ||
@@ -173,9 +187,16 @@ Additionally, there are some batch operations | ||
* `check.webUrl(thing)`: | ||
* `check.contains(thing, substring)`: | ||
Returns `true` | ||
if `thing` seems like an HTTP or HTTPS URL, | ||
if `thing` is a string | ||
that contains `substring`, | ||
`false` otherwise. | ||
* `check.length(thing, value)`: | ||
* `check.match(thing, regex)`: | ||
Returns `true` | ||
if `thing` is a string | ||
that matches `regex`, | ||
`false` otherwise. | ||
* `check.hasLength(thing, value)`: | ||
Returns `true` | ||
if `thing` has a length property | ||
@@ -199,2 +220,21 @@ that equals `value`, | ||
* `check.greater(thing, value)`: | ||
Returns `true` if `thing` is a number | ||
greater than `value`, | ||
`false` otherwise. | ||
* `check.less(thing, value)`: | ||
Returns `true` if `thing` is a number | ||
less than `value`, | ||
`false` otherwise. | ||
* `check.between(thing, a, b)`: | ||
Returns `true` if `thing` is a number | ||
between than `a` and `b`, | ||
`false` otherwise. | ||
The arguments `a` and `b` | ||
may be in any order, | ||
it doesn't matter | ||
which is greater. | ||
* `check.positive(thing)`: | ||
@@ -211,2 +251,7 @@ Returns `true` if `thing` is a number | ||
* `check.zero(thing)`: | ||
Returns `true` | ||
if `thing` is zero, | ||
`false` otherwise. | ||
* `check.odd(thing)`: | ||
@@ -241,4 +286,9 @@ Returns `true` | ||
* `check.length(thing, value)`: | ||
* `check.emptyArray(thing)`: | ||
Returns `true` | ||
if `thing` is an empty array, | ||
`false` otherwise. | ||
* `check.hasLength(thing, value)`: | ||
Returns `true` | ||
if `thing` has a length property | ||
@@ -257,2 +307,9 @@ that equals `value`, | ||
#### Error functions | ||
* `check.error(thing)`: | ||
Returns `true` | ||
if `thing` is an error, | ||
`false` otherwise. | ||
#### Object functions | ||
@@ -308,2 +365,6 @@ | ||
* `check.not(value)`: | ||
Returns the negation | ||
of `value`. | ||
* `check.not.xxx(...)`: | ||
@@ -313,2 +374,7 @@ Returns the negation | ||
* `check.maybe(value)`: | ||
Returns `true` | ||
if `value` is `null` or `undefined`, | ||
otherwise it returns `value`. | ||
* `check.maybe.xxx(...)`: | ||
@@ -327,2 +393,8 @@ Returns `true` | ||
* `check.assert(value, message)`: | ||
Throws an `Error` | ||
if `value` is `false`, | ||
setting `message` | ||
on the `Error` instance. | ||
* `check.assert.xxx(...)`: | ||
@@ -339,3 +411,3 @@ Throws an `Error` | ||
* `check.apply(things, predicates)`: | ||
Applies each value from the array of things | ||
Applies each value from the `things` array | ||
to the corresponding predicate | ||
@@ -349,5 +421,6 @@ and returns the array of results. | ||
* `check.map(things, predicates)`: | ||
Maps each value from the data | ||
Maps each value from the `things` object | ||
to the corresponding predicate | ||
and returns a results object. | ||
and returns an object | ||
containing the results. | ||
Supports nested objects. | ||
@@ -410,4 +483,9 @@ | ||
```javascript | ||
check.assert(myFunction(), 'Something went wrong'); | ||
// Throws if myFunction returns `false` | ||
``` | ||
```javascript | ||
check.apply([ 'foo', 'bar', '' ], check.unemptyString); | ||
// Returns false | ||
// Returns [ true, true, false ] | ||
``` | ||
@@ -466,2 +544,26 @@ | ||
## What changed from 2.x to 3.x? | ||
Breaking changes | ||
were made to the API | ||
in version 3.0.0. | ||
Specifically: | ||
* The modifiers `assert`, `not` and `maybe` were changed to functions. | ||
* The predicate `webUrl` was removed. | ||
* The predicate `length` was renamed `hasLength`. | ||
* The predicate `match` was implemented for general regex-matching. | ||
* The predicate `contains` was implemented. | ||
* The predicate `between` was implemented. | ||
* The predicate `greater` was implemented. | ||
* The predicate `less` was implemented. | ||
* The predicate `zero` was implemented. | ||
* The predicate `emptyArray` was implemented. | ||
* The predicate `error` was implemented. | ||
* `map` was fixed to stop erroneously throwing when the property counts do not match. | ||
See the [history][history3] | ||
for more details. | ||
## What changed from 1.x to 2.x? | ||
@@ -536,4 +638,5 @@ | ||
[releases]: https://github.com/philbooth/check-types.js/releases | ||
[history2]: https://github.com/philbooth/check-types.js/blob/master/HISTORY.md#20 | ||
[history1]: https://github.com/philbooth/check-types.js/blob/master/HISTORY.md#10 | ||
[history3]: HISTORY.md#30 | ||
[history2]: HISTORY.md#20 | ||
[history1]: HISTORY.md#10 | ||
[node]: http://nodejs.org/ | ||
@@ -540,0 +643,0 @@ [npm]: https://npmjs.org/ |
@@ -21,7 +21,10 @@ /** | ||
null: 'Invalid value', | ||
length: 'Invalid length', | ||
hasLength: 'Invalid length', | ||
emptyArray: 'Invalid array', | ||
array: 'Invalid array', | ||
date: 'Invalid date', | ||
error: 'Invalid error', | ||
fn: 'Invalid function', | ||
webUrl: 'Invalid URL', | ||
match: 'Invalid string', | ||
contains: 'Invalid string', | ||
unemptyString: 'Invalid string', | ||
@@ -31,5 +34,9 @@ string: 'Invalid string', | ||
even: 'Invalid number', | ||
between: 'Invalid number', | ||
greater: 'Invalid number', | ||
less: 'Invalid number', | ||
positive: 'Invalid number', | ||
negative: 'Invalid number', | ||
integer: 'Invalid number', | ||
zero: 'Invalid number', | ||
number: 'Invalid number', | ||
@@ -47,7 +54,10 @@ boolean: 'Invalid boolean' | ||
null: isNull, | ||
length: length, | ||
hasLength: hasLength, | ||
emptyArray: emptyArray, | ||
array: array, | ||
date: date, | ||
error: error, | ||
function: isFunction, | ||
webUrl: webUrl, | ||
match: match, | ||
contains: contains, | ||
unemptyString: unemptyString, | ||
@@ -57,5 +67,9 @@ string: string, | ||
even: even, | ||
between: between, | ||
greater: greater, | ||
less: less, | ||
positive: positive, | ||
negative: negative, | ||
integer : integer, | ||
zero: zero, | ||
number: number, | ||
@@ -73,5 +87,5 @@ boolean: boolean | ||
functions = mixin(functions, predicates); | ||
assert = createModifiedPredicates(assertModifier); | ||
not = createModifiedPredicates(notModifier); | ||
maybe = createModifiedPredicates(maybeModifier); | ||
assert = createModifiedPredicates(assertModifier, assertImpl); | ||
not = createModifiedPredicates(notModifier, notImpl); | ||
maybe = createModifiedPredicates(maybeModifier, maybeImpl); | ||
either = createModifiedPredicates(eitherModifier); | ||
@@ -191,3 +205,3 @@ assert.not = createModifiedFunctions(assertModifier, not); | ||
/** | ||
* Public function `length`. | ||
* Public function `hasLength`. | ||
* | ||
@@ -198,3 +212,3 @@ * Returns `true` if something is has a length property | ||
*/ | ||
function length (data, value) { | ||
function hasLength (data, value) { | ||
assert.not.undefined(value); | ||
@@ -206,2 +220,13 @@ | ||
/** | ||
* Public function `emptyArray`. | ||
* | ||
* Returns `true` if something is an empty array, | ||
* `false` otherwise. | ||
* | ||
*/ | ||
function emptyArray (data) { | ||
return array(data) && data.length === 0; | ||
} | ||
/** | ||
* Public function `array`. | ||
@@ -230,2 +255,13 @@ * | ||
/** | ||
* Public function `error`. | ||
* | ||
* Returns `true` if something is a plain-old JS object, | ||
* `false` otherwise. | ||
* | ||
*/ | ||
function error (data) { | ||
return Object.prototype.toString.call(data) === '[object Error]'; | ||
} | ||
/** | ||
* Public function `function`. | ||
@@ -242,17 +278,28 @@ * | ||
/** | ||
* Public function `webUrl`. | ||
* Public function `match`. | ||
* | ||
* Returns `true` if something is an HTTP or HTTPS URL, | ||
* `false` otherwise. | ||
* Returns `true` if something is a string | ||
* that matches `regex`, `false` otherwise. | ||
* | ||
*/ | ||
function webUrl (data) { | ||
return unemptyString(data) && /^(https?:)?\/\/([\w-\.~:@]+)(\/[\w-\.~\/\?#\[\]&\(\)\*\+,;=%]*)?$/.test(data); | ||
function match (data, regex) { | ||
return string(data) && !!data.match(regex); | ||
} | ||
/** | ||
* Public function `contains`. | ||
* | ||
* Returns `true` if something is a string | ||
* that contains `substring`, `false` otherwise. | ||
* | ||
*/ | ||
function contains (data, substring) { | ||
return string(data) && data.indexOf(substring) !== -1; | ||
} | ||
/** | ||
* Public function `unemptyString`. | ||
* | ||
* Returns `true` if something is a non-empty string, `false` | ||
* otherwise. | ||
* Returns `true` if something is a non-empty string, | ||
* `false` otherwise. | ||
* | ||
@@ -308,2 +355,39 @@ */ | ||
/** | ||
* Public function `between`. | ||
* | ||
* Returns `true` if something is a number | ||
* between `a` and `b`, `false` otherwise. | ||
* | ||
*/ | ||
function between (data, a, b) { | ||
if (a < b) { | ||
return greater(data, a) && less(data, b); | ||
} | ||
return less(data, a) && greater(data, b); | ||
} | ||
/** | ||
* Public function `greater`. | ||
* | ||
* Returns `true` if something is a number | ||
* greater than `value`, `false` otherwise. | ||
* | ||
*/ | ||
function greater (data, value) { | ||
return number(data) && data > value; | ||
} | ||
/** | ||
* Public function `less`. | ||
* | ||
* Returns `true` if something is a number | ||
* less than `value`, `false` otherwise. | ||
* | ||
*/ | ||
function less (data, value) { | ||
return number(data) && data < value; | ||
} | ||
/** | ||
* Public function `positive`. | ||
@@ -316,3 +400,3 @@ * | ||
function positive (data) { | ||
return number(data) && data > 0; | ||
return greater(data, 0); | ||
} | ||
@@ -329,3 +413,3 @@ | ||
function negative (data) { | ||
return number(data) && data < 0; | ||
return less(data, 0); | ||
} | ||
@@ -347,2 +431,14 @@ | ||
/** | ||
* Public function `zero`. | ||
* | ||
* Returns `true` if something is zero, | ||
* `false` otherwise. | ||
* | ||
* @param data The thing to test. | ||
*/ | ||
function zero (data) { | ||
return data === 0; | ||
} | ||
/** | ||
* Public function `boolean`. | ||
@@ -376,3 +472,3 @@ * | ||
assert.array(predicates); | ||
assert.length(data, predicates.length); | ||
assert.hasLength(data, predicates.length); | ||
@@ -398,3 +494,2 @@ return data.map(function (value, index) { | ||
keys = Object.keys(predicates); | ||
assert.length(Object.keys(data), keys.length); | ||
@@ -502,7 +597,9 @@ keys.forEach(function (key) { | ||
function assertPredicate (predicate, args, defaultMessage) { | ||
var message; | ||
var message = args[args.length - 1]; | ||
assertImpl(predicate.apply(null, args), unemptyString(message) ? message : defaultMessage); | ||
} | ||
if (!predicate.apply(null, args)) { | ||
message = args[args.length - 1]; | ||
throw new Error(unemptyString(message) ? message : defaultMessage); | ||
function assertImpl (value, message) { | ||
if (value === false) { | ||
throw new Error(message || 'Assertion failed'); | ||
} | ||
@@ -544,6 +641,10 @@ } | ||
return function () { | ||
return !predicate.apply(null, arguments); | ||
return notImpl(predicate.apply(null, arguments)); | ||
}; | ||
} | ||
function notImpl (value) { | ||
return !value; | ||
} | ||
/** | ||
@@ -565,2 +666,10 @@ * Public modifier `maybe`. | ||
function maybeImpl (value) { | ||
if (assigned(value) === false) { | ||
return true; | ||
} | ||
return value; | ||
} | ||
/** | ||
@@ -590,11 +699,16 @@ * Public modifier `either`. | ||
function createModifiedPredicates (modifier) { | ||
return createModifiedFunctions(modifier, predicates); | ||
function createModifiedPredicates (modifier, object) { | ||
return createModifiedFunctions(modifier, predicates, object); | ||
} | ||
function createModifiedFunctions (modifier, functions) { | ||
var result = {}; | ||
function createModifiedFunctions (modifier, functions, object) { | ||
var result = object || {}; | ||
Object.keys(functions).forEach(function (key) { | ||
result[key] = modifier(functions[key], messages[key]); | ||
Object.defineProperty(result, key, { | ||
configurable: false, | ||
enumerable: true, | ||
writable: false, | ||
value: modifier(functions[key], messages[key]) | ||
}); | ||
}); | ||
@@ -601,0 +715,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(n){"use strict";function t(n,e){var r;$.object(n),$.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)&&!c(n)}function o(n){return void 0===n}function c(n){return null===n}function a(n,t){return $.not.undefined(t),i(n)&&n.length===t}function f(n){return Array.isArray(n)}function l(n){return"[object Date]"===Object.prototype.toString.call(n)&&!isNaN(n.getTime())}function d(n){return"function"==typeof n}function s(n){return b(n)&&/^(https?:)?\/\/([\w-\.~:@]+)(\/[\w-\.~\/\?#\[\]&\(\)\*\+,;=%]*)?$/.test(n)}function b(n){return y(n)&&""!==n}function y(n){return"string"==typeof n}function v(n){return g(n)&&!p(n)}function p(n){return h(n)&&n%2===0}function g(n){return h(n)&&n%1===0}function m(n){return h(n)&&n>0}function I(n){return h(n)&&0>n}function h(n){return"number"==typeof n&&isNaN(n)===!1&&n!==Number.POSITIVE_INFINITY&&n!==Number.NEGATIVE_INFINITY}function j(n){return n===!1||n===!0}function O(n,t){return $.array(n),d(t)?n.map(function(n){return t(n)}):($.array(t),$.length(n,t.length),n.map(function(n,e){return t[e](n)}))}function N(n,t){var e,r={};return $.object(n),$.object(t),e=Object.keys(t),$.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]=N(n[e],i))}),r}function k(n){return f(n)?w(n,!1):($.object(n),E(n,!1))}function w(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)?w(n,!0):($.object(n),E(n,!0))}function T(n,t){return Object.keys(t).forEach(function(e){n[e]=t[e]}),n}function P(n,t){return function(){A(n,arguments,t)}}function A(n,t,e){var r;if(!n.apply(null,t))throw r=t[t.length-1],new Error(b(r)?r:e)}function U(n,t){return function(){function e(n,t){return n[t]=function(){if(r&&!L[t].apply(null,arguments))throw r},n}var r;try{A(n,arguments,t)}catch(u){r=u}return{or:Object.keys(L).reduce(e,{})}}}function x(n){return function(){return!n.apply(null,arguments)}}function F(n){return function(){return i(arguments[0])?n.apply(null,arguments):!0}}function V(n){function t(){return!0}return function(){function e(n,e){return n[e]=r?t:L[e],n}var r=n.apply(null,arguments);return{or:Object.keys(L).reduce(e,{})}}}function Y(n){return _(n,L)}function _(n,t){var e={};return Object.keys(t).forEach(function(r){e[r]=n(t[r],G[r])}),e}function D(t){"function"==typeof define&&define.amd?define(function(){return t}):"undefined"!=typeof module&&null!==module&&module.exports?module.exports=t:n.check=t}var G,L,R,$,q,z,B;G={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"},L={like:t,instance:e,emptyObject:r,object:u,assigned:i,undefined:o,"null":c,length:a,array:f,date:l,"function":d,webUrl:s,unemptyString:b,string:y,odd:v,even:p,positive:m,negative:I,integer:g,number:h,"boolean":j},R={apply:O,map:N,all:k,any:S},R=T(R,L),$=Y(P),q=Y(x),z=Y(F),B=Y(V),$.not=_(P,q),$.maybe=_(P,z),$.either=_(U,L),D(T(R,{assert:$,not:q,maybe:z,either:B}))}(this); | ||
!function(n){"use strict";function t(n,e){var r;X.object(n),X.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&&b(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 X.not.undefined(t),i(n)&&n.length===t}function f(n){return l(n)&&0===n.length}function l(n){return Array.isArray(n)}function d(n){return"[object Date]"===Object.prototype.toString.call(n)&&!isNaN(n.getTime())}function s(n){return"[object Error]"===Object.prototype.toString.call(n)}function b(n){return"function"==typeof n}function v(n,t){return m(n)&&!!n.match(t)}function y(n,t){return m(n)&&-1!==n.indexOf(t)}function p(n){return m(n)&&""!==n}function m(n){return"string"==typeof n}function g(n){return h(n)&&!I(n)}function I(n){return E(n)&&n%2===0}function h(n){return E(n)&&n%1===0}function j(n,t,e){return e>t?O(n,t)&&N(n,e):N(n,t)&&O(n,e)}function O(n,t){return E(n)&&n>t}function N(n,t){return E(n)&&t>n}function k(n){return O(n,0)}function w(n){return N(n,0)}function E(n){return"number"==typeof n&&isNaN(n)===!1&&n!==Number.POSITIVE_INFINITY&&n!==Number.NEGATIVE_INFINITY}function A(n){return 0===n}function S(n){return n===!1||n===!0}function P(n,t){return X.array(n),b(t)?n.map(function(n){return t(n)}):(X.array(t),X.hasLength(n,t.length),n.map(function(n,e){return t[e](n)}))}function T(n,t){var e,r={};return X.object(n),X.object(t),e=Object.keys(t),e.forEach(function(e){var i=t[e];b(i)?r[e]=i(n[e]):u(i)&&(r[e]=T(n[e],i))}),r}function x(n){return l(n)?L(n,!1):(X.object(n),z(n,!1))}function L(n,t){var e;for(e=0;e<n.length;e+=1)if(n[e]===t)return t;return!t}function z(n,t){var e,r;for(e in n)if(n.hasOwnProperty(e)){if(r=n[e],u(r)&&z(r,t)===t)return t;if(r===t)return t}return!t}function F(n){return l(n)?L(n,!0):(X.object(n),z(n,!0))}function V(n,t){return Object.keys(t).forEach(function(e){n[e]=t[e]}),n}function Y(n,t){return function(){_(n,arguments,t)}}function _(n,t,e){var r=t[t.length-1];D(n.apply(null,t),p(r)?r:e)}function D(n,t){if(n===!1)throw new Error(t||"Assertion failed")}function G(n,t){return function(){function e(n,t){return n[t]=function(){if(r&&!U[t].apply(null,arguments))throw r},n}var r;try{_(n,arguments,t)}catch(u){r=u}return{or:Object.keys(U).reduce(e,{})}}}function q(n){return function(){return B(n.apply(null,arguments))}}function B(n){return!n}function C(n){return function(){return i(arguments[0])?n.apply(null,arguments):!0}}function H(n){return i(n)===!1?!0:n}function J(n){function t(){return!0}return function(){function e(n,e){return n[e]=r?t:U[e],n}var r=n.apply(null,arguments);return{or:Object.keys(U).reduce(e,{})}}}function K(n,t){return M(n,U,t)}function M(n,t,e){var r=e||{};return Object.keys(t).forEach(function(e){Object.defineProperty(r,e,{configurable:!1,enumerable:!0,writable:!1,value:n(t[e],R[e])})}),r}function Q(t){"function"==typeof define&&define.amd?define(function(){return t}):"undefined"!=typeof module&&null!==module&&module.exports?module.exports=t:n.check=t}var R,U,W,X,Z,$,nt;R={like:"Invalid type",instance:"Invalid type",emptyObject:"Invalid object",object:"Invalid object",assigned:"Invalid value",undefined:"Invalid value","null":"Invalid value",hasLength:"Invalid length",emptyArray:"Invalid array",array:"Invalid array",date:"Invalid date",error:"Invalid error",fn:"Invalid function",match:"Invalid string",contains:"Invalid string",unemptyString:"Invalid string",string:"Invalid string",odd:"Invalid number",even:"Invalid number",between:"Invalid number",greater:"Invalid number",less:"Invalid number",positive:"Invalid number",negative:"Invalid number",integer:"Invalid number",zero:"Invalid number",number:"Invalid number","boolean":"Invalid boolean"},U={like:t,instance:e,emptyObject:r,object:u,assigned:i,undefined:o,"null":a,hasLength:c,emptyArray:f,array:l,date:d,error:s,"function":b,match:v,contains:y,unemptyString:p,string:m,odd:g,even:I,between:j,greater:O,less:N,positive:k,negative:w,integer:h,zero:A,number:E,"boolean":S},W={apply:P,map:T,all:x,any:F},W=V(W,U),X=K(Y,D),Z=K(q,B),$=K(C,H),nt=K(J),X.not=M(Y,Z),X.maybe=M(Y,$),X.either=M(G,U),Q(V(W,{assert:X,not:Z,maybe:$,either:nt}))}(this); |
@@ -243,52 +243,56 @@ /*globals require, chai */ | ||
test('length function is defined', function () { | ||
assert.isFunction(check.length); | ||
test('object with error returns false', function () { | ||
assert.isFalse(check.object(new Error())); | ||
}); | ||
test('length without length argument throws', function () { | ||
test('hasLength function is defined', function () { | ||
assert.isFunction(check.hasLength); | ||
}); | ||
test('hasLength without length argument throws', function () { | ||
assert.throws(function () { | ||
check.length({}); | ||
check.hasLength({}); | ||
}); | ||
}); | ||
test('length with length argument does not throw', function () { | ||
test('hasLength with length argument does not throw', function () { | ||
assert.doesNotThrow(function () { | ||
check.length({}, 5); | ||
check.hasLength({}, 5); | ||
}); | ||
}); | ||
test('length with zero on empty array returns true', function () { | ||
assert.isTrue(check.length([], 0)); | ||
test('hasLength with zero on empty array returns true', function () { | ||
assert.isTrue(check.hasLength([], 0)); | ||
}); | ||
test('length with zero on empty string returns true', function () { | ||
assert.isTrue(check.length('', 0)); | ||
test('hasLength with zero on empty string returns true', function () { | ||
assert.isTrue(check.hasLength('', 0)); | ||
}); | ||
test('length with zero on empty object returns false', function () { | ||
assert.isFalse(check.length({}, 0)); | ||
test('hasLength with zero on empty object returns false', function () { | ||
assert.isFalse(check.hasLength({}, 0)); | ||
}); | ||
test('length with matching length on array returns true', function () { | ||
assert.isTrue(check.length([ 'foo', 'bar' ], 2)); | ||
test('hasLength with matching length on array returns true', function () { | ||
assert.isTrue(check.hasLength([ 'foo', 'bar' ], 2)); | ||
}); | ||
test('length with contrasting length on array returns false', function () { | ||
assert.isFalse(check.length([ 'foo', 'bar', 'baz' ], 2)); | ||
test('hasLength with contrasting length on array returns false', function () { | ||
assert.isFalse(check.hasLength([ 'foo', 'bar', 'baz' ], 2)); | ||
}); | ||
test('length with matching length on string returns true', function () { | ||
assert.isTrue(check.length('foo', 3)); | ||
test('hasLength with matching length on string returns true', function () { | ||
assert.isTrue(check.hasLength('foo', 3)); | ||
}); | ||
test('length with contrasting length on string returns false', function () { | ||
assert.isFalse(check.length('foobar', 3)); | ||
test('hasLength with contrasting length on string returns false', function () { | ||
assert.isFalse(check.hasLength('foobar', 3)); | ||
}); | ||
test('length with matching length on object returns true', function () { | ||
assert.isTrue(check.length({ length: 1 }, 1)); | ||
test('hasLength with matching length on object returns true', function () { | ||
assert.isTrue(check.hasLength({ length: 1 }, 1)); | ||
}); | ||
test('length with contrasting length on object returns false', function () { | ||
assert.isFalse(check.length({ length: 2 }, 1)); | ||
test('hasLength with contrasting length on object returns false', function () { | ||
assert.isFalse(check.hasLength({ length: 2 }, 1)); | ||
}); | ||
@@ -316,2 +320,22 @@ | ||
test('emptyArray function is defined', function () { | ||
assert.isFunction(check.emptyArray); | ||
}); | ||
test('emptyArray with empty array returns true', function () { | ||
assert.isTrue(check.emptyArray([])); | ||
}); | ||
test('emptyArray with empty object returns false', function () { | ||
assert.isFalse(check.emptyArray({})); | ||
}); | ||
test('emptyArray with null returns false', function () { | ||
assert.isFalse(check.emptyArray(null)); | ||
}); | ||
test('emptyArray with non-empty array returns false', function () { | ||
assert.isFalse(check.emptyArray([ 'foo' ])); | ||
}); | ||
test('date function is defined', function () { | ||
@@ -333,2 +357,14 @@ assert.isFunction(check.date); | ||
test('error function is defined', function () { | ||
assert.isFunction(check.error); | ||
}); | ||
test('error with error returns true', function () { | ||
assert.isTrue(check.error(new Error())); | ||
}); | ||
test('error with object returns false', function () { | ||
assert.isFalse(check.error({})); | ||
}); | ||
test('function function is defined', function () { | ||
@@ -346,56 +382,46 @@ assert.isFunction(check.function); | ||
test('webUrl function is defined', function () { | ||
assert.isFunction(check.webUrl); | ||
test('match function is defined', function () { | ||
assert.isFunction(check.match); | ||
}); | ||
test('webUrl with http scheme returns true', function () { | ||
assert.isTrue(check.webUrl('http://127.0.0.1:8080/')); | ||
test('match with match returns true', function () { | ||
assert.isTrue(check.match('foo', /^FOO$/i)); | ||
}); | ||
test('webUrl with ftp scheme returns false', function () { | ||
assert.isFalse(check.webUrl('ftp://example.com/')); | ||
test('match with no match returns false', function () { | ||
assert.isFalse(check.match('foo', /^foO$/)); | ||
}); | ||
test('webUrl with https scheme returns true', function () { | ||
assert.isTrue(check.webUrl('https://example.com/')); | ||
}); | ||
test('webUrl with httpss scheme returns false', function () { | ||
assert.isFalse(check.webUrl('httpss://')); | ||
}); | ||
test('webUrl with object returns false', function () { | ||
test('match with object returns false', function () { | ||
assert.isFalse( | ||
check.webUrl({ | ||
check.match({ | ||
toString: function () { | ||
return 'https://example.com/'; | ||
return 'foo'; | ||
} | ||
}) | ||
}, /^foo$/) | ||
); | ||
}); | ||
test('webUrl with no scheme returns true', function () { | ||
assert.isTrue(check.webUrl('//example.com/')); | ||
test('contains function is defined', function () { | ||
assert.isFunction(check.contains); | ||
}); | ||
test('webUrl without domain returns false', function () { | ||
assert.isFalse(check.webUrl('http:///')); | ||
test('contains with match returns true', function () { | ||
assert.isTrue(check.contains('foo', 'oo')); | ||
}); | ||
test('webUrl with single-word domain returns true', function () { | ||
assert.isTrue(check.webUrl('http://ws/')); | ||
test('contains with no match returns false', function () { | ||
assert.isFalse(check.contains('foo', 'bar')); | ||
}); | ||
test('webUrl without path returns true', function () { | ||
assert.isTrue(check.webUrl('http://example.com')); | ||
test('contains with object returns false', function () { | ||
assert.isFalse( | ||
check.contains({ | ||
toString: function () { | ||
return 'foo'; | ||
} | ||
}, 'oo') | ||
); | ||
}); | ||
test('webUrl with bad character returns false', function () { | ||
assert.isFalse(check.webUrl('http://example.com/`')); | ||
}); | ||
test('webUrl with percent-encoding returns true', function () { | ||
assert.isTrue(check.webUrl('http://example.com/%20')); | ||
}); | ||
test('unemptyString function is defined', function () { | ||
@@ -501,2 +527,106 @@ assert.isFunction(check.unemptyString); | ||
test('between function is defined', function () { | ||
assert.isFunction(check.between); | ||
}); | ||
test('between with 1, 0, 1 returns false', function () { | ||
assert.isFalse(check.between(1, 0, 1)); | ||
}); | ||
test('between with 1, 0, 2 returns true', function () { | ||
assert.isTrue(check.between(1, 0, 2)); | ||
}); | ||
test('between with 1, 2, 0 returns true', function () { | ||
assert.isTrue(check.between(1, 2, 0)); | ||
}); | ||
test('between works with fractions', function () { | ||
assert.isTrue(check.between(1/2, 1/4, 1)); | ||
}); | ||
test('between works with negative numbers', function () { | ||
assert.isTrue(check.between(-2, -3, -1)); | ||
}); | ||
test('between with positive infinity returns false', function () { | ||
assert.isFalse(check.between(Number.POSITIVE_INFINITY, 0, Number.POSITIVE_INFINITY)); | ||
}); | ||
test('between with negative infinity returns false', function () { | ||
assert.isFalse(check.between(Number.NEGATIVE_INFINITY, 0, Number.NEGATIVE_INFINITY)); | ||
}); | ||
test('between with NaN returns false', function () { | ||
assert.isFalse(check.between(NaN, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)); | ||
}); | ||
test('between with string returns false', function () { | ||
assert.isFalse(check.between('-1', 0, -2)); | ||
}); | ||
test('greater function is defined', function () { | ||
assert.isFunction(check.greater); | ||
}); | ||
test('greater with 0, 1 returns false', function () { | ||
assert.isFalse(check.greater(0, 1)); | ||
}); | ||
test('greater with 2, 1 returns true', function () { | ||
assert.isTrue(check.greater(2, 1)); | ||
}); | ||
test('greater works with fractions', function () { | ||
assert.isTrue(check.greater(1/2, 1/4)); | ||
}); | ||
test('greater works with negative numbers', function () { | ||
assert.isFalse(check.greater(-2, -1)); | ||
}); | ||
test('greater with positive infinity returns false', function () { | ||
assert.isFalse(check.greater(Number.POSITIVE_INFINITY, 0)); | ||
}); | ||
test('greater with NaN returns false', function () { | ||
assert.isFalse(check.greater(NaN, -1)); | ||
}); | ||
test('greater with string returns false', function () { | ||
assert.isFalse(check.greater('1', 0)); | ||
}); | ||
test('less function is defined', function () { | ||
assert.isFunction(check.less); | ||
}); | ||
test('less with 1, 0 returns false', function () { | ||
assert.isFalse(check.less(1, 0)); | ||
}); | ||
test('less with 1, 2 returns true', function () { | ||
assert.isTrue(check.less(1, 2)); | ||
}); | ||
test('less works with fractions', function () { | ||
assert.isFalse(check.less(1/2, 1/4)); | ||
}); | ||
test('less works with negative numbers', function () { | ||
assert.isTrue(check.less(-2, -1)); | ||
}); | ||
test('less with negative infinity returns false', function () { | ||
assert.isFalse(check.less(Number.NEGATIVE_INFINITY, 0)); | ||
}); | ||
test('less with NaN returns false', function () { | ||
assert.isFalse(check.less(NaN, 1)); | ||
}); | ||
test('less with string returns false', function () { | ||
assert.isFalse(check.less('-1', 0)); | ||
}); | ||
test('positive function is defined', function () { | ||
@@ -634,2 +764,26 @@ assert.isFunction(check.positive); | ||
test('zero function is defined', function () { | ||
assert.isFunction(check.zero); | ||
}); | ||
test('zero with zero returns true', function () { | ||
assert.isTrue(check.zero(0)); | ||
}); | ||
test('zero with positive integer returns false', function () { | ||
assert.isFalse(check.zero(1)); | ||
}); | ||
test('zero with negative integer returns false', function () { | ||
assert.isFalse(check.zero(-1)); | ||
}); | ||
test('zero with positive fraction returns false', function () { | ||
assert.isFalse(check.zero(0.00000001)); | ||
}); | ||
test('zero with string returns false', function () { | ||
assert.isFalse(check.zero('0')); | ||
}); | ||
test('boolean function is defined', function () { | ||
@@ -742,14 +896,2 @@ assert.isFunction(check.boolean); | ||
test('map with insufficient data throws', function() { | ||
assert.throws(function() { | ||
check.map({ foo: '' }, { foo: check.string, bar: check.string }); | ||
}); | ||
}); | ||
test('map with insufficient predicates throws', function() { | ||
assert.throws(function() { | ||
check.map({ foo: '' }, {}); | ||
}); | ||
}); | ||
test('map returns the correct results', function() { | ||
@@ -781,2 +923,16 @@ var result = | ||
test('map returns the correct results with maybe modifier', function() { | ||
var result = | ||
check.map( | ||
{ foo: null, baz: { qux: '' } }, | ||
{ foo: check.maybe.string, bar: check.maybe.string, baz: { qux: check.maybe.string } } | ||
); | ||
assert.lengthOf(Object.keys(result), 3); | ||
assert.isTrue(result.foo); | ||
assert.isTrue(result.bar); | ||
assert.isObject(result.baz); | ||
assert.lengthOf(Object.keys(result.baz), 1); | ||
assert.isTrue(result.baz.qux); | ||
}); | ||
test('all function is defined', function () { | ||
@@ -851,3 +1007,3 @@ assert.isFunction(check.all); | ||
test('assert modifier is defined', function() { | ||
assert.isObject(check.assert); | ||
assert.isFunction(check.assert); | ||
}); | ||
@@ -863,7 +1019,10 @@ | ||
assert.isFunction(check.assert.assigned); | ||
assert.isFunction(check.assert.length); | ||
assert.isFunction(check.assert.hasLength); | ||
assert.isFunction(check.assert.emptyArray); | ||
assert.isFunction(check.assert.array); | ||
assert.isFunction(check.assert.date); | ||
assert.isFunction(check.assert.error); | ||
assert.isFunction(check.assert.function); | ||
assert.isFunction(check.assert.webUrl); | ||
assert.isFunction(check.assert.match); | ||
assert.isFunction(check.assert.contains); | ||
assert.isFunction(check.assert.unemptyString); | ||
@@ -873,4 +1032,8 @@ assert.isFunction(check.assert.string); | ||
assert.isFunction(check.assert.even); | ||
assert.isFunction(check.assert.between); | ||
assert.isFunction(check.assert.greater); | ||
assert.isFunction(check.assert.less); | ||
assert.isFunction(check.assert.positive); | ||
assert.isFunction(check.assert.negative); | ||
assert.isFunction(check.assert.zero); | ||
assert.isFunction(check.assert.integer); | ||
@@ -883,3 +1046,3 @@ assert.isFunction(check.assert.number); | ||
assert.isUndefined(check.assert.map); | ||
assert.isUndefined(check.assert.apply); | ||
assert.strictEqual(check.assert.apply, Function.apply); | ||
assert.isUndefined(check.assert.all); | ||
@@ -895,3 +1058,3 @@ assert.isUndefined(check.assert.any); | ||
assert.isObject(check.assert.not); | ||
assert.lengthOf(Object.keys(check.assert.not), 21); | ||
assert.lengthOf(Object.keys(check.assert.not), 28); | ||
}); | ||
@@ -901,3 +1064,3 @@ | ||
assert.isObject(check.assert.maybe); | ||
assert.lengthOf(Object.keys(check.assert.maybe), 21); | ||
assert.lengthOf(Object.keys(check.assert.maybe), 28); | ||
}); | ||
@@ -907,7 +1070,7 @@ | ||
assert.isObject(check.assert.either); | ||
assert.lengthOf(Object.keys(check.assert.either), 21); | ||
assert.lengthOf(Object.keys(check.assert.either), 28); | ||
}); | ||
test('assert modifier has correct number of keys', function () { | ||
assert.lengthOf(Object.keys(check.assert), 24); | ||
assert.lengthOf(Object.keys(check.assert), 31); | ||
}); | ||
@@ -959,4 +1122,14 @@ | ||
test('assert modifier runs standalone', function () { | ||
assert.doesNotThrow(function () { | ||
check.assert(true); | ||
}); | ||
assert.throws(function () { | ||
check.assert(false); | ||
}); | ||
}); | ||
test('not modifier is defined', function () { | ||
assert.isObject(check.not); | ||
assert.isFunction(check.not); | ||
}); | ||
@@ -981,3 +1154,3 @@ | ||
test('not modifier has correct number of keys', function () { | ||
assert.lengthOf(Object.keys(check.not), 21); | ||
assert.lengthOf(Object.keys(check.not), 28); | ||
}); | ||
@@ -993,4 +1166,9 @@ | ||
test('not modifier runs standalone', function () { | ||
assert.isFalse(check.not(true)); | ||
assert.isTrue(check.not(false)); | ||
}); | ||
test('maybe modifier is defined', function () { | ||
assert.isObject(check.maybe); | ||
assert.isFunction(check.maybe); | ||
}); | ||
@@ -1015,3 +1193,3 @@ | ||
test('maybe modifier has correct number of keys', function () { | ||
assert.lengthOf(Object.keys(check.maybe), 21); | ||
assert.lengthOf(Object.keys(check.maybe), 28); | ||
}); | ||
@@ -1036,2 +1214,8 @@ | ||
test('maybe modifier runs standalone', function () { | ||
assert.isTrue(check.maybe(null)); | ||
assert.isTrue(check.maybe(undefined)); | ||
assert.isFalse(check.maybe(false)); | ||
}); | ||
test('either modifier is defined', function () { | ||
@@ -1058,3 +1242,3 @@ assert.isObject(check.either); | ||
test('either modifier has correct number of keys', function () { | ||
assert.lengthOf(Object.keys(check.either), 21); | ||
assert.lengthOf(Object.keys(check.either), 28); | ||
}); | ||
@@ -1065,3 +1249,3 @@ | ||
assert.isObject(check.either.string('').or); | ||
assert.lengthOf(Object.keys(check.either.string('').or), 21); | ||
assert.lengthOf(Object.keys(check.either.string('').or), 28); | ||
}); | ||
@@ -1068,0 +1252,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87585
1670
631