UNPKG

mingo

Version:

MongoDB query language for in-memory objects

1 lines 78.3 kB
var mingo=(()=>{var Xr=Object.defineProperty;var Xn=Object.getOwnPropertyDescriptor;var Zn=Object.getOwnPropertyNames;var to=Object.prototype.hasOwnProperty;var lt=(t,r)=>{for(var n in r)Xr(t,n,{get:r[n],enumerable:!0})},ro=(t,r,n,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of Zn(r))!to.call(t,o)&&o!==n&&Xr(t,o,{get:()=>r[o],enumerable:!(e=Xn(r,o))||e.enumerable});return t};var eo=t=>ro(Xr({},"__esModule",{value:!0}),t);var sa={};lt(sa,{Aggregator:()=>et,ProcessingMode:()=>jt,Query:()=>B,aggregate:()=>oa,createUpdater:()=>Hr,find:()=>na,update:()=>Ye});var Dt=class extends Error{},Jt=Symbol("missing"),Je="mingo: cycle detected while processing object/array",Xt=t=>{let r=vt(t),n=0,e=r.length;for(;e;)n=(n<<5)-n^r.charCodeAt(--e);return n>>>0},xt=t=>typeof t!="object"&&typeof t!="function"||t===null,Xe=t=>xt(t)||k(t)||J(t),Ze={undefined:1,null:2,number:3,string:4,symbol:5,object:6,array:7,arraybuffer:8,boolean:9,date:10,regexp:11,function:12};function w(t,r){t===Jt&&(t=void 0),r===Jt&&(r=void 0);let n=100,[e,o]=[t,r].map(s=>Ze[rn(s)?"arraybuffer":q(s)]??n);return e!==o?e-o:(e===n&&(t=vt(t),r=vt(r)),L(t,r)?0:t<r?-1:1)}var Q=class t extends Map{#t=Xt;#r=new Map;#e=r=>{let n=this.#t(r);return[(this.#r.get(n)||[]).find(e=>L(e,r)),n]};constructor(){super()}static init(r){let n=new t;return r&&(n.#t=r),n}clear(){super.clear(),this.#r.clear()}delete(r){if(xt(r))return super.delete(r);let[n,e]=this.#e(r);return super.delete(n)?(this.#r.set(e,this.#r.get(e).filter(o=>!L(o,n))),!0):!1}get(r){if(xt(r))return super.get(r);let[n,e]=this.#e(r);return super.get(n)}has(r){if(xt(r))return super.has(r);let[n,e]=this.#e(r);return super.has(n)}set(r,n){if(xt(r))return super.set(r,n);let[e,o]=this.#e(r);if(super.has(e))super.set(e,n);else{super.set(r,n);let s=this.#r.get(o)||[];s.push(r),this.#r.set(o,s)}return this}get size(){return super.size}};function m(t,r){if(!t)throw new Dt(r)}function q(t){if(t===null)return"null";let r=typeof t;if(r!=="object"&&r in Ze)return r;if(y(t))return"array";if(k(t))return"date";if(J(t))return"regexp";let n=Object.prototype.toString.call(t);return n==="[object Object]"?t?.constructor?.name?.toLowerCase()||"object":n.substring(8,n.length-1).toLowerCase()}var ft=t=>typeof t=="boolean",h=t=>typeof t=="string",no=t=>typeof t=="symbol",b=t=>!isNaN(t)&&typeof t=="number",y=Array.isArray,j=t=>q(t)==="object",tn=t=>!xt(t),k=t=>t instanceof Date,J=t=>t instanceof RegExp,Mt=t=>typeof t=="function",l=t=>t==null,Y=(t,r=!0)=>!!t||r&&t==="",G=t=>l(t)||h(t)&&!t||y(t)&&t.length===0||j(t)&&Object.keys(t).length===0,nt=t=>y(t)?t:[t],M=(t,r)=>!!t&&Object.prototype.hasOwnProperty.call(t,r),rn=t=>typeof ArrayBuffer<"u"&&ArrayBuffer.isView(t),X=(t,r)=>{if(l(t)||ft(t)||b(t)||h(t))return t;if(k(t))return new Date(t);if(J(t))return new RegExp(t);if(rn(t)){let n=t.constructor;return new n(t)}if(r instanceof Set||(r=new Set),r.has(t))throw new Error(Je);r.add(t);try{if(y(t)){let n=new Array(t.length);for(let e=0;e<t.length;e++)n[e]=X(t[e],r);return n}if(j(t)){let n={};for(let e of Object.keys(t))n[e]=X(t[e],r);return n}}finally{r.delete(t)}return t},Ge=t=>t===Jt;function Or(t,r){if(Ge(t)||l(t))return r;if(Ge(r)||l(r))return t;if(xt(t)||xt(r))return r;y(t)&&y(r)&&m(t.length===r.length,"arrays must be of equal length to merge.");for(let n of Object.keys(r))t[n]=Or(t[n],r[n]);return t}function Pt(t,r=Xt){let n=[Q.init(r),Q.init(r)];if(t.length===0)return[];if(t.some(e=>e.length===0))return[];if(t.length===1)return[...t];t[t.length-1].forEach(e=>n[0].set(e,!0));for(let e=t.length-2;e>-1;e--){if(t[e].forEach(o=>{n[0].has(o)&&n[1].set(o,!0)}),n[1].size===0)return[];n.reverse(),n[1].clear()}return Array.from(n[0].keys())}function ot(t,r=1){let n=new Array;function e(o,s){for(let p=0,i=o.length;p<i;p++)y(o[p])&&(s>0||s<0)?e(o[p],Math.max(-1,s-1)):n.push(o[p])}return e(t,r),n}function oo(t){let r={};for(;t;){for(let n of Object.getOwnPropertyNames(t))n in r||(r[n]=t[n]);t=Object.getPrototypeOf(t)}return r}var en=t=>t!=null&&t.toString!==Object.prototype.toString;function L(t,r){if(t===r||Object.is(t,r))return!0;if(t===null||r===null||typeof t!=typeof r||typeof t!="object")return!1;if(k(t))return k(r)&&+t==+r;if(J(t))return J(r)&&t.toString()===r.toString();let n=q(t);if(n!==q(r))return!1;switch(n){case"array":if(t.length!==r.length)return!1;for(let e=0,o=t.length;e<o;e++)if(!L(t[e],r[e]))return!1;return!0;case"object":{let e=Object.keys(t),o=Object.keys(r);if(e.length!==o.length)return!1;for(let s of e)if(!(s in r&&L(t[s],r[s])))return!1;return!0}default:return en(t)&&t.toString()===r.toString()}}function Rt(t,r=Xt){let n=Q.init(r);return t.forEach(e=>n.set(e,!0)),Array.from(n.keys())}function vt(t,r){if(t===null)return"null";if(t===void 0)return"undefined";if(h(t)||b(t)||ft(t))return JSON.stringify(t);if(k(t))return t.toISOString();if(J(t)||no(t)||Mt(t))return t.toString();if(r instanceof Set||(r=new Set),r.has(t))throw new Error(Je);try{if(r.add(t),y(t))return"["+t.map(e=>vt(e,r)).join(",")+"]";if(j(t))return"{"+Object.keys(t).sort().map(o=>`${o}:${vt(t[o],r)}`).join()+"}";let n=en(t)?t.toString():vt(oo(t),r);return q(t)+"("+n+")"}finally{r.delete(t)}}function te(t,r){return l(t)?null:(r=r||Xt,r(t))}function Ut(t,r,n=Xt){if(t.length<1)return new Map;let e=Q.init(n);for(let o=0;o<t.length;o++){let s=t[o],p=r(s,o)??null,i=e.get(p);i?i.push(s):(i=[s],e.set(p,i))}return e}function Zr(t,r){return tn(t)?t[r]:void 0}function so(t,r){if(r<1)return t;for(;r--&&t.length===1;)t=t[0];return t}function S(t,r,n){let e=0;function o(p,i){let a=p;for(let c=0;c<i.length;c++){let f=i[c];if(/^\d+$/.exec(f)===null&&y(a)){if(c===0&&e>0)break;e+=1;let A=i.slice(c);a=a.reduce((d,x)=>{let g=o(x,A);return g!==void 0&&d.push(g),d},[]);break}else a=Zr(a,f);if(a===void 0)break}return a}let s=Xe(t)?t:o(t,r.split("."));return y(s)&&n?.unwrapArray?so(s,e):s}function At(t,r,n){let e=r.indexOf("."),o=e==-1?r:r.substring(0,e),s=r.substring(e+1),p=e!=-1;if(y(t)){let c=/^\d+$/.test(o),f=c&&n?.preserveIndex?[...t]:[];if(c){let O=parseInt(o),A=Zr(t,O);p&&(A=At(A,s,n)),n?.preserveIndex?f[O]=A:f.push(A)}else for(let O of t){let A=At(O,r,n);n?.preserveMissing?f.push(A??Jt):(A!=null||n?.preserveIndex)&&f.push(A)}return f}let i=n?.preserveKeys?{...t}:{},a=Zr(t,o);if(p&&(a=At(a,s,n)),a!==void 0)return i[o]=a,i}function yr(t){if(y(t))for(let r=t.length-1;r>=0;r--)t[r]===Jt?t.splice(r,1):yr(t[r]);else if(j(t))for(let r in t)M(t,r)&&yr(t[r])}var He=/^\d+$/;function _t(t,r,n,e){let o=r.split("."),s=o[0],p=o.slice(1).join(".");if(o.length===1)(j(t)||y(t)&&He.test(s))&&n(t,s);else{e?.buildGraph&&l(t[s])&&(t[s]={});let i=t[s];if(!i)return;let a=!!(o.length>1&&He.test(o[1]));y(i)&&e?.descendArray&&!a?i.forEach(c=>_t(c,p,n,e)):_t(i,p,n,e)}}function gt(t,r,n){_t(t,r,(e,o)=>{e[o]=Mt(n)?n(e[o]):n},{buildGraph:!0})}function Tt(t,r,n){_t(t,r,(e,o)=>{y(e)?e.splice(parseInt(o),1):j(e)&&delete e[o]},n)}var io=/^\$[a-zA-Z0-9_]+$/;function Z(t){return io.test(t)}function Ar(t){if(Xe(t))return J(t)?{$regex:t}:{$eq:t};if(tn(t)){if(!Object.keys(t).some(Z))return{$eq:t};if(M(t,"$regex")){let r={...t};return r.$regex=new RegExp(t.$regex,t.$options),delete r.$options,r}}return t}function Vt(t,r,n=w){let e=0,o=t.length-1;for(;e<=o;){let s=Math.round(e+(o-e)/2);if(n(r,t[s])<0)o=s-1;else if(n(r,t[s])>0)e=s+1;else return s}return e}var jt=(o=>(o[o.CLONE_OFF=0]="CLONE_OFF",o[o.CLONE_INPUT=1]="CLONE_INPUT",o[o.CLONE_OUTPUT=2]="CLONE_OUTPUT",o[o.CLONE_ALL=3]="CLONE_ALL",o))(jt||{}),I=class t{#t;#r;#e;constructor(r,n,e){this.#t=r,this.update(n,e)}static init(r,n,e){return r instanceof t?new t(r.#t,r.root??n,{...r.#e,...e,variables:Object.assign({},r.#e?.variables,e?.variables)}):new t(r,n,e)}update(r,n){this.#r=r;let e=Object.assign({},this.#e?.variables,n?.variables);return Object.keys(e).length?this.#e={...n,variables:e}:this.#e=n??{},this}getOptions(){return Object.freeze({...this.#t,context:ht.from(this.#t.context)})}get root(){return this.#r}get local(){return this.#e}get idKey(){return this.#t.idKey}get collation(){return this.#t?.collation}get processingMode(){return this.#t?.processingMode||0}get useStrictMode(){return this.#t?.useStrictMode}get scriptEnabled(){return this.#t?.scriptEnabled}get useGlobalContext(){return this.#t?.useGlobalContext}get hashFunction(){return this.#t?.hashFunction}get collectionResolver(){return this.#t?.collectionResolver}get jsonSchemaValidator(){return this.#t?.jsonSchemaValidator}get variables(){return this.#t?.variables}get context(){return this.#t?.context}};function yt(t){return t instanceof I?t.getOptions():Object.freeze({idKey:"_id",scriptEnabled:!0,useStrictMode:!0,useGlobalContext:!0,processingMode:0,...t,context:t?.context?ht.from(t?.context):ht.init()})}var ht=class t{#t=new Map;constructor(){}static init(){return new t}static from(r){let n=t.init();return l(r)||r.#t.forEach((e,o)=>n.addOperators(o,e)),n}addOperators(r,n){this.#t.has(r)||this.#t.set(r,{});for(let[e,o]of Object.entries(n))this.getOperator(r,e)||(this.#t.get(r)[e]=o);return this}getOperator(r,n){return(this.#t.get(r)??{})[n]??null}addAccumulatorOps(r){return this.addOperators("accumulator",r)}addExpressionOps(r){return this.addOperators("expression",r)}addQueryOps(r){return this.addOperators("query",r)}addPipelineOps(r){return this.addOperators("pipeline",r)}addProjectionOps(r){return this.addOperators("projection",r)}addWindowOps(r){return this.addOperators("window",r)}},st=ht.init(),po={accumulator:st.addAccumulatorOps.bind(st),expression:st.addExpressionOps.bind(st),pipeline:st.addPipelineOps.bind(st),projection:st.addProjectionOps.bind(st),query:st.addQueryOps.bind(st),window:st.addWindowOps.bind(st)};function pt(t,r){for(let[n,e]of Object.entries(r)){m(Mt(e)&&Z(n),`'${n}' is not a valid operator`);let o=tt(t,n,null);m(!o||e===o,`${n} already exists for '${t}' operators. Cannot change operator function once registered.`)}po[t](r)}function tt(t,r,n){let{context:e,useGlobalContext:o}=n||{},s=e?e.getOperator(t,r):null;return!s&&o?st.getOperator(t,r):s}function u(t,r,n,e){let o=I.init(e,t);return n&&Z(n)?nn(t,r,n,o):br(t,r,o)}var ao=["$$ROOT","$$CURRENT","$$REMOVE","$$NOW"];function br(t,r,n){if(h(r)&&r.length>0&&r[0]==="$"){if(mo.includes(r))return r;let e=n.root,o=r.split(".");if(ao.includes(o[0])){switch(o[0]){case"$$ROOT":break;case"$$CURRENT":e=t;break;case"$$REMOVE":e=void 0;break;case"$$NOW":e=new Date;break}r=r.slice(o[0].length+1)}else if(o[0].slice(0,2)==="$$"){e=Object.assign({},n.variables,{this:t},n?.local?.variables);let s=o[0].slice(2);m(M(e,s),`Use of undefined variable: ${s}`),r=r.slice(2)}else r=r.slice(1);return r===""?e:S(e,r)}if(y(r))return r.map(e=>br(t,e,n));if(j(r)){let e={},o=Object.entries(r);for(let[s,p]of o){if(Z(s))return m(o.length==1,"expression must have single operator."),nn(t,p,s,n);e[s]=br(t,p,n)}return e}return r}function nn(t,r,n,e){let o=tt("expression",n,e);if(o)return o(t,r,e);let s=tt("accumulator",n,e);return m(!!s,`accumulator '${n}' is not registered.`),y(t)||(t=br(t,r,e),r=null),m(y(t),`arguments must resolve to array for ${n}.`),s(t,r,e)}var mo=["$$KEEP","$$PRUNE","$$DESCEND"];function dr(t,r,n){let e=u(t,r,null,n);switch(e){case"$$KEEP":return t;case"$$PRUNE":return;case"$$DESCEND":{if(!M(r,"$cond"))return t;let o={};for(let[s,p]of Object.entries(t))if(y(p)){let i=new Array;for(let a of p)j(a)&&(a=dr(a,r,n.update(a))),l(a)||i.push(a);o[s]=i}else if(j(p)){let i=dr(p,r,n.update(p));l(i)||(o[s]=i)}else o[s]=p;return o}default:return e}}var pe={};lt(pe,{$accumulator:()=>uo,$addToSet:()=>co,$avg:()=>lo,$bottom:()=>bo,$bottomN:()=>re,$count:()=>xo,$covariancePop:()=>go,$covarianceSamp:()=>ho,$first:()=>ee,$firstN:()=>hr,$last:()=>ne,$lastN:()=>jr,$max:()=>jo,$maxN:()=>Er,$median:()=>oe,$mergeObjects:()=>se,$min:()=>Eo,$minN:()=>$r,$percentile:()=>Zt,$push:()=>$,$stdDevPop:()=>$o,$stdDevSamp:()=>Io,$sum:()=>To,$top:()=>wo,$topN:()=>ie});var uo=(t,r,n)=>{if(m(!!n&&n.scriptEnabled,"$accumulator operator requires 'scriptEnabled' option to be true"),t.length==0)return r.initArgs;let e=I.init(n),o=u({},r.initArgs||[],null,e.update(e?.local?.groupId||{})),s=r.init.call(null,...o);for(let p of t){let i=u(p,r.accumulateArgs,null,e.update(p));s=r.accumulate.call(null,s,...i)}return r.finalize?r.finalize.call(null,s):s};var $=(t,r,n)=>{if(l(r))return t;let e=I.init(n);return t.map(o=>u(o,r,null,e.update(o)))};var co=(t,r,n)=>Rt($(t,r,n),n?.hashFunction);var lo=(t,r,n)=>{let e=$(t,r,n).filter(b);return e.reduce((s,p)=>s+p,0)/(e.length||1)};function N(t){return t instanceof Ft?t:new Ft(t)}function bt(...t){let r=0;return N(()=>{for(;r<t.length;){let n=t[r].next();if(!n.done)return n;r++}return{done:!0}})}function fo(t){return!!t&&typeof t=="object"&&typeof t?.next=="function"}function yo(t){return!!t&&(typeof t=="object"||typeof t=="function")&&typeof t[Symbol.iterator]=="function"}var Ft=class{#t=[];#r=[];#e;#o=!1;constructor(r){let n=yo(r)?r[Symbol.iterator]():fo(r)?r:typeof r=="function"?{next:r}:null;m(!!n,"Iterator must be initialized with an iterable or function.");let e=-1,o={done:!1};this.#e=()=>{for(;!o.done&&(o=n.next(),!o.done);){let s=o.value;if(e++,this.#t.every(({op:i,fn:a})=>{let c=a(s,e);return i==="map"?!!(s=c)||!0:c}))return{value:s,done:!1}}return{done:!0}}}push(r,n){return this.#t.push({op:r,fn:n}),this}next(){return this.#e()}map(r){return this.push("map",r)}filter(r){return this.push("filter",r)}take(r){return r>0?this.filter(n=>!(r===0||r--===0)):this}drop(r){return r>0?this.filter(n=>r===0||r--===0):this}transform(r){let n=this,e;return N(()=>(e||(e=N(r(n.value()))),e.next()))}value(){for(;!this.#o;){let{done:r,value:n}=this.#e();r||this.#r.push(n),this.#o=r}return this.#r}each(r){for(;;){let n=this.next();if(n.done)break;if(r(n.value)===!1)return!1}return!0}reduce(r,n){let e=this.next();for(n===void 0&&!e.done&&(n=e.value,e=this.next());!e.done;)n=r(n,e.value),e=this.next();return n}size(){return this.reduce((r,n)=>++r,0)}[Symbol.iterator](){return this}};var H=(t,r,n)=>{if(G(r)||!j(r))return t;let e=w,o=n.collation;return j(o)&&h(o.locale)&&(e=Ao(o)),t.transform(s=>{let p=Object.keys(r);for(let i of p.reverse()){let a=Ut(s,O=>S(O,i),n.hashFunction),c=Array.from(a.keys()).sort(e);r[i]===-1&&c.reverse();let f=0;for(let O of c)for(let A of a.get(O))s[f++]=A;m(f==s.length,"bug: counter must match collection size.")}return s})},Oo={1:"base",2:"accent",3:"variant"};function Ao(t){let r={sensitivity:Oo[t.strength||3],caseFirst:t.caseFirst==="off"?"false":t.caseFirst||"false",numeric:t.numericOrdering||!1,ignorePunctuation:t.alternate==="shifted"};t.caseLevel===!0&&(r.sensitivity==="base"&&(r.sensitivity="case"),r.sensitivity==="accent"&&(r.sensitivity="variant"));let n=new Intl.Collator(t.locale,r);return(e,o)=>{if(!h(e)||!h(o))return w(e,o);let s=n.compare(e,o);return s<0?-1:s>0?1:0}}var re=(t,r,n)=>{let e=I.init(n),o=u(e.local.groupId,r.n,null,e),s=H(N(t),r.sortBy,n).value(),p=s.length;return $(p<=o?s:s.slice(p-o),r.output,e)};var bo=(t,r,n)=>re(t,{...r,n:1},n);var xo=(t,r,n)=>t.length;function xr(t,r=!0){let n=t.reduce((s,p)=>s+p,0),e=t.length||1,o=n/e;return Math.sqrt(t.reduce((s,p)=>s+Math.pow(p-o,2),0)/(e-Number(r)))}function gr(t,r=!0){if(!t)return null;if(t.length<2)return r?null:0;let n=0,e=0;for(let[s,p]of t)n+=s,e+=p;n/=t.length,e/=t.length;let o=0;for(let[s,p]of t)o+=(s-n)*(p-e);return o/(t.length-Number(r))}var go=(t,r,n)=>gr($(t,r,n),!1);var ho=(t,r,n)=>gr($(t,r,n),!0);var ee=(t,r,n)=>{if(t.length===0)return;let e=I.init(n).update(t[0]);return u(t[0],r,null,e)};var hr=(t,r,n)=>{let e=I.init(n),o=t.length,s=u(e?.local?.groupId,r.n,null,e);return m(s>0,"$firstN: 'n' must resolve to a positive integer."),$(o<=s?t:t.slice(0,s),r.input,n)};var ne=(t,r,n)=>{if(t.length===0)return;let e=t[t.length-1],o=I.init(n).update(e);return u(e,r,null,o)};var jr=(t,r,n)=>{let e=I.init(n),o=t.length,s=u(e?.local?.groupId,r.n,null,e);return $(o<=s?t:t.slice(o-s),r.input,n)};var jo=(t,r,n)=>{let e=$(t,r,n);if(G(e))return null;m(y(e),"$max: input must resolve to array");let o=e[0];for(let s of e)l(s)||isNaN(s)||w(s,o)>=0&&(o=s);return o};var Er=(t,r,n)=>{let e=I.init(n),o=t.length,s=u(e?.local?.groupId,r.n,null,e),p=$(t,r.input,n).filter(i=>!l(i));return p.sort((i,a)=>-1*w(i,a)),o<=s?p:p.slice(0,s)};var Zt=(t,r,n)=>{let e=$(t,r.input,n).filter(b).sort(),o=$(r.p,"$$CURRENT",n).filter(b),s=r.method||"approximate";return o.map(p=>{m(p>0&&p<=1,`percentile value must be between 0 (exclusive) and 1 (inclusive): invalid '${p}'.`);let i=p*(e.length-1)+1,a=Math.floor(i),c=i===a?e[i-1]:e[a-1]+i%1*(e[a]-e[a-1]||0);switch(s){case"exact":return c;case"approximate":{let f=Vt(e,c);return f/e.length>=p?e[Math.max(f-1,0)]:e[f]}}})};var oe=(t,r,n)=>Zt(t,{...r,p:[.5]},n).pop();var se=(t,r,n)=>{let e={};for(let o of t)if(!l(o))for(let s of Object.keys(o))o[s]!==void 0&&(e[s]=o[s]);return e};var Eo=(t,r,n)=>{let e=$(t,r,n);if(G(e))return null;m(y(e),"$min: input must resolve to array");let o=e[0];for(let s of e)l(s)||isNaN(s)||w(s,o)<=0&&(o=s);return o};var $r=(t,r,n)=>{let e=I.init(n),o=t.length,s=u(e?.local?.groupId,r.n,null,e),p=$(t,r.input,n).filter(i=>!l(i));return p.sort(w),o<=s?p:p.slice(0,s)};var $o=(t,r,n)=>xr($(t,r,n).filter(b),!1);var Io=(t,r,n)=>xr($(t,r,n).filter(b),!0);var To=(t,r,n)=>y(t)?b(r)?t.length*r:$(t,r,n).filter(b).reduce((o,s)=>o+s,0):0;var ie=(t,r,n)=>{let e=I.init(n),{n:o,sortBy:s}=u(e.local.groupId,r,null,e),p=H(N(t),s,n).take(o).value();return $(p,r.output,e)};var wo=(t,r,n)=>ie(t,{...r,n:1},n);var Me={};lt(Me,{$abs:()=>No,$acos:()=>Ti,$acosh:()=>wi,$add:()=>So,$allElementsTrue:()=>Js,$and:()=>yn,$anyElementTrue:()=>Xs,$arrayElemAt:()=>zo,$arrayToObject:()=>Qo,$asin:()=>Ni,$asinh:()=>Si,$atan:()=>ki,$atan2:()=>Ci,$atanh:()=>vi,$bitAnd:()=>Os,$bitNot:()=>As,$bitOr:()=>bs,$bitXor:()=>ds,$ceil:()=>ko,$cmp:()=>bn,$concat:()=>oi,$concatArrays:()=>Yo,$cond:()=>xs,$convert:()=>Li,$cos:()=>Di,$cosh:()=>_i,$dateAdd:()=>Nt,$dateDiff:()=>ws,$dateFromParts:()=>ks,$dateFromString:()=>Ms,$dateSubtract:()=>Ps,$dateToParts:()=>Rs,$dateToString:()=>Te,$dateTrunc:()=>Ws,$dayOfMonth:()=>Oe,$dayOfWeek:()=>Ae,$dayOfYear:()=>be,$degreesToRadians:()=>Pi,$divide:()=>Co,$eq:()=>dn,$exp:()=>vo,$filter:()=>Ko,$first:()=>qo,$firstN:()=>Go,$floor:()=>Do,$function:()=>le,$getField:()=>Qs,$gt:()=>xn,$gte:()=>gn,$hour:()=>de,$ifNull:()=>ce,$in:()=>Ho,$indexOfArray:()=>Jo,$indexOfBytes:()=>si,$isArray:()=>Xo,$isNumber:()=>zi,$isoDayOfWeek:()=>xe,$isoWeek:()=>ge,$isoWeekYear:()=>Bs,$last:()=>Zo,$lastN:()=>ts,$let:()=>Ki,$literal:()=>Ls,$ln:()=>_o,$log:()=>Mo,$log10:()=>Po,$lt:()=>hn,$lte:()=>jn,$ltrim:()=>pi,$map:()=>rs,$maxN:()=>es,$median:()=>zs,$mergeObjects:()=>we,$millisecond:()=>he,$minN:()=>ns,$minute:()=>je,$mod:()=>Ro,$month:()=>Ee,$multiply:()=>Uo,$ne:()=>En,$nin:()=>as,$not:()=>On,$objectToArray:()=>qs,$or:()=>An,$percentile:()=>Hs,$pow:()=>Vo,$radiansToDegrees:()=>Ui,$rand:()=>Ys,$range:()=>ms,$reduce:()=>us,$regexFind:()=>ai,$regexFindAll:()=>mi,$regexMatch:()=>ui,$replaceAll:()=>ci,$replaceOne:()=>li,$reverseArray:()=>cs,$round:()=>Fo,$rtrim:()=>fi,$sampleRate:()=>Ks,$second:()=>$e,$setDifference:()=>Zs,$setEquals:()=>ti,$setField:()=>Ne,$setIntersection:()=>ri,$setIsSubset:()=>ei,$setUnion:()=>ni,$sin:()=>Vi,$sinh:()=>Fi,$size:()=>ls,$slice:()=>ue,$sortArray:()=>fs,$split:()=>yi,$sqrt:()=>Wo,$strLenBytes:()=>Ai,$strLenCP:()=>bi,$strcasecmp:()=>Oi,$substr:()=>Se,$substrBytes:()=>hi,$substrCP:()=>ji,$subtract:()=>Bo,$switch:()=>gs,$tan:()=>Wi,$tanh:()=>Bi,$toBool:()=>ke,$toDate:()=>Ce,$toDecimal:()=>Qi,$toDouble:()=>ar,$toInt:()=>ve,$toLong:()=>De,$toLower:()=>Ei,$toString:()=>_e,$toUpper:()=>$i,$trim:()=>Ii,$trunc:()=>Lo,$type:()=>Yi,$unsetField:()=>Gs,$week:()=>Ie,$year:()=>Lr,$zip:()=>ys});var No=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:Math.abs(e)};var So=(t,r,n)=>{let e=u(t,r,null,n),o=!1,s=0;for(let p of e)k(p)&&(m(!o,"'$add' can only have one date value"),o=!0),s+=+p;return o?new Date(s):s};var ko=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(b(e)||isNaN(e),"$ceil expression must resolve to a number."),Math.ceil(e))};var Co=(t,r,n)=>{let e=u(t,r,null,n);return e[0]/e[1]};var vo=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(b(e)||isNaN(e),"$exp expression must resolve to a number."),Math.exp(e))};var Do=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(b(e)||isNaN(e),"$floor expression must resolve to a number."),Math.floor(e))};var _o=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(b(e)||isNaN(e),"$ln expression must resolve to a number."),Math.log(e))};var Mo=(t,r,n)=>{let e=u(t,r,null,n),o="$log expression must resolve to array(2) of numbers";return m(y(e)&&e.length===2,o),e.some(l)?null:(m(e.some(isNaN)||e.every(b),o),Math.log10(e[0])/Math.log10(e[1]))};var Po=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(b(e)||isNaN(e),"$log10 expression must resolve to a number."),Math.log10(e))};var Ro=(t,r,n)=>{let e=u(t,r,null,n);return e[0]%e[1]};var Uo=(t,r,n)=>u(t,r,null,n).reduce((o,s)=>o*s,1);var Vo=(t,r,n)=>{let e=u(t,r,null,n);return m(y(e)&&e.length===2&&e.every(b),"$pow expression must resolve to array(2) of numbers"),m(!(e[0]===0&&e[1]<0),"$pow cannot raise 0 to a negative exponent"),Math.pow(e[0],e[1])};function Ir(t,r=0,n=!1){let e=Math.abs(t)===t?1:-1;t=Math.abs(t);let o=Math.trunc(t),s=parseFloat((t-o).toFixed(Math.abs(r)+1));if(r===0){let p=Math.trunc(10*s);n&&((o&1)===1&&p>=5||p>5)&&o++}else if(r>0){let p=Math.pow(10,r),i=Math.trunc(s*p),a=Math.trunc(s*p*10)%10;n&&a>5&&(i+=1),o=(o*p+i)/p}else if(r<0){let p=Math.pow(10,-1*r),i=o%p;if(o=Math.max(0,o-i),n&&e===-1){for(;i>10;)i-=i/10;o>0&&i>=5&&(o+=p)}}return o*e}var Fo=(t,r,n)=>{let e=u(t,r,null,n),o=e[0],s=e[1];return l(o)||isNaN(o)||Math.abs(o)===1/0?o:(m(b(o),"$round expression must resolve to a number."),Ir(o,s,!0))};var Wo=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(b(e)&&e>0||isNaN(e),"$sqrt expression must resolve to non-negative number."),Math.sqrt(e))};var Bo=(t,r,n)=>{let e=u(t,r,null,n),o="$subtract: must resolve to array(2) of numbers/dates";m(y(e)&&e.length===2,o);let s=e.map(q).join("|");return s==="date|number"?new Date(+e[0]-+e[1]):(m(s==="date|date"||s==="number|number",o),+e[0]-+e[1])};var Lo=(t,r,n)=>{let e=u(t,r,null,n),o=e[0],s=e[1];return l(o)||isNaN(o)||Math.abs(o)===1/0?o:(m(b(o),"$trunc expression must resolve to a number."),m(l(s)||b(s)&&s>-20&&s<100,"$trunc expression has invalid place"),Ir(o,s,!1))};var zo=(t,r,n)=>{let e=u(t,r,null,n);if(m(y(e)&&e.length===2,"$arrayElemAt expression must resolve to array(2)"),e.some(l))return null;let o=e[1],s=e[0];if(o<0&&Math.abs(o)<=s.length)return s[(o+s.length)%s.length];if(o>=0&&o<s.length)return s[o]};var Qo=(t,r,n)=>{let e=u(t,r,null,n);return m(y(e),"$arrayToObject: expression must resolve to an array"),e.reduce((o,s)=>{for(;y(s)&&s.length===1;)s=s[0];if(y(s)&&s.length==2)o[s[0]]=s[1];else{let p=s;m(j(p)&&M(p,"k")&&M(p,"v"),"$arrayToObject expression is invalid."),o[p.k]=p.v}return o},{})};var Yo=(t,r,n)=>{let e=u(t,r,null,n);m(y(e),"$concatArrays: input must resolve to an array");let o=0;for(let i of e){if(l(i))return null;o+=i.length}let s=new Array(o),p=0;for(let i of e)for(let a of i)s[p++]=a;return s};var Ko=(t,r,n)=>{let e=u(t,r.input,null,n);if(l(e))return null;m(y(e),"$filter 'input' expression must resolve to an array");let o=I.init(n,t),s=r.as||"this",p={variables:{[s]:null}};return e.filter(i=>{p.variables[s]=i;let a=u(t,r.cond,null,o.update(o.root,p));return Y(a,n.useStrictMode)})};var qo=(t,r,n)=>{if(y(t))return ee(t,r,n);let e=u(t,r,null,n);return l(e)?null:(m(y(e)&&e.length>0,"$first must resolve to a non-empty array."),ot(e)[0])};var Go=(t,r,n)=>{if(y(t))return hr(t,r,n);let{input:e,n:o}=u(t,r,null,n);return l(e)?null:(m(y(e),"$firstN: 'input' must resolve to an array."),hr(e,{n:o,input:"$$this"},n))};var Ho=(t,r,n)=>{let[e,o]=u(t,r,null,n);return m(y(o),"$in second argument must be an array"),o.some(s=>L(s,e))};var Jo=(t,r,n)=>{let e=u(t,r,null,n);if(l(e))return null;let o=e[0],s=e[1];if(l(o))return null;m(y(o),"$indexOfArray expression must resolve to an array.");let p=e[2]||0,i=e[3];if(l(i)&&(i=o.length),p>i)return-1;m(p>=0&&i>=0,"$indexOfArray expression is invalid"),(p>0||i<o.length)&&(o=o.slice(p,i));let a=-1;return o.some((c,f)=>{let O=L(c,s);return O&&(a=f),O}),a+p};var Xo=(t,r,n)=>y(u(t,r[0],null,n));var Zo=(t,r,n)=>{if(y(t))return ne(t,r,n);let e=u(t,r,null,n);return l(e)?null:(m(y(e)&&e.length>0,"$last must resolve to a non-empty array."),ot(e)[e.length-1])};var ts=(t,r,n)=>{if(y(t))return jr(t,r,n);let{input:e,n:o}=u(t,r,null,n);return l(e)?null:(m(y(e),"Must resolve to an array/null or missing"),jr(e,{n:o,input:"$$this"},n))};var rs=(t,r,n)=>{let e=u(t,r.input,null,n);if(l(e))return null;m(y(e),"$map 'input' expression must resolve to an array");let o=I.init(n),s=r.as||"this";return e.map(p=>u(t,r.in,null,o.update(o.root,{variables:{[s]:p}})))};var es=(t,r,n)=>{if(y(t))return Er(t,r,n);let{input:e,n:o}=u(t,r,null,n);return l(e)?null:(m(y(e),"Must resolve to an array/null or missing"),Er(e,{n:o,input:"$$this"},n))};var ns=(t,r,n)=>{if(y(t))return $r(t,r,n);let{input:e,n:o}=u(t,r,null,n);return l(e)?null:(m(y(e),"$minN: 'input' must resolve to an array."),$r(e,{n:o,input:"$$this"},n))};var tr=(t,r,n)=>t.take(r);var wt=(t,r,n)=>G(r)?t:(sn(r,n),t.map(on(r,I.init(n))));function on(t,r,n=!0){let e=r.idKey,o=Object.keys(t),s=new Array,p=new Array,i={};for(let d of o){let x=t[d];if(b(x)||ft(x))x?p.push(d):s.push(d);else if(y(x))i[d]=g=>x.map(E=>u(g,E,null,r.update(g))??null);else if(j(x)){let g=Object.keys(x),E=g.length==1?g[0]:"",T=tt("projection",E,r);T?E==="$slice"&&!nt(x[E]).every(b)?i[d]=D=>u(D,x,d,r.update(D)):i[d]=D=>T(D,x[E],d,r.update(D)):Z(E)?i[d]=V=>u(V,x[E],E,r):(sn(x,r),i[d]=V=>{if(!M(V,d))return u(V,x,null,r);n&&r.update(V);let D=S(V,d),z=on(x,r,!1);return y(D)?D.map(z):j(D)?z(D):z(V)})}else i[d]=h(x)&&x[0]==="$"?g=>u(g,x,d,r):g=>x}let a=Object.keys(i),c=s.includes(e);if(n&&c&&s.length===1&&!p.length&&!a.length)return d=>{let x={...d};return delete x[e],x};let O=n&&!c&&!p.includes(e),A={preserveMissing:!0};return d=>{let x={};if(s.length&&!p.length){Or(x,d);for(let g of s)Tt(x,g,{descendArray:!0})}for(let g of p){let E=At(d,g,A)??{};Or(x,E)}p.length&&yr(x);for(let g of a){let E=i[g](d);E===void 0?Tt(x,g,{descendArray:!0}):gt(x,g,E)}return O&&M(d,e)&&(x[e]=S(d,e)),x}}function sn(t,r){let n=!1,e=!1;for(let[o,s]of Object.entries(t))m(!o.startsWith("$"),"Field names may not start with '$'."),m(!o.endsWith(".$"),"Positional projection operator '$' is not supported."),o!==r?.idKey&&(s===0||s===!1?n=!0:(s===1||s===!0)&&(e=!0),m(!(n&&e),"Projection cannot have a mix of inclusion and exclusion."))}var er=(t,r,n)=>t.drop(r);var os={$sort:H,$skip:er,$limit:tr},Tr=class{#t;#r;#e;#o;#i={};#n=null;#s=[];constructor(r,n,e,o){this.#t=r,this.#r=n,this.#e=e,this.#o=o}fetch(){if(this.#n)return this.#n;this.#n=N(this.#t).filter(this.#r);let r=this.#o.processingMode;r&1&&this.#n.map(X);for(let n of["$sort","$skip","$limit"])M(this.#i,n)&&(this.#n=os[n](this.#n,this.#i[n],this.#o));return Object.keys(this.#e).length&&(this.#n=wt(this.#n,this.#e,this.#o)),r&2&&this.#n.map(X),this.#n}fetchAll(){let r=N([...this.#s]);return this.#s.length=0,bt(r,this.fetch())}all(){return this.fetchAll().value()}count(){return this.all().length}skip(r){return this.#i.$skip=r,this}limit(r){return this.#i.$limit=r,this}sort(r){return this.#i.$sort=r,this}collation(r){return this.#o={...this.#o,collation:r},this}next(){if(this.#s.length>0)return this.#s.pop();let r=this.fetch().next();if(!r.done)return r.value}hasNext(){if(this.#s.length>0)return!0;let r=this.fetch().next();return r.done?!1:(this.#s.push(r.value),!0)}map(r){return this.all().map(r)}forEach(r){this.all().forEach(r)}[Symbol.iterator](){return this.fetchAll()}};var ss=/^\$(and|or|nor|expr|jsonSchema)$/,B=class{#t;#r;#e;constructor(r,n){this.#e=X(r),this.#r=yt(n),this.#t=[],this.compile()}compile(){m(j(this.#e),`query criteria must be an object: ${JSON.stringify(this.#e)}`);let r={};for(let[n,e]of Object.entries(this.#e)){if(n==="$where")m(this.#r.scriptEnabled,"$where operator requires 'scriptEnabled' option to be true."),Object.assign(r,{field:n,expr:e});else if(ss.test(n))this.processOperator(n,n,e);else{m(!Z(n),`unknown top level operator: ${n}`);for(let[o,s]of Object.entries(Ar(e)))this.processOperator(n,o,s)}r.field&&this.processOperator(r.field,r.field,r.expr)}}processOperator(r,n,e){let o=tt("query",n,this.#r);m(!!o,`unknown query operator ${n}`),this.#t.push(o(r,e,this.#r))}test(r){return this.#t.every(n=>n(r))}find(r,n){return new Tr(r,e=>this.test(e),n||{},this.#r)}};function C(t,r,n,e){let o={unwrapArray:!0},s=Math.max(1,t.split(".").length-1);return p=>{let i=S(p,t,o);return e(i,r,{...n,depth:s})}}function rt(t,r,n,e){let o=u(t,r,null,n);return e(...o)}function nr(t,r,n){return L(t,r)||l(t)&&l(r)?!0:y(t)?t.some(e=>L(e,r))||ot(t,n?.depth).some(e=>L(e,r)):!1}function wr(t,r,n){return!nr(t,r,n)}function ae(t,r,n){return l(t)?r.some(e=>e===null):Pt([nt(t),r],n?.hashFunction).length>0}function Nr(t,r,n){return!ae(t,r,n)}function Sr(t,r,n){return Dr(t,r,(e,o)=>w(e,o)<0)}function kr(t,r,n){return Dr(t,r,(e,o)=>w(e,o)<=0)}function Cr(t,r,n){return Dr(t,r,(e,o)=>w(e,o)>0)}function vr(t,r,n){return Dr(t,r,(e,o)=>w(e,o)>=0)}function mn(t,r,n){return nt(t).some(e=>r.length===2&&e%r[0]===r[1])}function un(t,r,n){let e=nt(t),o=s=>h(s)&&Y(r.exec(s),n?.useStrictMode);return e.some(o)||ot(e,1).some(o)}function cn(t,r,n){if(!y(t)||!y(r)||!t.length||!r.length)return!1;let e=!0;for(let o of r){if(!e)break;j(o)&&Object.keys(o).includes("$elemMatch")?e=me(t,o.$elemMatch,n):J(o)?e=t.some(s=>typeof s=="string"&&o.test(s)):e=t.some(s=>L(o,s))}return e}function ln(t,r,n){return Array.isArray(t)&&t.length===r}function is(t){return Z(t)&&["$and","$or","$nor"].indexOf(t)===-1}function me(t,r,n){if(y(t)&&!G(t)){let e=p=>p,o=r;Object.keys(r).every(is)&&(o={temp:r},e=p=>({temp:p}));let s=new B(o,n);for(let p=0,i=t.length;p<i;p++)if(s.test(e(t[p])))return!0}return!1}var pn=t=>t===null,ps={array:y,boolean:ft,bool:ft,date:k,number:b,int:b,long:b,double:b,decimal:b,null:pn,object:j,regexp:J,regex:J,string:h,undefined:l,1:b,2:h,3:j,4:y,6:l,8:ft,9:k,10:pn,11:J,16:b,18:b,19:b};function an(t,r,n){let e=ps[r];return e?e(t):!1}function fn(t,r,n){return y(r)?r.findIndex(e=>an(t,e,n))>=0:an(t,r,n)}function Dr(t,r,n){return nt(t).some(e=>q(e)===q(r)&&n(e,r))}var as=(t,r,n)=>rt(t,r,n,Nr);var ms=(t,r,n)=>{let e=u(t,r,null,n),o=e[0],s=e[1],p=e[2]||1,i=new Array,a=o;for(;a<s&&p>0||a>s&&p<0;)i.push(a),a+=p;return i};var us=(t,r,n)=>{let e=I.init(n),o=u(t,r.input,null,e),s=u(t,r.initialValue,null,e),p=r.in;return l(o)?null:(m(y(o),"$reduce 'input' expression must resolve to an array"),o.reduce((i,a)=>u(a,p,null,e.update(e.root,{variables:{value:i}})),s))};var cs=(t,r,n)=>{let e=u(t,r,null,n);if(l(e))return null;m(y(e),"$reverseArray expression must resolve to an array");let o=e.slice(0);return o.reverse(),o};var ls=(t,r,n)=>{let e=u(t,r,null,n);return y(e)?e.length:void 0};var ue=(t,r,n)=>{let e=u(t,r,null,n),o=e[0],s=e[1],p=e[2];return l(p)?s<0?s=Math.max(0,o.length+s):(p=s,s=0):(s<0&&(s=Math.max(0,o.length+s)),m(p>0,"Invalid argument for $slice operator. Limit must be a positive number"),p+=s),o.slice(s,p)};var fs=(t,r,n)=>{let{input:e,sortBy:o}=u(t,r,null,n);if(l(e))return null;if(m(y(e),"$sortArray expression must resolve to an array"),j(o))return H(N(e),o,n).value();let s=[...e];return s.sort(w),o===-1&&s.reverse(),s};var ys=(t,r,n)=>{let e=u(t,r.inputs,null,n),o=r.useLongestLength||!1;if(l(e))return null;m(y(e),"'inputs' expression must resolve to an array"),m(ft(o),"'useLongestLength' must be a boolean"),y(r.defaults)&&m(o,"'useLongestLength' must be set to true to use 'defaults'");let s=0;for(let a of e){if(l(a))return null;m(y(a),"'inputs' expression values must resolve to an array or null"),s=o?Math.max(s,a.length):Math.min(s||a.length,a.length)}let p=[],i=r.defaults||[];for(let a=0;a<s;a++){let c=e.map((f,O)=>l(f[a])?i[O]||null:f[a]);p.push(c)}return p};function Wt(t,r,n,e){m(y(r),"expression must be an array.");let o=u(t,r,null,n);return o.some(l)?null:(m(o.every(b),"expression must evalue to array of numbers."),e(o))}var Os=(t,r,n)=>Wt(t,r,n,e=>e.reduce((o,s)=>o&s,-1));var As=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(b(e),"$bitNot: expression must evaluate to a number."),~e)};var bs=(t,r,n)=>Wt(t,r,n,e=>e.reduce((o,s)=>o|s,0));var ds=(t,r,n)=>Wt(t,r,n,e=>e.reduce((o,s)=>o^s,0));var or={};lt(or,{$and:()=>yn,$not:()=>On,$or:()=>An});var yn=(t,r,n)=>{let e=u(t,r,null,n);return Y(e,n.useStrictMode)&&e.every(o=>Y(o,n.useStrictMode))};var On=(t,r,n)=>{let e=nt(r);return e.length==0?!1:(m(e.length==1,"Expression $not takes exactly 1 argument"),!u(t,e[0],null,n))};var An=(t,r,n)=>{let e=u(t,r,null,n),o=n.useStrictMode;return Y(e,o)&&e.some(s=>Y(s,o))};var sr={};lt(sr,{$cmp:()=>bn,$eq:()=>dn,$gt:()=>xn,$gte:()=>gn,$lt:()=>hn,$lte:()=>jn,$ne:()=>En});var bn=(t,r,n)=>{let e=u(t,r,null,n);return m(y(e)&&e.length==2,"$cmp: expression must resolve to array of size 2."),w(e[0],e[1])};var dn=(t,r,n)=>rt(t,r,n,nr);var xn=(t,r,n)=>rt(t,r,n,Cr);var gn=(t,r,n)=>rt(t,r,n,vr);var hn=(t,r,n)=>rt(t,r,n,Sr);var jn=(t,r,n)=>rt(t,r,n,kr);var En=(t,r,n)=>rt(t,r,n,wr);var xs=(t,r,n)=>{let e,o,s,p="$cond: invalid arguments";y(r)?(m(r.length===3,p),e=r[0],o=r[1],s=r[2]):(m(j(r),p),e=r.if,o=r.then,s=r.else);let i=Y(u(t,e,null,n),n.useStrictMode);return u(t,i?o:s,null,n)};var ce=(t,r,n)=>{let e=u(t,r,null,n);return e.find(o=>!l(o))??e[e.length-1]};var gs=(t,r,n)=>{let e=null;return r.branches.some(o=>{let s=Y(u(t,o.case,null,n),n.useStrictMode);return s&&(e=o.then),s}),u(t,e!==null?e:r.default,null,n)};var le=(t,r,n)=>{m(n.scriptEnabled,"$function operator requires 'scriptEnabled' option to be true");let e=u(t,r,null,n);return e.body.apply(null,e.args)};var hs=["mon","mon","tue","wed","thu","fri","sat","sun"],js=-1e9,Bt=7,fe=t=>(t&3)==0&&(t%100!=0||t%400==0),Es=[365,366],$s=[[0,31,59,90,120,151,181,212,243,273,304,334],[0,31,60,91,121,152,182,213,244,274,305,335]],ir=t=>$s[+fe(t.getUTCFullYear())][t.getUTCMonth()]+t.getUTCDate(),_r=(t,r)=>{let n=t.getUTCDay()||7,e=r.toLowerCase().substring(0,3);return(n-hs.lastIndexOf(e)+Bt)%Bt},$n=t=>(t+Math.floor(t/4)-Math.floor(t/100)+Math.floor(t/400))%7,In=t=>52+ +($n(t)==4||$n(t-1)==3);function Lt(t){let r=t.getUTCDay()||7,n=Math.floor((10+ir(t)-r)/7);return n<1?In(t.getUTCFullYear()-1):n>In(t.getUTCFullYear())?1:n}function Pr(t){return t.getUTCFullYear()-+(t.getUTCMonth()===0&&t.getUTCDate()==1&&t.getUTCDay()<1)}var Et=60,mt={week:6048e5,day:864e5,hour:36e5,minute:6e4,second:1e3,millisecond:1},Rr="%Y-%m-%dT%H:%M:%S.%LZ",ye=[["year",0,9999],["month",1,12],["day",1,31],["hour",0,23],["minute",0,59],["second",0,59],["millisecond",0,999]];var zt={"%b":{name:"abbr_month",padding:3,re:/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i},"%B":{name:"full_month",padding:0,re:/(January|February|March|April|May|June|July|August|September|October|November|December)/i},"%Y":{name:"year",padding:4,re:/([0-9]{4})/},"%G":{name:"year",padding:4,re:/([0-9]{4})/},"%m":{name:"month",padding:2,re:/(0[1-9]|1[012])/},"%d":{name:"day",padding:2,re:/(0[1-9]|[12][0-9]|3[01])/},"%j":{name:"day_of_year",padding:3,re:/(0[0-9][1-9]|[12][0-9]{2}|3[0-5][0-9]|36[0-6])/},"%H":{name:"hour",padding:2,re:/([01][0-9]|2[0-3])/},"%M":{name:"minute",padding:2,re:/([0-5][0-9])/},"%S":{name:"second",padding:2,re:/([0-5][0-9]|60)/},"%L":{name:"millisecond",padding:3,re:/([0-9]{3})/},"%w":{name:"day_of_week",padding:1,re:/([0-6])/},"%u":{name:"day_of_week_iso",padding:1,re:/([1-7])/},"%U":{name:"week_of_year",padding:2,re:/([1-4][0-9]?|5[0-3]?)/},"%V":{name:"week_of_year_iso",padding:2,re:/([1-4][0-9]?|5[0-3]?)/},"%z":{name:"timezone",padding:2,re:/(([+-][01][0-9]|2[0-3]):?([0-5][0-9])?)/},"%Z":{name:"minute_offset",padding:3,re:/([+-][0-9]{3})/},"%%":{name:"percent_literal",padding:1,re:/%%/}},Ur=/(%[bBYGmdjHMSLwuUVzZ%])/g,Nn=/%[bBYGmdjHMSLwuUVzZ%]/,Is=/^[a-zA-Z_]+\/[a-zA-Z_]+$/;function it(t){if(l(t))return 0;if(Is.test(t)){let o=new Date,s=new Date(o.toLocaleString("en-US",{timeZone:"UTC"}));return(new Date(o.toLocaleString("en-US",{timeZone:t})).getTime()-s.getTime())/6e4}let r=zt["%z"].re.exec(t);m(!!r,`timezone '${t}' is invalid or not supported.`);let n=parseInt(r[2])||0,e=parseInt(r[3])||0;return(Math.abs(n*Et)+e)*(n<0?-1:1)}function Sn(t){return(t<0?"-":"+")+Mr(Math.abs(Math.floor(t/Et)),2)+Mr(Math.abs(t)%Et,2)}function at(t,r){t.setUTCMinutes(t.getUTCMinutes()+r)}function P(t,r,n){if(k(t))return t;let e=u(t,r,null,n);if(k(e))return new Date(e);if(b(e))return new Date(e*1e3);m(!!e?.date,`cannot convert ${JSON.stringify(r)} to date`);let o=k(e.date)?new Date(e.date):new Date(e.date*1e3);return e.timezone&&at(o,it(e.timezone)),o}function Mr(t,r){return new Array(Math.max(r-String(t).length+1,0)).join("0")+t.toString()}var Tn=t=>{let r=t-js;return Math.trunc(r/4)-Math.trunc(r/100)+Math.trunc(r/400)};function Ts(t,r){return Math.trunc(Tn(r-1)-Tn(t-1)+(r-t)*Es[0])}var Qt=(t,r)=>r.getUTCFullYear()-t.getUTCFullYear(),Vr=(t,r)=>r.getUTCMonth()-t.getUTCMonth()+Qt(t,r)*12,Fr=(t,r)=>{let n=Math.trunc(t.getUTCMonth()/3);return Math.trunc(r.getUTCMonth()/3)-n+Qt(t,r)*4},Yt=(t,r)=>ir(r)-ir(t)+Ts(t.getUTCFullYear(),r.getUTCFullYear()),Wr=(t,r,n)=>{let e=(n||"sun").substring(0,3);return Math.trunc((Yt(t,r)+_r(t,e)-_r(r,e))/Bt)},kn=(t,r)=>r.getUTCHours()-t.getUTCHours()+Yt(t,r)*24,wn=(t,r)=>{let n=t.getUTCMonth()+r,e=Math.floor(n/12);if(n<0){let o=n%12+12;t.setUTCFullYear(t.getUTCFullYear()+e,o,t.getUTCDate())}else t.setUTCFullYear(t.getUTCFullYear()+e,n%12,t.getUTCDate())},Br=(t,r,n,e)=>{let o=new Date(t);switch(r){case"year":o.setUTCFullYear(o.getUTCFullYear()+n);break;case"quarter":wn(o,3*n);break;case"month":wn(o,n);break;default:o.setTime(o.getTime()+mt[r]*n)}return o};var Nt=(t,r,n)=>{let e=u(t,r,null,n);return Br(e.startDate,e.unit,e.amount,e.timezone)};var ws=(t,r,n)=>{let{startDate:e,endDate:o,unit:s,timezone:p,startOfWeek:i}=u(t,r,null,n),a=new Date(e),c=new Date(o),f=it(p);switch(at(a,f),at(c,f),s){case"year":return Qt(a,c);case"quarter":return Fr(a,c);case"month":return Vr(a,c);case"week":return Wr(a,c,i);case"day":return Yt(a,c);case"hour":return kn(a,c);case"minute":return a.setUTCSeconds(0),a.setUTCMilliseconds(0),c.setUTCSeconds(0),c.setUTCMilliseconds(0),Math.round((c.getTime()-a.getTime())/mt[s]);default:return Math.round((c.getTime()-a.getTime())/mt[s])}};var Ns=[31,28,31,30,31,30,31,31,30,31,30,31],Ss=t=>t.month==2&&fe(t.year)?29:Ns[t.month-1],ks=(t,r,n)=>{let e=u(t,r,null,n),o=it(e.timezone);for(let s=ye.length-1,p=0;s>=0;s--){let i=ye[s],a=i[0],c=i[1],f=i[2],O=(e[a]||0)+p;p=0;let A=f+1;if(a=="hour"&&(O+=Math.floor(o/Et)*-1),a=="minute"&&(O+=o%Et*-1),O<c){let d=c-O;p=-1*Math.ceil(d/A),O=A-d%A}else O>f&&(O+=c,p=Math.trunc(O/A),O%=A);e[a]=O}return e.day=Math.min(e.day,Ss(e)),new Date(Date.UTC(e.year,e.month-1,e.day,e.hour,e.minute,e.second,e.millisecond))};function Cs(t){return t==="Z"?0:t>="A"&&t<"N"?t.charCodeAt(0)-64:77-t.charCodeAt(0)}var vs=t=>t.replace(/^\//,"").replace(/\/$/,""),Ds=["^",".","-","*","?","$"];function _s(t){return Ds.forEach(r=>{t=t.replace(r,`\\${r}`)}),t}var Ms=(t,r,n)=>{let e=u(t,r,null,n);e.format=e.format||Rr,e.onNull=e.onNull||null;let o=e.dateString;if(l(o))return e.onNull;let s=e.format.split(Nn);s.reverse();let p=e.format.match(Ur),i={},a="";for(let A=0,d=p.length;A<d;A++){let x=p[A],g=zt[x];if(j(g)){let E=g.re.exec(o),T=s.pop()||"";E!==null?(i[g.name]=/^\d+$/.exec(E[0])?parseInt(E[0]):E[0],o=o.substring(E.index+E[0].length+1),a+=_s(T)+vs(g.re.toString())):i[g.name]=null}}if(l(i.year)||l(i.month)||l(i.day)||!new RegExp("^"+a+"[A-Z]?$").exec(e.dateString))return e.onError;let c=e.dateString.match(/([A-Z])$/);m(!(c&&e.timezone),`$dateFromString: you cannot pass in a date/time string with time zone information ('${c&&c[0]}') together with a timezone argument`);let f=c?Cs(c[0])*Et:it(e.timezone),O=new Date(Date.UTC(i.year,i.month-1,i.day,0,0,0));return l(i.hour)||O.setUTCHours(i.hour),l(i.minute)||O.setUTCMinutes(i.minute),l(i.second)||O.setUTCSeconds(i.second),l(i.millisecond)||O.setUTCMilliseconds(i.millisecond),at(O,-f),O};var Ps=(t,r,n)=>{let e=u(t,r?.amount,null,n);return Nt(t,{...r,amount:-1*e},n)};var Rs=(t,r,n)=>{let e=u(t,r,null,n),o=new Date(e.date),s=it(e.timezone);at(o,s);let p={hour:o.getUTCHours(),minute:o.getUTCMinutes(),second:o.getUTCSeconds(),millisecond:o.getUTCMilliseconds()};return e.iso8601==!0?Object.assign(p,{isoWeekYear:Pr(o),isoWeek:Lt(o),isoDayOfWeek:o.getUTCDay()||7}):Object.assign(p,{year:o.getUTCFullYear(),month:o.getUTCMonth()+1,day:o.getUTCDate()})};var Oe=(t,r,n)=>P(t,r,n).getUTCDate();var Ae=(t,r,n)=>P(t,r,n).getUTCDay()+1;var be=(t,r,n)=>ir(P(t,r,n));var de=(t,r,n)=>P(t,r,n).getUTCHours();var xe=(t,r,n)=>P(t,r,n).getUTCDay()||7;var ge=(t,r,n)=>Lt(P(t,r,n));var he=(t,r,n)=>P(t,r,n).getUTCMilliseconds();var je=(t,r,n)=>P(t,r,n).getUTCMinutes();var Ee=(t,r,n)=>P(t,r,n).getUTCMonth()+1;var $e=(t,r,n)=>P(t,r,n).getUTCSeconds();var Ie=(t,r,n)=>{let e=P(t,r,n),o=Lt(e);return e.getUTCDay()>0&&e.getUTCDate()==1&&e.getUTCMonth()==0?0:e.getUTCDay()==0?o+1:o};var Lr=(t,r,n)=>P(t,r,n).getUTCFullYear();var Us={"%Y":Lr,"%G":Lr,"%m":Ee,"%d":Oe,"%H":de,"%M":je,"%S":$e,"%L":he,"%u":xe,"%U":Ie,"%V":ge,"%j":be,"%w":(t,r,n)=>Ae(t,r,n)-1},Te=(t,r,n)=>{let e=u(t,r,null,n);if(l(e.onNull)&&(e.onNull=null),l(e.date))return e.onNull;let o=P(t,e.date,n),s=e.format||Rr,p=it(e.timezone),i=s.match(Ur);at(o,p);for(let a=0,c=i.length;a<c;a++){let f=i[a];m(f in zt,`$dateToString: invalid format specifier ${f}`);let{name:O,padding:A}=zt[f],d=Us[f],x;if(d)x=Mr(d(t,o,n),A);else switch(O){case"timezone":x=Sn(p);break;case"minute_offset":x=p.toString();break;case"abbr_month":case"full_month":{let g=O.startsWith("abbr")?"short":"long";x=o.toLocaleString("en-US",{month:g});break}}s=s.replace(f,x)}return s};var pr=["year","quarter","month","week","day","hour","minute","second","millisecond"];var Cn=9466848e5,vn=(t,r)=>{let n=t%r;return n<0&&(n+=r),n},Vs={day:Yt,month:Vr,quarter:Fr,year:Qt},Fs=/(mon(day)?|tue(sday)?|wed(nesday)?|thu(rsday)?|fri(day)?|sat(urday)?|sun(day)?)/i,Ws=(t,r,n)=>{let{date:e,unit:o,binSize:s,timezone:p,startOfWeek:i}=u(t,r,null,n);if(l(e)||l(o))return null;let a=(i??"sun").toLowerCase().substring(0,3);m(k(e),"$dateTrunc: 'date' must resolve to a valid Date object."),m(pr.includes(o),"$dateTrunc: unit is invalid."),m(o!="week"||Fs.test(a),`$dateTrunc: startOfWeek '${a}' is not a valid.`),m(l(s)||s>0,"$dateTrunc requires 'binSize' to be greater than 0, but got value 0.");let c=s??1;switch(o){case"millisecond":case"second":case"minute":case"hour":{let f=c*mt[o],O=e.getTime()-Cn;return new Date(e.getTime()-vn(O,f))}default:{m(c<=1e11,"dateTrunc unsupported binSize value");let f=new Date(e),O=new Date(Cn),A=0;if(o=="week"){let E=_r(O,a),T=(Bt-E)%Bt;O.setTime(O.getTime()+T*mt.day),A=Wr(O,f,a)}else A=Vs[o](O,f);let d=A-vn(A,c),x=Br(O,o,d,p),g=it(p);return at(x,-g),x}}};var Bs=(t,r,n)=>Pr(P(t,r,n));var Ls=(t,r,n)=>r;var zs=(t,r,n)=>{let e=u(t,r.input,null,n);return oe(e,{input:"$$CURRENT"},n)};var Qs=(t,r,n)=>{let e=u(t,r,null,n),[o,s]=j(e)?[e.field,e.input||t]:[e,t];return l(s)?null:(m(j(s),"$getField expression 'input' must evaluate to an object"),m(h(o),"$getField expression 'field' must evaluate to a string"),s[o])};var Ys=(t,r,n)=>Math.random();var Ks=(t,r,n)=>Math.random()<=u(t,r,null,n);var we=(t,r,n)=>{let e=u(t,r,null,n)??[];return se(e,r,n)};var qs=(t,r,n)=>{let e=u(t,r,null,n);if(l(e))return null;m(j(e),`$objectToArray requires a document input, found: ${q(e)}`);let o=Object.entries(e),s=new Array(o.length),p=0;for(let[i,a]of o)s[p++]={k:i,v:a};return s};var Ne=(t,r,n)=>{let{input:e,field:o,value:s}=u(t,r,null,n);if(l(e))return null;m(j(e),"$setField expression 'input' must evaluate to an object"),m(h(o),"$setField expression 'field' must evaluate to a string");let p={...e};return r.value=="$$REMOVE"?delete p[o]:p[o]=s,p};var Gs=(t,r,n)=>Ne(t,{...r,value:"$$REMOVE"},n);var Hs=(t,r,n)=>{let e=u(t,r.input,null,n);return Zt(e,{...r,input:"$$CURRENT"},n)};var Js=(t,r,n)=>u(t,r,null,n)[0].every(o=>Y(o,n.useStrictMode));var Xs=(t,r,n)=>u(t,r,null,n)[0].some(o=>Y(o,n.useStrictMode));var Zs=(t,r,n)=>{let e=u(t,r,null,n);if(l(e)||(m(y(e),"$setDifference must be an arrays."),e.some(l)))return null;m(e.length==2,"$setDifference takes exactly 2 arguments."),m(e.every(y),"$setDifference operands must be arrays.");let o=Q.init(n.hashFunction);return e[0].forEach(s=>o.set(s,!0)),e[1].forEach(s=>o.delete(s)),Array.from(o.keys())};var ti=(t,r,n)=>{let e=u(t,r,null,n);m(y(e)&&e.every(y),"$setEquals operands must be arrays.");let o=Q.init();e[0].every((s,p)=>o.set(s,p));for(let s=1;s<e.length;s++){let p=e[s],i=new Set;for(let a=0;a<p.length;a++){let c=o.get(p[a])??-1;if(c===-1)return!1;i.add(c)}if(i.size!==o.size)return!1}return!0};var ri=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:(m(y(e)&&e.every(y),"$setIntersection operands must be arrays."),Pt(e,n?.hashFunction))};var ei=(t,r,n)=>{let e=u(t,r,null,n);m(y(e)&&e.every(y),"$setIsSubset operands must be arrays.");let o=e[0],s=e[1],p=Q.init(),i=new Set;o.every((a,c)=>p.set(a,c));for(let a of s)if(i.add(p.get(a)??-1),i.size>p.size)return!0;return i.delete(-1),i.size==p.size};var ni=(t,r,n)=>{let e=u(t,r,null,n);return l(e)||(m(y(e),"$setUnion operands must be arrays."),e.some(l))?null:Rt(ot(e),n?.hashFunction)};var oi=(t,r,n)=>{let e=u(t,r,null,n);return m(e.every(o=>h(o)||l(o)),"$concat only supports strings."),e.some(l)?null:e.join("")};var si=(t,r,n)=>{let e=u(t,r,null,n),o="$indexOfBytes expression resolves to invalid an argument";if(l(e[0]))return null;m(h(e[0])&&h(e[1]),o);let s=e[0],p=e[1],i=e[2],a=e[3],c=l(i)||b(i)&&i>=0&&Math.round(i)===i;if(c=c&&(l(a)||b(a)&&a>=0&&Math.round(a)===a),m(c,o),i=i||0,a=a||s.length,i>a)return-1;let f=s.substring(i,a).indexOf(p);return f>-1?f+i:f};var ii=[0,32,9,10,11,12,13,160,5760,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202];function Kt(t,r,n,e){let o=u(t,r,null,n),s=o.input;if(l(s))return null;let p=l(o.chars)?ii:o.chars.split("").map(c=>c.codePointAt(0)),i=0,a=s.length-1;for(;e.left&&i<=a&&p.indexOf(s[i].codePointAt(0))!==-1;)i++;for(;e.right&&i<=a&&p.indexOf(s[a].codePointAt(0))!==-1;)a--;return s.substring(i,a+1)}function qt(t,r,n,e){let o=u(t,r,null,n);if(!h(o.input))return[];let s=o.options;s&&(m(s.indexOf("x")===-1,"extended capability option 'x' not supported"),m(s.indexOf("g")===-1,"global option 'g' not supported"));let p=o.input,i=new RegExp(o.regex,s),a,c=new Array,f=0;for(;a=i.exec(p);){let O={match:a[0],idx:a.index+f,captures:[]};for(let A=1;A<a.length;A++)O.captures.push(a[A]||null);if(c.push(O),!e.global)break;f=a.index+a[0].length,p=p.substring(f)}return c}var pi=(t,r,n)=>Kt(t,r,n,{left:!0,right:!1});var ai=(t,r,n)=>{let e=qt(t,r,n,{global:!1});return e&&e.length>0?e[0]:null};var mi=(t,r,n)=>qt(t,r,n,{global:!0});var ui=(t,r,n)=>qt(t,r,n,{global:!1}).length!=0;var ci=(t,r,n)=>{let e=u(t,r,null,n),o=[e.input,e.find,e.replacement];return o.some(l)?null:(m(o.every(h),"$replaceAll expression fields must evaluate to string"),e.input.replace(new RegExp(e.find,"g"),e.replacement))};var li=(t,r,n)=>{let e=u(t,r,null,n),o=[e.input,e.find,e.replacement];return o.some(l)?null:(m(o.every(h),"$replaceOne expression fields must evaluate to string"),e.input.replace(e.find,e.replacement))};var fi=(t,r,n)=>Kt(t,r,n,{left:!1,right:!0});var yi=(t,r,n)=>{let e=u(t,r,null,n);return l(e[0])?null:(m(e.every(h),"$split expression must result to array(2) of strings"),e[0].split(e[1]))};var Oi=(t,r,n)=>{let e=u(t,r,null,n),o=e[0],s=e[1];return L(o,s)||e.every(l)?0:(m(e.every(h),"$strcasecmp must resolve to array(2) of strings"),o=o.toUpperCase(),s=s.toUpperCase(),o>s&&1||o<s&&-1||0)};var Ai=(t,r,n)=>~-encodeURI(u(t,r,null,n)).split(/%..|./).length;var bi=(t,r,n)=>u(t,r,null,n).length;var Se=(t,r,n)=>{let[e,o,s]=u(t,r,null,n);return o<0||!h(e)?"":s<0?e.substring(o):e.substring(o,o+s)};var di=[192,224,240];function xi(t){if(t<128)return[t];let r=t<2048&&1||t<65536&&2||3,n=di[r-1],e=[(t>>6*r)+n];for(;r>0;)e.push(128|t>>6*--r&63);return e}function gi(t){let r=[];for(let n=0,e=t.length;n<e;n++)r.push(xi(t.codePointAt(n)));return r}var hi=(t,r,n)=>{let e=u(t,r,null,n),o=e[0],s=e[1],p=e[2];m(h(o)&&b(s)&&s>=0&&b(p)&&p>=0,"$substrBytes: invalid arguments");let i=gi(o),a=[],c=0;for(let A=0;A<i.length;A++)a.push(c),c+=i[A].length;let f=a.indexOf(s),O=a.indexOf(s+p);return m(f>-1&&O>-1,"$substrBytes: invalid range, start or end index is a UTF-8 continuation byte."),o.substring(f,O)};var ji=(t,r,n)=>Se(t,r,n);var Ei=(t,r,n)=>{let e=u(t,r,null,n);return G(e)?"":e.toLowerCase()};var $i=(t,r,n)=>{let e=u(t,r,null,n);return G(e)?"":e.toUpperCase()};var Ii=(t,r,n)=>Kt(t,r,n,{left:!0,right:!0});function R(t,r,n,e,o){let s={undefined:null,null:null,NaN:NaN,Infinity:new Error,"-Infinity":new Error,...o},p=u(t,r,null,n);if(p in s){let i=s[p],a=i instanceof Error;return m(!a,`$${e.name}: value must be in range (-inf,inf)`),!a&&i}return e(p)}var Ti=(t,r,n)=>R(t,r,n,Math.acos,{Infinity:1/0,0:new Error});var wi=(t,r,n)=>R(t,r,n,Math.acosh,{Infinity:1/0,0:new Error});var Ni=(t,r,n)=>R(t,r,n,Math.asin);var Si=(t,r,n)=>R(t,r,n,Math.asinh,{Infinity:1/0,"-Infinity":-1/0});var ki=(t,r,n)=>R(t,r,n,Math.atan);var Ci=(t,r,n)=>{let[e,o]=u(t,r,null,n);return isNaN(e)||l(e)?e:isNaN(o)||l(o)?o:Math.atan2(e,o)};var vi=(t,r,n)=>R(t,r,n,Math.atanh,{1:1/0,"-1":-1/0});var Di=(t,r,n)=>R(t,r,n,Math.cos);var _i=(t,r,n)=>R(t,r,n,Math.cosh,{"-Infinity":1/0,Infinity:1/0});var Mi=t=>t*(Math.PI/180),Pi=(t,r,n)=>R(t,r,n,Mi,{Infinity:1/0,"-Infinity":1/0});var Ri=t=>t*(180/Math.PI),Ui=(t,r,n)=>R(t,r,n,Ri,{Infinity:1/0,"-Infinity":1/0});var Vi=(t,r,n)=>R(t,r,n,Math.sin);var Fi=(t,r,n)=>R(t,r,n,Math.sinh,{"-Infinity":-1/0,Infinity:1/0});var Wi=(t,r,n)=>R(t,r,n,Math.tan);var Bi=(t,r,n)=>R(t,r,n,Math.tanh,{Infinity:1,"-Infinity":-1});var ke=(t,r,n)=>{let e=u(t,r,null,n);return l(e)?null:h(e)?!0:!!e};var Ce=(t,r,n)=>{let e=u(t,r,null,n);if(k(e))return e;if(l(e))return null;let o=new Date(e);return m(!isNaN(o.getTime()),`cannot convert '${e