Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ Operators:
- `gt` greater than, `gte` greater than or equal
- `eq` equal, `ne` not equal
- `in` included in comma-separated list
- `contains` string contains (case-insensitive)
- `startsWith` string starts with (case-insensitive)
- `endsWith` string ends with (case-insensitive)

Examples:

Expand All @@ -175,6 +178,9 @@ GET /posts?views:gt=100
GET /posts?title:eq=Hello
GET /posts?id:in=1,2,3
GET /posts?author.name:eq=typicode
GET /posts?title:contains=hello
GET /posts?title:startsWith=Hello
GET /posts?title:endsWith=world
```

### Sort
Expand Down
18 changes: 18 additions & 0 deletions src/matches-where.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ await test('matchesWhere', async (t) => {
[{ 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) {
Expand Down
12 changes: 12 additions & 0 deletions src/matches-where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ export function matchesWhere(obj: JsonObject, where: JsonObject): boolean {
const inValues = Array.isArray(op.in) ? op.in : [op.in]
if (!inValues.some((v) => (field as any) === (v as any))) return false
}
if (knownOps.includes('contains')) {
if (typeof field !== 'string') return false
if (!field.toLowerCase().includes(String(op.contains).toLowerCase())) return false
}
if (knownOps.includes('startsWith')) {
if (typeof field !== 'string') return false
if (!field.toLowerCase().startsWith(String(op.startsWith).toLowerCase())) return false
}
if (knownOps.includes('endsWith')) {
if (typeof field !== 'string') return false
if (!field.toLowerCase().endsWith(String(op.endsWith).toLowerCase())) return false
}
continue
}

Expand Down
18 changes: 18 additions & 0 deletions src/parse-where.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ await test('parseWhere', async (t) => {
title: { in: ['hello', 'world'] },
},
],
[
'title:contains=ello',
{
title: { contains: 'ello' },
},
],
[
'title:startsWith=hel',
{
title: { startsWith: 'hel' },
},
],
[
'title:endsWith=rld',
{
title: { endsWith: 'rld' },
},
],
]

for (const [query, expected] of cases) {
Expand Down
13 changes: 12 additions & 1 deletion src/where-operators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
export const WHERE_OPERATORS = ['lt', 'lte', 'gt', 'gte', 'eq', 'ne', 'in'] as const
export const WHERE_OPERATORS = [
'lt',
'lte',
'gt',
'gte',
'eq',
'ne',
'in',
'contains',
'startsWith',
'endsWith',
] as const

export type WhereOperator = (typeof WHERE_OPERATORS)[number]

Expand Down