Skip to content

Commit

Permalink
Merge pull request #37 from marmelab/better-filters
Browse files Browse the repository at this point in the history
add _neq filter
  • Loading branch information
fzaninotto committed Mar 29, 2021
2 parents 91290e9 + 94aedbd commit 708d4f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ FakeRest uses a standard REST flavor, described below.
{ "id": 3, "author_id": 1, "title": "Sense and Sensibility" }
]

// use _gt, _gte, _lte, or _lt suffix on filter names to make range queries
// use _gt, _gte, _lte, _lt, or _neq suffix on filter names to make range queries
GET /books?filter={"price_lte":20} // return books where price is less than or equal to 20
GET /books?filter={"price_gt":20} // return books where price is greater than 20

Expand Down
5 changes: 5 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const getSimpleFilter = (key, value) => {
let realKey = key.replace(/(_gt)$/, '');
return item => get(item, realKey) > value;
}
if (key.indexOf('_neq') !== -1) {
// not equal
let realKey = key.replace(/(_neq)$/, '');
return item => get(item, realKey) != value;
}
if (Array.isArray(value)) {
return item => {
if (Array.isArray(get(item, key))) {
Expand Down
8 changes: 8 additions & 0 deletions src/Collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@ describe("Collection", () => {
expect(collection.getAll({ filter: { v_lte: 0 } })).toEqual([]);
});

it("should filter by inequality using _neq", () => {
const collection = new Collection([{ v: 1 }, { v: 2 }, { v: 3 }]);
expect(collection.getAll({ filter: { v_neq: 2 } })).toEqual([
{ v: 1, id: 0 },
{ v: 3, id: 2 },
]);
});

it("should filter by array", () => {
const collection = new Collection([
{ a: "H" },
Expand Down

0 comments on commit 708d4f3

Please sign in to comment.