Skip to content

Commit d061cc8

Browse files
committed
docs(filters): document filters match modes
1 parent e0a7bf3 commit d061cc8

17 files changed

Lines changed: 1040 additions & 11 deletions

bun.lockb

0 Bytes
Binary file not shown.

docs/.vitepress/config.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,19 @@ export default defineConfig({
7373
{ text: 'lessThan', link: '/filter-match-modes/less-than' },
7474
{ text: 'lessThanOrEqual', link: '/filter-match-modes/less-than-or-equal' },
7575
{ text: 'exists', link: '/filter-match-modes/exists' },
76-
{ text: 'objectStringMap', link: '/filter-match-modes/object-string-map' },
7776
{ text: 'arrayLength', link: '/filter-match-modes/array-length' },
7877
{ text: 'objectMatch', link: '/filter-match-modes/object-match' },
7978
],
8079
},
81-
{
82-
text: 'API Reference',
83-
items: [
84-
{ text: 'Query Function', link: '/api-reference/query-function' },
85-
{ text: 'QueryParams', link: '/api-reference/query-params' },
86-
{ text: 'QueryFilter', link: '/api-reference/query-filter' },
87-
{ text: 'FilterMatchMode', link: '/api-reference/filter-match-mode' },
88-
],
89-
},
80+
// {
81+
// text: 'API Reference',
82+
// items: [
83+
// { text: 'Query Function', link: '/api-reference/query-function' },
84+
// { text: 'QueryParams', link: '/api-reference/query-params' },
85+
// { text: 'QueryFilter', link: '/api-reference/query-filter' },
86+
// { text: 'FilterMatchMode', link: '/api-reference/filter-match-mode' },
87+
// ],
88+
// },
9089
],
9190

9291
},

