var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var util_exports = {}; __export(util_exports, { EMPTY_ARRAY: () => EMPTY_ARRAY, MAX_INT: () => MAX_INT, MAX_LONG: () => MAX_LONG, MIN_INT: () => MIN_INT, MIN_LONG: () => MIN_LONG, MingoError: () => MingoError, ValueMap: () => ValueMap, assert: () => assert, cloneDeep: () => cloneDeep, compare: () => compare, ensureArray: () => ensureArray, filterMissing: () => filterMissing, findInsertIndex: () => findInsertIndex, flatten: () => flatten, groupBy: () => groupBy, has: () => has, hashCode: () => hashCode, inArray: () => inArray, intersection: () => intersection, into: () => into, isArray: () => isArray, isBoolean: () => isBoolean, isDate: () => isDate, isEmpty: () => isEmpty, isEqual: () => isEqual, isFunction: () => isFunction, isMissing: () => isMissing, isNil: () => isNil, isNotNaN: () => isNotNaN, isNumber: () => isNumber, isObject: () => isObject, isObjectLike: () => isObjectLike, isOperator: () => isOperator, isPrimitive: () => isPrimitive, isRegExp: () => isRegExp, isString: () => isString, isSymbol: () => isSymbol, memoize: () => memoize, normalize: () => normalize, notInArray: () => notInArray, removeValue: () => removeValue, resolve: () => resolve, resolveGraph: () => resolveGraph, setValue: () => setValue, stringify: () => stringify, truthy: () => truthy, typeOf: () => typeOf, unique: () => unique, walk: () => walk }); module.exports = __toCommonJS(util_exports); class MingoError extends Error { } const MAX_INT = 2147483647; const MIN_INT = -2147483648; const MAX_LONG = Number.MAX_SAFE_INTEGER; const MIN_LONG = Number.MIN_SAFE_INTEGER; const MISSING = Symbol("missing"); const CYCLE_FOUND_ERROR = Object.freeze( new Error("mingo: cycle detected while processing object/array") ); const OBJECT_TAG = "[object Object]"; const DEFAULT_HASH_FUNCTION = (value) => { const s = stringify(value); let hash = 0; let i = s.length; while (i) hash = (hash << 5) - hash ^ s.charCodeAt(--i); return hash >>> 0; }; const EMPTY_ARRAY = []; const isPrimitive = (v) => typeof v !== "object" && typeof v !== "function" || v === null; const JS_SIMPLE_TYPES = /* @__PURE__ */ new Set([ "null", "undefined", "boolean", "number", "string", "date", "regex" ]); const SORT_ORDER_BY_TYPE = { null: 0, undefined: 0, number: 1, string: 2, object: 3, array: 4, boolean: 5, date: 6, regex: 7, function: 8 }; const compare = (a, b) => { if (a === MISSING) a = void 0; if (b === MISSING) b = void 0; const [u, v] = [a, b].map((n) => SORT_ORDER_BY_TYPE[typeOf(n)]); if (u !== v) return u - v; if (u === 1 || u === 2 || u === 6) { if (a < b) return -1; if (a > b) return 1; return 0; } if (isEqual(a, b)) return 0; if (a < b) return -1; if (a > b) return 1; return 0; }; class ValueMap extends Map { // The hash function #hashFn = DEFAULT_HASH_FUNCTION; // maps the hashcode to key set #keyMap = /* @__PURE__ */ new Map(); // returns a tuple of [, ]. Expects an object key. #unpack = (key) => { const hash = this.#hashFn(key); return [ (this.#keyMap.get(hash) || EMPTY_ARRAY).find((k) => isEqual(k, key)), hash ]; }; constructor() { super(); } /** * Returns a new {@link ValueMap} object. * @param fn An optional custom hash function */ static init(fn) { const m = new ValueMap(); if (fn) m.#hashFn = fn; return m; } clear() { super.clear(); this.#keyMap.clear(); } /** * @returns true if an element in the Map existed and has been removed, or false if the element does not exist. */ delete(key) { if (isPrimitive(key)) return super.delete(key); const [masterKey, hash] = this.#unpack(key); if (!super.delete(masterKey)) return false; this.#keyMap.set( hash, this.#keyMap.get(hash).filter((k) => !isEqual(k, masterKey)) ); return true; } /** * Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map. * @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned. */ get(key) { if (isPrimitive(key)) return super.get(key); const [masterKey, _] = this.#unpack(key); return super.get(masterKey); } /** * @returns boolean indicating whether an element with the specified key exists or not. */ has(key) { if (isPrimitive(key)) return super.has(key); const [masterKey, _] = this.#unpack(key); return super.has(masterKey); } /** * Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated. */ set(key, value) { if (isPrimitive(key)) return super.set(key, value); const [masterKey, hash] = this.#unpack(key); if (super.has(masterKey)) { super.set(masterKey, value); } else { super.set(key, value); const keys = this.#keyMap.get(hash) || []; keys.push(key); this.#keyMap.set(hash, keys); } return this; } /** * @returns the number of elements in the Map. */ get size() { return super.size; } } function assert(condition, message) { if (!condition) throw new MingoError(message); } const typeOf = (v) => { if (v === null) return "null"; const n = typeof v; if (n !== "object") return n; if (isDate(v)) return "date"; if (isArray(v)) return "array"; if (isRegExp(v)) return "regex"; return n; }; const isBoolean = (v) => typeof v === "boolean"; const isString = (v) => typeof v === "string"; const isSymbol = (v) => typeof v === "symbol"; const isNumber = (v) => !isNaN(v) && typeof v === "number"; const isNotNaN = (v) => !(isNaN(v) && typeof v === "number"); const isArray = Array.isArray; const isObject = (v) => { if (!v) return false; const proto = Object.getPrototypeOf(v); return (proto === Object.prototype || proto === null) && OBJECT_TAG === Object.prototype.toString.call(v); }; const isObjectLike = (v) => !isPrimitive(v); const isDate = (v) => v instanceof Date; const isRegExp = (v) => v instanceof RegExp; const isFunction = (v) => typeof v === "function"; const isNil = (v) => v === null || v === void 0; const inArray = (arr, item) => arr.includes(item); const notInArray = (arr, item) => !inArray(arr, item); const truthy = (arg, strict = true) => !!arg || strict && arg === ""; const isEmpty = (x) => isNil(x) || isString(x) && !x || isArray(x) && x.length === 0 || isObject(x) && Object.keys(x).length === 0; const isMissing = (v) => v === MISSING; const ensureArray = (x) => isArray(x) ? x : [x]; const has = (obj, prop) => !!obj && Object.prototype.hasOwnProperty.call(obj, prop); const isTypedArray = (v) => typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(v); const cloneInternal = (v, refs) => { if (refs.has(v)) throw CYCLE_FOUND_ERROR; if (isPrimitive(v)) return v; if (isDate(v)) return new Date(v); if (isRegExp(v)) return new RegExp(v); if (isTypedArray(v)) { const ctor = v.constructor; return new ctor(v); } try { refs.add(v); if (isArray(v)) return v.map((e) => cloneInternal(e, refs)); if (isObject(v)) { const res = {}; for (const k of Object.keys(v)) res[k] = cloneInternal(v[k], refs); return res; } } finally { refs.delete(v); } return v; }; const cloneDeep = (obj) => cloneInternal(obj, /* @__PURE__ */ new Set()); function intersection(input, hashFunction = DEFAULT_HASH_FUNCTION) { const vmaps = [ValueMap.init(hashFunction), ValueMap.init(hashFunction)]; if (input.length === 0) return []; if (input.some((arr) => arr.length === 0)) return []; if (input.length === 1) return [...input]; input[input.length - 1].forEach((v) => vmaps[0].set(v, true)); for (let i = input.length - 2; i > -1; i--) { input[i].forEach((v) => { if (vmaps[0].has(v)) vmaps[1].set(v, true); }); if (vmaps[1].size === 0) return []; vmaps.reverse(); vmaps[1].clear(); } return Array.from(vmaps[0].keys()); } function flatten(xs, depth = 1) { const arr = new Array(); function flatten2(ys, n) { for (let i = 0, len = ys.length; i < len; i++) { if (isArray(ys[i]) && (n > 0 || n < 0)) { flatten2(ys[i], Math.max(-1, n - 1)); } else { arr.push(ys[i]); } } } flatten2(xs, depth); return arr; } const getMembersOf = (value) => { let [proto, names] = [ Object.getPrototypeOf(value), Object.getOwnPropertyNames(value) ]; let activeProto = proto; while (!names.length && proto !== Object.prototype && proto !== Array.prototype) { activeProto = proto; names = Object.getOwnPropertyNames(proto); proto = Object.getPrototypeOf(proto); } const o = {}; names.forEach((k) => o[k] = value[k]); return [o, activeProto]; }; function isEqual(a, b) { if (a === b || Object.is(a, b)) return true; const ctor = !!a && a.constructor || a; if (a === null || b === null || a === void 0 || b === void 0 || ctor !== b.constructor || ctor === Function) { return false; } if (ctor === Array || ctor === Object) { const aKeys = Object.keys(a); const bKeys = Object.keys(b); if (aKeys.length !== bKeys.length) return false; if ((/* @__PURE__ */ new Set([...aKeys, ...bKeys])).size != aKeys.length) return false; for (const k of aKeys) if (!isEqual(a[k], b[k])) return false; return true; } const proto = Object.getPrototypeOf(a); const cmp = isTypedArray(a) || proto !== Object.prototype && proto !== Array.prototype && Object.prototype.hasOwnProperty.call(proto, "toString"); return cmp && a.toString() === b.toString(); } function unique(input, hashFunction = DEFAULT_HASH_FUNCTION) { const m = ValueMap.init(hashFunction); input.forEach((v) => m.set(v, true)); return Array.from(m.keys()); } const toString = (v, cycle) => { if (v === null) return "null"; if (v === void 0) return "undefined"; const ctor = v.constructor; switch (ctor) { case RegExp: case Number: case Boolean: case Function: case Symbol: return v.toString(); case String: return JSON.stringify(v); case Date: return v.toISOString(); } if (isTypedArray(v)) return ctor.name + "[" + v.toString() + "]"; if (cycle.has(v)) throw CYCLE_FOUND_ERROR; try { cycle.add(v); if (isArray(v)) { return "[" + v.map((s) => toString(s, cycle)).join(",") + "]"; } if (ctor === Object) { return "{" + Object.keys(v).sort().map((k) => k + ":" + toString(v[k], cycle)).join(",") + "}"; } const proto = Object.getPrototypeOf(v); if (proto !== Object.prototype && proto !== Array.prototype && Object.prototype.hasOwnProperty.call(proto, "toString")) { return ctor.name + "(" + JSON.stringify(v.toString()) + ")"; } const [members, _] = getMembersOf(v); return ctor.name + toString(members, cycle); } finally { cycle.delete(v); } }; const stringify = (value) => toString(value, /* @__PURE__ */ new Set()); function hashCode(value, hashFunction) { if (isNil(value)) return null; hashFunction = hashFunction || DEFAULT_HASH_FUNCTION; return hashFunction(value); } function groupBy(collection, keyFn, hashFunction = DEFAULT_HASH_FUNCTION) { if (collection.length < 1) return /* @__PURE__ */ new Map(); const lookup = /* @__PURE__ */ new Map(); const result = /* @__PURE__ */ new Map(); for (let i = 0; i < collection.length; i++) { const obj = collection[i]; const key = keyFn(obj, i); const hash = hashCode(key, hashFunction); if (hash === null) { if (result.has(null)) { result.get(null).push(obj); } else { result.set(null, [obj]); } } else { const existingKey = lookup.has(hash) ? lookup.get(hash).find((k) => isEqual(k, key)) : null; if (isNil(existingKey)) { result.set(key, [obj]); if (lookup.has(hash)) { lookup.get(hash).push(key); } else { lookup.set(hash, [key]); } } else { result.get(existingKey).push(obj); } } } return result; } const MAX_ARRAY_PUSH = 5e4; function into(target, ...rest) { if (isArray(target)) { return rest.reduce( (acc, arr) => { let i = Math.ceil(arr.length / MAX_ARRAY_PUSH); let begin = 0; while (i-- > 0) { Array.prototype.push.apply( acc, arr.slice(begin, begin + MAX_ARRAY_PUSH) ); begin += MAX_ARRAY_PUSH; } return acc; }, target ); } else { return rest.filter(isObjectLike).reduce((acc, item) => { Object.assign(acc, item); return acc; }, target); } } function memoize(fn, hashFunction = DEFAULT_HASH_FUNCTION) { const memo = ValueMap.init(hashFunction); return (...args) => { if (!memo.has(args)) memo.set(args, fn.apply(this, args)); return memo.get(args); }; } function getValue(obj, key) { return isObjectLike(obj) ? obj[key] : void 0; } function unwrap(arr, depth) { if (depth < 1) return arr; while (depth-- && arr.length === 1) arr = arr[0]; return arr; } function resolve(obj, selector, options) { let depth = 0; function resolve2(o, path) { let value = o; for (let i = 0; i < path.length; i++) { const field = path[i]; const isText = /^\d+$/.exec(field) === null; if (isText && isArray(value)) { if (i === 0 && depth > 0) break; depth += 1; const subpath = path.slice(i); value = value.reduce((acc, item) => { const v = resolve2(item, subpath); if (v !== void 0) acc.push(v); return acc; }, []); break; } else { value = getValue(value, field); } if (value === void 0) break; } return value; } const result = JS_SIMPLE_TYPES.has(typeOf(obj)) ? obj : resolve2(obj, selector.split(".")); return isArray(result) && options?.unwrapArray ? unwrap(result, depth) : result; } function resolveGraph(obj, selector, options) { const names = selector.split("."); const key = names[0]; const next = names.slice(1).join("."); const isIndex = /^\d+$/.test(key); const hasNext = names.length > 1; let result; let value; if (isArray(obj)) { if (isIndex) { result = getValue(obj, Number(key)); if (hasNext) { result = resolveGraph(result, next, options); } result = [result]; } else { result = []; for (const item of obj) { value = resolveGraph(item, selector, options); if (options?.preserveMissing) { if (value === void 0) { value = MISSING; } result.push(value); } else if (value !== void 0) { result.push(value); } } } } else { value = getValue(obj, key); if (hasNext) { value = resolveGraph(value, next, options); } if (value === void 0) return void 0; result = options?.preserveKeys ? { ...obj } : {}; result[key] = value; } return result; } function filterMissing(obj) { if (isArray(obj)) { for (let i = obj.length - 1; i >= 0; i--) { if (obj[i] === MISSING) { obj.splice(i, 1); } else { filterMissing(obj[i]); } } } else if (isObject(obj)) { for (const k in obj) { if (has(obj, k)) { filterMissing(obj[k]); } } } } const NUMBER_RE = /^\d+$/; function walk(obj, selector, fn, options) { const names = selector.split("."); const key = names[0]; const next = names.slice(1).join("."); if (names.length === 1) { if (isObject(obj) || isArray(obj) && NUMBER_RE.test(key)) { fn(obj, key); } } else { if (options?.buildGraph && isNil(obj[key])) { obj[key] = {}; } const item = obj[key]; if (!item) return; const isNextArrayIndex = !!(names.length > 1 && NUMBER_RE.test(names[1])); if (isArray(item) && options?.descendArray && !isNextArrayIndex) { item.forEach((e) => walk(e, next, fn, options)); } else { walk(item, next, fn, options); } } } function setValue(obj, selector, value) { walk( obj, selector, (item, key) => { item[key] = isFunction(value) ? value(item[key]) : value; }, { buildGraph: true } ); } function removeValue(obj, selector, options) { walk( obj, selector, (item, key) => { if (isArray(item)) { if (/^\d+$/.test(key)) { item.splice(parseInt(key), 1); } else if (options && options.descendArray) { for (const elem of item) { if (isObject(elem)) { delete elem[key]; } } } } else if (isObject(item)) { delete item[key]; } }, options ); } const OPERATOR_NAME_PATTERN = /^\$[a-zA-Z0-9_]+$/; function isOperator(name) { return OPERATOR_NAME_PATTERN.test(name); } function normalize(expr) { if (JS_SIMPLE_TYPES.has(typeOf(expr))) { return isRegExp(expr) ? { $regex: expr } : { $eq: expr }; } if (isObjectLike(expr)) { const exprObj = expr; if (!Object.keys(exprObj).some(isOperator)) { return { $eq: expr }; } if (has(expr, "$regex")) { const newExpr = { ...expr }; newExpr["$regex"] = new RegExp( expr["$regex"], expr["$options"] ); delete newExpr["$options"]; return newExpr; } } return expr; } function findInsertIndex(sorted, item, comparator = compare) { let lo = 0; let hi = sorted.length - 1; while (lo <= hi) { const mid = Math.round(lo + (hi - lo) / 2); if (comparator(item, sorted[mid]) < 0) { hi = mid - 1; } else if (comparator(item, sorted[mid]) > 0) { lo = mid + 1; } else { return mid; } } return lo; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { EMPTY_ARRAY, MAX_INT, MAX_LONG, MIN_INT, MIN_LONG, MingoError, ValueMap, assert, cloneDeep, compare, ensureArray, filterMissing, findInsertIndex, flatten, groupBy, has, hashCode, inArray, intersection, into, isArray, isBoolean, isDate, isEmpty, isEqual, isFunction, isMissing, isNil, isNotNaN, isNumber, isObject, isObjectLike, isOperator, isPrimitive, isRegExp, isString, isSymbol, memoize, normalize, notInArray, removeValue, resolve, resolveGraph, setValue, stringify, truthy, typeOf, unique, walk });