UNPKG

mingo

Version:

MongoDB query language for in-memory objects

186 lines (185 loc) 5.24 kB
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 hash_exports = {}; __export(hash_exports, { hashCode: () => hashCode }); module.exports = __toCommonJS(hash_exports); const numBuf = new ArrayBuffer(8); const numView = new DataView(numBuf); const numBytes = new Uint8Array(numBuf); const murmurHash3 = (buf, seed) => { let h = seed >>> 0; const c1 = 3432918353; const c2 = 461845907; const len = buf.length; const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength); let i = 0; for (; i + 4 <= len; i += 4) { let k = view.getUint32(i, true); k = Math.imul(k, c1); k = k << 15 | k >>> 17; k = Math.imul(k, c2); h ^= k; h = h << 13 | h >>> 19; h = Math.imul(h, 5) + 3864292196; } let k1 = 0; switch (len & 3) { case 3: k1 ^= buf[i + 2] << 16; case 2: k1 ^= buf[i + 1] << 8; case 1: k1 ^= buf[i]; k1 = Math.imul(k1, c1); k1 = k1 << 15 | k1 >>> 17; k1 = Math.imul(k1, c2); h ^= k1; } h ^= len; h ^= h >>> 16; h = Math.imul(h, 2246822507); h ^= h >>> 13; h = Math.imul(h, 3266489909); h ^= h >>> 16; return h >>> 0; }; const feedBytes = (h, bytes) => murmurHash3(bytes, h); const singleByte = new Uint8Array(1); const feedByte = (h, b) => { singleByte[0] = b; return feedBytes(h, singleByte); }; const enc = typeof TextEncoder !== "undefined" ? new TextEncoder() : null; const encodeString = enc ? (s) => enc.encode(s) : (s) => { const buf = new Uint8Array(s.length); for (let i = 0; i < s.length; i++) buf[i] = s.charCodeAt(i); return buf; }; const encodeNumber = (n) => { numView.setFloat64(0, n, true); return numBytes; }; const TAG = { null: 48, // '0' undefined: 85, // 'U' boolean: 66, // 'B' number: 78, // 'N' string: 83, // 'S' bigint: 71, // 'G' symbol: 89, // 'Y' function: 70, // 'F' cycle: 88, // 'X' array: 65, // 'A' object: 79, // 'O' date: 68, // 'D' regexp: 82 // 'R' }; const feedNull = (h) => feedByte(h, TAG.null); const feedUndefined = (h) => feedByte(h, TAG.undefined); const feedBoolean = (h, v) => feedByte(feedByte(h, TAG.boolean), v ? 1 : 0); const feedNumber = (h, v) => feedBytes(feedByte(h, TAG.number), encodeNumber(v)); const feedString = (h, v) => feedBytes(feedByte(h, TAG.string), encodeString(v)); const feedBigInt = (h, v) => { let x = v; h = feedByte(feedByte(h, TAG.bigint), x >= 0n ? 1 : 2); if (x < 0n) x = -x; if (x === 0n) return feedByte(h, 0); while (x > 0n) { h = feedByte(h, Number(x & 0xffn)); x >>= 8n; } return h; }; const feedSymbol = (h, v) => feedString(feedByte(h, TAG.symbol), v.description || ""); const feedFunction = (h, v) => { const fn = v; h = feedString(feedByte(h, TAG.function), fn.name || ""); return feedString(feedNumber(h, fn.length), fn.toString()); }; function hashCode(value) { const seen = /* @__PURE__ */ new WeakMap(); let nextId = 1; const feedObject = (h, v) => { const obj = v; const existingId = seen.get(obj); if (existingId) { return feedNumber(feedByte(h, TAG.cycle), existingId); } seen.set(obj, nextId++); if (Array.isArray(obj)) { h = feedByte(h, TAG.array); for (let i = 0; i < obj.length; i++) { h = feed(feedNumber(h, i), obj[i]); } return h; } if (ArrayBuffer.isView(obj) && !(obj instanceof DataView)) { const bytes = new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength); return feedBytes(feedByte(h, TAG.array), bytes); } if (obj instanceof Date) { return feedNumber(feedByte(h, TAG.date), obj.getTime()); } if (obj instanceof RegExp) { h = feedString(feedByte(h, TAG.regexp), obj.source); return feedString(h, obj.flags); } h = feed(feedByte(h, TAG.object), obj.constructor); const keys = Object.keys(obj).sort(); for (const k of keys) { h = feed(feedString(h, k), obj[k]); } return h; }; const handlers = { null: feedNull, undefined: feedUndefined, boolean: feedBoolean, number: feedNumber, string: feedString, bigint: feedBigInt, symbol: feedSymbol, function: feedFunction, object: feedObject }; const feed = (h, v) => { const t = v === null ? "null" : typeof v; const handler = handlers[t]; return handler ? handler(h, v) : h; }; return feed(0, value) >>> 0; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { hashCode });