Skip to content

Commit

Permalink
Add support for object filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Mar 26, 2021
1 parent 3431881 commit d0323e1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
bower_components
dist/FakeRest.js
dist
example/ng-admin/bower-components
1 change: 0 additions & 1 deletion dist/FakeRest.js.map

This file was deleted.

2 changes: 0 additions & 2 deletions dist/FakeRest.min.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/FakeRest.min.js.map

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fakerest",
"version": "3.1.0",
"version": "3.2.0",
"repository": "https://github.com/marmelab/FakeRest",
"description": "Patch XMLHttpRequest to fake a REST server based on JSON data. ",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from 'lodash/get';
import matches from 'lodash/matches';

const every = (array, predicate) =>
array.reduce((acc, value) => acc && predicate(value), true);
Expand Down Expand Up @@ -59,6 +60,11 @@ const getSimpleFilter = (key, value) => {
return value.filter(v => v == get(item, key)).length > 0;
}
}

if (typeof value === 'object') {
return item => matches(value)(get(item, key));
}

return item => {
if (Array.isArray(get(item, key)) && typeof value == 'string') {
// simple filter but array item value: where value in item
Expand Down
10 changes: 10 additions & 0 deletions src/Collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ describe("Collection", () => {
expect(collection.getAll({ filter: { "deep.value": "b" } })).toEqual(expected);
});

it("should filter values with objects", () => {
const collection = new Collection([
{ name: "c", deep: { value: "c" } },
{ name: "a", deep: { value: "a" } },
{ name: "b", deep: { value: "b" } },
]);
const expected = [{ name: "b", deep: { value: "b" }, id: 2 }];
expect(collection.getAll({ filter: { deep: { value: "b" } } })).toEqual(expected);
});

it("should filter boolean values properly", () => {
const collection = new Collection([
{ name: "a", is: true },
Expand Down

0 comments on commit d0323e1

Please sign in to comment.