Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
]
}
44 changes: 44 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 All @@ -116,6 +143,23 @@ describe('Backend', () => {

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

it('behaves when field values are absent', () => {
const result = backend.filterEntries(
{
entries: [
{
data: {
otherField: 'testValue',
},
},
],
},
Map({ field: 'testField', value: 'testValue' }),
);

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

describe('getLocalDraftBackup', () => {
Expand Down
36 changes: 31 additions & 5 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 @@ -1359,14 +1360,39 @@ 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.
//
// If the value is null/undefined, return an empty array.
//
valuesAsArray(rawValue: string | List<string> | StaticallyTypedRecord<List<string>>): string[] {
let values: string[] = [];

if (rawValue === null || rawValue === undefined) {
values = [];
} else 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 @@ -575,7 +575,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
Loading