UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

1 lines • 2.86 kB
{"version":3,"file":"equality-conjunct.cjs","sources":["../../../src/query/equality-conjunct.ts"],"sourcesContent":["import { normalizeValue } from '../utils/comparison.js'\nimport { isVirtualPropName } from '../virtual-props.js'\nimport type { BasicExpression, PropRef } from './ir.js'\n\ntype Row = Record<string, unknown>\n\n/** An `eq(field, literal)` conjunct on a row's own field. */\nexport type EqualityConjunct = { path: Array<string>; literalKey: string }\n\n// Typed so that 1, '1', and true stay distinct.\nexport function equalityKey(value: unknown): string | undefined {\n const normalized = normalizeValue(value)\n const type = typeof normalized\n return type === `string` || type === `number` || type === `boolean`\n ? `${type}:${String(normalized)}`\n : undefined\n}\n\nexport function readPath(row: Row, path: Array<string>): unknown {\n try {\n let value: unknown = row\n for (const segment of path) value = (value as Row | undefined)?.[segment]\n return value\n } catch {\n // The full predicate treats a throwing read as false.\n return undefined\n }\n}\n\n/**\n * The `eq(field, literal)` conjunct `expression` is, with the field's path in\n * the row as `rowPath` gives it, or undefined for any other expression.\n */\nexport function equalityConjunct(\n expression: BasicExpression,\n rowPath: (ref: PropRef) => Array<string> | undefined,\n): EqualityConjunct | undefined {\n if (expression.type !== `func` || expression.name !== `eq`) return undefined\n const args = expression.args\n if (args.length !== 2) return undefined\n const left = args[0]!\n const right = args[1]!\n const ref = left.type === `ref` ? left : right\n const literal = left.type === `val` ? left : right\n if (ref.type !== `ref` || literal.type !== `val`) return undefined\n const path = rowPath(ref)\n if (!path?.length || isVirtualPropName(path[0]!)) return undefined\n const literalKey = equalityKey(literal.value)\n return literalKey === undefined ? undefined : { path, literalKey }\n}\n"],"names":["normalizeValue","isVirtualPropName"],"mappings":";;;;AAUO,SAAS,YAAY,OAAoC;AAC9D,QAAM,aAAaA,WAAAA,eAAe,KAAK;AACvC,QAAM,OAAO,OAAO;AACpB,SAAO,SAAS,YAAY,SAAS,YAAY,SAAS,YACtD,GAAG,IAAI,IAAI,OAAO,UAAU,CAAC,KAC7B;AACN;AAEO,SAAS,SAAS,KAAU,MAA8B;AAC/D,MAAI;AACF,QAAI,QAAiB;AACrB,eAAW,WAAW,KAAM,SAAS,QAA4B,OAAO;AACxE,WAAO;AAAA,EACT,QAAQ;AAEN,WAAO;AAAA,EACT;AACF;AAMO,SAAS,iBACd,YACA,SAC8B;AAC9B,MAAI,WAAW,SAAS,UAAU,WAAW,SAAS,KAAM,QAAO;AACnE,QAAM,OAAO,WAAW;AACxB,MAAI,KAAK,WAAW,EAAG,QAAO;AAC9B,QAAM,OAAO,KAAK,CAAC;AACnB,QAAM,QAAQ,KAAK,CAAC;AACpB,QAAM,MAAM,KAAK,SAAS,QAAQ,OAAO;AACzC,QAAM,UAAU,KAAK,SAAS,QAAQ,OAAO;AAC7C,MAAI,IAAI,SAAS,SAAS,QAAQ,SAAS,MAAO,QAAO;AACzD,QAAM,OAAO,QAAQ,GAAG;AACxB,MAAI,CAAC,MAAM,UAAUC,aAAAA,kBAAkB,KAAK,CAAC,CAAE,EAAG,QAAO;AACzD,QAAM,aAAa,YAAY,QAAQ,KAAK;AAC5C,SAAO,eAAe,SAAY,SAAY,EAAE,MAAM,WAAA;AACxD;;;;"}