Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better matching for lists and objects #895

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
View Filters config structure changed
View Groups config structure changed
`theme` props removed (use new `useTheme` hook instead)
date string template filter now use date-fns instead of momentjs

- View Filters config structure changed
- View Groups config structure changed
- `theme` props removed (use new `useTheme` hook instead)
- date string template filter now use date-fns instead of momentjs
- filter rules behavior changed for objects and lists

# Typescript
`StringOrTextField` has been split into `StringField` and `TextField`.
- `StringOrTextField` has been split into `StringField` and `TextField`.
4 changes: 2 additions & 2 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export interface BaseFieldFilterRule {
}

export interface FieldPatternFilterRule extends BaseFieldFilterRule {
pattern: string;
pattern: string | RegExp;
}

export interface FieldValueFilterRule extends BaseFieldFilterRule {
Expand All @@ -194,7 +194,7 @@ export interface FieldValueFilterRule extends BaseFieldFilterRule {
export type FieldFilterRule = FieldPatternFilterRule | FieldValueFilterRule;

export interface FileNameFilterRule {
pattern: string;
pattern: string | RegExp;
}

export type FilterRule = FieldFilterRule | FileNameFilterRule;
Expand Down
265 changes: 263 additions & 2 deletions packages/core/src/lib/util/__tests__/field.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('filterEntries', () => {
expect(isHidden(mockBodyField, mockExternalEntry, undefined)).toBeTruthy();
});

it('should show field if single condition is met', () => {
it('should show field if multiple conditions are met', () => {
expect(isHidden(mockBodyField, mockHasSummaryEntry, undefined)).toBeFalsy();
expect(isHidden(mockBodyField, mockInternalEntry, undefined)).toBeFalsy();
});
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('filterEntries', () => {
expect(isHidden(mockBodyField, mockInsideListEntry, 'list.0')).toBeTruthy();
});

it('should show field if single condition is met', () => {
it('should show field if multiple conditions are met', () => {
expect(isHidden(mockBodyField, mockInsideListEntry, 'list.2')).toBeFalsy();
expect(isHidden(mockBodyField, mockInsideListEntry, 'list.1')).toBeFalsy();
});
Expand All @@ -152,5 +152,266 @@ describe('filterEntries', () => {
expect(isHidden(mockBodyField, undefined, 'list.0')).toBeFalsy();
});
});

describe('searching list items', () => {
const mockInsideListEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
list: [
{
title: 'I am a title',
type: 'external',
url: 'http://example.com',
hasSummary: false,
},
{
title: 'I am a title',
type: 'internal',
body: 'I am the body of your post',
hasSummary: false,
},
{
title: 'I am a title',
type: 'external',
url: 'http://example.com',
body: 'I am the body of your post',
hasSummary: true,
},
],
},
});

const mockPatternUrlField: StringField = {
label: 'URL',
name: 'url',
widget: 'string',
i18n: I18N_FIELD_NONE,
condition: {
field: 'list.*',
pattern: /external/,
},
};

const mockPatternUrlMultipleConditionsField: StringField = {
label: 'URL',
name: 'url',
widget: 'string',
i18n: I18N_FIELD_NONE,
condition: [
{
field: 'list.*',
pattern: /external/,
},
{
field: 'list.*',
pattern: /true/,
},
],
};

it('should show field by default', () => {
expect(isHidden(mockTitleField, mockInsideListEntry, undefined)).toBeFalsy();
});

it('should hide field if single condition is not met', () => {
expect(isHidden(mockUrlField, mockInsideListEntry, undefined)).toBeTruthy();
});

it('should show field if single condition is met', () => {
expect(isHidden(mockPatternUrlField, mockInsideListEntry, undefined)).toBeFalsy();
});

it('should hide field if all multiple conditions are not met', () => {
expect(isHidden(mockBodyField, mockInsideListEntry, undefined)).toBeTruthy();
});

it('should show field if multiple conditions are met', () => {
expect(
isHidden(mockPatternUrlMultipleConditionsField, mockInsideListEntry, undefined),
).toBeFalsy();
});

it('should show field if entry is undefined', () => {
expect(isHidden(mockTitleField, undefined, undefined)).toBeFalsy();
expect(isHidden(mockUrlField, undefined, undefined)).toBeFalsy();
expect(isHidden(mockBodyField, undefined, undefined)).toBeFalsy();
});
});

describe('matching list', () => {
const mockDependsOnEmptyListField: StringField = {
label: 'Title',
name: 'title',
widget: 'string',
condition: [
{
field: 'list',
pattern: /^(\[\])?$/,
},
],
};

const mockDependsOnListWithValueField: StringField = {
label: 'Title',
name: 'title',
widget: 'string',
condition: [
{
field: 'list',
pattern: /"something":"yes"/,
},
],
};

const mockEmptyListEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
default: 'http://example.com',
list: [],
},
});

const mockUndefinedListEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
default: 'http://example.com',
list: undefined,
},
});

const mockListWithValueEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
default: 'http://example.com',
list: [
{
something: 'yes',
},
],
},
});

it('should match empty list', () => {
expect(isHidden(mockDependsOnEmptyListField, mockEmptyListEntry, undefined)).toBeFalsy();
});

it('should match undefined list', () => {
expect(
isHidden(mockDependsOnEmptyListField, mockUndefinedListEntry, undefined),
).toBeFalsy();
});

it('should not match list with value', () => {
expect(
isHidden(mockDependsOnEmptyListField, mockListWithValueEntry, undefined),
).toBeTruthy();
});

it('should not match empty list', () => {
expect(
isHidden(mockDependsOnListWithValueField, mockEmptyListEntry, undefined),
).toBeTruthy();
});

it('should not match undefined list', () => {
expect(
isHidden(mockDependsOnListWithValueField, mockUndefinedListEntry, undefined),
).toBeTruthy();
});

it('should match list with specific value', () => {
expect(
isHidden(mockDependsOnListWithValueField, mockListWithValueEntry, undefined),
).toBeFalsy();
});
});

describe('matching object', () => {
const mockDependsOnEmptyObjectField: StringField = {
label: 'Title',
name: 'title',
widget: 'string',
condition: [
{
field: 'object',
pattern: /^({})?$/,
},
],
};

const mockDependsOnObjectWithValueField: StringField = {
label: 'Title',
name: 'title',
widget: 'string',
condition: [
{
field: 'object',
pattern: /"something":"yes"/,
},
],
};

const mockEmptyObjectEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
default: 'http://example.com',
object: {},
},
});

const mockUndefinedObjectEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
default: 'http://example.com',
object: undefined,
},
});

const mockObjectWithValueEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
default: 'http://example.com',
object: {
something: 'yes',
},
},
});

it('should match empty object', () => {
expect(
isHidden(mockDependsOnEmptyObjectField, mockEmptyObjectEntry, undefined),
).toBeFalsy();
});

it('should match undefined object', () => {
expect(
isHidden(mockDependsOnEmptyObjectField, mockUndefinedObjectEntry, undefined),
).toBeFalsy();
});

it('should not match object with value', () => {
expect(
isHidden(mockDependsOnEmptyObjectField, mockObjectWithValueEntry, undefined),
).toBeTruthy();
});

it('should not match empty object', () => {
expect(
isHidden(mockDependsOnObjectWithValueField, mockEmptyObjectEntry, undefined),
).toBeTruthy();
});

it('should not match undefined object', () => {
expect(
isHidden(mockDependsOnObjectWithValueField, mockUndefinedObjectEntry, undefined),
).toBeTruthy();
});

it('should match object with specific value', () => {
expect(
isHidden(mockDependsOnObjectWithValueField, mockObjectWithValueEntry, undefined),
).toBeFalsy();
});
});
});
});
Loading