UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

203 lines (202 loc) 5.53 kB
import { isTemporal } from "../utils.js"; import { getRuntimeReferenceIdentity } from "../query/runtime-reference-identity.js"; const objectIds = /* @__PURE__ */ new WeakMap(); let nextObjectId = 1; function getObjectId(obj) { if (objectIds.has(obj)) { return objectIds.get(obj); } const id = nextObjectId++; objectIds.set(obj, id); return id; } function isUnorderable(value) { return typeof value === `number` && Number.isNaN(value) || value instanceof Date && Number.isNaN(value.getTime()); } const ascComparator = (a, b, opts) => { const { nulls } = opts; if (a == null && b == null) return 0; if (a == null) return nulls === `first` ? -1 : 1; if (b == null) return nulls === `first` ? 1 : -1; const aUnordered = isUnorderable(a); const bUnordered = isUnorderable(b); if (aUnordered && bUnordered) return 0; if (aUnordered) return 1; if (bUnordered) return -1; if (typeof a === `string` && typeof b === `string`) { if (opts.stringSort === `locale`) { return a.localeCompare(b, opts.locale, opts.localeOptions); } } if (Array.isArray(a) && Array.isArray(b)) { for (let i = 0; i < Math.min(a.length, b.length); i++) { const result = ascComparator(a[i], b[i], opts); if (result !== 0) { return result; } } return a.length - b.length; } if (a instanceof Date && b instanceof Date) { return a.getTime() - b.getTime(); } if (isTemporal(a) && isTemporal(b)) { return compareTemporalValues(a, b); } const aIsSymbol = typeof a === `symbol`; const bIsSymbol = typeof b === `symbol`; if (aIsSymbol && bIsSymbol) { if (a === b) return 0; return getRuntimeReferenceIdentity(a)[2] - getRuntimeReferenceIdentity(b)[2]; } if (aIsSymbol) return 1; if (bIsSymbol) return -1; const aIsObject = typeof a === `object`; const bIsObject = typeof b === `object`; if (aIsObject || bIsObject) { if (aIsObject && bIsObject) { const aId = getObjectId(a); const bId = getObjectId(b); return aId - bId; } if (aIsObject) return 1; if (bIsObject) return -1; } if (a < b) return -1; if (a > b) return 1; return 0; }; const descComparator = (a, b, opts) => { return ascComparator(b, a, { ...opts, nulls: opts.nulls === `first` ? `last` : `first` }); }; function makeComparator(opts) { return (a, b) => { if (opts.direction === `asc`) { return ascComparator(a, b, opts); } else { return descComparator(a, b, opts); } }; } const defaultComparator = makeComparator({ direction: `asc`, nulls: `first`, stringSort: `locale` }); function isUint8Array(value) { return value instanceof Uint8Array || typeof Buffer !== `undefined` && value instanceof Buffer; } function areUint8ArraysEqual(a, b) { if (a.byteLength !== b.byteLength) { return false; } for (let i = 0; i < a.byteLength; i++) { if (a[i] !== b[i]) { return false; } } return true; } const NORMALIZED_KEY_PREFIX = `\0tanstack-db:`; function normalizedKey(kind, value) { return `${NORMALIZED_KEY_PREFIX}${kind}:${value}`; } function normalizeBinary(value) { let bytes = ``; for (let index = 0; index < value.byteLength; index++) { bytes += String.fromCharCode(value[index]); } return normalizedKey(`binary`, bytes); } const UNDEFINED_SENTINEL = normalizedKey(`undefined`, ``); function normalizeValue(value) { if (typeof value === `string`) { return value.startsWith(NORMALIZED_KEY_PREFIX) ? normalizedKey(`string`, value) : value; } if (typeof value !== `object` || value === null) { return value; } if (value instanceof Date) { return value.getTime(); } if (isTemporal(value)) { return normalizedKey( `temporal`, `${value[Symbol.toStringTag]}:${value.toString()}` ); } if (isUint8Array(value)) { return normalizeBinary(value); } return value; } function normalizeForBTree(value) { if (value === void 0) { return UNDEFINED_SENTINEL; } return normalizeValue(value); } function areSameValueZeroEqual(a, b) { return a === b || Number.isNaN(a) && Number.isNaN(b); } function denormalizeUndefined(value) { if (value === UNDEFINED_SENTINEL) { return void 0; } return value; } const temporalCompareByTag = /* @__PURE__ */ new Map(); function compareTemporalValues(a, b) { const aTag = a[Symbol.toStringTag]; const bTag = b[Symbol.toStringTag]; if (aTag !== bTag) { throw new TypeError( `Cannot order Temporal values of different types: ${aTag} vs ${bTag}` ); } let compare = temporalCompareByTag.get(aTag); if (compare === void 0) { const fn = a.constructor.compare; compare = typeof fn === `function` ? fn : null; temporalCompareByTag.set(aTag, compare); } if (compare === null) { throw new TypeError(`${aTag} has no defined ordering`); } return compare(a, b); } function compareValues(a, b) { if (isTemporal(a) && isTemporal(b)) { return compareTemporalValues(a, b); } return a < b ? -1 : a > b ? 1 : 0; } function areValuesEqual(a, b) { if (a === b) { return true; } if (isUint8Array(a) && isUint8Array(b)) { return areUint8ArraysEqual(a, b); } return false; } export { UNDEFINED_SENTINEL, areSameValueZeroEqual, areValuesEqual, ascComparator, compareTemporalValues, compareValues, defaultComparator, denormalizeUndefined, descComparator, isUint8Array, isUnorderable, makeComparator, normalizeForBTree, normalizeValue }; //# sourceMappingURL=comparison.js.map