The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 3 | if (!where || typeof where !== "object") return true; |
| 4 | return Object.entries(where).every(([key, condition]) => { |
| 5 | const value = data[key]; |
| 6 | if (condition === null || typeof condition !== "object") return value === condition; |
| 7 | return Object.entries(condition).every(([operator, expected]) => { |
| 8 | if (operator === "eq") return value === expected; |
| 9 | if (operator === "contains") |
| 10 | return ( |
| 11 | typeof value === "string" && |
| 12 | typeof expected === "string" && |
| 13 | value.toLowerCase().includes(expected.toLowerCase()) |
| 14 | ); |
| 15 | if ( |
| 16 | typeof value !== typeof expected || |
| 17 | (typeof value !== "string" && typeof value !== "number") |
| 18 | ) |
| 19 | return false; |
| 20 | const comparison = |
| 21 | typeof value === "number" |
| 22 | ? value - Number(expected) |
| 23 | : value.localeCompare(String(expected)); |
| 24 | if (operator === "gt") return comparison > 0; |
| 25 | if (operator === "gte") return comparison >= 0; |
| 26 | if (operator === "lt") return comparison < 0; |
| 27 | if (operator === "lte") return comparison <= 0; |
| 28 | return false; |
| 29 | }); |
| 30 | }); |
| 31 | } |
| 32 |
| 33 | /** Numeric fields sort by value; mixed scalar types keep numbers together. */ |
| 34 | export function compareRecordValues(a: unknown, b: unknown): number { |
| 35 | if (typeof a === "number" && typeof b === "number") return a - b; |
| 36 | if (typeof a === "number") return -1; |
| 37 | if (typeof b === "number") return 1; |
| 38 | const left = String(a ?? ""), |
| 39 | right = String(b ?? ""); |
| 40 | // PostgreSQL's C collation follows Unicode code points for valid UTF-8. |
| 41 | // JavaScript's default comparison instead sorts surrogate code units. |
| 42 | let i = 0, |
| 43 | j = 0; |
| 44 | while (i < left.length && j < right.length) { |
| 45 | const leftPoint = left.codePointAt(i)!, |
| 46 | rightPoint = right.codePointAt(j)!; |
| 47 | if (leftPoint !== rightPoint) return leftPoint - rightPoint; |
| 48 | i += leftPoint > 0xffff ? 2 : 1; |
| 49 | j += rightPoint > 0xffff ? 2 : 1; |
| 50 | } |
| 51 | return left.length - right.length; |
| 52 | } |
| 53 |