docs/features/filtering.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Filtering
2+
3+
ArrayQuery provides a powerful and flexible filtering engine that allows you to apply complex conditions to your data. The filtering system supports a wide range of match modes, nested conditions, and logical grouping with built-in array handling.
4+
5+
## Filter Structure
6+
7+
Filters in ArrayQuery can be either an array of individual filter conditions or an array of filter groups. Here's the updated filter structure:
8+
9+
```ts
10+
export type FilterOptions = Array<QueryFilterGroup> | Array<QueryFilter>
11+
12+
interface QueryFilter {
13+
key: string
14+
value: any
15+
operator?: Operator
16+
matchMode: FilterMatchMode
17+
params?: ComparatorParams | ObjectMapFilterParams
18+
}
19+
20+
interface QueryFilterGroup {
21+
operator: Operator
22+
filters: QueryFilter[]
23+
}
24+
25+
type Operator = 'AND' | 'OR' | (() => 'AND' | 'OR')
26+
```
27+
28+
## Basic Usage
29+
30+
Here's a simple example of how to use the filter feature with ArrayQuery:
31+
32+
```ts twoslash
33+
import { query } from '@chronicstone/array-query'
34+
35+
const users = [
36+
{ id: 1, name: 'John Doe', age: 30, roles: ['admin', 'user'] },
37+
{ id: 2, name: 'Jane Smith', age: 25, roles: ['user'] },
38+
{ id: 3, name: 'Bob Johnson', age: 35, roles: ['manager', 'user'] }
39+
]
40+
41+
const result = query(users, {
42+
filter: [
43+
{ key: 'age', matchMode: 'greaterThan', value: 25 }
44+
]
45+
})
46+
```
47+
48+
This query will return users older than 25.
49+
50+
## Advanced Filtering
51+
52+
ArrayQuery supports complex filtering scenarios, including logical groups and built-in array handling.
53+
54+
### Multiple Conditions with Regular Filters
55+
56+
When you use an array of regular filters, all conditions must be met for a row to be included in the result:
57+
58+
```ts twoslash
59+
import { query } from '@chronicstone/array-query'
60+
61+
const users = [
62+
{ id: 1, name: 'John Doe', age: 30, roles: ['admin', 'user'] },
63+
{ id: 2, name: 'Jane Smith', age: 25, roles: ['user'] },
64+
{ id: 3, name: 'Bob Johnson', age: 35, roles: ['manager', 'user'] }
65+
]
66+
67+
const result = query(users, {
68+
filter: [
69+
{ key: 'age', matchMode: 'greaterThan', value: 25 },
70+
{ key: 'name', matchMode: 'contains', value: 'John' }
71+
]
72+
})
73+
```
74+
75+
This query will return users who are older than 25 AND have 'John' in their name.
76+
77+
### Logical Groups
78+
79+
You can use filter groups to create more complex conditions. When using filter groups, a row will be included if it matches at least one of the groups:
80+
81+
```ts twoslash
82+
import { query } from '@chronicstone/array-query'
83+
84+
const users = [
85+
{ id: 1, name: 'John Doe', age: 30, roles: ['admin', 'user'] },
86+
{ id: 2, name: 'Jane Smith', age: 25, roles: ['user'] },
87+
{ id: 3, name: 'Bob Johnson', age: 35, roles: ['manager', 'user'] },
88+
{ id: 4, name: 'Alice Williams', age: 65, roles: ['user'] }
89+
]
90+
91+
const result = query(users, {
92+
filter: [
93+
{
94+
operator: 'AND',
95+
filters: [
96+
{ key: 'age', matchMode: 'greaterThan', value: 18 },
97+
{ key: 'age', matchMode: 'lessThan', value: 45 }
98+
]
99+
},
100+
{
101+
operator: 'AND',
102+
filters: [
103+
{ key: 'age', matchMode: 'greaterThan', value: 60 },
104+
{ key: 'age', matchMode: 'lessThan', value: 90 }
105+
]
106+
}
107+
]
108+
})
109+
```
110+
111+
This example filters users who are either between 18 and 45 years old, OR between 60 and 90 years old. The row will be included if it matches either of these groups.
112+
113+
### Built-in Array Handling
114+
115+
ArrayQuery automatically handles array values in the data. When a filter is applied to a field that contains an array, the filter condition is checked against each element of the array:
116+
117+
```ts twoslash
118+
import { query } from '@chronicstone/array-query'
119+
120+
const users = [
121+
{ id: 1, name: 'John Doe', age: 30, roles: ['admin', 'user'] },
122+
{ id: 2, name: 'Jane Smith', age: 25, roles: ['user'] },
123+
{ id: 3, name: 'Bob Johnson', age: 35, roles: ['manager', 'user'] }
124+
]
125+
126+
const result = query(users, {
127+
filter: [
128+
{
129+
key: 'roles',
130+
matchMode: 'equals',
131+
value: 'admin',
132+
operator: 'OR'
133+
}
134+
]
135+
})
136+
```
137+
138+
In this example:
139+
- For a user with `roles: ['admin', 'manager']`, the filter will return true because at least one element ("admin") matches the condition.
140+
- The `operator: "OR"` means that if any element in the array matches, the condition is considered true.
141+
- If `operator: "AND"` was used, all elements in the array would need to match the condition.
142+
143+
### Nested Object Filtering
144+
145+
You can filter based on nested object properties using dot notation:
146+
147+
```ts twoslash
148+
import { query } from '@chronicstone/array-query'
149+
150+
const users = [
151+
{ id: 1, name: 'John Doe', age: 30, address: { city: 'New York', country: 'USA' } },
152+
{ id: 2, name: 'Jane Smith', age: 25, address: { city: 'London', country: 'UK' } },
153+
{ id: 3, name: 'Bob Johnson', age: 35, address: { city: 'Paris', country: 'France' } }
154+
]
155+
156+
const result = query(users, {
157+
filter: [
158+
{ key: 'address.city', matchMode: 'equals', value: 'New York' }
159+
]
160+
})
161+
```
162+
163+
## Match Modes
164+
165+
ArrayQuery supports various match modes for different types of comparisons. Each match mode has its own dedicated documentation page for detailed usage. The available match modes include:
166+
167+
- 'contains'
168+
- 'between'
169+
- 'equals'
170+
- 'notEquals'
171+
- 'greaterThan'
172+
- 'greaterThanOrEqual'
173+
- 'lessThan'
174+
- 'lessThanOrEqual'
175+
- 'exists'
176+
- 'arrayLength'
177+
- 'objectMatch'

