UNPKG

@ucast/core

Version:

git@github.com:stalniy/ucast.git

2 lines 4.7 kB
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{constructor(e,t){this.operator=e,this.value=t,Object.defineProperty(this,`_notes`,{writable:!0})}get notes(){return this._notes}addNote(e){this._notes=this._notes||[],this._notes.push(e)}},t=class extends e{},n=class extends t{constructor(e,t){if(!Array.isArray(t))throw Error(`"${e}" operator expects to receive an array of conditions`);super(e,t)}},r=class extends e{constructor(e,t,n){super(e,n),this.field=t}};const i=new t(`__null__`,null),a=Object.hasOwn||((e,t)=>Object.prototype.hasOwnProperty.call(e,t));function o(e,t){return t instanceof n&&t.operator===e}function s(e,t){let r;for(let i=0,a=t.length;i<a;i++){let a=t[i];if(a instanceof n){if(a.operator!==e){r&&r.push(a);continue}if(a.value.length===0)continue;r||(r=t.slice(0,i));let n=a.value;for(let e=0,t=n.length;e<t;e++)r.push(n[e])}else r&&r.push(a)}return r||t}function c(e,t){if(t.length===1)return t[0];if(t.length===0)return new n(e,t);let r=s(e,t);return r.length===1?r[0]:(r.length,new n(e,r))}const l=e=>e,u=()=>Object.create(null),d=Object.defineProperty(u(),`__@type@__`,{value:`ignore value`});function f(e,t,n=!1){if(!e||e&&e.constructor!==Object)return!1;for(let r in e)if(a(e,r)&&a(t,r)&&(!n||e[r]!==d))return!0;return!1}function p(e){let t=[];for(let n in e)a(e,n)&&e[n]!==d&&t.push(n);return t}function m(e,t){t!==i&&e.push(t)}const h=e=>c(`and`,e),g=e=>c(`or`,e);function _(e,t){let n=e[t];if(typeof n!=`function`)throw Error(`Unable to interpret "${String(t)}" condition. Did you forget to register interpreter for it?`);return n}function v(e){return e.operator}function y(e,t){let n=t,r=n&&n.getInterpreterName||v,i;switch(n?n.numberOfArguments:0){case 1:i=(t=>_(e,r(t,n))(t,a));break;case 3:i=((t,i,o)=>_(e,r(t,n))(t,i,o,a));break;default:i=((t,i)=>_(e,r(t,n))(t,i,a));break}let a={...n,interpret:i};return a.interpret}const b={compound(e,t,r){let i=(Array.isArray(t)?t:[t]).map(e=>r.parse(e));return new n(e.name,i)},field(e,t,n){return new r(e.name,n.field,t)},document(e,n){return new t(e.name,n)}};var x=class{constructor(e,t=u()){this.parse=this.parse.bind(this),this._options={operatorToConditionName:t.operatorToConditionName||l,defaultOperatorName:t.defaultOperatorName||`eq`,mergeFinalConditions:t.mergeFinalConditions||h},this._instructions=Object.keys(e).reduce((t,n)=>(t[n]={name:this._options.operatorToConditionName(n),...e[n]},t),{}),this._fieldInstructionContext={...t.fieldContext,field:``,query:{},parse:this.parse,hasOperators:e=>f(e,this._instructions,t.useIgnoreValue)},this._documentInstructionContext={...t.documentContext,parse:this.parse,query:{}},this._objectKeys=t.useIgnoreValue?p:Object.keys}setParse(e){this.parse=e,this._fieldInstructionContext.parse=e,this._documentInstructionContext.parse=e}parseField(e,t,n,r){let i=this._instructions[t];if(!i)throw Error(`Unsupported operator "${t}"`);if(i.type!==`field`)throw Error(`Unexpected ${i.type} operator "${t}" at field level`);return this._fieldInstructionContext.field=e,this._fieldInstructionContext.query=r,this.parseInstruction(i,n,this._fieldInstructionContext)}parseInstruction(e,t,n){return typeof e.validate==`function`&&e.validate(e,t),(e.parse||b[e.type])(e,t,n)}parseFieldOperators(e,t){let n=[],r=this._objectKeys(t);for(let i=0,a=r.length;i<a;i++){let a=r[i];if(!this._instructions[a])throw Error(`Field query for "${e}" may contain only operators or a plain object as a value`);m(n,this.parseField(e,a,t[a],t))}return n}parse(e){let t=[],n=this._objectKeys(e);this._documentInstructionContext.query=e;for(let r=0,i=n.length;r<i;r++){let i=n[r],a=e[i],o=this._instructions[i];if(o){if(o.type!==`document`&&o.type!==`compound`)throw Error(`Cannot use parsing instruction for operator "${i}" in "document" context as it is supposed to be used in "${o.type}" context`);m(t,this.parseInstruction(o,a,this._documentInstructionContext))}else this._fieldInstructionContext.hasOperators(a)?t.push(...this.parseFieldOperators(i,a)):m(t,this.parseField(i,this._options.defaultOperatorName,a,e))}return this._options.mergeFinalConditions(t)}};function S(e,t){return(n,...r)=>{let i=e(n,...r),a=t.bind(null,i);return Object.defineProperty(a,`ast`,{value:i}),a}}exports.CompoundCondition=n,exports.Condition=e,exports.DocumentCondition=t,exports.FieldCondition=r,exports.ITSELF=`__itself__`,exports.NULL_CONDITION=i,exports.ObjectQueryParser=x,exports.buildAnd=h,exports.buildOr=g,exports.createInterpreter=y,exports.createTranslatorFactory=S,exports.defaultInstructionParsers=b,exports.hasOperators=f,exports.identity=l,exports.ignoreValue=d,exports.isCompound=o,exports.object=u,exports.optimizedCompoundCondition=c; //# sourceMappingURL=index.js.map