Skip to content

refactor: move default comparator to Grouper #2018

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

Closed
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
16 changes: 15 additions & 1 deletion src/Query/Filter/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Comparator } from '../Sorter';
import * as RegExpTools from '../../lib/RegExpTools';
import { Grouper } from '../Grouper';
import type { GrouperFunction } from '../Grouper';
import type { Task } from './../../Task';
import type { FilterOrErrorMessage } from './Filter';

/**
Expand Down Expand Up @@ -287,7 +288,7 @@ export abstract class Field {
* @param reverse - false for normal group order, true for reverse group order.
*/
public createGrouper(reverse: boolean): Grouper {
return new Grouper(this.fieldNameSingular(), this.grouper(), reverse);
return new Grouper(this.fieldNameSingular(), this.grouper(), reverse, this.defaultComparator);
}

/**
Expand All @@ -309,4 +310,17 @@ export abstract class Field {
public createReverseGrouper(): Grouper {
return this.createGrouper(true);
}

protected defaultComparator: Comparator = (a: Task, b: Task) => {
const groupA = this.grouper()(a);
const groupB = this.grouper()(b);

for (let i = 0; i < groupA.length; i++) {
// The containers are guaranteed to be identical sizes since we are calling the same grouper
return groupA[i].localeCompare(groupB[i], undefined, { numeric: true });
}

// identical if we reach here
return 0;
};
}
2 changes: 1 addition & 1 deletion src/Query/Filter/MultiTextField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export abstract class MultiTextField extends TextField {
* This overloads {@link Field.createGrouper} to put a plural field name in the {@link Grouper.property}.
*/
public createGrouper(reverse: boolean): Grouper {
return new Grouper(this.fieldNamePlural(), this.grouper(), reverse);
return new Grouper(this.fieldNamePlural(), this.grouper(), reverse, this.defaultComparator);
}

protected grouperRegExp(): RegExp {
Expand Down
11 changes: 10 additions & 1 deletion src/Query/Grouper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Task } from '../Task';
import type { Comparator } from './Sorter';

/**
* A group-naming function, that takes a Task object and returns zero or more
Expand Down Expand Up @@ -36,12 +37,20 @@ export class Grouper {

/**
* Whether the headings for this group should be reversed.
* TODO now reverse used only in TaskGroups.toString(), shall be removed.
*/
public readonly reverse: boolean;

constructor(property: string, grouper: GrouperFunction, reverse: boolean) {
public readonly comparator: Comparator;

constructor(property: string, grouper: GrouperFunction, reverse: boolean, comparator: Comparator) {
this.property = property;
this.grouper = grouper;
this.reverse = reverse;
this.comparator = (a: Task, b: Task) => {
const result = comparator(a, b);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something but we talked elsewhere about comparator() throwing if sorting is unimplemented in the field...

Copy link
Collaborator Author

@ilandikov ilandikov Jun 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but this is just refactoring - moving the comparator from TaskGroups.sortTaskGroups into Field.defaultComparator. For now Field.comparator() is never called, so no throwing when sorting is not supported

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - right. Now I see what's going on.

I need to actually test this out and think about it some more.

I would be a lot more comfortable, and the review would be a lot quicker, if there were already the thorough group-sorting tests that we have talked about previously.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall correctly, you said we don't need to have extensive tests till the end to start changing the code. I may have misunderstood.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can quite believe I said that, but it wasn't my intention.

Once it became possible to change the sort order of groups, it revealed a gaping hole in the grouping tests, which had previously only tested the names of groups and not the order.

I would like that hole to be fixed for all fields before the grouping code is changed please.


return reverse ? -result : result;
};
}
}
16 changes: 4 additions & 12 deletions src/Query/TaskGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,12 @@ export class TaskGroups {

private sortTaskGroups() {
const compareFn = (group1: TaskGroup, group2: TaskGroup) => {
// Compare two TaskGroup objects, sorting them by the group names at each grouping level.
const groupNames1 = group1.groups;
const groupNames2 = group2.groups;
// The containers are guaranteed to be identical sizes,
// they have one value for each 'group by' line in the query.
for (let i = 0; i < groupNames1.length; i++) {
// For now, we only have one sort option: sort by the names of the groups.
// In future, we will add control over the sorting of group headings,
// which will likely involve adjusting this code to sort by applying a Comparator
// to the first Task in each group.
// Compare two TaskGroup objects, sorting them by first task in each group.
for (let i = 0; i < this._groupers.length; i++) {
const grouper = this._groupers[i];
const result = groupNames1[i].localeCompare(groupNames2[i], undefined, { numeric: true });
const result = grouper.comparator(group1.tasks[0], group2.tasks[0]);
if (result !== 0) {
return grouper.reverse ? -result : result;
return result;
}
}
// identical if we reach here
Expand Down
3 changes: 2 additions & 1 deletion tests/TaskGroups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ describe('Grouping tasks', () => {
const inputs = [a, b];

const groupByTags: GrouperFunction = (task: Task) => task.tags;
const grouper = new Grouper('custom tag grouper', groupByTags, false);
const groupComparator = new TagsField().comparator();
const grouper = new Grouper('custom tag grouper', groupByTags, false, groupComparator);
const groups = new TaskGroups([grouper], inputs);

expect(groups.totalTasksCount()).toEqual(2);
Expand Down