Files
json-server/src/matches-where.test.ts
Copilot 43822ad8ad Add contains, startsWith, endsWith string query operators (#1714)
* Initial plan

* Add contains, startsWith, endsWith query operators for partial string matching

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>

* Delete package-lock.json

* docs: document contains, startsWith, endsWith operators in README

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>

* test: add numeric-value tests for contains, startsWith, endsWith operators

Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: typicode <5502029+typicode@users.noreply.github.com>
Co-authored-by: typicode <typicode@gmail.com>
2026-02-26 23:46:25 +01:00

64 lines
2.1 KiB
JavaScript

import assert from 'node:assert/strict'
import test from 'node:test'
import type { JsonObject } from 'type-fest'
import { matchesWhere } from './matches-where.ts'
await test('matchesWhere', async (t) => {
const obj: JsonObject = { a: 10, b: 20, c: 'x', nested: { a: 10, b: 20 } }
const cases: [JsonObject, boolean][] = [
[{ a: { eq: 10 } }, true],
[{ a: { eq: 11 } }, false],
[{ c: { ne: 'y' } }, true],
[{ c: { ne: 'x' } }, false],
[{ a: { lt: 11 } }, true],
[{ a: { lt: 10 } }, false],
[{ a: { lte: 10 } }, true],
[{ a: { lte: 9 } }, false],
[{ b: { gt: 19 } }, true],
[{ b: { gt: 20 } }, false],
[{ b: { gte: 20 } }, true],
[{ b: { gte: 21 } }, false],
[{ a: { gt: 0 }, b: { lt: 30 } }, true],
[{ a: { gt: 10 }, b: { lt: 30 } }, false],
[{ or: [{ a: { lt: 0 } }, { b: { gt: 19 } }] }, true],
[{ or: [{ a: { lt: 0 } }, { b: { gt: 20 } }] }, false],
[{ nested: { a: { eq: 10 } } }, true],
[{ nested: { b: { lt: 20 } } }, false],
[{ a: { in: [10, 11] } }, true],
[{ a: { in: [1, 2] } }, false],
[{ c: { in: ['x', 'y'] } }, true],
[{ c: { in: ['y', 'z'] } }, false],
[{ a: { in: [10, 11], gt: 9 } }, true],
[{ a: { in: [10, 11], gt: 10 } }, false],
[{ a: { foo: 10 } }, true],
[{ a: { foo: 10, eq: 10 } }, true],
[{ missing: { foo: 1 } }, true],
// contains
[{ c: { contains: 'x' } }, true],
[{ c: { contains: 'X' } }, true],
[{ c: { contains: 'z' } }, false],
[{ a: { contains: '1' } }, false],
[{ c: { contains: 1 } }, false],
// startsWith
[{ c: { startsWith: 'x' } }, true],
[{ c: { startsWith: 'X' } }, true],
[{ c: { startsWith: 'z' } }, false],
[{ a: { startsWith: '1' } }, false],
[{ c: { startsWith: 1 } }, false],
// endsWith
[{ c: { endsWith: 'x' } }, true],
[{ c: { endsWith: 'X' } }, true],
[{ c: { endsWith: 'z' } }, false],
[{ a: { endsWith: '1' } }, false],
[{ c: { endsWith: 1 } }, false],
]
for (const [query, expected] of cases) {
await t.test(JSON.stringify(query), () => {
assert.equal(matchesWhere(obj, query), expected)
})
}
})