UNPKG

@wootapa/polygraph

Version:

Tests objects with queries constructed using logical and comparison operators.

4 lines (3 loc) 14.4 kB
/*! @wootapa/polygraph v1.0.1 | author:Andreas Petersson | license:MIT */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).polygraph={})}(this,(function(t){"use strict";var e=function(t,n){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},e(t,n)};function n(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var r=function(){return r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)};var o="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},i={exports:{}};(function(){var t,e,n,r,o,s;"undefined"!=typeof performance&&null!==performance&&performance.now?i.exports=function(){return performance.now()}:"undefined"!=typeof process&&null!==process&&process.hrtime?(i.exports=function(){return(t()-o)/1e6},e=process.hrtime,r=(t=function(){var t;return 1e9*(t=e())[0]+t[1]})(),s=1e9*process.uptime(),o=r-s):Date.now?(i.exports=function(){return Date.now()-n},n=Date.now()):(i.exports=function(){return(new Date).getTime()-n},n=(new Date).getTime())}).call(o);var s,a=i.exports,u=function(){function t(){}return t.resolveObjectValue=function(key,obj){var value=obj instanceof Function?t.resolveObjectValue(key,obj.apply(obj,[key])):obj instanceof Object?t.getDictValue(obj,key):obj;return value instanceof Function?t.resolveObjectValue(key,value.apply(value,[key])):value},t.resolveOperatorValue=function(value){return value instanceof Function?t.resolveOperatorValue(value.apply(value)):value},t.classOf=function(t){return t.constructor},t.getDictValue=function(obj,key){if(key in obj)return obj[key];var value=key.split(".").reduce((function(obj,t){return obj[t]||{}}),obj);return value instanceof Object&&0===Object.keys(value).length?void 0:value},t}(),c=function(){function t(t){this._id=t,this._start=0,this._duration=0,this._truths=0,this._falses=0}return t.prototype.start=function(){return this._start=a(),this},t.prototype.stop=function(t){return this._duration+=a()-this._start,t?this._truths++:this._falses++,this.getReport()},t.prototype.reset=function(){return this._truths=0,this._falses=0,this._duration=0,this},t.prototype.getReport=function(){return{id:this._id,duration:this._duration,truths:this._truths,falses:this._falses}},t}(),l=function(){function t(key,value){this._key=key,this._value=u.resolveOperatorValue(value)}return Object.defineProperty(t.prototype,"key",{get:function(){return this._key},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"value",{get:function(){return this._value},enumerable:!1,configurable:!0}),t}(),p=function(t){function e(key,value,opts){var e,n=t.call(this,key,value)||this;return n._opts=opts,(null===(e=n._opts)||void 0===e?void 0:e.isDate)&&(n._value,1)&&(n._value=new Date(Date.parse(n._value))),n._opts=r({isDate:n._value instanceof Date},opts),n._reporter=new c("".concat(n.getAlias(),":").concat(n.key)),n}return n(e,t),Object.defineProperty(e.prototype,"opts",{get:function(){return this._opts},enumerable:!1,configurable:!0}),e.prototype.getAlias=function(){return this.constructor.alias},e.prototype.getReport=function(){return this._reporter.getReport()},e.prototype.resetReport=function(){this._reporter.reset()},e.prototype.asJson=function(){return{type:this.getAlias(),ctorArgs:[this._key,this._value,this._opts]}},e.prototype.evaluate=function(obj){var t=u.resolveObjectValue(this._key,obj);this._reporter.start();var e=!1;return this instanceof f?e=t===this._value:this instanceof h?e=null==t:this instanceof _?e=t>this._value:this instanceof g?e=t>=this._value:this instanceof y?e=t<this._value:this instanceof d?e=t<=this._value:this instanceof v&&(e=this._valueRe.test(null==t?void 0:t.toString())),this._reporter.stop(e),e},e}(l),f=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="eq",e}(p),h=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="isnull",e}(p),_=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="gt",e}(p),g=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="gte",e}(p),y=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="lt",e}(p),d=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="lte",e}(p),v=function(t){function e(key,value,opts){var e=t.call(this,key,value,r({matchCase:!0,wildCard:"*"},opts))||this,n=e._value.toString().replace(/[-[\]/{}()+.\\^$|]/g,"\\$&").replace(new RegExp("\\".concat(e.opts.wildCard),"g"),".*").replace(/\?/g,"."),o=e.opts.matchCase?"":"i";return e._valueRe=new RegExp(n,o),e}return n(e,t),Object.defineProperty(e.prototype,"opts",{get:function(){return this._opts},enumerable:!1,configurable:!0}),e.alias="like",e}(p),w=function(t){function e(key,value,opts){return t.call(this,key,value,r(r({},opts),{matchCase:!1}))||this}return n(e,t),e.alias="ilike",e}(v),O=function(alias,func){this.alias=alias,this.func=func},b=function(t){function e(key,e){var n=t.call(this,key,"(custom operator)")||this;return n._def=e,n._reporter=new c("".concat(n.getAlias(),":").concat(n.key)),n}return n(e,t),e.fromJson=function(json,t){var n=json.ctorArgs,key=n[0],r=n[1];if(!(json.type in t)){var func=new Function("return ".concat(r))();t[json.type]=new O(json.type,func)}return new e(key,t[json.type])},e.prototype.getAlias=function(){return this._def.alias},e.prototype.getReport=function(){return this._reporter.getReport()},e.prototype.resetReport=function(){this._reporter.reset()},e.prototype.asJson=function(){return{isRuntime:!0,type:this.getAlias(),ctorArgs:[this.key,this._def.func.toString()]}},e.prototype.evaluate=function(obj){var t=u.resolveObjectValue(this.key,obj);this._reporter.start();var e=!!this._def.func.apply(this._def.func,[t]);return this._reporter.stop(e),e},e}(l),m=function(){function t(t){this._parent=t,this._operators=[],this._reporter=new c("".concat(this.getAlias()))}return t.fromJson=function(json,e,n){var r=new e[json.type](n);return json.operators.forEach((function(n){if(n.isLogical)return r.add(t.fromJson(n,e,r));if(n.isRuntime)return r.add(b.fromJson(n,e));var o=e[n.type];r.add(new(o.bind.apply(o,function(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}([void 0],n.ctorArgs,!1))))})),r},t.prototype.getParent=function(){return this._parent},t.prototype.getOperators=function(){return this._operators},t.prototype.getOperatorsTree=function(){var e=[],n=function(r){r.getOperators().forEach((function(r){e.push(r),r instanceof t&&n(r)}))};return n(this),e},t.prototype.add=function(t){return this._operators.push(t),t},t.prototype.clear=function(){this._operators=[]},t.prototype.getAlias=function(){return this.constructor.alias},t.prototype.getReport=function(){return this._reporter.getReport()},t.prototype.resetReport=function(){this._reporter.reset()},t.prototype.asJson=function(){return{type:this.getAlias(),isLogical:!0,operators:this.getOperators().map((function(t){return t.asJson()}))}},t.prototype.evaluate=function(obj){var t=!0;return this._reporter.start(),this._operators.length>0&&(this instanceof A?t=this._operators.every((function(t){return t.evaluate(obj)})):this instanceof R?t=this._operators.some((function(t){return t.evaluate(obj)})):this instanceof j&&(t=this._operators.every((function(t){return!t.evaluate(obj)})))),this._reporter.stop(t),t},t}(),A=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="and",e}(m),R=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="or",e}(m),j=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.alias="not",e}(m),k=((s={})[A.alias]=A,s[R.alias]=R,s[j.alias]=j,s[f.alias]=f,s[h.alias]=h,s[_.alias]=_,s[g.alias]=g,s[y.alias]=y,s[d.alias]=d,s[v.alias]=v,s[w.alias]=w,s),T=function(){function t(){this._this=this._getPolygraph(),this._logical=new A(this._this),k=r(r({},k),this._this._getClassDict())}return t.fromJson=function(json){var t="string"==typeof json?JSON.parse(json):json,e=new this;return e._setConfiguration(t.polygraphOpts),e._logical=m.fromJson(t,k,e),e},t.and=function(){var t=new this;return t._logical=new A(t),t},t.or=function(){var t=new this;return t._logical=new R(t),t},t.not=function(){var t=new this;return t._logical=new j(t),t},t.define=function(alias,func){if(alias in k)throw new Error("Operator:".concat(alias," already defined"));k[alias]=new O(alias,func)},t.getOperatorAlias=function(){return Object.keys(k).sort()},t.prototype.asJson=function(){return r(r({},this._logical.asJson()),{polygraphOpts:this._getConfiguration()})},t.prototype.asTree=function(){var t=function(e,n){var r;if(void 0===n&&(n=0),e instanceof m)return"".concat("#".repeat(n)).concat(e.getAlias(),"↘\n ").concat(e.getOperators().map((function(r){return t(r,n+e.getAlias().length)})).join("\n"));var o=e;return"".concat("#".repeat(n)).concat(o.key," ").concat(e.getAlias()," ").concat(null!==(r=o.value)&&void 0!==r?r:"")};return t(this._logical).split("\n").map((function(t){return t.trim().replace(new RegExp("#","g")," ")})).join("\n")},t.prototype.getReport=function(){var t=this._logical.getReport(),e={duration:t.duration,truths:t.truths,falses:t.falses,details:[t]};return this._logical.getOperatorsTree().forEach((function(t){var n=t.getReport();e.duration+=n.duration,e.truths+=n.truths,e.falses+=n.falses,e.details.push(n)})),e},t.prototype.resetReport=function(){return this._logical.resetReport(),this._logical.getOperatorsTree().forEach((function(t){return t.resetReport()})),this._this},t.prototype.evaluate=function(obj){return this._logical.evaluate(obj)},t.prototype.clear=function(){return this._logical.clear(),this._this},t.prototype.done=function(){for(;this._logical.getParent()!==this._this;)this.up();return this._this},t.prototype.up=function(){return this._logical.getParent()===this._this||(this._logical=this._logical.getParent()),this._this},t.prototype.down=function(){var t=this._logical.getOperators().find((function(t){return t instanceof m}));return t&&(this._logical=t),this._this},t.prototype.next=function(){var t=this._logical.getParent();if(t instanceof m){var e=t.getOperators().filter((function(t){return t instanceof m})),n=e.indexOf(this._logical);n<e.length-1&&(this._logical=e[n+1])}return this._this},t.prototype.prev=function(){var t=this._logical.getParent();if(t instanceof m){var e=t.getOperators().filter((function(t){return t instanceof m})),n=e.indexOf(this._logical);n>0&&(this._logical=e[n-1])}return this._this},t.prototype.clone=function(){return u.classOf(this._this).fromJson(this._this.asJson())},t.prototype.addPolygraph=function(t){return this._logical.add(t._logical),this._this},t.prototype.getKeysAndValues=function(){var t={},e=function(n){n.forEach((function(n){if(n instanceof m)return e(n.getOperators());var r=n;t[r.key]=t[r.key]?Array.isArray(t[r.key])?t[r.key].concat(r.value):[t[r.key],r.value]:r.value}))};return e(this._logical.getOperators()),t},t.prototype.and=function(){return this._logical=this._logical.add(new A(this._logical)),this._this},t.prototype.or=function(){return this._logical=this._logical.add(new R(this._logical)),this._this},t.prototype.not=function(){return this._logical=this._logical.add(new j(this._logical)),this._this},t.prototype.equals=function(key,value){return this._logical.add(new f(key,value)),this._this},t.prototype.eq=function(key,value){return this.equals(key,value)},t.prototype.isNull=function(key){return this._logical.add(new h(key,null)),this._this},t.prototype.greaterThan=function(key,value){return this._logical.add(new _(key,value)),this._this},t.prototype.gt=function(key,value){return this.greaterThan(key,value)},t.prototype.greaterThanEquals=function(key,value){return this._logical.add(new g(key,value)),this._this},t.prototype.gte=function(key,value){return this.greaterThanEquals(key,value)},t.prototype.lessThan=function(key,value){return this._logical.add(new y(key,value)),this._this},t.prototype.lt=function(key,value){return this.lessThan(key,value)},t.prototype.lessThanEquals=function(key,value){return this._logical.add(new d(key,value)),this._this},t.prototype.lte=function(key,value){return this.lessThanEquals(key,value)},t.prototype.like=function(key,value){return this._logical.add(new v(key,value)),this._this},t.prototype.ilike=function(key,value){return this._logical.add(new w(key,value)),this._this},t.prototype.any=function(key,t){if(t.length){var e=this._logical.add(new R(this._logical));t.forEach((function(value){return e.add(new f(key,value))}))}return this._this},t.prototype.operator=function(alias,key,value,opts){if(!(alias in k))throw new Error("Invalid operator alias:".concat(alias));var t=k[alias],e=t instanceof O?new b(key,t):new t(key,value,opts);return this._logical.add(e),this._this},t.prototype.op=function(alias,key,value,opts){return this.operator(alias,key,value,opts)},t}(),P=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.prototype._setConfiguration=function(){},e.prototype._getConfiguration=function(){return{}},e.prototype._getClassDict=function(){return{}},e.prototype._getPolygraph=function(){return this},e}(T);t.Polygraph=P,t.and=function(){return P.and()},t.define=function(alias,func){return P.define(alias,func)},t.fromJson=function(json){return P.fromJson(json)},t.getOperatorAlias=function(){return P.getOperatorAlias()},t.not=function(){return P.not()},t.or=function(){return P.or()},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=polygraph.umd.js.map