Skip to content

Commit

Permalink
Unify sorting types (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrynes7 authored Jan 13, 2024
1 parent 3599809 commit f7af1ab
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/data/transformations.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "mocha";
import type { Task } from "./task";
import type { Project } from "../api/domain/project";
import { UnknownProject, groupByProject, type GroupedTasks, type Sort, sortTasks, type TaskTree, buildTaskTree } from "./transformations";
import { UnknownProject, groupByProject, type GroupedTasks, sortTasks, type TaskTree, buildTaskTree } from "./transformations";
import { assert } from "chai";
import type { Sort } from "../query/query";

function makeTask(id: string, opts?: Partial<Task>): Task {
return {
Expand Down
20 changes: 6 additions & 14 deletions src/data/transformations.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { getDueDateInfo } from "../api/domain/dueDate";
import type { Project } from "../api/domain/project";
import type { TaskId } from "../api/domain/task";
import type { Sort } from "../query/query";
import type { Task } from "./task";

export type GroupedTasks = {
project: Project,
tasks: Task[],
};

export const UnknownProject: Project = {
id: "unknown-project-fake",
parentId: null,
name: "Unknown Project",
order: Number.MAX_SAFE_INTEGER,
};

export type GroupedTasks = {
project: Project,
tasks: Task[],
};

export function groupByProject(tasks: Task[]): GroupedTasks[] {
const projects = new Map<Project, Task[]>();

Expand All @@ -32,13 +33,6 @@ export function groupByProject(tasks: Task[]): GroupedTasks[] {
return Array.from(projects.entries()).map(([project, tasks]) => { return { project: project, tasks: tasks }; })
}

export type Sort =
"priority" | "priorityAscending"
| "priorityDescending"
| "date" | "dateAscending"
| "dateDescending"
| "order";

export function sortTasks<T extends Task>(tasks: T[], sort: Sort[]) {
tasks.sort((first, second) => {
for (const sorting of sort) {
Expand All @@ -54,7 +48,6 @@ export function sortTasks<T extends Task>(tasks: T[], sort: Sort[]) {
})
}


// Result of "LT zero" means that self is before other,
// Result of '0' means that they are equal
// Result of "GT zero" means that self is after other
Expand Down Expand Up @@ -115,7 +108,6 @@ function compareTaskDate<T extends Task>(self: T, other: T): number {
return selfDate.isBefore(otherDate) ? -1 : 1;
}


export type TaskTree = Task & { children: TaskTree[] };

// Builds a task tree, preserving the sorting order.
Expand Down
4 changes: 2 additions & 2 deletions src/query/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSortingOption, sortingOptions } from "./query";
import { isSortingOption, sortingVariants } from "./query";
import type { Query } from "./query";
import YAML from "yaml";

Expand Down Expand Up @@ -93,5 +93,5 @@ function parseObject(query: any): Query {
}

function formatSortingOpts(): string {
return sortingOptions.map((e) => `'${e}'`).join(", ");
return sortingVariants.map((e) => `'${e}'`).join(", ");
}
24 changes: 17 additions & 7 deletions src/query/query.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
export const sortingOptions = [
export type Sort =
"priority" | "priorityAscending"
| "priorityDescending"
| "date" | "dateAscending"
| "dateDescending"
| "order";

// This should be kept in sync with the above, not sure how to enforce
// exhaustiveness.
export const sortingVariants: Sort[] = [
"priority",
"priorityAscending",
"priorityDescending",
"date",
"dateAscending",
"dateDescending",
"priority",
"order",
];

export type SortingOption = typeof sortingOptions[number];

export function isSortingOption(value: string): value is SortingOption;
export function isSortingOption(value: string): value is Sort;
export function isSortingOption(value: any) {
return sortingOptions.includes(value);
return sortingVariants.includes(value);
}

export type Query = {
name: string;
filter: string;
autorefresh?: number;
sorting?: SortingOption[];
sorting?: Sort[];
group: boolean;
};
3 changes: 2 additions & 1 deletion src/ui/GroupedTasks.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script lang="ts">
import type { Task } from "../data/task";
import { groupByProject } from "../data/transformations";
import type { Sort } from "../query/query";
import TaskListRoot from "./TaskListRoot.svelte";
export let tasks: Task[];
export let sorting: string[];
export let sorting: Sort[];
$: grouped = groupByProject(tasks).sort(
(first, second) => first.project.order - second.project.order
Expand Down
6 changes: 3 additions & 3 deletions src/ui/TaskListRoot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import {
buildTaskTree,
sortTasks,
type Sort,
type TaskTree,
} from "../data/transformations";
import type { Sort } from "../query/query";
import TaskList from "./TaskList.svelte";
export let tasks: Task[];
export let sorting: string[];
export let sorting: Sort[];
$: taskTrees = getTaskTree(tasks);
function getTaskTree(tasks: Task[]): TaskTree[] {
sortTasks(tasks, sorting as Sort[]);
sortTasks(tasks, sorting);
return buildTaskTree(tasks);
}
</script>
Expand Down

0 comments on commit f7af1ab

Please sign in to comment.