Skip to content

Commit

Permalink
Merge pull request #3214 from obsidian-tasks-group/refactor-layout-op…
Browse files Browse the repository at this point in the history
…tion-parsing

refactor: Reduce repetition of parsing of show/hide options
  • Loading branch information
claremacrae authored Dec 1, 2024
2 parents 4483c27 + 3aee643 commit 9c73402
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 67 deletions.
38 changes: 15 additions & 23 deletions src/Layout/QueryLayoutOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,21 @@ export class QueryLayoutOptions {
* @see parseTaskShowHideOptions
*/
export function parseQueryShowHideOptions(queryLayoutOptions: QueryLayoutOptions, option: string, hide: boolean) {
if (option.startsWith('tree')) {
queryLayoutOptions.hideTree = hide;
return true;
}
if (option.startsWith('task count')) {
queryLayoutOptions.hideTaskCount = hide;
return true;
}
if (option.startsWith('backlink')) {
queryLayoutOptions.hideBacklinks = hide;
return true;
}
if (option.startsWith('postpone button')) {
queryLayoutOptions.hidePostponeButton = hide;
return true;
}
if (option.startsWith('edit button')) {
queryLayoutOptions.hideEditButton = hide;
return true;
}
if (option.startsWith('urgency')) {
queryLayoutOptions.hideUrgency = hide;
return true;
const optionMap = new Map<string, keyof QueryLayoutOptions>([
// Alphabetical order
['backlink', 'hideBacklinks'],
['edit button', 'hideEditButton'],
['postpone button', 'hidePostponeButton'],
['task count', 'hideTaskCount'],
['tree', 'hideTree'],
['urgency', 'hideUrgency'],
]);

for (const [key, property] of optionMap.entries()) {
if (option.startsWith(key)) {
queryLayoutOptions[property] = hide;
return true;
}
}
return false;
}
65 changes: 22 additions & 43 deletions src/Layout/TaskLayoutOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,53 +103,32 @@ export class TaskLayoutOptions {
* @see parseQueryShowHideOptions
*/
export function parseTaskShowHideOptions(taskLayoutOptions: TaskLayoutOptions, option: string, visible: boolean) {
if (option.startsWith('priority')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.Priority, visible);
return true;
}
if (option.startsWith('cancelled date')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.CancelledDate, visible);
return true;
}
if (option.startsWith('created date')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.CreatedDate, visible);
return true;
}
if (option.startsWith('start date')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.StartDate, visible);
return true;
}
if (option.startsWith('scheduled date')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.ScheduledDate, visible);
return true;
}
if (option.startsWith('due date')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.DueDate, visible);
return true;
}
if (option.startsWith('done date')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.DoneDate, visible);
return true;
}
if (option.startsWith('recurrence rule')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.RecurrenceRule, visible);
return true;
const optionMap = new Map<string, TaskLayoutComponent>([
// Alphabetical order
['cancelled date', TaskLayoutComponent.CancelledDate],
['created date', TaskLayoutComponent.CreatedDate],
['depends on', TaskLayoutComponent.DependsOn],
['done date', TaskLayoutComponent.DoneDate],
['due date', TaskLayoutComponent.DueDate],
['id', TaskLayoutComponent.Id],
['on completion', TaskLayoutComponent.OnCompletion],
['priority', TaskLayoutComponent.Priority],
['recurrence rule', TaskLayoutComponent.RecurrenceRule],
['scheduled date', TaskLayoutComponent.ScheduledDate],
['start date', TaskLayoutComponent.StartDate],
]);

for (const [key, component] of optionMap.entries()) {
if (option.startsWith(key)) {
taskLayoutOptions.setVisibility(component, visible);
return true;
}
}

if (option.startsWith('tags')) {
taskLayoutOptions.setTagsVisibility(visible);
return true;
}
if (option.startsWith('id')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.Id, visible);
return true;
}
if (option.startsWith('depends on')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.DependsOn, visible);
return true;
}
if (option.startsWith('on completion')) {
taskLayoutOptions.setVisibility(TaskLayoutComponent.OnCompletion, visible);
return true;
}

return false;
}
29 changes: 29 additions & 0 deletions tests/Layout/QueryLayoutOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { QueryLayoutOptions, parseQueryShowHideOptions } from '../../src/Layout/QueryLayoutOptions';

describe('parsing query show/hide layout options', () => {
function parseOptionAndCheck(options: QueryLayoutOptions, option: string, hide: boolean) {
const success = parseQueryShowHideOptions(options, option, hide);
expect(success).toEqual(true);
}

const testCases: [string, keyof QueryLayoutOptions, boolean][] = [
// Alphabetical order
['backlink', 'hideBacklinks', false],
['edit button', 'hideEditButton', false],
['postpone button', 'hidePostponeButton', false],
['task count', 'hideTaskCount', false],
['tree', 'hideTree', true],
['urgency', 'hideUrgency', true],
];

it.each(testCases)('should parse "%s" option', (option, property, hiddenByDefault) => {
const options = new QueryLayoutOptions();
expect(options[property]).toBe(hiddenByDefault);

parseOptionAndCheck(options, option, !hiddenByDefault);
expect(options[property]).toEqual(!hiddenByDefault);

parseOptionAndCheck(options, option, hiddenByDefault);
expect(options[property]).toEqual(hiddenByDefault);
});
});
37 changes: 36 additions & 1 deletion tests/Layout/TaskLayoutOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TaskLayoutComponent, TaskLayoutOptions } from '../../src/Layout/TaskLayoutOptions';
import { TaskLayoutComponent, TaskLayoutOptions, parseTaskShowHideOptions } from '../../src/Layout/TaskLayoutOptions';

describe('TaskLayoutOptions', () => {
it('should be constructable', () => {
Expand Down Expand Up @@ -151,3 +151,38 @@ describe('TaskLayoutOptions', () => {
`);
});
});

describe('parsing task show/hide layout options', () => {
it.each([
// Alphabetical order
['cancelled date', TaskLayoutComponent.CancelledDate],
['created date', TaskLayoutComponent.CreatedDate],
['depends on', TaskLayoutComponent.DependsOn],
['done date', TaskLayoutComponent.DoneDate],
['due date', TaskLayoutComponent.DueDate],
['id', TaskLayoutComponent.Id],
['on completion', TaskLayoutComponent.OnCompletion],
['priority', TaskLayoutComponent.Priority],
['recurrence rule', TaskLayoutComponent.RecurrenceRule],
['scheduled date', TaskLayoutComponent.ScheduledDate],
['start date', TaskLayoutComponent.StartDate],
])('should parse option: %s', (option: string, component: TaskLayoutComponent) => {
const options = new TaskLayoutOptions();

parseTaskShowHideOptions(options, option, false);
expect(options.isShown(component)).toEqual(false);

parseTaskShowHideOptions(options, option, true);
expect(options.isShown(component)).toEqual(true);
});

it('should parse tags option', () => {
const options = new TaskLayoutOptions();

parseTaskShowHideOptions(options, 'tags', false);
expect(options.areTagsShown()).toEqual(false);

parseTaskShowHideOptions(options, 'tags', true);
expect(options.areTagsShown()).toEqual(true);
});
});

0 comments on commit 9c73402

Please sign in to comment.