Skip to content

Commit 9c73402

Browse files
authored
Merge pull request #3214 from obsidian-tasks-group/refactor-layout-option-parsing
refactor: Reduce repetition of parsing of show/hide options
2 parents 4483c27 + 3aee643 commit 9c73402

File tree

4 files changed

+102
-67
lines changed

4 files changed

+102
-67
lines changed

src/Layout/QueryLayoutOptions.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,21 @@ export class QueryLayoutOptions {
2323
* @see parseTaskShowHideOptions
2424
*/
2525
export function parseQueryShowHideOptions(queryLayoutOptions: QueryLayoutOptions, option: string, hide: boolean) {
26-
if (option.startsWith('tree')) {
27-
queryLayoutOptions.hideTree = hide;
28-
return true;
29-
}
30-
if (option.startsWith('task count')) {
31-
queryLayoutOptions.hideTaskCount = hide;
32-
return true;
33-
}
34-
if (option.startsWith('backlink')) {
35-
queryLayoutOptions.hideBacklinks = hide;
36-
return true;
37-
}
38-
if (option.startsWith('postpone button')) {
39-
queryLayoutOptions.hidePostponeButton = hide;
40-
return true;
41-
}
42-
if (option.startsWith('edit button')) {
43-
queryLayoutOptions.hideEditButton = hide;
44-
return true;
45-
}
46-
if (option.startsWith('urgency')) {
47-
queryLayoutOptions.hideUrgency = hide;
48-
return true;
26+
const optionMap = new Map<string, keyof QueryLayoutOptions>([
27+
// Alphabetical order
28+
['backlink', 'hideBacklinks'],
29+
['edit button', 'hideEditButton'],
30+
['postpone button', 'hidePostponeButton'],
31+
['task count', 'hideTaskCount'],
32+
['tree', 'hideTree'],
33+
['urgency', 'hideUrgency'],
34+
]);
35+
36+
for (const [key, property] of optionMap.entries()) {
37+
if (option.startsWith(key)) {
38+
queryLayoutOptions[property] = hide;
39+
return true;
40+
}
4941
}
5042
return false;
5143
}

src/Layout/TaskLayoutOptions.ts

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -103,53 +103,32 @@ export class TaskLayoutOptions {
103103
* @see parseQueryShowHideOptions
104104
*/
105105
export function parseTaskShowHideOptions(taskLayoutOptions: TaskLayoutOptions, option: string, visible: boolean) {
106-
if (option.startsWith('priority')) {
107-
taskLayoutOptions.setVisibility(TaskLayoutComponent.Priority, visible);
108-
return true;
109-
}
110-
if (option.startsWith('cancelled date')) {
111-
taskLayoutOptions.setVisibility(TaskLayoutComponent.CancelledDate, visible);
112-
return true;
113-
}
114-
if (option.startsWith('created date')) {
115-
taskLayoutOptions.setVisibility(TaskLayoutComponent.CreatedDate, visible);
116-
return true;
117-
}
118-
if (option.startsWith('start date')) {
119-
taskLayoutOptions.setVisibility(TaskLayoutComponent.StartDate, visible);
120-
return true;
121-
}
122-
if (option.startsWith('scheduled date')) {
123-
taskLayoutOptions.setVisibility(TaskLayoutComponent.ScheduledDate, visible);
124-
return true;
125-
}
126-
if (option.startsWith('due date')) {
127-
taskLayoutOptions.setVisibility(TaskLayoutComponent.DueDate, visible);
128-
return true;
129-
}
130-
if (option.startsWith('done date')) {
131-
taskLayoutOptions.setVisibility(TaskLayoutComponent.DoneDate, visible);
132-
return true;
133-
}
134-
if (option.startsWith('recurrence rule')) {
135-
taskLayoutOptions.setVisibility(TaskLayoutComponent.RecurrenceRule, visible);
136-
return true;
106+
const optionMap = new Map<string, TaskLayoutComponent>([
107+
// Alphabetical order
108+
['cancelled date', TaskLayoutComponent.CancelledDate],
109+
['created date', TaskLayoutComponent.CreatedDate],
110+
['depends on', TaskLayoutComponent.DependsOn],
111+
['done date', TaskLayoutComponent.DoneDate],
112+
['due date', TaskLayoutComponent.DueDate],
113+
['id', TaskLayoutComponent.Id],
114+
['on completion', TaskLayoutComponent.OnCompletion],
115+
['priority', TaskLayoutComponent.Priority],
116+
['recurrence rule', TaskLayoutComponent.RecurrenceRule],
117+
['scheduled date', TaskLayoutComponent.ScheduledDate],
118+
['start date', TaskLayoutComponent.StartDate],
119+
]);
120+
121+
for (const [key, component] of optionMap.entries()) {
122+
if (option.startsWith(key)) {
123+
taskLayoutOptions.setVisibility(component, visible);
124+
return true;
125+
}
137126
}
127+
138128
if (option.startsWith('tags')) {
139129
taskLayoutOptions.setTagsVisibility(visible);
140130
return true;
141131
}
142-
if (option.startsWith('id')) {
143-
taskLayoutOptions.setVisibility(TaskLayoutComponent.Id, visible);
144-
return true;
145-
}
146-
if (option.startsWith('depends on')) {
147-
taskLayoutOptions.setVisibility(TaskLayoutComponent.DependsOn, visible);
148-
return true;
149-
}
150-
if (option.startsWith('on completion')) {
151-
taskLayoutOptions.setVisibility(TaskLayoutComponent.OnCompletion, visible);
152-
return true;
153-
}
132+
154133
return false;
155134
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { QueryLayoutOptions, parseQueryShowHideOptions } from '../../src/Layout/QueryLayoutOptions';
2+
3+
describe('parsing query show/hide layout options', () => {
4+
function parseOptionAndCheck(options: QueryLayoutOptions, option: string, hide: boolean) {
5+
const success = parseQueryShowHideOptions(options, option, hide);
6+
expect(success).toEqual(true);
7+
}
8+
9+
const testCases: [string, keyof QueryLayoutOptions, boolean][] = [
10+
// Alphabetical order
11+
['backlink', 'hideBacklinks', false],
12+
['edit button', 'hideEditButton', false],
13+
['postpone button', 'hidePostponeButton', false],
14+
['task count', 'hideTaskCount', false],
15+
['tree', 'hideTree', true],
16+
['urgency', 'hideUrgency', true],
17+
];
18+
19+
it.each(testCases)('should parse "%s" option', (option, property, hiddenByDefault) => {
20+
const options = new QueryLayoutOptions();
21+
expect(options[property]).toBe(hiddenByDefault);
22+
23+
parseOptionAndCheck(options, option, !hiddenByDefault);
24+
expect(options[property]).toEqual(!hiddenByDefault);
25+
26+
parseOptionAndCheck(options, option, hiddenByDefault);
27+
expect(options[property]).toEqual(hiddenByDefault);
28+
});
29+
});

tests/Layout/TaskLayoutOptions.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TaskLayoutComponent, TaskLayoutOptions } from '../../src/Layout/TaskLayoutOptions';
1+
import { TaskLayoutComponent, TaskLayoutOptions, parseTaskShowHideOptions } from '../../src/Layout/TaskLayoutOptions';
22

33
describe('TaskLayoutOptions', () => {
44
it('should be constructable', () => {
@@ -151,3 +151,38 @@ describe('TaskLayoutOptions', () => {
151151
`);
152152
});
153153
});
154+
155+
describe('parsing task show/hide layout options', () => {
156+
it.each([
157+
// Alphabetical order
158+
['cancelled date', TaskLayoutComponent.CancelledDate],
159+
['created date', TaskLayoutComponent.CreatedDate],
160+
['depends on', TaskLayoutComponent.DependsOn],
161+
['done date', TaskLayoutComponent.DoneDate],
162+
['due date', TaskLayoutComponent.DueDate],
163+
['id', TaskLayoutComponent.Id],
164+
['on completion', TaskLayoutComponent.OnCompletion],
165+
['priority', TaskLayoutComponent.Priority],
166+
['recurrence rule', TaskLayoutComponent.RecurrenceRule],
167+
['scheduled date', TaskLayoutComponent.ScheduledDate],
168+
['start date', TaskLayoutComponent.StartDate],
169+
])('should parse option: %s', (option: string, component: TaskLayoutComponent) => {
170+
const options = new TaskLayoutOptions();
171+
172+
parseTaskShowHideOptions(options, option, false);
173+
expect(options.isShown(component)).toEqual(false);
174+
175+
parseTaskShowHideOptions(options, option, true);
176+
expect(options.isShown(component)).toEqual(true);
177+
});
178+
179+
it('should parse tags option', () => {
180+
const options = new TaskLayoutOptions();
181+
182+
parseTaskShowHideOptions(options, 'tags', false);
183+
expect(options.areTagsShown()).toEqual(false);
184+
185+
parseTaskShowHideOptions(options, 'tags', true);
186+
expect(options.areTagsShown()).toEqual(true);
187+
});
188+
});

0 commit comments

Comments
 (0)