docs/features/sorting.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Sorting
2+
3+
ArrayQuery provides a flexible sorting capability that allows you to order your data based on one or multiple fields. This feature is crucial for organizing and presenting data in a meaningful way.
4+
5+
## How Sorting Works
6+
7+
The sort feature in ArrayQuery operates based on the `SortOptions` interface:
8+
9+
```ts twoslash
10+
interface SortOptions {
11+
key: string
12+
dir: 'asc' | 'desc'
13+
}
14+
```
15+
16+
- `key`: The field name to sort by
17+
- `dir`: The sort direction ('asc' for ascending, 'desc' for descending)
18+
19+
You can provide either a single `SortOptions` object or an array of `SortOptions` to sort by multiple fields.
20+
21+
## Basic Usage
22+
23+
Here's a simple example of how to use the sort feature with ArrayQuery:
24+
25+
```ts twoslash
26+
import { query } from '@chronicstone/array-query'
27+
28+
const users = [
29+
{ id: 1, name: 'John Doe', age: 30 },
30+
{ id: 2, name: 'Jane Smith', age: 25 },
31+
{ id: 3, name: 'Bob Johnson', age: 35 }
32+
]
33+
34+
const result = query(users, {
35+
sort: { key: 'age', dir: 'asc' }
36+
})
37+
```
38+
39+
This query will return the following result:
40+
41+
```ts twoslash
42+
[
43+
{ id: 2, name: 'Jane Smith', age: 25 },
44+
{ id: 1, name: 'John Doe', age: 30 },
45+
{ id: 3, name: 'Bob Johnson', age: 35 }
46+
]
47+
```
48+
49+
## Multi-Field Sorting
50+
51+
ArrayQuery supports sorting by multiple fields. The sort options are applied in the order they are provided:
52+
53+
```ts twoslash
54+
import { query } from '@chronicstone/array-query'
55+
56+
const users = [
57+
{ id: 1, name: 'John Doe', age: 30 },
58+
{ id: 2, name: 'Jane Smith', age: 25 },
59+
{ id: 3, name: 'Martin Sam', age: 35 },
60+
{ id: 4, name: 'Bob Johnson', age: 35 },
61+
{ id: 5, name: 'Robert John', age: 28 },
62+
{ id: 6, name: 'Alice Williams', age: 28 },
63+
{ id: 7, name: 'Emma Jones', age: 32 }
64+
]
65+
66+
const result = query(users, {
67+
sort: [
68+
{ key: 'age', dir: 'asc' },
69+
{ key: 'name', dir: 'asc' }
70+
]
71+
})
72+
```
73+
74+
This will return:
75+
76+
```ts twoslash
77+
[
78+
{ id: 2, name: 'Jane Smith', age: 25 },
79+
{ id: 6, name: 'Alice Williams', age: 28 },
80+
{ id: 5, name: 'Robert John', age: 28 },
81+
{ id: 1, name: 'John Doe', age: 30 },
82+
{ id: 7, name: 'Emma Jones', age: 32 },
83+
{ id: 4, name: 'Bob Johnson', age: 35 },
84+
{ id: 3, name: 'Martin Sam', age: 35 }
85+
]
86+
```
87+
88+
In this example, the data is first sorted by age in ascending order. For entries with the same age, it then sorts by name in ascending order.
89+
90+
## Sorting Nested Fields
91+
92+
You can sort by nested fields using dot notation:
93+
94+
```ts twoslash
95+
import { query } from '@chronicstone/array-query'
96+
97+
const data = [
98+
{ id: 1, name: 'John', scores: { math: 85, science: 92 } },
99+
{ id: 2, name: 'Jane', scores: { math: 90, science: 88 } },
100+
{ id: 3, name: 'Bob', scores: { math: 78, science: 95 } }
101+
]
102+
103+
const result = query(data, {
104+
sort: { key: 'scores.math', dir: 'desc' }
105+
})
106+
```
107+
108+
This will sort the data based on the math scores in descending order.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Array Length Match Mode
2+
3+
The 'arrayLength' match mode checks the length of an array field against a specified value.
4+
5+
```ts twoslash
6+
import { query } from '@chronicstone/array-query'
7+
8+
const data = [
9+
{ id: 1, name: 'John', skills: ['JavaScript', 'TypeScript', 'React'] },
10+
{ id: 2, name: 'Jane', skills: ['Python', 'Django'] },
11+
{ id: 3, name: 'Bob', skills: ['Java', 'Spring', 'Hibernate', 'SQL'] }
12+
]
13+
14+
const result = query(data, {
15+
filter: [
16+
{ key: 'skills', matchMode: 'arrayLength', value: 3 }
17+
]
18+
})
19+
```
20+
21+
Output:
22+
```ts twoslash
23+
[
24+
{ id: 1, name: 'John', skills: ['JavaScript', 'TypeScript', 'React'] }
25+
]
26+
```
27+
28+
This filter returns all items where the 'skills' array has exactly 3 elements.
29+
30+
You can also use comparison operators with 'arrayLength':
31+
32+
```ts twoslash
33+
import { query } from '@chronicstone/array-query'
34+
35+
const data = [
36+
{ id: 1, name: 'John', skills: ['JavaScript', 'TypeScript', 'React'] },
37+
{ id: 2, name: 'Jane', skills: ['Python', 'Django'] },
38+
{ id: 3, name: 'Bob', skills: ['Java', 'Spring', 'Hibernate', 'SQL'] }
39+
]
40+
41+
const result = query(data, {
42+
filter: [
43+
{ key: 'skills', matchMode: 'arrayLength', value: 2 }
44+
]
45+
})
46+
```
47+
48+
Output:
49+
```ts twoslash
50+
[
51+
{ id: 2, name: 'Jane', skills: ['Python', 'Django'] },
52+
]
53+
```
54+
55+
This filter returns all items where the 'skills' array has more than 2 elements.

0 commit comments

Comments
 (0)