From 25f527684c763c4447cf9777b52d103ef7f90c44 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 6 Apr 2016 11:39:36 -0400 Subject: [PATCH] feat(src): refactored most of the source --- dist/check-more-types.js | 239 +++++++++++++++-------------------- dist/check-more-types.min.js | 2 +- package.json | 2 +- src/arrays.js | 5 +- src/git.js | 4 +- src/index.js | 61 ++------- src/internet.js | 8 +- src/logic.js | 10 +- src/low-level.js | 52 ++++---- src/mid-level.js | 15 ++- src/schema.js | 12 +- 11 files changed, 168 insertions(+), 242 deletions(-) diff --git a/dist/check-more-types.js b/dist/check-more-types.js index 614c181..b5bd149 100644 --- a/dist/check-more-types.js +++ b/dist/check-more-types.js @@ -69,8 +69,7 @@ return /******/ (function(modules) { // webpackBootstrap throw new Error('Missing Function.prototype.bind, please load es5-shim first') } - var curry2 = __webpack_require__(1).curry2 - var low = __webpack_require__(2) + var low = __webpack_require__(1) var mid = __webpack_require__(3) var arrays = __webpack_require__(5) var logic = __webpack_require__(6) @@ -131,13 +130,13 @@ return /******/ (function(modules) { // webpackBootstrap /** Adds new predicate to all objects @method mixin */ check.mixin = function mixin (fn, name) { - if (low.isString(fn) && low.isFn(name)) { + if (low.string(fn) && low.fn(name)) { var tmp = fn fn = name name = tmp } - if (!low.isFn(fn)) { + if (!low.fn(fn)) { throw new Error('expected predicate function for name ' + name) } if (!low.unemptyString(name)) { @@ -148,13 +147,13 @@ return /******/ (function(modules) { // webpackBootstrap } function registerPredicate (obj, name, fn) { - if (!low.isObject(obj)) { + if (!low.object(obj)) { throw new Error('missing object ' + obj) } if (!low.unemptyString(name)) { throw new Error('missing name') } - if (!low.isFn(fn)) { + if (!low.fn(fn)) { throw new Error('missing function') } @@ -204,11 +203,11 @@ return /******/ (function(modules) { // webpackBootstrap } var promiseSchema = { - then: low.isFn + then: low.fn } // work around reserved keywords checks - promiseSchema['catch'] = low.isFn + promiseSchema['catch'] = low.fn var hasPromiseApi = schema.schema.bind(null, promiseSchema) @@ -220,54 +219,10 @@ return /******/ (function(modules) { // webpackBootstrap return check.object(p) && hasPromiseApi(p) } - // TODO just mix in all low and mid level predicates // new predicates to be added to check object. Use object to preserve names var predicates = { - nulled: low.isNull, - fn: low.isFn, - string: low.isString, - unemptyString: low.unemptyString, - object: low.isObject, - number: low.isNumber, array: Array.isArray, - positiveNumber: low.positiveNumber, - negativeNumber: low.negativeNumber, - // a couple of aliases - positive: low.positiveNumber, - negative: low.negativeNumber, - defined: low.defined, - same: low.same, - allSame: mid.allSame, - bit: low.bit, - bool: low.bool, - has: low.has, - lowerCase: low.lowerCase, - raises: mid.raises, - empty: low.empty, - found: mid.found, - emptyString: low.emptyString, - unempty: low.unempty, - unit: mid.unit, - hexRgb: mid.hexRgb, - sameLength: mid.sameLength, - index: mid.index, - oneOf: curry2(mid.oneOf, true), - promise: isPromise, - validDate: low.validDate, - equal: curry2(low.equal), - primitive: low.primitive, - zero: low.zero, - date: low.isDate, - regexp: low.isRegExp, - instance: low.instance, - emptyObject: low.isEmptyObject, - length: curry2(low.hasLength), - floatNumber: low.isFloat, - intNumber: low.isInteger, - startsWith: mid.startsWith, - contains: mid.contains, - error: low.isError, - type: curry2(mid.type) + promise: isPromise } function mixCollection (collection) { @@ -276,7 +231,7 @@ return /******/ (function(modules) { // webpackBootstrap }) } - [predicates, git, internet, arrays, logic, schema].forEach(mixCollection) + [low, mid, predicates, git, internet, arrays, logic, schema].forEach(mixCollection) check.VERSION = '{{ packageVersion }}' @@ -285,36 +240,11 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }, /* 1 */ -/***/ function(module, exports) { +/***/ function(module, exports, __webpack_require__) { 'use strict' - // utility methods - function curry2 (fn, strict2) { - return function curried (a) { - if (strict2 && arguments.length > 2) { - throw new Error('Curry2 function ' + fn.name + - ' called with too many arguments ' + arguments.length) - } - if (arguments.length === 2) { - return fn(arguments[0], arguments[1]) - } - return function second (b) { - return fn(a, b) - } - } - } - - module.exports = { - curry2: curry2 - } - - -/***/ }, -/* 2 */ -/***/ function(module, exports) { - - 'use strict' + var curry2 = __webpack_require__(2).curry2 // low level predicates @@ -518,36 +448,65 @@ return /******/ (function(modules) { // webpackBootstrap } module.exports = { - isFn: isFn, - isString: isString, - isObject: isObject, - isNull: isNull, - unemptyString: unemptyString, - isEmptyObject: isEmptyObject, - isInteger: isInteger, - isFloat: isFloat, - positiveNumber: positiveNumber, - negativeNumber: negativeNumber, - isRegExp: isRegExp, - isError: isError, - instance: instance, - hasLength: hasLength, - isNumber: isNumber, - isDate: isDate, - defined: defined, - validDate: validDate, - primitive: primitive, - zero: zero, - same: same, bit: bit, bool: bool, + date: isDate, + defined: defined, + empty: empty, + emptyObject: isEmptyObject, + emptyString: emptyString, + equal: curry2(equal), + error: isError, + floatNumber: isFloat, + fn: isFn, has: has, + instance: instance, + intNumber: isInteger, isArray: isArray, + length: curry2(hasLength), lowerCase: lowerCase, - empty: empty, - emptyString: emptyString, + negative: negativeNumber, + negativeNumber: negativeNumber, + nulled: isNull, + number: isNumber, + object: isObject, + positive: positiveNumber, + positiveNumber: positiveNumber, + primitive: primitive, + regexp: isRegExp, + same: same, + string: isString, unempty: unempty, - equal: equal + unemptyString: unemptyString, + validDate: validDate, + zero: zero + } + + +/***/ }, +/* 2 */ +/***/ function(module, exports) { + + 'use strict' + + // utility methods + function curry2 (fn, strict2) { + return function curried (a) { + if (strict2 && arguments.length > 2) { + throw new Error('Curry2 function ' + fn.name + + ' called with too many arguments ' + arguments.length) + } + if (arguments.length === 2) { + return fn(arguments[0], arguments[1]) + } + return function second (b) { + return fn(a, b) + } + } + } + + module.exports = { + curry2: curry2 } @@ -557,8 +516,9 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict' - var low = __webpack_require__(2) + var low = __webpack_require__(1) var verify = __webpack_require__(4) + var curry2 = __webpack_require__(2).curry2 /** Checks if the given index is valid in an array or string or -1 @@ -570,8 +530,8 @@ return /******/ (function(modules) { // webpackBootstrap } function startsWith (prefix, x) { - return low.isString(prefix) && - low.isString(x) && + return low.string(prefix) && + low.string(x) && x.indexOf(prefix) === 0 } @@ -660,7 +620,7 @@ return /******/ (function(modules) { // webpackBootstrap @method unit */ function unit (value) { - return low.isNumber(value) && + return low.number(value) && value >= 0.0 && value <= 1.0 } @@ -670,7 +630,7 @@ return /******/ (function(modules) { // webpackBootstrap @method hexRgb */ function hexRgb (value) { - return low.isString(value) && + return low.string(value) && rgb.test(value) } @@ -679,7 +639,7 @@ return /******/ (function(modules) { // webpackBootstrap @method raises */ function raises (fn, errorValidator) { - verify(low.isFn(fn), 'expected function that raises') + verify(low.fn(fn), 'expected function that raises') try { fn() } catch (err) { @@ -699,9 +659,9 @@ return /******/ (function(modules) { // webpackBootstrap found: found, startsWith: startsWith, contains: contains, - type: type, + type: curry2(type), index: index, - oneOf: oneOf, + oneOf: curry2(oneOf, true), sameLength: sameLength, allSame: allSame, unit: unit, @@ -716,7 +676,7 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict' - var low = __webpack_require__(2) + var low = __webpack_require__(1) /** * Public modifier `verify`. @@ -743,8 +703,11 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict' - var low = __webpack_require__(2) + var low = __webpack_require__(1) var logic = __webpack_require__(6) + var verify = __webpack_require__(4) + + verify(low.fn(low.isArray), 'missing low level isArray') /** Returns true if the argument is an array with at least one value @@ -784,7 +747,7 @@ return /******/ (function(modules) { // webpackBootstrap @param checkLowerCase Checks if all strings are lowercase */ function arrayOfStrings (a, checkLowerCase) { - var v = low.isArray(a) && a.every(low.isString) + var v = low.isArray(a) && a.every(low.string) if (v && low.bool(checkLowerCase) && checkLowerCase) { return a.every(low.lowerCase) } @@ -818,7 +781,7 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict' - var low = __webpack_require__(2) + var low = __webpack_require__(1) /** Combines multiple predicate functions to produce new OR predicate @@ -834,7 +797,7 @@ return /******/ (function(modules) { // webpackBootstrap var values = Array.prototype.slice.call(arguments, 0) return predicates.some(function (predicate) { try { - return low.isFn(predicate) ? predicate.apply(null, values) : Boolean(predicate) + return low.fn(predicate) ? predicate.apply(null, values) : Boolean(predicate) } catch (err) { // treat exceptions as false return false @@ -856,7 +819,7 @@ return /******/ (function(modules) { // webpackBootstrap return function orCheck () { var values = Array.prototype.slice.call(arguments, 0) return predicates.every(function (predicate) { - return low.isFn(predicate) ? predicate.apply(null, values) : Boolean(predicate) + return low.fn(predicate) ? predicate.apply(null, values) : Boolean(predicate) }) } } @@ -879,7 +842,7 @@ return /******/ (function(modules) { // webpackBootstrap if (predicateResults.hasOwnProperty(property)) { value = predicateResults[property] - if (low.isObject(value) && every(value) === false) { + if (low.object(value) && every(value) === false) { return false } @@ -899,9 +862,9 @@ return /******/ (function(modules) { // webpackBootstrap if (predicates.hasOwnProperty(property)) { predicate = predicates[property] - if (low.isFn(predicate)) { + if (low.fn(predicate)) { result[property] = predicate(things[property]) - } else if (low.isObject(predicate)) { + } else if (low.object(predicate)) { result[property] = map(things[property], predicate) } } @@ -925,7 +888,7 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict' - var low = __webpack_require__(2) + var low = __webpack_require__(1) // functions that deal with git information @@ -958,7 +921,7 @@ return /******/ (function(modules) { // webpackBootstrap @method commitId */ function commitId (id) { - return low.isString(id) && + return low.string(id) && id.length === 40 && shaReg.test(id) } @@ -971,7 +934,7 @@ return /******/ (function(modules) { // webpackBootstrap @method shortCommitId */ function shortCommitId (id) { - return low.isString(id) && + return low.string(id) && id.length === 7 && shortShaReg.test(id) } @@ -990,22 +953,22 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict' - var low = __webpack_require__(2) + var low = __webpack_require__(1) var mid = __webpack_require__(3) var startsWithHttp = mid.startsWith.bind(null, 'http://') var startsWithHttps = mid.startsWith.bind(null, 'https://') function http (x) { - return low.isString(x) && startsWithHttp(x) + return low.string(x) && startsWithHttp(x) } function https (x) { - return low.isString(x) && startsWithHttps(x) + return low.string(x) && startsWithHttps(x) } function webUrl (x) { - return low.isString(x) && + return low.string(x) && (startsWithHttp(x) || startsWithHttps(x)) } @@ -1026,7 +989,7 @@ return /******/ (function(modules) { // webpackBootstrap @method email */ function email (s) { - return low.isString(s) && + return low.string(s) && /^.+@.+\..+$/.test(s) } @@ -1049,12 +1012,15 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict' - var curry2 = __webpack_require__(1).curry2 - var low = __webpack_require__(2) + var curry2 = __webpack_require__(2).curry2 + var low = __webpack_require__(1) var verify = __webpack_require__(4) var every = __webpack_require__(6).every var map = __webpack_require__(6).map + verify(low.fn(every), 'missing check.every method') + verify(low.fn(map), 'missing check.map method') + /** Checks if object passes all rules in predicates. @@ -1070,14 +1036,11 @@ return /******/ (function(modules) { // webpackBootstrap @returns true or false */ function all (obj, predicates) { - // verify.fn(low.isFn(check.every, 'missing check.every method') - // check.verify.fn(check.map, 'missing check.map method') - - verify(low.isObject(obj), 'missing object to check') - verify(low.isObject(predicates), 'missing predicates object') + verify(low.object(obj), 'missing object to check') + verify(low.object(predicates), 'missing predicates object') Object.keys(predicates).forEach(function (property) { - if (!low.isFn(predicates[property])) { + if (!low.fn(predicates[property])) { throw new Error('not a predicate function for ' + property + ' but ' + predicates[property]) } }) diff --git a/dist/check-more-types.min.js b/dist/check-more-types.min.js index 9297ffb..bb8f36a 100644 --- a/dist/check-more-types.min.js +++ b/dist/check-more-types.min.js @@ -2,4 +2,4 @@ homepage: https://github.com/kensho/check-more-types Copyright @ 2014 Kensho license: MIT */ -!function(a,b){"object"==typeof exports&&"object"==typeof module?module.exports=b():"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?exports.check=b():a.check=b()}(this,function(){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={exports:{},id:d,loaded:!1};return a[d].call(e.exports,e,e.exports,b),e.loaded=!0,e.exports}var c={};return b.m=a,b.c=c,b.p="",b(0)}([function(a,b,c){"use strict";function d(a){return n.object(a)&&q(a)}function e(a){Object.keys(a).forEach(function(b){n.mixin(a[b],b)})}if("function"!=typeof Function.prototype.bind)throw new Error("Missing Function.prototype.bind, please load es5-shim first");var f=c(1).curry2,g=c(2),h=c(3),i=c(5),j=c(6),k=c(7),l=c(8),m=c(9),n={maybe:{},verify:{},not:{},every:j.every,map:j.map};if(!n.defend){var o=function(a,b,c){n.verify.fn(a,"expected a function"),n.verify.array(b,"expected list of predicates"),n.verify.defined(c,"missing args");var d=0,e=0,f=b.length;for(d=0;f>d;d+=1){var h=b[d];if(n.fn(h)){if(!h(c[e])){var i="Argument "+(e+1)+": "+c[e]+" does not pass predicate";throw g.unemptyString(b[d+1])&&(i+=": "+b[d+1]),new Error(i)}e+=1}}return a.apply(null,c)};n.defend=function(a){var b=Array.prototype.slice.call(arguments,1);return function(){return o(a,b,arguments)}}}n.mixin||(n.mixin=function(a,b){function d(a,b,c){if(!g.isObject(a))throw new Error("missing object "+a);if(!g.unemptyString(b))throw new Error("missing name");if(!g.isFn(c))throw new Error("missing function");a[b]||(a[b]=c)}function e(a){return function(){return!n.defined(arguments[0])||n.nulled(arguments[0])?!0:a.apply(null,arguments)}}if(g.isString(a)&&g.isFn(b)){var f=a;a=b,b=f}if(!g.isFn(a))throw new Error("expected predicate function for name "+b);if(g.unemptyString(b)||(b=a.name),!g.unemptyString(b))throw new Error("predicate function missing name\n"+a.toString());var h=c(4);d(n,b,a),d(n.maybe,b,e(a)),d(n.not,b,j.notModifier(a)),d(n.verify,b,h(a,b+" failed"))}),n.then||(n.then=function(a,b){return function(){var c="function"==typeof a?a.apply(null,arguments):a;return c?b.apply(null,arguments):void 0}});var p={then:g.isFn};p["catch"]=g.isFn;var q=m.schema.bind(null,p),r={nulled:g.isNull,fn:g.isFn,string:g.isString,unemptyString:g.unemptyString,object:g.isObject,number:g.isNumber,array:Array.isArray,positiveNumber:g.positiveNumber,negativeNumber:g.negativeNumber,positive:g.positiveNumber,negative:g.negativeNumber,defined:g.defined,same:g.same,allSame:h.allSame,bit:g.bit,bool:g.bool,has:g.has,lowerCase:g.lowerCase,raises:h.raises,empty:g.empty,found:h.found,emptyString:g.emptyString,unempty:g.unempty,unit:h.unit,hexRgb:h.hexRgb,sameLength:h.sameLength,index:h.index,oneOf:f(h.oneOf,!0),promise:d,validDate:g.validDate,equal:f(g.equal),primitive:g.primitive,zero:g.zero,date:g.isDate,regexp:g.isRegExp,instance:g.instance,emptyObject:g.isEmptyObject,length:f(g.hasLength),floatNumber:g.isFloat,intNumber:g.isInteger,startsWith:h.startsWith,contains:h.contains,error:g.isError,type:f(h.type)};[r,k,l,i,j,m].forEach(e),n.VERSION="{{ packageVersion }}",a.exports=n},function(a,b){"use strict";function c(a,b){return function(c){if(b&&arguments.length>2)throw new Error("Curry2 function "+a.name+" called with too many arguments "+arguments.length);return 2===arguments.length?a(arguments[0],arguments[1]):function(b){return a(c,b)}}}a.exports={curry2:c}},function(a,b){"use strict";function c(a){return"function"==typeof a}function d(a){return"string"==typeof a}function e(a){return d(a)&&Boolean(a)}function f(a){return"object"==typeof a&&!F(a)&&!k(a)&&!n(a)}function g(a){return f(a)&&0===Object.keys(a).length}function h(a){return"number"==typeof a&&!isNaN(a)&&a!==1/0&&a!==-(1/0)}function i(a){return h(a)&&a%1===0}function j(a){return h(a)&&a%1!==0}function k(a){return null===a}function l(a){return h(a)&&a>0}function m(a){return h(a)&&0>a}function n(a){return a instanceof Date}function o(a){return a instanceof RegExp}function p(a){return a instanceof Error}function q(a,b){return a instanceof b}function r(a,b){return"number"==typeof a&&"number"!=typeof b?r(b,a):(F(a)||d(a))&&a.length===b}function s(a){return"undefined"!=typeof a}function t(a){return n(a)&&h(Number(a))}function u(a){var b=typeof a;return"number"===b||"boolean"===b||"string"===b||"symbol"===b}function v(a){return"number"==typeof a&&0===a}function w(a,b){return a===b}function x(a){return 0===a||1===a}function y(a){return"boolean"==typeof a}function z(a){return d(a)&&a.toLowerCase()===a}function A(a,b){if(2!==arguments.length)throw new Error("Expected two arguments to check.has, got only "+arguments.length);return Boolean(a&&b&&"string"==typeof b&&"undefined"!=typeof a[b])}function B(a){return""===a}function C(a){var b="string"==typeof a||Array.isArray(a);return b?!a.length:a instanceof Object?!Object.keys(a).length:!1}function D(a){var b="string"==typeof a||Array.isArray(a);return b?a.length:a instanceof Object?Object.keys(a).length:!0}function E(a,b){return a===b}var F=Array.isArray;a.exports={isFn:c,isString:d,isObject:f,isNull:k,unemptyString:e,isEmptyObject:g,isInteger:i,isFloat:j,positiveNumber:l,negativeNumber:m,isRegExp:o,isError:p,instance:q,hasLength:r,isNumber:h,isDate:n,defined:s,validDate:t,primitive:u,zero:v,same:w,bit:x,bool:y,has:A,isArray:F,lowerCase:z,empty:C,emptyString:B,unempty:D,equal:E}},function(a,b,c){"use strict";function d(a){return a>=0}function e(a,b){return o.isString(a)&&o.isString(b)&&0===b.indexOf(a)}function f(a,b){if(Array.isArray(a))return-1!==a.indexOf(b);if("string"==typeof a){if("string"!=typeof b)throw new Error("Contains in string should search for string also "+b);return-1!==a.indexOf(b)}return!1}function g(a,b){return typeof b===a}function h(a,b){return o.defined(a)&&o.has(a,"length")&&b>=0&&b=0&&1>=a}function m(a){return o.isString(a)&&q.test(a)}function n(a,b){p(o.isFn(a),"expected function that raises");try{a()}catch(c){return"undefined"==typeof b?!0:"function"==typeof b?b(c):!1}return!1}var o=c(2),p=c(4),q=/^#(?:[0-9a-fA-F]{3}){1,2}$/;a.exports={found:d,startsWith:e,contains:f,type:g,index:h,oneOf:k,sameLength:i,allSame:j,unit:l,hexRgb:m,raises:n}},function(a,b,c){"use strict";function d(a,b){return function(){var c;if(a.apply(null,arguments)===!1)throw c=arguments[arguments.length-1],new Error(e.unemptyString(c)?c:b)}}var e=c(2);a.exports=d},function(a,b,c){"use strict";function d(a){return i.isArray(a)&&a.length>0}function e(a,b){return i.isArray(b)&&b.every(a)}function f(a,b){if(!i.isArray(b))throw new Error("expected array to find bad items");return b.filter(j.notModifier(a))}function g(a,b){var c=i.isArray(a)&&a.every(i.isString);return c&&i.bool(b)&&b?a.every(i.lowerCase):c}function h(a,b){return i.isArray(a)&&a.every(function(a){return g(a,b)})}var i=c(2),j=c(6);a.exports={arrayOf:e,arrayOfArraysOfStrings:h,arrayOfStrings:g,badItems:f,unemptyArray:d}},function(a,b,c){"use strict";function d(){var a=Array.prototype.slice.call(arguments,0);if(!a.length)throw new Error("empty list of arguments to or");return function(){var b=Array.prototype.slice.call(arguments,0);return a.some(function(a){try{return i.isFn(a)?a.apply(null,b):Boolean(a)}catch(c){return!1}})}}function e(){var a=Array.prototype.slice.call(arguments,0);if(!a.length)throw new Error("empty list of arguments to or");return function(){var b=Array.prototype.slice.call(arguments,0);return a.every(function(a){return i.isFn(a)?a.apply(null,b):Boolean(a)})}}function f(a){return function(){return!a.apply(null,arguments)}}function g(a){var b,c;for(b in a)if(a.hasOwnProperty(b)){if(c=a[b],i.isObject(c)&&g(c)===!1)return!1;if(c===!1)return!1}return!0}function h(a,b){var c,d,e={};for(c in b)b.hasOwnProperty(c)&&(d=b[c],i.isFn(d)?e[c]=d(a[c]):i.isObject(d)&&(e[c]=h(a[c],d)));return e}var i=c(2);a.exports={or:d,and:e,notModifier:f,every:g,map:h}},function(a,b,c){"use strict";function d(a){return h.unemptyString(a)&&/^\d+\.\d+\.\d+$/.test(a)}function e(a){return h.unemptyString(a)&&/^git@/.test(a)}function f(a){return h.isString(a)&&40===a.length&&i.test(a)}function g(a){return h.isString(a)&&7===a.length&&j.test(a)}var h=c(2),i=/^[0-9a-f]{40}$/,j=/^[0-9a-f]{7}$/;a.exports={semver:d,git:e,commitId:f,shortCommitId:g}},function(a,b,c){"use strict";function d(a){return k.isString(a)&&m(a)}function e(a){return k.isString(a)&&n(a)}function f(a){return k.isString(a)&&(m(a)||n(a))}function g(a){return k.positiveNumber(a)&&65535>=a}function h(a){return k.positiveNumber(a)&&1024>=a}function i(a){return g(a)&&a>1024}function j(a){return k.isString(a)&&/^.+@.+\..+$/.test(a)}var k=c(2),l=c(3),m=l.startsWith.bind(null,"http://"),n=l.startsWith.bind(null,"https://");a.exports={email:j,http:d,https:e,port:g,secure:e,systemPort:h,url:f,userPort:i,webUrl:f}},function(a,b,c){"use strict";function d(a,b){return h(g.isObject(a),"missing object to check"),h(g.isObject(b),"missing predicates object"),Object.keys(b).forEach(function(a){if(!g.isFn(b[a]))throw new Error("not a predicate function for "+a+" but "+b[a])}),i(j(a,b))}function e(a,b){return d(b,a)}var f=c(1).curry2,g=c(2),h=c(4),i=c(6).every,j=c(6).map;a.exports={all:d,schema:f(e)}}])}); \ No newline at end of file +!function(a,b){"object"==typeof exports&&"object"==typeof module?module.exports=b():"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?exports.check=b():a.check=b()}(this,function(){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={exports:{},id:d,loaded:!1};return a[d].call(e.exports,e,e.exports,b),e.loaded=!0,e.exports}var c={};return b.m=a,b.c=c,b.p="",b(0)}([function(a,b,c){"use strict";function d(a){return m.object(a)&&p(a)}function e(a){Object.keys(a).forEach(function(b){m.mixin(a[b],b)})}if("function"!=typeof Function.prototype.bind)throw new Error("Missing Function.prototype.bind, please load es5-shim first");var f=c(1),g=c(3),h=c(5),i=c(6),j=c(7),k=c(8),l=c(9),m={maybe:{},verify:{},not:{},every:i.every,map:i.map};if(!m.defend){var n=function(a,b,c){m.verify.fn(a,"expected a function"),m.verify.array(b,"expected list of predicates"),m.verify.defined(c,"missing args");var d=0,e=0,g=b.length;for(d=0;g>d;d+=1){var h=b[d];if(m.fn(h)){if(!h(c[e])){var i="Argument "+(e+1)+": "+c[e]+" does not pass predicate";throw f.unemptyString(b[d+1])&&(i+=": "+b[d+1]),new Error(i)}e+=1}}return a.apply(null,c)};m.defend=function(a){var b=Array.prototype.slice.call(arguments,1);return function(){return n(a,b,arguments)}}}m.mixin||(m.mixin=function(a,b){function d(a,b,c){if(!f.object(a))throw new Error("missing object "+a);if(!f.unemptyString(b))throw new Error("missing name");if(!f.fn(c))throw new Error("missing function");a[b]||(a[b]=c)}function e(a){return function(){return!m.defined(arguments[0])||m.nulled(arguments[0])?!0:a.apply(null,arguments)}}if(f.string(a)&&f.fn(b)){var g=a;a=b,b=g}if(!f.fn(a))throw new Error("expected predicate function for name "+b);if(f.unemptyString(b)||(b=a.name),!f.unemptyString(b))throw new Error("predicate function missing name\n"+a.toString());var h=c(4);d(m,b,a),d(m.maybe,b,e(a)),d(m.not,b,i.notModifier(a)),d(m.verify,b,h(a,b+" failed"))}),m.then||(m.then=function(a,b){return function(){var c="function"==typeof a?a.apply(null,arguments):a;return c?b.apply(null,arguments):void 0}});var o={then:f.fn};o["catch"]=f.fn;var p=l.schema.bind(null,o),q={array:Array.isArray,promise:d};[f,g,q,j,k,h,i,l].forEach(e),m.VERSION="{{ packageVersion }}",a.exports=m},function(a,b,c){"use strict";function d(a){return"function"==typeof a}function e(a){return"string"==typeof a}function f(a){return e(a)&&Boolean(a)}function g(a){return"object"==typeof a&&!H(a)&&!l(a)&&!o(a)}function h(a){return g(a)&&0===Object.keys(a).length}function i(a){return"number"==typeof a&&!isNaN(a)&&a!==1/0&&a!==-(1/0)}function j(a){return i(a)&&a%1===0}function k(a){return i(a)&&a%1!==0}function l(a){return null===a}function m(a){return i(a)&&a>0}function n(a){return i(a)&&0>a}function o(a){return a instanceof Date}function p(a){return a instanceof RegExp}function q(a){return a instanceof Error}function r(a,b){return a instanceof b}function s(a,b){return"number"==typeof a&&"number"!=typeof b?s(b,a):(H(a)||e(a))&&a.length===b}function t(a){return"undefined"!=typeof a}function u(a){return o(a)&&i(Number(a))}function v(a){var b=typeof a;return"number"===b||"boolean"===b||"string"===b||"symbol"===b}function w(a){return"number"==typeof a&&0===a}function x(a,b){return a===b}function y(a){return 0===a||1===a}function z(a){return"boolean"==typeof a}function A(a){return e(a)&&a.toLowerCase()===a}function B(a,b){if(2!==arguments.length)throw new Error("Expected two arguments to check.has, got only "+arguments.length);return Boolean(a&&b&&"string"==typeof b&&"undefined"!=typeof a[b])}function C(a){return""===a}function D(a){var b="string"==typeof a||Array.isArray(a);return b?!a.length:a instanceof Object?!Object.keys(a).length:!1}function E(a){var b="string"==typeof a||Array.isArray(a);return b?a.length:a instanceof Object?Object.keys(a).length:!0}function F(a,b){return a===b}var G=c(2).curry2,H=Array.isArray;a.exports={bit:y,bool:z,date:o,defined:t,empty:D,emptyObject:h,emptyString:C,equal:G(F),error:q,floatNumber:k,fn:d,has:B,instance:r,intNumber:j,isArray:H,length:G(s),lowerCase:A,negative:n,negativeNumber:n,nulled:l,number:i,object:g,positive:m,positiveNumber:m,primitive:v,regexp:p,same:x,string:e,unempty:E,unemptyString:f,validDate:u,zero:w}},function(a,b){"use strict";function c(a,b){return function(c){if(b&&arguments.length>2)throw new Error("Curry2 function "+a.name+" called with too many arguments "+arguments.length);return 2===arguments.length?a(arguments[0],arguments[1]):function(b){return a(c,b)}}}a.exports={curry2:c}},function(a,b,c){"use strict";function d(a){return a>=0}function e(a,b){return o.string(a)&&o.string(b)&&0===b.indexOf(a)}function f(a,b){if(Array.isArray(a))return-1!==a.indexOf(b);if("string"==typeof a){if("string"!=typeof b)throw new Error("Contains in string should search for string also "+b);return-1!==a.indexOf(b)}return!1}function g(a,b){return typeof b===a}function h(a,b){return o.defined(a)&&o.has(a,"length")&&b>=0&&b=0&&1>=a}function m(a){return o.string(a)&&r.test(a)}function n(a,b){p(o.fn(a),"expected function that raises");try{a()}catch(c){return"undefined"==typeof b?!0:"function"==typeof b?b(c):!1}return!1}var o=c(1),p=c(4),q=c(2).curry2,r=/^#(?:[0-9a-fA-F]{3}){1,2}$/;a.exports={found:d,startsWith:e,contains:f,type:q(g),index:h,oneOf:q(k,!0),sameLength:i,allSame:j,unit:l,hexRgb:m,raises:n}},function(a,b,c){"use strict";function d(a,b){return function(){var c;if(a.apply(null,arguments)===!1)throw c=arguments[arguments.length-1],new Error(e.unemptyString(c)?c:b)}}var e=c(1);a.exports=d},function(a,b,c){"use strict";function d(a){return i.isArray(a)&&a.length>0}function e(a,b){return i.isArray(b)&&b.every(a)}function f(a,b){if(!i.isArray(b))throw new Error("expected array to find bad items");return b.filter(j.notModifier(a))}function g(a,b){var c=i.isArray(a)&&a.every(i.string);return c&&i.bool(b)&&b?a.every(i.lowerCase):c}function h(a,b){return i.isArray(a)&&a.every(function(a){return g(a,b)})}var i=c(1),j=c(6),k=c(4);k(i.fn(i.isArray),"missing low level isArray"),a.exports={arrayOf:e,arrayOfArraysOfStrings:h,arrayOfStrings:g,badItems:f,unemptyArray:d}},function(a,b,c){"use strict";function d(){var a=Array.prototype.slice.call(arguments,0);if(!a.length)throw new Error("empty list of arguments to or");return function(){var b=Array.prototype.slice.call(arguments,0);return a.some(function(a){try{return i.fn(a)?a.apply(null,b):Boolean(a)}catch(c){return!1}})}}function e(){var a=Array.prototype.slice.call(arguments,0);if(!a.length)throw new Error("empty list of arguments to or");return function(){var b=Array.prototype.slice.call(arguments,0);return a.every(function(a){return i.fn(a)?a.apply(null,b):Boolean(a)})}}function f(a){return function(){return!a.apply(null,arguments)}}function g(a){var b,c;for(b in a)if(a.hasOwnProperty(b)){if(c=a[b],i.object(c)&&g(c)===!1)return!1;if(c===!1)return!1}return!0}function h(a,b){var c,d,e={};for(c in b)b.hasOwnProperty(c)&&(d=b[c],i.fn(d)?e[c]=d(a[c]):i.object(d)&&(e[c]=h(a[c],d)));return e}var i=c(1);a.exports={or:d,and:e,notModifier:f,every:g,map:h}},function(a,b,c){"use strict";function d(a){return h.unemptyString(a)&&/^\d+\.\d+\.\d+$/.test(a)}function e(a){return h.unemptyString(a)&&/^git@/.test(a)}function f(a){return h.string(a)&&40===a.length&&i.test(a)}function g(a){return h.string(a)&&7===a.length&&j.test(a)}var h=c(1),i=/^[0-9a-f]{40}$/,j=/^[0-9a-f]{7}$/;a.exports={semver:d,git:e,commitId:f,shortCommitId:g}},function(a,b,c){"use strict";function d(a){return k.string(a)&&m(a)}function e(a){return k.string(a)&&n(a)}function f(a){return k.string(a)&&(m(a)||n(a))}function g(a){return k.positiveNumber(a)&&65535>=a}function h(a){return k.positiveNumber(a)&&1024>=a}function i(a){return g(a)&&a>1024}function j(a){return k.string(a)&&/^.+@.+\..+$/.test(a)}var k=c(1),l=c(3),m=l.startsWith.bind(null,"http://"),n=l.startsWith.bind(null,"https://");a.exports={email:j,http:d,https:e,port:g,secure:e,systemPort:h,url:f,userPort:i,webUrl:f}},function(a,b,c){"use strict";function d(a,b){return h(g.object(a),"missing object to check"),h(g.object(b),"missing predicates object"),Object.keys(b).forEach(function(a){if(!g.fn(b[a]))throw new Error("not a predicate function for "+a+" but "+b[a])}),i(j(a,b))}function e(a,b){return d(b,a)}var f=c(2).curry2,g=c(1),h=c(4),i=c(6).every,j=c(6).map;h(g.fn(i),"missing check.every method"),h(g.fn(j),"missing check.map method"),a.exports={all:d,schema:f(e)}}])}); \ No newline at end of file diff --git a/package.json b/package.json index 0660732..cb2ced5 100644 --- a/package.json +++ b/package.json @@ -112,7 +112,7 @@ "node-test": "node test/load-under-node-test.js", "pack": "webpack", "pkgfiles": "pkgfiles", - "pretest": "npm run format && npm run lint", + "pretest": "npm run format && npm run lint && npm run build", "semantic-release": "semantic-release pre && npm run set-version && npm publish && semantic-release post", "set-version": "grunt version && grunt", "size": "tarball=\"$(npm pack .)\"; wc -c \"${tarball}\"; tar tvf \"${tarball}\"; rm \"${tarball}\";", diff --git a/src/arrays.js b/src/arrays.js index 989df78..efcd84c 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -2,6 +2,9 @@ var low = require('./low-level') var logic = require('./logic') +var verify = require('./verify') + +verify(low.fn(low.isArray), 'missing low level isArray') /** Returns true if the argument is an array with at least one value @@ -41,7 +44,7 @@ Returns true if given array only has strings @param checkLowerCase Checks if all strings are lowercase */ function arrayOfStrings (a, checkLowerCase) { - var v = low.isArray(a) && a.every(low.isString) + var v = low.isArray(a) && a.every(low.string) if (v && low.bool(checkLowerCase) && checkLowerCase) { return a.every(low.lowerCase) } diff --git a/src/git.js b/src/git.js index af3dc2c..48f62f9 100644 --- a/src/git.js +++ b/src/git.js @@ -33,7 +33,7 @@ var shaReg = /^[0-9a-f]{40}$/ @method commitId */ function commitId (id) { - return low.isString(id) && + return low.string(id) && id.length === 40 && shaReg.test(id) } @@ -46,7 +46,7 @@ var shortShaReg = /^[0-9a-f]{7}$/ @method shortCommitId */ function shortCommitId (id) { - return low.isString(id) && + return low.string(id) && id.length === 7 && shortShaReg.test(id) } diff --git a/src/index.js b/src/index.js index 6a6befe..ea3473a 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,6 @@ if (typeof Function.prototype.bind !== 'function') { throw new Error('Missing Function.prototype.bind, please load es5-shim first') } -var curry2 = require('./utils').curry2 var low = require('./low-level') var mid = require('./mid-level') var arrays = require('./arrays') @@ -75,13 +74,13 @@ if (!check.mixin) { /** Adds new predicate to all objects @method mixin */ check.mixin = function mixin (fn, name) { - if (low.isString(fn) && low.isFn(name)) { + if (low.string(fn) && low.fn(name)) { var tmp = fn fn = name name = tmp } - if (!low.isFn(fn)) { + if (!low.fn(fn)) { throw new Error('expected predicate function for name ' + name) } if (!low.unemptyString(name)) { @@ -92,13 +91,13 @@ if (!check.mixin) { } function registerPredicate (obj, name, fn) { - if (!low.isObject(obj)) { + if (!low.object(obj)) { throw new Error('missing object ' + obj) } if (!low.unemptyString(name)) { throw new Error('missing name') } - if (!low.isFn(fn)) { + if (!low.fn(fn)) { throw new Error('missing function') } @@ -148,11 +147,11 @@ if (!check.then) { } var promiseSchema = { - then: low.isFn + then: low.fn } // work around reserved keywords checks -promiseSchema['catch'] = low.isFn +promiseSchema['catch'] = low.fn var hasPromiseApi = schema.schema.bind(null, promiseSchema) @@ -164,54 +163,10 @@ function isPromise (p) { return check.object(p) && hasPromiseApi(p) } -// TODO just mix in all low and mid level predicates // new predicates to be added to check object. Use object to preserve names var predicates = { - nulled: low.isNull, - fn: low.isFn, - string: low.isString, - unemptyString: low.unemptyString, - object: low.isObject, - number: low.isNumber, array: Array.isArray, - positiveNumber: low.positiveNumber, - negativeNumber: low.negativeNumber, - // a couple of aliases - positive: low.positiveNumber, - negative: low.negativeNumber, - defined: low.defined, - same: low.same, - allSame: mid.allSame, - bit: low.bit, - bool: low.bool, - has: low.has, - lowerCase: low.lowerCase, - raises: mid.raises, - empty: low.empty, - found: mid.found, - emptyString: low.emptyString, - unempty: low.unempty, - unit: mid.unit, - hexRgb: mid.hexRgb, - sameLength: mid.sameLength, - index: mid.index, - oneOf: curry2(mid.oneOf, true), - promise: isPromise, - validDate: low.validDate, - equal: curry2(low.equal), - primitive: low.primitive, - zero: low.zero, - date: low.isDate, - regexp: low.isRegExp, - instance: low.instance, - emptyObject: low.isEmptyObject, - length: curry2(low.hasLength), - floatNumber: low.isFloat, - intNumber: low.isInteger, - startsWith: mid.startsWith, - contains: mid.contains, - error: low.isError, - type: curry2(mid.type) + promise: isPromise } function mixCollection (collection) { @@ -220,7 +175,7 @@ function mixCollection (collection) { }) } -[predicates, git, internet, arrays, logic, schema].forEach(mixCollection) +[low, mid, predicates, git, internet, arrays, logic, schema].forEach(mixCollection) check.VERSION = '{{ packageVersion }}' diff --git a/src/internet.js b/src/internet.js index d36212d..de0e7eb 100644 --- a/src/internet.js +++ b/src/internet.js @@ -7,15 +7,15 @@ var startsWithHttp = mid.startsWith.bind(null, 'http://') var startsWithHttps = mid.startsWith.bind(null, 'https://') function http (x) { - return low.isString(x) && startsWithHttp(x) + return low.string(x) && startsWithHttp(x) } function https (x) { - return low.isString(x) && startsWithHttps(x) + return low.string(x) && startsWithHttps(x) } function webUrl (x) { - return low.isString(x) && + return low.string(x) && (startsWithHttp(x) || startsWithHttps(x)) } @@ -36,7 +36,7 @@ function isUserPortNumber (x) { @method email */ function email (s) { - return low.isString(s) && + return low.string(s) && /^.+@.+\..+$/.test(s) } diff --git a/src/logic.js b/src/logic.js index 631f435..8d5ed91 100644 --- a/src/logic.js +++ b/src/logic.js @@ -16,7 +16,7 @@ function or () { var values = Array.prototype.slice.call(arguments, 0) return predicates.some(function (predicate) { try { - return low.isFn(predicate) ? predicate.apply(null, values) : Boolean(predicate) + return low.fn(predicate) ? predicate.apply(null, values) : Boolean(predicate) } catch (err) { // treat exceptions as false return false @@ -38,7 +38,7 @@ function and () { return function orCheck () { var values = Array.prototype.slice.call(arguments, 0) return predicates.every(function (predicate) { - return low.isFn(predicate) ? predicate.apply(null, values) : Boolean(predicate) + return low.fn(predicate) ? predicate.apply(null, values) : Boolean(predicate) }) } } @@ -61,7 +61,7 @@ function every (predicateResults) { if (predicateResults.hasOwnProperty(property)) { value = predicateResults[property] - if (low.isObject(value) && every(value) === false) { + if (low.object(value) && every(value) === false) { return false } @@ -81,9 +81,9 @@ function map (things, predicates) { if (predicates.hasOwnProperty(property)) { predicate = predicates[property] - if (low.isFn(predicate)) { + if (low.fn(predicate)) { result[property] = predicate(things[property]) - } else if (low.isObject(predicate)) { + } else if (low.object(predicate)) { result[property] = map(things[property], predicate) } } diff --git a/src/low-level.js b/src/low-level.js index e05e8d1..53c347c 100644 --- a/src/low-level.js +++ b/src/low-level.js @@ -1,5 +1,7 @@ 'use strict' +var curry2 = require('./utils').curry2 + // low level predicates // most of the old methods same as check-types.js @@ -202,34 +204,36 @@ function equal (a, b) { } module.exports = { - isFn: isFn, - isString: isString, - isObject: isObject, - isNull: isNull, - unemptyString: unemptyString, - isEmptyObject: isEmptyObject, - isInteger: isInteger, - isFloat: isFloat, - positiveNumber: positiveNumber, - negativeNumber: negativeNumber, - isRegExp: isRegExp, - isError: isError, - instance: instance, - hasLength: hasLength, - isNumber: isNumber, - isDate: isDate, - defined: defined, - validDate: validDate, - primitive: primitive, - zero: zero, - same: same, bit: bit, bool: bool, + date: isDate, + defined: defined, + empty: empty, + emptyObject: isEmptyObject, + emptyString: emptyString, + equal: curry2(equal), + error: isError, + floatNumber: isFloat, + fn: isFn, has: has, + instance: instance, + intNumber: isInteger, isArray: isArray, + length: curry2(hasLength), lowerCase: lowerCase, - empty: empty, - emptyString: emptyString, + negative: negativeNumber, + negativeNumber: negativeNumber, + nulled: isNull, + number: isNumber, + object: isObject, + positive: positiveNumber, + positiveNumber: positiveNumber, + primitive: primitive, + regexp: isRegExp, + same: same, + string: isString, unempty: unempty, - equal: equal + unemptyString: unemptyString, + validDate: validDate, + zero: zero } diff --git a/src/mid-level.js b/src/mid-level.js index 057c73c..758dea7 100644 --- a/src/mid-level.js +++ b/src/mid-level.js @@ -2,6 +2,7 @@ var low = require('./low-level') var verify = require('./verify') +var curry2 = require('./utils').curry2 /** Checks if the given index is valid in an array or string or -1 @@ -13,8 +14,8 @@ function found (index) { } function startsWith (prefix, x) { - return low.isString(prefix) && - low.isString(x) && + return low.string(prefix) && + low.string(x) && x.indexOf(prefix) === 0 } @@ -103,7 +104,7 @@ function oneOf (arr, x) { @method unit */ function unit (value) { - return low.isNumber(value) && + return low.number(value) && value >= 0.0 && value <= 1.0 } @@ -113,7 +114,7 @@ var rgb = /^#(?:[0-9a-fA-F]{3}){1,2}$/ @method hexRgb */ function hexRgb (value) { - return low.isString(value) && + return low.string(value) && rgb.test(value) } @@ -122,7 +123,7 @@ function hexRgb (value) { @method raises */ function raises (fn, errorValidator) { - verify(low.isFn(fn), 'expected function that raises') + verify(low.fn(fn), 'expected function that raises') try { fn() } catch (err) { @@ -142,9 +143,9 @@ module.exports = { found: found, startsWith: startsWith, contains: contains, - type: type, + type: curry2(type), index: index, - oneOf: oneOf, + oneOf: curry2(oneOf, true), sameLength: sameLength, allSame: allSame, unit: unit, diff --git a/src/schema.js b/src/schema.js index 1e5ccd4..5bbdadc 100644 --- a/src/schema.js +++ b/src/schema.js @@ -6,6 +6,9 @@ var verify = require('./verify') var every = require('./logic').every var map = require('./logic').map +verify(low.fn(every), 'missing check.every method') +verify(low.fn(map), 'missing check.map method') + /** Checks if object passes all rules in predicates. @@ -21,14 +24,11 @@ var map = require('./logic').map @returns true or false */ function all (obj, predicates) { - // verify.fn(low.isFn(check.every, 'missing check.every method') - // check.verify.fn(check.map, 'missing check.map method') - - verify(low.isObject(obj), 'missing object to check') - verify(low.isObject(predicates), 'missing predicates object') + verify(low.object(obj), 'missing object to check') + verify(low.object(predicates), 'missing predicates object') Object.keys(predicates).forEach(function (property) { - if (!low.isFn(predicates[property])) { + if (!low.fn(predicates[property])) { throw new Error('not a predicate function for ' + property + ' but ' + predicates[property]) } })