UNPKG

@wootapa/polygraph

Version:

Tests objects with queries constructed using logical and comparison operators.

4 lines (3 loc) 14.1 kB
/*! @wootapa/polygraph v1.0.1 | author:Andreas Petersson | license:MIT */ var t=function(e,n){return t=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])},t(e,n)};function e(e,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=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var n=function(){return n=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},n.apply(this,arguments)};var r="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},o={exports:{}};(function(){var t,e,n,r,i,s;"undefined"!=typeof performance&&null!==performance&&performance.now?o.exports=function(){return performance.now()}:"undefined"!=typeof process&&null!==process&&process.hrtime?(o.exports=function(){return(t()-i)/1e6},e=process.hrtime,r=(t=function(){var t;return 1e9*(t=e())[0]+t[1]})(),s=1e9*process.uptime(),i=r-s):Date.now?(o.exports=function(){return Date.now()-n},n=Date.now()):(o.exports=function(){return(new Date).getTime()-n},n=(new Date).getTime())}).call(r);var i,s=o.exports,a=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}(),u=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=s(),this},t.prototype.stop=function(t){return this._duration+=s()-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}(),c=function(){function t(key,value){this._key=key,this._value=a.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}(),l=function(t){function r(key,value,opts){var e,r=t.call(this,key,value)||this;return r._opts=opts,(null===(e=r._opts)||void 0===e?void 0:e.isDate)&&(r._value,1)&&(r._value=new Date(Date.parse(r._value))),r._opts=n({isDate:r._value instanceof Date},opts),r._reporter=new u("".concat(r.getAlias(),":").concat(r.key)),r}return e(r,t),Object.defineProperty(r.prototype,"opts",{get:function(){return this._opts},enumerable:!1,configurable:!0}),r.prototype.getAlias=function(){return this.constructor.alias},r.prototype.getReport=function(){return this._reporter.getReport()},r.prototype.resetReport=function(){this._reporter.reset()},r.prototype.asJson=function(){return{type:this.getAlias(),ctorArgs:[this._key,this._value,this._opts]}},r.prototype.evaluate=function(obj){var t=a.resolveObjectValue(this._key,obj);this._reporter.start();var e=!1;return this instanceof p?e=t===this._value:this instanceof h?e=null==t:this instanceof f?e=t>this._value:this instanceof _?e=t>=this._value:this instanceof g?e=t<this._value:this instanceof y?e=t<=this._value:this instanceof d&&(e=this._valueRe.test(null==t?void 0:t.toString())),this._reporter.stop(e),e},r}(c),p=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="eq",n}(l),h=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="isnull",n}(l),f=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="gt",n}(l),_=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="gte",n}(l),g=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="lt",n}(l),y=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="lte",n}(l),d=function(t){function r(key,value,opts){var e=t.call(this,key,value,n({matchCase:!0,wildCard:"*"},opts))||this,r=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(r,o),e}return e(r,t),Object.defineProperty(r.prototype,"opts",{get:function(){return this._opts},enumerable:!1,configurable:!0}),r.alias="like",r}(l),v=function(t){function r(key,value,opts){return t.call(this,key,value,n(n({},opts),{matchCase:!1}))||this}return e(r,t),r.alias="ilike",r}(d),w=function(alias,func){this.alias=alias,this.func=func},O=function(t){function n(key,e){var n=t.call(this,key,"(custom operator)")||this;return n._def=e,n._reporter=new u("".concat(n.getAlias(),":").concat(n.key)),n}return e(n,t),n.fromJson=function(json,t){var e=json.ctorArgs,key=e[0],r=e[1];if(!(json.type in t)){var func=new Function("return ".concat(r))();t[json.type]=new w(json.type,func)}return new n(key,t[json.type])},n.prototype.getAlias=function(){return this._def.alias},n.prototype.getReport=function(){return this._reporter.getReport()},n.prototype.resetReport=function(){this._reporter.reset()},n.prototype.asJson=function(){return{isRuntime:!0,type:this.getAlias(),ctorArgs:[this.key,this._def.func.toString()]}},n.prototype.evaluate=function(obj){var t=a.resolveObjectValue(this.key,obj);this._reporter.start();var e=!!this._def.func.apply(this._def.func,[t]);return this._reporter.stop(e),e},n}(c),m=function(){function t(t){this._parent=t,this._operators=[],this._reporter=new u("".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(O.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 b?t=this._operators.every((function(t){return t.evaluate(obj)})):this instanceof A?t=this._operators.some((function(t){return t.evaluate(obj)})):this instanceof R&&(t=this._operators.every((function(t){return!t.evaluate(obj)})))),this._reporter.stop(t),t},t}(),b=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="and",n}(m),A=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="or",n}(m),R=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.alias="not",n}(m),k=((i={})[b.alias]=b,i[A.alias]=A,i[R.alias]=R,i[p.alias]=p,i[h.alias]=h,i[f.alias]=f,i[_.alias]=_,i[g.alias]=g,i[y.alias]=y,i[d.alias]=d,i[v.alias]=v,i),j=function(){function t(){this._this=this._getPolygraph(),this._logical=new b(this._this),k=n(n({},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 b(t),t},t.or=function(){var t=new this;return t._logical=new A(t),t},t.not=function(){var t=new this;return t._logical=new R(t),t},t.define=function(alias,func){if(alias in k)throw new Error("Operator:".concat(alias," already defined"));k[alias]=new w(alias,func)},t.getOperatorAlias=function(){return Object.keys(k).sort()},t.prototype.asJson=function(){return n(n({},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 a.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 b(this._logical)),this._this},t.prototype.or=function(){return this._logical=this._logical.add(new A(this._logical)),this._this},t.prototype.not=function(){return this._logical=this._logical.add(new R(this._logical)),this._this},t.prototype.equals=function(key,value){return this._logical.add(new p(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 f(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 _(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 g(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 y(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 d(key,value)),this._this},t.prototype.ilike=function(key,value){return this._logical.add(new v(key,value)),this._this},t.prototype.any=function(key,t){if(t.length){var e=this._logical.add(new A(this._logical));t.forEach((function(value){return e.add(new p(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 w?new O(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 n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.prototype._setConfiguration=function(){},n.prototype._getConfiguration=function(){return{}},n.prototype._getClassDict=function(){return{}},n.prototype._getPolygraph=function(){return this},n}(j),T=function(){return P.and()},E=function(){return P.or()},J=function(){return P.not()},x=function(json){return P.fromJson(json)},D=function(alias,func){return P.define(alias,func)},C=function(){return P.getOperatorAlias()};export{P as Polygraph,T as and,D as define,x as fromJson,C as getOperatorAlias,J as not,E as or}; //# sourceMappingURL=polygraph.esm.js.map