Comparing version 0.12.0 to 1.0.0
## changelog | ||
#### 1.0.0 | ||
- Convert to nodejs 4.x es6 support | ||
#### 0.12.0 | ||
@@ -10,3 +13,3 @@ - Add `predicate.matches` | ||
- Add alias `predicate.ge` | ||
#### 0.10.2 | ||
@@ -13,0 +16,0 @@ - Changed from is.js to predicate.js |
/** | ||
* @license predicate.js | ||
* (c) 2014-2015 Trevor Landau <landautrevor@gmail.com> @trevor_landau | ||
* (c) 2014-2016 Trevor Landau <landautrevor@gmail.com> @trevor_landau | ||
* predicate.js may be freely distributed under the MIT license. | ||
*/ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.predicate=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.predicate = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -11,10 +11,5 @@ | ||
var predicate = {}; | ||
predicate.VERSION = '0.12.0'; | ||
predicate.VERSION = '1.0.0'; | ||
[ | ||
utils, | ||
require('./lib/predicates'), | ||
require('./lib/chain'), | ||
require('./lib/other'), | ||
].reduce(utils.assign, predicate); | ||
[utils, require('./lib/predicates'), require('./lib/chain'), require('./lib/other')].reduce(utils.assign, predicate); | ||
@@ -26,3 +21,10 @@ module.exports = predicate; | ||
var utils = require('./utils'); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var predicates = require('./predicates'); | ||
@@ -32,49 +34,76 @@ var predicate = module.exports; | ||
// chaining mixin | ||
function lazy() { | ||
/* jshint validthis:true */ | ||
// Enable invocation with operators (+, !, etc) | ||
this.valueOf = function () { | ||
return this.val(); | ||
}; | ||
var Lazy = function () { | ||
function Lazy() { | ||
_classCallCheck(this, Lazy); | ||
this.val = function () { | ||
return this.lazy.map(function (args) { | ||
return args[0].apply(null, args[1]); | ||
})[this.method](predicates.truthy); | ||
}; | ||
this.lazy = []; | ||
} | ||
return this; | ||
} | ||
_createClass(Lazy, [{ | ||
key: 'valueOf', | ||
value: function valueOf() { | ||
return this.val(); | ||
} | ||
}, { | ||
key: 'val', | ||
value: function val() { | ||
return this.lazy.map(function (args) { | ||
return args[0].apply(null, args[1]); | ||
})[this.method](predicates.truthy); | ||
} | ||
}]); | ||
function Every() { | ||
this.method = 'every'; | ||
this.lazy = []; | ||
} | ||
return Lazy; | ||
}(); | ||
Every.prototype = Object.keys(predicates).reduce(function (acc, fnName) { | ||
if (!predicates.fn(predicates[fnName])) return acc; | ||
var Every = function (_Lazy) { | ||
_inherits(Every, _Lazy); | ||
acc[fnName] = function() { | ||
this.lazy.push([predicates[fnName], arguments]); | ||
return this; | ||
}; | ||
function Every() { | ||
_classCallCheck(this, Every); | ||
return acc; | ||
}, {}); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Every).call(this)); | ||
lazy.call(Every.prototype); | ||
_this.method = 'every'; | ||
return _this; | ||
} | ||
return Every; | ||
}(Lazy); | ||
var Some = function (_Lazy2) { | ||
_inherits(Some, _Lazy2); | ||
function Some() { | ||
_classCallCheck(this, Some); | ||
var _this2 = _possibleConstructorReturn(this, Object.getPrototypeOf(Some).call(this)); | ||
_this2.method = 'some'; | ||
return _this2; | ||
} | ||
return Some; | ||
}(Lazy); | ||
// Extend chaining methods onto the prototypes | ||
[Every, Some].forEach(function (cls) { | ||
Object.keys(predicates).reduce(function (proto, fnName) { | ||
if (!predicates.fn(predicates[fnName])) return proto; | ||
proto[fnName] = function () { | ||
this.lazy.push([predicates[fnName], arguments]); | ||
return this; | ||
}; | ||
return proto; | ||
}, cls.prototype); | ||
}); | ||
predicate.all = predicate.every = function () { | ||
return new Every(); | ||
}; | ||
function Some() { | ||
this.method = 'some'; | ||
this.lazy = []; | ||
} | ||
Some.prototype = utils.assign({}, Every.prototype); | ||
lazy.call(Some.prototype); | ||
predicate.any = predicate.some = function () { | ||
@@ -84,3 +113,3 @@ return new Some(); | ||
},{"./predicates":4,"./utils":5}],3:[function(require,module,exports){ | ||
},{"./predicates":4}],3:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -102,2 +131,4 @@ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var utils = require('./utils'); | ||
@@ -112,3 +143,3 @@ var predicate = module.exports; | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is | ||
predicate.is = curry(function(v1, v2) { | ||
predicate.is = curry(function (v1, v2) { | ||
if (v1 === 0 && v2 === 0) { | ||
@@ -168,3 +199,3 @@ return 1 / v1 === 1 / v2; | ||
var __toString = Object.prototype.toString; | ||
var eqToStr = curry(function(str, val) { | ||
var eqToStr = curry(function (str, val) { | ||
return predicate.equal(str, __toString.call(val)); | ||
@@ -198,4 +229,4 @@ }); | ||
// creates fns for predicate.string, etc | ||
var typeofBuilder = curry(function(type, val) { | ||
return predicate.equal(type, typeof val); | ||
var typeofBuilder = curry(function (type, val) { | ||
return predicate.equal(type, typeof val === 'undefined' ? 'undefined' : _typeof(val)); | ||
}); | ||
@@ -207,7 +238,3 @@ | ||
// predicate.fn, predicate.num, etc | ||
[ | ||
['function', 'fn'], | ||
['string', 'str'], | ||
['boolean', 'bool'] | ||
].reduce(function (predicate, type) { | ||
[['function', 'fn'], ['string', 'str'], ['boolean', 'bool']].reduce(function (predicate, type) { | ||
predicate[type[0]] = predicate[type[1]] = typeofBuilder(type[0]); | ||
@@ -217,3 +244,3 @@ return predicate; | ||
predicate.number = predicate.num = function(val) { | ||
predicate.number = predicate.num = function (val) { | ||
return typeof val === 'number' && predicate.not.NaN(val); | ||
@@ -239,11 +266,7 @@ }; | ||
predicate.even = function (val) { | ||
return predicate.num(val) && | ||
predicate.not.zero(val) && | ||
predicate.zero(utils.mod(val, 2)); | ||
return predicate.num(val) && predicate.not.zero(val) && predicate.zero(utils.mod(val, 2)); | ||
}; | ||
predicate.odd = function (val) { | ||
return predicate.num(val) && | ||
predicate.not.zero(val) && | ||
predicate.not.zero(utils.mod(val, 2)); | ||
return predicate.num(val) && predicate.not.zero(val) && predicate.not.zero(utils.mod(val, 2)); | ||
}; | ||
@@ -256,7 +279,7 @@ | ||
} | ||
return !!~arr.indexOf(val); | ||
return !! ~arr.indexOf(val); | ||
}); | ||
var __has = Object.prototype.hasOwnProperty; | ||
predicate.has = curry(function has(o, key) { | ||
predicate.has = curry(function (o, key) { | ||
return __has.call(o, key); | ||
@@ -269,4 +292,5 @@ }); | ||
if (predicate.obj(o)) { | ||
for (var k in o) if (predicate.has(o, k)) return false; | ||
return true; | ||
for (var k in o) { | ||
if (predicate.has(o, k)) return false; | ||
}return true; | ||
} | ||
@@ -277,7 +301,6 @@ throw new TypeError(); | ||
predicate.primitive = function (val) { | ||
return predicate.string(val) || predicate.num(val) || predicate.bool(val) || | ||
predicate.null(val) || predicate.undef(val) || predicate.NaN(val); | ||
return predicate.string(val) || predicate.num(val) || predicate.bool(val) || predicate.null(val) || predicate.undef(val) || predicate.NaN(val); | ||
}; | ||
predicate.matches = curry(function matches(rgx, val) { | ||
predicate.matches = curry(function (rgx, val) { | ||
return rgx.test(val); | ||
@@ -294,2 +317,3 @@ }); | ||
'use strict'; | ||
var predicate = module.exports; | ||
@@ -299,4 +323,4 @@ var _slice = Array.prototype.slice; | ||
// Useful for debuging curried functions | ||
function setSrc(curried, src) { | ||
curried.toString = function() { | ||
var setSrc = function setSrc(curried, src) { | ||
curried.toString = function () { | ||
return src.toString(); | ||
@@ -306,11 +330,12 @@ }; | ||
return curried; | ||
} | ||
}; | ||
// Curry's fn's with arity 2 | ||
var curry = predicate.curry = function(f) { | ||
var curry = predicate.curry = function (f) { | ||
return setSrc(function curried(a, b) { | ||
switch (arguments.length) { | ||
case 0: throw new TypeError('Function called with no arguments'); | ||
case 0: | ||
throw new TypeError('Function called with no arguments'); | ||
case 1: | ||
return setSrc(function curried(b) { | ||
return setSrc(function (b) { | ||
return f(a, b); | ||
@@ -324,5 +349,6 @@ }, f); | ||
// TODO: es6ing this breaks! | ||
predicate.partial = function (fn) { | ||
var args = _slice.call(arguments, 1); | ||
return function() { | ||
return function () { | ||
return fn.apply(null, args.concat(_slice.call(arguments))); | ||
@@ -333,2 +359,3 @@ }; | ||
predicate.complement = predicate.invert = function (pred) { | ||
// TODO: es6ing this breaks! | ||
return function () { | ||
@@ -347,3 +374,3 @@ var ret = pred.apply(null, arguments); | ||
// assign b's props to a | ||
predicate.assign = curry(function(a, b) { | ||
predicate.assign = curry(function (a, b) { | ||
// use crummy for/in for perf purposes | ||
@@ -359,4 +386,3 @@ for (var prop in b) { | ||
},{}]},{},[1])(1) | ||
}); |
/** | ||
* @license predicate.js | ||
* (c) 2014-2015 Trevor Landau <landautrevor@gmail.com> @trevor_landau | ||
* (c) 2014-2016 Trevor Landau <landautrevor@gmail.com> @trevor_landau | ||
* predicate.js may be freely distributed under the MIT license. | ||
*/ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.predicate=e()}}(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";var utils=require("./lib/utils");var predicate={};predicate.VERSION="0.12.0";[utils,require("./lib/predicates"),require("./lib/chain"),require("./lib/other")].reduce(utils.assign,predicate);module.exports=predicate},{"./lib/chain":2,"./lib/other":3,"./lib/predicates":4,"./lib/utils":5}],2:[function(require,module,exports){"use strict";var utils=require("./utils");var predicates=require("./predicates");var predicate=module.exports;function lazy(){this.valueOf=function(){return this.val()};this.val=function(){return this.lazy.map(function(args){return args[0].apply(null,args[1])})[this.method](predicates.truthy)};return this}function Every(){this.method="every";this.lazy=[]}Every.prototype=Object.keys(predicates).reduce(function(acc,fnName){if(!predicates.fn(predicates[fnName]))return acc;acc[fnName]=function(){this.lazy.push([predicates[fnName],arguments]);return this};return acc},{});lazy.call(Every.prototype);predicate.all=predicate.every=function(){return new Every};function Some(){this.method="some";this.lazy=[]}Some.prototype=utils.assign({},Every.prototype);lazy.call(Some.prototype);predicate.any=predicate.some=function(){return new Some}},{"./predicates":4,"./utils":5}],3:[function(require,module,exports){"use strict";var predicates=require("./predicates");var utils=require("./utils");var predicate=module.exports;predicate.ternary=function(pred,a,b){if(predicates.bool(pred))return pred?a:b;if(predicates.undef(a))return utils.partial(predicate.ternary,pred);if(predicates.undef(b))return utils.partial(predicate.ternary,pred,a);return predicate.ternary(pred(a,b),a,b)}},{"./predicates":4,"./utils":5}],4:[function(require,module,exports){"use strict";var utils=require("./utils");var predicate=module.exports;var curry=utils.curry;if(Object.is){predicate.is=curry(Object.is)}else{predicate.is=curry(function(v1,v2){if(v1===0&&v2===0){return 1/v1===1/v2}if(v1!==v1){return v2!==v2}return v1===v2})}predicate.exists=function(val){return val!=null};predicate.truthy=function(val){return!!(val&&predicate.exists(val))};predicate.falsey=utils.complement(predicate.truthy);predicate.equal=curry(function(a,b){return a===b});predicate.eq=curry(function(a,b){return a==b});predicate.null=predicate.equal(null);predicate.undef=predicate.equal(undefined);predicate.lt=predicate.less=curry(function(a,b){return a<b});predicate.ltEq=predicate.le=predicate.lessEq=curry(function(a,b){return predicate.equal(a,b)||predicate.less(a,b)});predicate.gt=predicate.greater=curry(function(a,b){return a>b});predicate.gtEq=predicate.ge=predicate.greaterEq=curry(function(a,b){return predicate.equal(a,b)||predicate.greater(a,b)});var __toString=Object.prototype.toString;var eqToStr=curry(function(str,val){return predicate.equal(str,__toString.call(val))});predicate.object=predicate.obj=function(val){return val===Object(val)};predicate.array=predicate.arr=Array.isArray||eqToStr("[object Array]");predicate.date=eqToStr("[object Date]");predicate.regex=predicate.regexp=predicate.rgx=predicate.RegExp=eqToStr("[object RegExp]");predicate.finite=Number.isFinite||function(val){return predicate.number(val)&&isFinite(val)};predicate.nan=predicate.NaN=predicate.is(NaN);predicate.instance=curry(function(Cls,inst){return inst instanceof Cls});predicate.arguments=eqToStr("[object Arguments]");predicate.error=predicate.instance(Error);var typeofBuilder=curry(function(type,val){return predicate.equal(type,typeof val)});[["function","fn"],["string","str"],["boolean","bool"]].reduce(function(predicate,type){predicate[type[0]]=predicate[type[1]]=typeofBuilder(type[0]);return predicate},predicate);predicate.number=predicate.num=function(val){return typeof val==="number"&&predicate.not.NaN(val)};predicate.int=function(val){return predicate.num(val)&&predicate.zero(utils.mod(val,1))};predicate.pos=function(val){return predicate.num(val)&&predicate.greater(val,0)};predicate.neg=function(val){return predicate.num(val)&&predicate.less(val,0)};predicate.zero=function(val){return predicate.num(val)&&predicate.equal(val,0)};predicate.even=function(val){return predicate.num(val)&&predicate.not.zero(val)&&predicate.zero(utils.mod(val,2))};predicate.odd=function(val){return predicate.num(val)&&predicate.not.zero(val)&&predicate.not.zero(utils.mod(val,2))};predicate.contains=curry(function(arr,val){if(!predicate.array(arr))throw new TypeError("Expected an array");if(predicate.NaN(val)){return arr.some(predicate.NaN)}return!!~arr.indexOf(val)});var __has=Object.prototype.hasOwnProperty;predicate.has=curry(function has(o,key){return __has.call(o,key)});predicate.empty=function(o){if(predicate.not.exists(o))return true;if(predicate.arr(o)||predicate.str(o))return!o.length;if(predicate.obj(o)){for(var k in o)if(predicate.has(o,k))return false;return true}throw new TypeError};predicate.primitive=function(val){return predicate.string(val)||predicate.num(val)||predicate.bool(val)||predicate.null(val)||predicate.undef(val)||predicate.NaN(val)};predicate.matches=curry(function matches(rgx,val){return rgx.test(val)});predicate.not=Object.keys(predicate).reduce(function(acc,fnName){acc[fnName]=utils.complement(predicate[fnName]);return acc},{})},{"./utils":5}],5:[function(require,module,exports){"use strict";var predicate=module.exports;var _slice=Array.prototype.slice;function setSrc(curried,src){curried.toString=function(){return src.toString()};curried.src=src;return curried}var curry=predicate.curry=function(f){return setSrc(function curried(a,b){switch(arguments.length){case 0:throw new TypeError("Function called with no arguments");case 1:return setSrc(function curried(b){return f(a,b)},f)}return f(a,b)},f)};predicate.partial=function(fn){var args=_slice.call(arguments,1);return function(){return fn.apply(null,args.concat(_slice.call(arguments)))}};predicate.complement=predicate.invert=function(pred){return function(){var ret=pred.apply(null,arguments);if(typeof ret==="function")return predicate.complement(ret);return!ret}};predicate.mod=curry(function(a,b){return a%b});predicate.assign=curry(function(a,b){for(var prop in b){if(b.hasOwnProperty(prop)){a[prop]=b[prop]}}return a})},{}]},{},[1])(1)}); | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.predicate=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";var utils=require("./lib/utils");var predicate={};predicate.VERSION="1.0.0";[utils,require("./lib/predicates"),require("./lib/chain"),require("./lib/other")].reduce(utils.assign,predicate);module.exports=predicate},{"./lib/chain":2,"./lib/other":3,"./lib/predicates":4,"./lib/utils":5}],2:[function(require,module,exports){"use strict";var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return call&&(typeof call==="object"||typeof call==="function")?call:self}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass)}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}var predicates=require("./predicates");var predicate=module.exports;var Lazy=function(){function Lazy(){_classCallCheck(this,Lazy);this.lazy=[]}_createClass(Lazy,[{key:"valueOf",value:function valueOf(){return this.val()}},{key:"val",value:function val(){return this.lazy.map(function(args){return args[0].apply(null,args[1])})[this.method](predicates.truthy)}}]);return Lazy}();var Every=function(_Lazy){_inherits(Every,_Lazy);function Every(){_classCallCheck(this,Every);var _this=_possibleConstructorReturn(this,Object.getPrototypeOf(Every).call(this));_this.method="every";return _this}return Every}(Lazy);var Some=function(_Lazy2){_inherits(Some,_Lazy2);function Some(){_classCallCheck(this,Some);var _this2=_possibleConstructorReturn(this,Object.getPrototypeOf(Some).call(this));_this2.method="some";return _this2}return Some}(Lazy);[Every,Some].forEach(function(cls){Object.keys(predicates).reduce(function(proto,fnName){if(!predicates.fn(predicates[fnName]))return proto;proto[fnName]=function(){this.lazy.push([predicates[fnName],arguments]);return this};return proto},cls.prototype)});predicate.all=predicate.every=function(){return new Every};predicate.any=predicate.some=function(){return new Some}},{"./predicates":4}],3:[function(require,module,exports){"use strict";var predicates=require("./predicates");var utils=require("./utils");var predicate=module.exports;predicate.ternary=function(pred,a,b){if(predicates.bool(pred))return pred?a:b;if(predicates.undef(a))return utils.partial(predicate.ternary,pred);if(predicates.undef(b))return utils.partial(predicate.ternary,pred,a);return predicate.ternary(pred(a,b),a,b)}},{"./predicates":4,"./utils":5}],4:[function(require,module,exports){"use strict";var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj};var utils=require("./utils");var predicate=module.exports;var curry=utils.curry;if(Object.is){predicate.is=curry(Object.is)}else{predicate.is=curry(function(v1,v2){if(v1===0&&v2===0){return 1/v1===1/v2}if(v1!==v1){return v2!==v2}return v1===v2})}predicate.exists=function(val){return val!=null};predicate.truthy=function(val){return!!(val&&predicate.exists(val))};predicate.falsey=utils.complement(predicate.truthy);predicate.equal=curry(function(a,b){return a===b});predicate.eq=curry(function(a,b){return a==b});predicate.null=predicate.equal(null);predicate.undef=predicate.equal(undefined);predicate.lt=predicate.less=curry(function(a,b){return a<b});predicate.ltEq=predicate.le=predicate.lessEq=curry(function(a,b){return predicate.equal(a,b)||predicate.less(a,b)});predicate.gt=predicate.greater=curry(function(a,b){return a>b});predicate.gtEq=predicate.ge=predicate.greaterEq=curry(function(a,b){return predicate.equal(a,b)||predicate.greater(a,b)});var __toString=Object.prototype.toString;var eqToStr=curry(function(str,val){return predicate.equal(str,__toString.call(val))});predicate.object=predicate.obj=function(val){return val===Object(val)};predicate.array=predicate.arr=Array.isArray||eqToStr("[object Array]");predicate.date=eqToStr("[object Date]");predicate.regex=predicate.regexp=predicate.rgx=predicate.RegExp=eqToStr("[object RegExp]");predicate.finite=Number.isFinite||function(val){return predicate.number(val)&&isFinite(val)};predicate.nan=predicate.NaN=predicate.is(NaN);predicate.instance=curry(function(Cls,inst){return inst instanceof Cls});predicate.arguments=eqToStr("[object Arguments]");predicate.error=predicate.instance(Error);var typeofBuilder=curry(function(type,val){return predicate.equal(type,typeof val==="undefined"?"undefined":_typeof(val))});[["function","fn"],["string","str"],["boolean","bool"]].reduce(function(predicate,type){predicate[type[0]]=predicate[type[1]]=typeofBuilder(type[0]);return predicate},predicate);predicate.number=predicate.num=function(val){return typeof val==="number"&&predicate.not.NaN(val)};predicate.int=function(val){return predicate.num(val)&&predicate.zero(utils.mod(val,1))};predicate.pos=function(val){return predicate.num(val)&&predicate.greater(val,0)};predicate.neg=function(val){return predicate.num(val)&&predicate.less(val,0)};predicate.zero=function(val){return predicate.num(val)&&predicate.equal(val,0)};predicate.even=function(val){return predicate.num(val)&&predicate.not.zero(val)&&predicate.zero(utils.mod(val,2))};predicate.odd=function(val){return predicate.num(val)&&predicate.not.zero(val)&&predicate.not.zero(utils.mod(val,2))};predicate.contains=curry(function(arr,val){if(!predicate.array(arr))throw new TypeError("Expected an array");if(predicate.NaN(val)){return arr.some(predicate.NaN)}return!!~arr.indexOf(val)});var __has=Object.prototype.hasOwnProperty;predicate.has=curry(function(o,key){return __has.call(o,key)});predicate.empty=function(o){if(predicate.not.exists(o))return true;if(predicate.arr(o)||predicate.str(o))return!o.length;if(predicate.obj(o)){for(var k in o){if(predicate.has(o,k))return false}return true}throw new TypeError};predicate.primitive=function(val){return predicate.string(val)||predicate.num(val)||predicate.bool(val)||predicate.null(val)||predicate.undef(val)||predicate.NaN(val)};predicate.matches=curry(function(rgx,val){return rgx.test(val)});predicate.not=Object.keys(predicate).reduce(function(acc,fnName){acc[fnName]=utils.complement(predicate[fnName]);return acc},{})},{"./utils":5}],5:[function(require,module,exports){"use strict";var predicate=module.exports;var _slice=Array.prototype.slice;var setSrc=function setSrc(curried,src){curried.toString=function(){return src.toString()};curried.src=src;return curried};var curry=predicate.curry=function(f){return setSrc(function curried(a,b){switch(arguments.length){case 0:throw new TypeError("Function called with no arguments");case 1:return setSrc(function(b){return f(a,b)},f)}return f(a,b)},f)};predicate.partial=function(fn){var args=_slice.call(arguments,1);return function(){return fn.apply(null,args.concat(_slice.call(arguments)))}};predicate.complement=predicate.invert=function(pred){return function(){var ret=pred.apply(null,arguments);if(typeof ret==="function")return predicate.complement(ret);return!ret}};predicate.mod=curry(function(a,b){return a%b});predicate.assign=curry(function(a,b){for(var prop in b){if(b.hasOwnProperty(prop)){a[prop]=b[prop]}}return a})},{}]},{},[1])(1)}); |
/** | ||
* @license predicate.js | ||
* (c) 2014-2015 Trevor Landau <landautrevor@gmail.com> @trevor_landau | ||
* (c) 2014-2016 Trevor Landau <landautrevor@gmail.com> @trevor_landau | ||
* predicate.js may be freely distributed under the MIT license. | ||
*/ |
'use strict'; | ||
var utils = require('./lib/utils'); | ||
var predicate = {}; | ||
predicate.VERSION = '0.12.0'; | ||
const utils = require('./lib/utils'); | ||
const predicate = {}; | ||
predicate.VERSION = '1.0.0'; | ||
@@ -7,0 +7,0 @@ [ |
'use strict'; | ||
var utils = require('./utils'); | ||
var predicates = require('./predicates'); | ||
var predicate = module.exports; | ||
const predicates = require('./predicates'); | ||
const predicate = module.exports; | ||
// chaining mixin | ||
function lazy() { | ||
/* jshint validthis:true */ | ||
class Lazy { | ||
constructor() { | ||
this.lazy = []; | ||
} | ||
// Enable invocation with operators (+, !, etc) | ||
this.valueOf = function () { | ||
valueOf() { | ||
return this.val(); | ||
}; | ||
} | ||
this.val = function () { | ||
val() { | ||
return this.lazy.map(function (args) { | ||
return args[0].apply(null, args[1]); | ||
})[this.method](predicates.truthy); | ||
}; | ||
} | ||
} | ||
return this; | ||
class Every extends Lazy { | ||
constructor() { | ||
super(); | ||
this.method = 'every'; | ||
} | ||
} | ||
function Every() { | ||
this.method = 'every'; | ||
this.lazy = []; | ||
class Some extends Lazy { | ||
constructor() { | ||
super(); | ||
this.method = 'some'; | ||
} | ||
} | ||
Every.prototype = Object.keys(predicates).reduce(function (acc, fnName) { | ||
if (!predicates.fn(predicates[fnName])) return acc; | ||
// Extend chaining methods onto the prototypes | ||
[Every, Some].forEach((cls) => { | ||
Object.keys(predicates).reduce((proto, fnName) => { | ||
if (!predicates.fn(predicates[fnName])) return proto; | ||
acc[fnName] = function() { | ||
this.lazy.push([predicates[fnName], arguments]); | ||
return this; | ||
}; | ||
proto[fnName] = function() { | ||
this.lazy.push([predicates[fnName], arguments]); | ||
return this; | ||
}; | ||
return acc; | ||
}, {}); | ||
return proto; | ||
}, cls.prototype); | ||
}); | ||
lazy.call(Every.prototype); | ||
predicate.all = predicate.every = function () { | ||
return new Every(); | ||
}; | ||
function Some() { | ||
this.method = 'some'; | ||
this.lazy = []; | ||
} | ||
Some.prototype = utils.assign({}, Every.prototype); | ||
lazy.call(Some.prototype); | ||
predicate.any = predicate.some = function () { | ||
return new Some(); | ||
}; | ||
predicate.all = predicate.every = () => new Every(); | ||
predicate.any = predicate.some = () => new Some(); |
'use strict'; | ||
var predicates = require('./predicates'); | ||
var utils = require('./utils'); | ||
var predicate = module.exports; | ||
const predicates = require('./predicates'); | ||
const utils = require('./utils'); | ||
const predicate = module.exports; | ||
predicate.ternary = function (pred, a, b) { | ||
predicate.ternary = (pred, a, b) => { | ||
if (predicates.bool(pred)) return pred ? a : b; | ||
@@ -9,0 +9,0 @@ if (predicates.undef(a)) return utils.partial(predicate.ternary, pred); |
'use strict'; | ||
var utils = require('./utils'); | ||
var predicate = module.exports; | ||
const utils = require('./utils'); | ||
const predicate = module.exports; | ||
var curry = utils.curry; | ||
const curry = utils.curry; | ||
@@ -23,7 +23,7 @@ if (Object.is) { | ||
predicate.exists = function (val) { | ||
predicate.exists = (val) => { | ||
return val != null; | ||
}; | ||
predicate.truthy = function (val) { | ||
predicate.truthy = (val) => { | ||
// coerce for null != null | ||
@@ -37,7 +37,7 @@ return !!(val && predicate.exists(val)); | ||
predicate.equal = curry(function (a, b) { | ||
predicate.equal = curry((a, b) => { | ||
return a === b; | ||
}); | ||
predicate.eq = curry(function (a, b) { | ||
predicate.eq = curry((a, b) => { | ||
return a == b; | ||
@@ -49,15 +49,15 @@ }); | ||
predicate.lt = predicate.less = curry(function (a, b) { | ||
predicate.lt = predicate.less = curry((a, b) => { | ||
return a < b; | ||
}); | ||
predicate.ltEq = predicate.le = predicate.lessEq = curry(function (a, b) { | ||
predicate.ltEq = predicate.le = predicate.lessEq = curry((a, b) => { | ||
return predicate.equal(a, b) || predicate.less(a, b); | ||
}); | ||
predicate.gt = predicate.greater = curry(function (a, b) { | ||
predicate.gt = predicate.greater = curry((a, b) => { | ||
return a > b; | ||
}); | ||
predicate.gtEq = predicate.ge = predicate.greaterEq = curry(function (a, b) { | ||
predicate.gtEq = predicate.ge = predicate.greaterEq = curry((a, b) => { | ||
return predicate.equal(a, b) || predicate.greater(a, b); | ||
@@ -69,4 +69,4 @@ }); | ||
// Forces objects toString called returned as [object Object] for instance | ||
var __toString = Object.prototype.toString; | ||
var eqToStr = curry(function(str, val) { | ||
const __toString = Object.prototype.toString; | ||
const eqToStr = curry(function(str, val) { | ||
return predicate.equal(str, __toString.call(val)); | ||
@@ -77,3 +77,3 @@ }); | ||
predicate.object = predicate.obj = function (val) { | ||
predicate.object = predicate.obj = (val) => { | ||
return val === Object(val); | ||
@@ -87,11 +87,7 @@ }; | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite | ||
predicate.finite = Number.isFinite || function (val) { | ||
return predicate.number(val) && isFinite(val); | ||
}; | ||
predicate.finite = Number.isFinite || ((val) => predicate.number(val) && isFinite(val)); | ||
predicate.nan = predicate.NaN = predicate.is(NaN); | ||
predicate.instance = curry(function (Cls, inst) { | ||
return inst instanceof Cls; | ||
}); | ||
predicate.instance = curry((Cls, inst) => inst instanceof Cls); | ||
@@ -102,3 +98,3 @@ predicate.arguments = eqToStr('[object Arguments]'); | ||
// creates fns for predicate.string, etc | ||
var typeofBuilder = curry(function(type, val) { | ||
const typeofBuilder = curry((type, val) => { | ||
return predicate.equal(type, typeof val); | ||
@@ -115,3 +111,3 @@ }); | ||
['boolean', 'bool'] | ||
].reduce(function (predicate, type) { | ||
].reduce((predicate, type) => { | ||
predicate[type[0]] = predicate[type[1]] = typeofBuilder(type[0]); | ||
@@ -121,23 +117,21 @@ return predicate; | ||
predicate.number = predicate.num = function(val) { | ||
return typeof val === 'number' && predicate.not.NaN(val); | ||
}; | ||
predicate.number = predicate.num = (val) => typeof val === 'number' && predicate.not.NaN(val); | ||
predicate.int = function (val) { | ||
predicate.int = (val) => { | ||
return predicate.num(val) && predicate.zero(utils.mod(val, 1)); | ||
}; | ||
predicate.pos = function (val) { | ||
predicate.pos = (val) => { | ||
return predicate.num(val) && predicate.greater(val, 0); | ||
}; | ||
predicate.neg = function (val) { | ||
predicate.neg = (val) => { | ||
return predicate.num(val) && predicate.less(val, 0); | ||
}; | ||
predicate.zero = function (val) { | ||
predicate.zero = (val) => { | ||
return predicate.num(val) && predicate.equal(val, 0); | ||
}; | ||
predicate.even = function (val) { | ||
predicate.even = (val) => { | ||
return predicate.num(val) && | ||
@@ -148,3 +142,3 @@ predicate.not.zero(val) && | ||
predicate.odd = function (val) { | ||
predicate.odd = (val) => { | ||
return predicate.num(val) && | ||
@@ -155,3 +149,3 @@ predicate.not.zero(val) && | ||
predicate.contains = curry(function (arr, val) { | ||
predicate.contains = curry((arr, val) => { | ||
if (!predicate.array(arr)) throw new TypeError('Expected an array'); | ||
@@ -164,12 +158,10 @@ if (predicate.NaN(val)) { | ||
var __has = Object.prototype.hasOwnProperty; | ||
predicate.has = curry(function has(o, key) { | ||
return __has.call(o, key); | ||
}); | ||
const __has = Object.prototype.hasOwnProperty; | ||
predicate.has = curry((o, key) => __has.call(o, key)); | ||
predicate.empty = function (o) { | ||
predicate.empty = (o) => { | ||
if (predicate.not.exists(o)) return true; | ||
if (predicate.arr(o) || predicate.str(o)) return !o.length; | ||
if (predicate.obj(o)) { | ||
for (var k in o) if (predicate.has(o, k)) return false; | ||
for (let k in o) if (predicate.has(o, k)) return false; | ||
return true; | ||
@@ -180,3 +172,3 @@ } | ||
predicate.primitive = function (val) { | ||
predicate.primitive = (val) => { | ||
return predicate.string(val) || predicate.num(val) || predicate.bool(val) || | ||
@@ -186,10 +178,8 @@ predicate.null(val) || predicate.undef(val) || predicate.NaN(val); | ||
predicate.matches = curry(function matches(rgx, val) { | ||
return rgx.test(val); | ||
}); | ||
predicate.matches = curry((rgx, val) => rgx.test(val)); | ||
// Assign inverse of each predicate | ||
predicate.not = Object.keys(predicate).reduce(function (acc, fnName) { | ||
predicate.not = Object.keys(predicate).reduce((acc, fnName) => { | ||
acc[fnName] = utils.complement(predicate[fnName]); | ||
return acc; | ||
}, {}); |
'use strict'; | ||
var predicate = module.exports; | ||
var _slice = Array.prototype.slice; | ||
const predicate = module.exports; | ||
const _slice = Array.prototype.slice; | ||
// Useful for debuging curried functions | ||
function setSrc(curried, src) { | ||
curried.toString = function() { | ||
return src.toString(); | ||
}; | ||
const setSrc = (curried, src) => { | ||
curried.toString = () => src.toString(); | ||
curried.src = src; | ||
return curried; | ||
} | ||
}; | ||
// Curry's fn's with arity 2 | ||
var curry = predicate.curry = function(f) { | ||
const curry = predicate.curry = (f) => { | ||
return setSrc(function curried(a, b) { | ||
switch (arguments.length) { | ||
case 0: throw new TypeError('Function called with no arguments'); | ||
case 1: | ||
return setSrc(function curried(b) { | ||
return f(a, b); | ||
}, f); | ||
case 1: return setSrc((b) => f(a, b), f); | ||
} | ||
@@ -29,4 +24,5 @@ | ||
// TODO: es6ing this breaks! | ||
predicate.partial = function (fn) { | ||
var args = _slice.call(arguments, 1); | ||
const args = _slice.call(arguments, 1); | ||
return function() { | ||
@@ -37,5 +33,6 @@ return fn.apply(null, args.concat(_slice.call(arguments))); | ||
predicate.complement = predicate.invert = function (pred) { | ||
predicate.complement = predicate.invert = (pred) => { | ||
// TODO: es6ing this breaks! | ||
return function () { | ||
var ret = pred.apply(null, arguments); | ||
const ret = pred.apply(null, arguments); | ||
// Handle curried fns | ||
@@ -47,3 +44,3 @@ if (typeof ret === 'function') return predicate.complement(ret); | ||
predicate.mod = curry(function (a, b) { | ||
predicate.mod = curry((a, b) => { | ||
return a % b; | ||
@@ -53,5 +50,5 @@ }); | ||
// assign b's props to a | ||
predicate.assign = curry(function(a, b) { | ||
predicate.assign = curry((a, b) => { | ||
// use crummy for/in for perf purposes | ||
for (var prop in b) { | ||
for (let prop in b) { | ||
if (b.hasOwnProperty(prop)) { | ||
@@ -64,2 +61,1 @@ a[prop] = b[prop]; | ||
}); | ||
{ | ||
"name": "predicate", | ||
"version": "0.12.0", | ||
"version": "1.0.0", | ||
"description": "A set of predicate functions to improve your value testing and comparisons.", | ||
"scripts": { | ||
"pretest": "jshint --reporter node_modules/jshint-stylish/stylish.js index.js ./test", | ||
"pretest": "jshint index.js ./lib ./test", | ||
"test": "mocha -R spec --recursive test", | ||
@@ -11,3 +11,3 @@ "docs": "jade ./docs/index.jade --out ./docs", | ||
"build-docs": "npm run docs && npm run changelog", | ||
"build-dev": "cat ./docs/banner.txt > ./dist/predicate.js && browserify -s predicate index.js >> ./dist/predicate.js", | ||
"build-dev": "cat ./docs/banner.txt > ./dist/predicate.js && browserify -t [babelify --presets es2015] -s predicate index.js >> ./dist/predicate.js", | ||
"build-prod": "cat ./docs/banner.txt > ./dist/predicate.min.js && uglifyjs ./dist/predicate.js >> ./dist/predicate.min.js", | ||
@@ -50,12 +50,13 @@ "build": "npm run build-dev && npm run build-prod", | ||
"devDependencies": { | ||
"browserify": "^8.1.3", | ||
"babel-preset-es2015": "6.6.0", | ||
"babelify": "7.3.0", | ||
"browserify": "13.0.0", | ||
"jade": "^1.5.0", | ||
"jshint": "^2.5.2", | ||
"jshint-stylish": "*", | ||
"lodash": "^3.2.0", | ||
"jshint": "^2.6.3", | ||
"lodash": "4.11.2", | ||
"marked": "^0.3.2", | ||
"mocha": "^2.0.1", | ||
"should": "^5.0.0", | ||
"uglify-js": "^2.4.13" | ||
"mocha": "^2.2.1", | ||
"should": "^5.2.0", | ||
"uglify-js": "^2.4.19" | ||
} | ||
} |
@@ -17,13 +17,10 @@ [![Build Status](https://travis-ci.org/landau/predicate.svg)](https://travis-ci.org/landau/predicate) | ||
## Author | ||
[Trevor Landau](http://trevorlandau.net) | ||
## Tests | ||
Simply run `npm ts` | ||
`npm ts` | ||
## Building | ||
## Build for the browser | ||
Simply run `npm run build`. | ||
`npm run build`. | ||
This will create a UMDified version of is in the `dist` directory along with a minified version. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
115752
989
1
10
26