Skip to content

Commit 8a4bdf3

Browse files
authored
Merge pull request #1977 from obsidian-tasks-group/simplify-sorting-code
refactor: Simplify sorting code in Field.ts
2 parents 8d3b397 + d665b65 commit 8a4bdf3

File tree

5 files changed

+28
-77
lines changed

5 files changed

+28
-77
lines changed

src/Query/Filter/Field.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -131,41 +131,6 @@ export abstract class Field {
131131
return false;
132132
}
133133

134-
/**
135-
* Parse a 'sort by' line and return a {@link Sorter} object.
136-
*
137-
* Returns null line does not match this field or is invalid,
138-
* or this field does not support sorting.
139-
*/
140-
public parseSortLine(line: string): Sorter | null {
141-
if (!this.supportsSorting()) {
142-
return null;
143-
}
144-
145-
if (!this.canCreateSorterForLine(line)) {
146-
return null;
147-
}
148-
149-
return this.createSorterFromLine(line);
150-
}
151-
152-
/**
153-
* Returns true if the class can parse the given 'sort by' instruction line.
154-
*
155-
* Current implementation simply checks whether the class does support sorting,
156-
* and whether the line matches this.sorterRegExp().
157-
* @param line - A line from a ```tasks``` block.
158-
*
159-
* @see {@link createSorterFromLine}
160-
*/
161-
public canCreateSorterForLine(line: string): boolean {
162-
if (!this.supportsSorting()) {
163-
return false;
164-
}
165-
166-
return Field.lineMatchesFilter(this.sorterRegExp(), line);
167-
}
168-
169134
/**
170135
* Parse the line, and return either a {@link Sorter} object or null.
171136
*
@@ -176,8 +141,6 @@ export abstract class Field {
176141
* this method.
177142
*
178143
* @param line - A 'sort by' line from a ```tasks``` block.
179-
*
180-
* @see {@link canCreateSorterForLine}
181144
*/
182145
public createSorterFromLine(line: string): Sorter | null {
183146
if (!this.supportsSorting()) {

src/Query/FilterParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function parseSorter(sorterString: string): Sorter | null {
8181
// See if any of the fields can parse the line.
8282
for (const creator of fieldCreators) {
8383
const field = creator();
84-
const sorter = field.parseSortLine(sorterString);
84+
const sorter = field.createSorterFromLine(sorterString);
8585
if (sorter) {
8686
return sorter;
8787
}

tests/Query/Filter/Field.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ describe('sorting - base class usability and implementation', () => {
6262
});
6363

6464
it('should fail to parse a "valid" line', () => {
65-
// expect(unsupported.parseSortLine('sort by unsupported')).toThrow(Error);
6665
const line = 'sort by unsupported';
67-
expect(unsupported.canCreateSorterForLine(line)).toBe(false);
68-
expect(unsupported.createSorterFromLine(line)).toBeNull();
69-
const sorting = unsupported.parseSortLine(line);
66+
const sorting = unsupported.createSorterFromLine(line);
7067
expect(sorting).toBeNull();
7168
});
7269
});
@@ -84,23 +81,19 @@ describe('sorting - base class usability and implementation', () => {
8481

8582
it('should parse a valid line', () => {
8683
const line = 'sort by description-length';
87-
expect(supported.canCreateSorterForLine(line)).toBe(true);
88-
expect(supported.createSorterFromLine(line)).not.toBeNull();
89-
const sorting = supported.parseSortLine(line);
84+
const sorting = supported.createSorterFromLine(line);
9085
expect(sorting).not.toBeNull();
9186
expect(sorting?.property).toEqual('description-length');
9287
});
9388

9489
it('should fail to parse a invalid line', () => {
9590
const line = 'sort by jsdajhasdfa';
96-
expect(supported.canCreateSorterForLine(line)).toBe(false);
97-
expect(supported.createSorterFromLine(line)).toBeNull();
98-
const sorting = supported.parseSortLine(line);
91+
const sorting = supported.createSorterFromLine(line);
9992
expect(sorting).toBeNull();
10093
});
10194

10295
it('should compare two tasks', () => {
103-
const sorting = supported.parseSortLine('sort by description-length');
96+
const sorting = supported.createSorterFromLine('sort by description-length');
10497
const a = new TaskBuilder().description('short description').build();
10598
const b = new TaskBuilder().description('very looooooooong description').build();
10699
expectTaskComparesBefore(sorting!, a, b);

tests/Query/Filter/RecurringField.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('sorting by recurring', () => {
6060

6161
it('parses sort by recurrence', () => {
6262
const field = new RecurringField();
63-
expect(field.parseSortLine('sort by recurring')).not.toBeNull();
63+
expect(field.createSorterFromLine('sort by recurring')).not.toBeNull();
6464
});
6565

6666
it('sort by due', () => {

tests/Query/Filter/TagsField.test.ts

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,6 @@ describe('Sort by tags', () => {
345345
expect(tagsField.supportsSorting()).toEqual(true);
346346
});
347347

348-
it('should report whether it can parse lines', () => {
349-
// Valid sort by tag lines:
350-
expect(tagsField.canCreateSorterForLine('sort by tag')).toBe(true);
351-
expect(tagsField.canCreateSorterForLine('sort by tag 2')).toBe(true);
352-
expect(tagsField.canCreateSorterForLine('sort by tag reverse')).toBe(true);
353-
expect(tagsField.canCreateSorterForLine('sort by tag reverse 3')).toBe(true);
354-
355-
// Invalid lines:
356-
expect(tagsField.canCreateSorterForLine('sort by description')).toBe(false);
357-
});
358-
359348
const tag_a = new TaskBuilder().tags(['#a']).build();
360349
const tag_b = new TaskBuilder().tags(['#b']).build();
361350
const tags_a_b = new TaskBuilder().tags(['#a', '#b']).build();
@@ -373,20 +362,20 @@ describe('Sort by tags', () => {
373362
});
374363

375364
it('should parse a valid line with default tag number', () => {
376-
const sorter = tagsField.parseSortLine('sort by tag');
365+
const sorter = tagsField.createSorterFromLine('sort by tag');
377366
expect(sorter?.property).toEqual('tag');
378367
expect(sorter?.comparator(tag_a, tag_b)).toBeLessThan(0);
379368
expectTaskComparesBefore(sorter!, tag_a, tag_b);
380369
});
381370

382371
it('should parse a valid line with a non-default tag number', () => {
383-
const sorter = tagsField.parseSortLine('sort by tag 2');
372+
const sorter = tagsField.createSorterFromLine('sort by tag 2');
384373
expect(sorter?.property).toEqual('tag');
385374
expectTaskComparesBefore(sorter!, tags_a_b, tags_a_c);
386375
});
387376

388377
it('should parse a valid line with reverse and a non-default tag number', () => {
389-
const sorter = tagsField.parseSortLine('sort by tag reverse 2');
378+
const sorter = tagsField.createSorterFromLine('sort by tag reverse 2');
390379
expect(sorter?.property).toEqual('tag');
391380
expectTaskComparesAfter(sorter!, tags_a_b, tags_a_c);
392381
});
@@ -406,9 +395,7 @@ describe('Sort by tags', () => {
406395

407396
it('should fail to parse a invalid line', () => {
408397
const line = 'sort by jsdajhasdfa';
409-
expect(tagsField.canCreateSorterForLine(line)).toBe(false);
410-
expect(tagsField.createSorterFromLine(line)).toBeNull();
411-
const sorting = tagsField.parseSortLine(line);
398+
const sorting = tagsField.createSorterFromLine(line);
412399
expect(sorting).toBeNull();
413400
});
414401
});
@@ -429,7 +416,10 @@ describe('Sort by tags', () => {
429416

430417
// Act / Assert
431418
expect(
432-
Sort.by([new TagsField().parseSortLine('sort by tag 1')!], [t1, t3, t5, t7, t6, t4, t2, t8, t9, t10]),
419+
Sort.by(
420+
[new TagsField().createSorterFromLine('sort by tag 1')!],
421+
[t1, t3, t5, t7, t6, t4, t2, t8, t9, t10],
422+
),
433423
).toEqual(expectedOrder);
434424
});
435425

@@ -450,7 +440,7 @@ describe('Sort by tags', () => {
450440
// Act / Assert
451441
expect(
452442
Sort.by(
453-
[new TagsField().parseSortLine('sort by tag reverse 1')!],
443+
[new TagsField().createSorterFromLine('sort by tag reverse 1')!],
454444
[t1, t3, t5, t7, t6, t4, t2, t8, t9, t10],
455445
),
456446
).toEqual(expectedOrder);
@@ -463,7 +453,9 @@ describe('Sort by tags', () => {
463453
const t4 = fromLine({ line: '- [ ] a #iii #ddd' });
464454
const t5 = fromLine({ line: '- [ ] a #hhh #eee' });
465455
const expectedOrder = [t1, t2, t3, t4, t5];
466-
expect(Sort.by([new TagsField().parseSortLine('sort by tag 2')!], [t4, t3, t2, t1, t5])).toEqual(expectedOrder);
456+
expect(Sort.by([new TagsField().createSorterFromLine('sort by tag 2')!], [t4, t3, t2, t1, t5])).toEqual(
457+
expectedOrder,
458+
);
467459
});
468460

469461
it('should sort correctly reversed by second tag with no global filter', () => {
@@ -473,7 +465,7 @@ describe('Sort by tags', () => {
473465
const t4 = fromLine({ line: '- [ ] a #iii #ddd' });
474466
const t5 = fromLine({ line: '- [ ] a #hhh #eee' });
475467
const expectedOrder = [t5, t4, t3, t2, t1];
476-
expect(Sort.by([new TagsField().parseSortLine('sort by tag reverse 2')!], [t4, t3, t2, t1, t5])).toEqual(
468+
expect(Sort.by([new TagsField().createSorterFromLine('sort by tag reverse 2')!], [t4, t3, t2, t1, t5])).toEqual(
477469
expectedOrder,
478470
);
479471
});
@@ -501,7 +493,7 @@ describe('Sort by tags', () => {
501493
// Act
502494
expect(
503495
Sort.by(
504-
[new TagsField().parseSortLine('sort by tag 1')!],
496+
[new TagsField().createSorterFromLine('sort by tag 1')!],
505497
[t1, t12, t3, t13, t5, t7, t6, t4, t2, t8, t9, t10, t11],
506498
),
507499
).toEqual(expectedOrder);
@@ -533,7 +525,7 @@ describe('Sort by tags', () => {
533525
// Act
534526
expect(
535527
Sort.by(
536-
[new TagsField().parseSortLine('sort by tag reverse 1')!],
528+
[new TagsField().createSorterFromLine('sort by tag reverse 1')!],
537529
[t1, t12, t3, t13, t5, t7, t6, t4, t2, t8, t9, t10, t11],
538530
),
539531
).toEqual(expectedOrder);
@@ -557,7 +549,10 @@ describe('Sort by tags', () => {
557549
const expectedOrder = [t1, t2, t3, t4, t5, t6, t7, t8];
558550

559551
// Act
560-
const result = Sort.by([new TagsField().parseSortLine('sort by tag 2')!], [t4, t7, t5, t2, t3, t1, t8, t6]);
552+
const result = Sort.by(
553+
[new TagsField().createSorterFromLine('sort by tag 2')!],
554+
[t4, t7, t5, t2, t3, t1, t8, t6],
555+
);
561556

562557
// Assert
563558
expect(result).toEqual(expectedOrder);
@@ -582,7 +577,7 @@ describe('Sort by tags', () => {
582577

583578
// Act
584579
const result = Sort.by(
585-
[new TagsField().parseSortLine('sort by tag reverse 2')!],
580+
[new TagsField().createSorterFromLine('sort by tag reverse 2')!],
586581
[t4, t7, t5, t2, t3, t1, t8, t6],
587582
);
588583

@@ -620,8 +615,8 @@ describe('Sort by tags', () => {
620615
expect(
621616
Sort.by(
622617
[
623-
new TagsField().parseSortLine('sort by tag 2')!, // tag 2 - ascending
624-
new TagsField().parseSortLine('sort by tag 1')!, // tag 1 - ascending
618+
new TagsField().createSorterFromLine('sort by tag 2')!, // tag 2 - ascending
619+
new TagsField().createSorterFromLine('sort by tag 1')!, // tag 1 - ascending
625620
new DescriptionField().createNormalSorter(), // then description - ascending
626621
],
627622
input,

0 commit comments

Comments
 (0)