Skip to content

Commit 2261822

Browse files
authored
Merge pull request #59 from marmelab/add-operators
Add several operators
2 parents ba9e64d + 6c832c1 commit 2261822

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,36 @@ If the REST flavor you want to simulate differs from the one chosen for FakeRest
262262

263263
Note that all of the above apply only to collections. Single objects respond to `GET /bar`, `PUT /bar` and `PATCH /bar` in a manner identical to those operations for `/foo/:id`, including embedding. `POST /bar` and `DELETE /bar` are not enabled.
264264

265+
## Supported Filters
266+
267+
Operators are specified as suffixes on each filtered field. For instance, applying the `_lte` operator on the `price` field for the `books` resource is done by like this:
268+
269+
GET /books?filter={"price_lte":20} // return books where price is less than or equal to 20
270+
271+
- `_eq`: check for equality on simple values:
272+
273+
GET /books?filter={"price_eq":20} // return books where price is equal to 20
274+
275+
- `_neq`: check for inequality on simple values
276+
277+
GET /books?filter={"price_neq":20} // return books where price is not equal to 20
278+
279+
- `_eq_any`: check for equality on any passed values
280+
281+
GET /books?filter={"price_eq_any":[20, 30]} // return books where price is equal to 20 or 30
282+
283+
- `_neq_any`: check for inequality on any passed values
284+
285+
GET /books?filter={"price_neq_any":[20, 30]} // return books where price is not equal to 20 nor 30
286+
287+
- `_inc_any`: check for items that includes any of the passed values
288+
289+
GET /books?filter={"authors_inc_any":['William Gibson', 'Pat Cadigan']} // return books where authors includes either 'William Gibson' or 'Pat Cadigan' or both
290+
291+
- `_q`: check for items that contains the provided text
292+
293+
GET /books?filter={"author_q":['Gibson']} // return books where author includes 'Gibson' not considering the other fields
294+
265295
## Usage and Configuration
266296

267297
```js

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fakerest",
3-
"version": "3.4.0",
3+
"version": "3.5.0",
44
"repository": "https://github.com/marmelab/FakeRest",
55
"description": "Patch XMLHttpRequest to fake a REST server based on JSON data. ",
66
"scripts": {

src/Collection.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,46 @@ const getSimpleFilter = (key, value) => {
5454
let realKey = key.replace(/(_gt)$/, '');
5555
return item => get(item, realKey) > value;
5656
}
57+
if (key.indexOf("_neq_any") !== -1) {
58+
// not equal to any
59+
let realKey = key.replace(/(_neq_any)$/, "");
60+
let finalValue = Array.isArray(value) ? value : [value];
61+
return (item) => finalValue.every((val) => get(item, realKey) != val);
62+
}
5763
if (key.indexOf('_neq') !== -1) {
5864
// not equal
5965
let realKey = key.replace(/(_neq)$/, '');
6066
return item => get(item, realKey) != value;
6167
}
68+
if (key.indexOf("_eq_any") !== -1) {
69+
// equal any
70+
let realKey = key.replace(/(_eq_any)$/, "");
71+
let finalValue = Array.isArray(value) ? value : [value];
72+
return (item) => finalValue.some((val) => get(item, realKey) == val);
73+
}
74+
if (key.indexOf('_eq') !== -1) {
75+
// equal
76+
let realKey = key.replace(/(_eq)$/, '');
77+
return item => get(item, realKey) == value;
78+
}
79+
if (key.indexOf('_inc_any') !== -1) {
80+
// include any
81+
let realKey = key.replace(/(_inc_any)$/, "");
82+
let finalValue = Array.isArray(value) ? value : [value];
83+
return item => finalValue.some(val => get(item, realKey).includes(val));
84+
}
85+
if (key.indexOf('_inc') !== -1) {
86+
// includes all
87+
let realKey = key.replace(/(_inc)$/, "");
88+
let finalValue = Array.isArray(value) ? value : [value];
89+
return item => finalValue.every(val => get(item, realKey).includes(val));
90+
}
91+
if (key.indexOf('_ninc_any') !== -1) {
92+
// does not include any
93+
let realKey = key.replace(/(_ninc_any)$/, "");
94+
let finalValue = Array.isArray(value) ? value : [value];
95+
return item => finalValue.every(val => !get(item, realKey).includes(val));
96+
}
6297
if (Array.isArray(value)) {
6398
return item => {
6499
if (Array.isArray(get(item, key))) {

src/Collection.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,54 @@ describe("Collection", () => {
375375
]);
376376
});
377377

378+
it("should filter by equality using _eq", () => {
379+
const collection = new Collection([{ v: 1 }, { v: 2 }, { v: 3 }]);
380+
expect(collection.getAll({ filter: { v_eq: 2 } })).toEqual([
381+
{ v: 2, id: 1 },
382+
]);
383+
});
384+
385+
it("should filter using _eq_any", () => {
386+
const collection = new Collection([{ v: 1 }, { v: 2 }, { v: 3 }]);
387+
expect(collection.getAll({ filter: { v_eq_any: [1, 3] } })).toEqual([
388+
{ v: 1, id: 0 },
389+
{ v: 3, id: 2 },
390+
]);
391+
});
392+
393+
it("should filter using _neq_any", () => {
394+
const collection = new Collection([{ v: 1 }, { v: 2 }, { v: 3 }]);
395+
expect(collection.getAll({ filter: { v_neq_any: [1, 3] } })).toEqual([
396+
{ v: 2, id: 1 },
397+
]);
398+
});
399+
400+
it("should filter using _inc_any", () => {
401+
const collection = new Collection([{ v: [1, 2] }, { v: [2, 4] }, { v: [3, 1] }]);
402+
expect(collection.getAll({ filter: { v_inc_any: [1, 3] } })).toEqual([
403+
{ v: [1, 2], id: 0 },
404+
{ v: [3, 1], id: 2 },
405+
]);
406+
});
407+
408+
it("should filter using _ninc_any", () => {
409+
const collection = new Collection([{ v: [1, 2] }, { v: [2, 4] }, { v: [3, 1] }]);
410+
expect(collection.getAll({ filter: { v_ninc_any: [1, 3] } })).toEqual([
411+
{ v: [2, 4], id: 1 },
412+
]);
413+
});
414+
415+
it("should filter using _inc", () => {
416+
const collection = new Collection([
417+
{ v: [1, 2] },
418+
{ v: [2, 4] },
419+
{ v: [3, 1] },
420+
]);
421+
expect(collection.getAll({ filter: { v_inc: [1, 3] } })).toEqual([
422+
{ v: [3, 1], id: 2 },
423+
]);
424+
});
425+
378426
it("should filter by text search using _q", () => {
379427
const collection = new Collection([{ v: 'abCd' }, { v: 'cDef' }, { v: 'EFgh' }]);
380428
expect(collection.getAll({ filter: { v_q: 'cd' } })).toEqual([

0 commit comments

Comments
 (0)