Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

Commit 402cab3

Browse files
authored
feat: Adds $search operator. (#14)
1 parent adb5e05 commit 402cab3

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

Diff for: docs/options.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Operator | Description
5454
[$gte](https://docs.mongodb.com/manual/reference/operator/query/gt/#op._S_gt) | Includes entities where the value of a given property is greater than or equal to the specified value.
5555
[$in](https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in) | Includes entities where the value of a given property is equal to one of the specified values.
5656
[$nin](https://docs.mongodb.com/manual/reference/operator/query/ni/#op._S_ni) | Includes entities where the value of a given property is not equal to any of the specified values.
57+
$search | Includes entities where the value of a given property contains the specified value as a substring.
5758

5859
The filter below is comprehensive example using all of the operators.
5960

@@ -72,6 +73,9 @@ The filter below is comprehensive example using all of the operators.
7273
"$and": [
7374
{
7475
"stringProp1": "string value 1",
76+
"stringProp2": {
77+
"$search": "string value 2"
78+
},
7579
"numberProp2": {
7680
"$gte": 0,
7781
"$lte": 1
@@ -80,11 +84,11 @@ The filter below is comprehensive example using all of the operators.
8084
"numberProp4": { "$eq": 0 }
8185
},
8286
{
83-
"stringProp2": {
84-
"$in": ["string value 2", "string value 3"]
85-
},
8687
"stringProp3": {
87-
"$nin": ["string value 4", "string value 5"]
88+
"$in": ["string value 3", "string value 4"]
89+
},
90+
"stringProp4": {
91+
"$nin": ["string value 5", "string value 6"]
8892
}
8993
}
9094
]
@@ -93,7 +97,7 @@ The filter below is comprehensive example using all of the operators.
9397
{
9498
"$nor": [
9599
{
96-
"stringProp4": "string value 6"
100+
"stringProp5": "string value 7"
97101
}
98102
]
99103
}

Diff for: src/tests/utils/filterTest.ts

+36-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// tslint:disable:max-file-line-count
12
import 'mocha'; // tslint:disable-line:no-import-side-effect
23
import Facade from '../../Facade';
34
import Filter from '../../types/Filter';
@@ -13,8 +14,8 @@ export interface Opts {
1314
}
1415
export const firstId = 'test_id_1';
1516
export const secondId = 'test_id_2';
16-
export const firstEntity = { ...testEntity, id: firstId, stringProp: 'a', numberProp: 1 };
17-
export const secondEntity = { ...testEntity, id: secondId, stringProp: 'b', numberProp: 2 };
17+
export const firstEntity = { ...testEntity, id: firstId, stringProp: 'abc', numberProp: 1 };
18+
export const secondEntity = { ...testEntity, id: secondId, stringProp: 'def', numberProp: 2 };
1819

1920
export default (opts: Opts) => {
2021
const createTestEntities = async () => {
@@ -39,17 +40,17 @@ export default (opts: Opts) => {
3940

4041
it('should filter correctly when not using an operator', async () => {
4142
await createTestEntities();
42-
await opts.assertFirstEntityFilter({ stringProp: 'a' });
43+
await opts.assertFirstEntityFilter({ stringProp: firstEntity.stringProp });
4344
});
4445

4546
it('should filter correctly when using $eq operator', async () => {
4647
await createTestEntities();
47-
await opts.assertFirstEntityFilter({ stringProp: { $eq: 'a' } });
48+
await opts.assertFirstEntityFilter({ stringProp: { $eq: firstEntity.stringProp } });
4849
});
4950

5051
it('should filter correctly when using $ne operator', async () => {
5152
await createTestEntities();
52-
await opts.assertFirstEntityFilter({ stringProp: { $ne: 'b' } });
53+
await opts.assertFirstEntityFilter({ stringProp: { $ne: secondEntity.stringProp } });
5354
});
5455

5556
it('should filter correctly when using $gt and $lt operators', async () => {
@@ -64,26 +65,51 @@ export default (opts: Opts) => {
6465

6566
it('should filter correctly when using $in operator', async () => {
6667
await createTestEntities();
67-
await opts.assertFirstEntityFilter({ stringProp: { $in: ['a'] } });
68+
await opts.assertFirstEntityFilter({ stringProp: { $in: [firstEntity.stringProp] } });
6869
});
6970

7071
it('should filter correctly when using $and operator', async () => {
7172
await createTestEntities();
72-
await opts.assertFirstEntityFilter({ $and: [{ stringProp: 'a' }, { numberProp: 1 }] });
73+
await opts.assertFirstEntityFilter({
74+
$and: [
75+
{ stringProp: firstEntity.stringProp },
76+
{ numberProp: 1 },
77+
],
78+
});
7379
});
7480

7581
it('should filter correctly when using $or operator', async () => {
7682
await createTestEntities();
77-
await opts.assertFirstEntityFilter({ $or: [{ stringProp: 'a' }, { numberProp: 1 }] });
83+
await opts.assertFirstEntityFilter({
84+
$or: [
85+
{ stringProp: firstEntity.stringProp },
86+
{ numberProp: 1 },
87+
],
88+
});
7889
});
7990

8091
it('should filter correctly when using $nor operator', async () => {
8192
await createTestEntities();
82-
await opts.assertFirstEntityFilter({ $nor: [{ stringProp: 'b' }, { numberProp: 2 }] });
93+
await opts.assertFirstEntityFilter({
94+
$nor: [
95+
{ stringProp: secondEntity.stringProp },
96+
{ numberProp: 2 },
97+
],
98+
});
8399
});
84100

85101
it('should filter correctly when using $not operator', async () => {
86102
await createTestEntities();
87-
await opts.assertFirstEntityFilter({ stringProp: { $not: { $eq: 'b' } } });
103+
await opts.assertFirstEntityFilter({ stringProp: { $not: { $eq: secondEntity.stringProp } } });
104+
});
105+
106+
it('should filter correctly when using $search operator with lowercase', async () => {
107+
await createTestEntities();
108+
await opts.assertFirstEntityFilter({ stringProp: { $search: 'b' } });
109+
});
110+
111+
it('should filter correctly when using $search operator with uppercase', async () => {
112+
await createTestEntities();
113+
await opts.assertFirstEntityFilter({ stringProp: { $search: 'B' } });
88114
});
89115
};

Diff for: src/types/Filter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface PropFilter<Prop> {
1010
readonly $eq?: Prop;
1111
readonly $ne?: Prop;
1212
readonly $not?: PropFilter<Prop>;
13+
readonly $search?: Prop;
1314
}
1415

1516
export type EntityFilter<E extends Entity> = {

0 commit comments

Comments
 (0)