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 15, 2024
1 parent 022dbe5 commit ed53741
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .stylelintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
{ "ignoreKeywords": ["dummyValue"], "camelCaseSvgKeywords": true }
]
},
"ignoreFiles": ["packages/decap-cms-lib-auth/index.d.ts"]
"ignoreFiles": [
"packages/decap-cms-lib-auth/index.d.ts",
"packages/decap-cms-core/src/backend.ts"
]
}
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
32 changes: 27 additions & 5 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
I18N_STRUCTURE,
} from './lib/i18n';

import type { StaticallyTypedRecord } from './types/immutable';
import type { I18nInfo } from './lib/i18n';
import type AssetProxy from './valueObjects/AssetProxy';
import type {
Expand Down Expand Up @@ -1351,14 +1352,35 @@ export class Backend {
}

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

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

return collection.entries.filter(entry => {
const fieldValue = entry.data[filterRule.get('field')];
if (Array.isArray(fieldValue)) {
return fieldValue.includes(filterRule.get('value'));
}
return fieldValue === filterRule.get('value');
const fieldValues = this.valuesAsArray(entry.data[fieldName]);

return filterValues.some(filterValue => fieldValues.includes(filterValue));
});
}

// Values can be a string, a list of strings, or statically-typed-record lists of strings
// depending on context (unit test vs. actual config, single vs. multiple values, etc.)
//
// We normalize to a simple list for easier processing above.
//
valuesAsArray(rawValue: string | List<string> | StaticallyTypedRecord<List<string>>): string[] {
let values: string[] = [];

if ((<StaticallyTypedRecord<List<string>>>rawValue).toJS) {
values = (<StaticallyTypedRecord<List<string>>>rawValue).toJS().toArray();
} else if (Array.isArray(rawValue)) {
values = <string[]>rawValue;
} else {
values = [<string>rawValue];
}

return values;
}
}

export function resolveBackend(config: CmsConfig) {
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> | StaticallyTypedRecord<List<string>>;
field: string;
}>;

Expand Down

0 comments on commit ed53741

Please sign in to comment.