Skip to content

Commit

Permalink
feat: allow multiple filter values
Browse files Browse the repository at this point in the history
  • Loading branch information
paulroub committed Nov 14, 2024
1 parent 022dbe5 commit e507ff3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
27 changes: 27 additions & 0 deletions packages/decap-cms-core/src/__tests__/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ describe('Backend', () => {
expect(result.length).toBe(1);
});

it('filters multiple values', () => {
const result = backend.filterEntries(
{
entries: [
{
data: {
testField: 'one',
},
},
{
data: {
testField: 'two',
},
},
{
data: {
testField: 'three',
},
},
],
},
Map({ field: 'testField', value: ['one', 'two'] }),
);

expect(result.length).toBe(2);
});

it('filters list values', () => {
const result = backend.filterEntries(
{
Expand Down
33 changes: 30 additions & 3 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1351,12 +1351,39 @@ export class Backend {
}

filterEntries(collection: { entries: EntryValue[] }, filterRule: FilterRule) {
let filterValues = filterRule.get('value');

if (filterValues.toJS) {

Check failure on line 1356 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

Property 'toJS' does not exist on type 'string | List<string>'.

Check failure on line 1356 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Property 'toJS' does not exist on type 'string | List<string>'.

Check failure on line 1356 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Property 'toJS' does not exist on type 'string | List<string>'.
filterValues = filterValues.toJS();

Check failure on line 1357 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

Property 'toJS' does not exist on type 'string | List<string>'.

Check failure on line 1357 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Property 'toJS' does not exist on type 'string | List<string>'.

Check failure on line 1357 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Property 'toJS' does not exist on type 'string | List<string>'.
}

if (!Array.isArray(filterValues)) {
filterValues = [filterValues];

Check failure on line 1361 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

Type '(string | List<string>)[]' is not assignable to type 'string | List<string>'.

Check failure on line 1361 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Type '(string | List<string>)[]' is not assignable to type 'string | List<string>'.

Check failure on line 1361 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Type '(string | List<string>)[]' is not assignable to type 'string | List<string>'.
}

const fieldName = filterRule.get('field');

return collection.entries.filter(entry => {
const fieldValue = entry.data[filterRule.get('field')];
const fieldValue = entry.data[fieldName];

let fieldValues = [];

if (Array.isArray(fieldValue)) {
return fieldValue.includes(filterRule.get('value'));
fieldValues = fieldValue;
} else {
fieldValues = [fieldValue];
}
return fieldValue === filterRule.get('value');

let found = false;

for (const filterValue of filterValues) {

Check failure on line 1379 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

Type 'string | List<string>' must have a '[Symbol.iterator]()' method that returns an iterator.

Check failure on line 1379 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Type 'string | List<string>' must have a '[Symbol.iterator]()' method that returns an iterator.

Check failure on line 1379 in packages/decap-cms-core/src/backend.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Type 'string | List<string>' must have a '[Symbol.iterator]()' method that returns an iterator.
if (fieldValues.includes(filterValue)) {
found = true;
break;
}
}

return found;
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/decap-cms-core/src/types/redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ export type EntryField = StaticallyTypedRecord<{
export type EntryFields = List<EntryField>;

export type FilterRule = StaticallyTypedRecord<{
value: string;
value: string | List<string>;
field: string;
}>;

Expand Down

0 comments on commit e507ff3

Please sign in to comment.