@ucast/core
Version:
git@github.com:stalniy/ucast.git
2 lines • 4.57 kB
JavaScript
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)}};const r=`__itself__`;var i=class extends e{constructor(e,t,n){super(e,n),this.field=t}};const a=new t(`__null__`,null),o=Object.hasOwn||((e,t)=>Object.prototype.hasOwnProperty.call(e,t));function s(e,t){return t instanceof n&&t.operator===e}function c(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 l(e,t){if(t.length===1)return t[0];if(t.length===0)return new n(e,t);let r=c(e,t);return r.length===1?r[0]:(r.length,new n(e,r))}const u=e=>e,d=()=>Object.create(null),f=Object.defineProperty(d(),`__@type@__`,{value:`ignore value`});function p(e,t,n=!1){if(!e||e&&e.constructor!==Object)return!1;for(let r in e)if(o(e,r)&&o(t,r)&&(!n||e[r]!==f))return!0;return!1}function m(e){let t=[];for(let n in e)o(e,n)&&e[n]!==f&&t.push(n);return t}function h(e,t){t!==a&&e.push(t)}const g=e=>l(`and`,e),_=e=>l(`or`,e);function v(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 y(e){return e.operator}function b(e,t){let n=t,r=n&&n.getInterpreterName||y,i;switch(n?n.numberOfArguments:0){case 1:i=(t=>v(e,r(t,n))(t,a));break;case 3:i=((t,i,o)=>v(e,r(t,n))(t,i,o,a));break;default:i=((t,i)=>v(e,r(t,n))(t,i,a));break}let a={...n,interpret:i};return a.interpret}const x={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 i(e.name,n.field,t)},document(e,n){return new t(e.name,n)}};var S=class{constructor(e,t=d()){this.parse=this.parse.bind(this),this._options={operatorToConditionName:t.operatorToConditionName||u,defaultOperatorName:t.defaultOperatorName||`eq`,mergeFinalConditions:t.mergeFinalConditions||g},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=>p(e,this._instructions,t.useIgnoreValue)},this._documentInstructionContext={...t.documentContext,parse:this.parse,query:{}},this._objectKeys=t.useIgnoreValue?m: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||x[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`);h(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`);h(t,this.parseInstruction(o,a,this._documentInstructionContext))}else this._fieldInstructionContext.hasOperators(a)?t.push(...this.parseFieldOperators(i,a)):h(t,this.parseField(i,this._options.defaultOperatorName,a,e))}return this._options.mergeFinalConditions(t)}};function C(e,t){return(n,...r)=>{let i=e(n,...r),a=t.bind(null,i);return Object.defineProperty(a,`ast`,{value:i}),a}}export{n as CompoundCondition,e as Condition,t as DocumentCondition,i as FieldCondition,r as ITSELF,a as NULL_CONDITION,S as ObjectQueryParser,g as buildAnd,_ as buildOr,b as createInterpreter,C as createTranslatorFactory,x as defaultInstructionParsers,p as hasOperators,u as identity,f as ignoreValue,s as isCompound,d as object,l as optimizedCompoundCondition};
//# sourceMappingURL=index.mjs.map