Skip to content

Commit

Permalink
Add compare function to API (#65)
Browse files Browse the repository at this point in the history
* 👷 disable vitest watch for package build

* 🩹 fix TS import path; remove obsidian .first

* ✨ compareEvent API method
  • Loading branch information
ebullient authored Sep 5, 2023
1 parent bbc208f commit cfb2cd9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "The ultimate Obsidian plugin for crafting mind-bending fantasy and sci-fi calendars",
"main": "main.js",
"scripts": {
"build": "node ./esbuild.config.mjs production && npm test",
"build": "node ./esbuild.config.mjs production && vitest --run",
"dev": "node ./esbuild.config.mjs",
"release-as": "scripty",
"test": "vitest"
Expand Down
7 changes: 7 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Calendar, CalDate, CalEvent } from "src/@types";
import { CalendarStore } from "src/stores/calendar.store";
import { compareEvents } from "src/utils/functions";
import { get } from "svelte/store";

export class API {
Expand Down Expand Up @@ -32,8 +33,14 @@ export class API {
getEvents(): CalEvent[] {
return this.#store.eventStore.getEvents();
}

/** Get all events on a specific date. */
getEventsOnDay(day: CalDate): CalEvent[] {
return get(this.#store.eventStore.getEventsForDate(day));
}

/** Compare two events */
compareEvents(event1: CalEvent, event2: CalEvent): number {
return compareEvents(event1, event2);
}
}
6 changes: 3 additions & 3 deletions src/settings/import/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import type {
import distinct from "distinct-colors";

import { decode } from "he";
import type { ImportedCalendar } from "src/@types/import";
import type { ImportedCalendar } from "../../@types/import";
import deepmerge from "deepmerge";
import { DEFAULT_CALENDAR } from "../settings.constants";
import { nanoid } from "src/utils/functions";
import { nanoid } from "../../utils/functions";

export default class Import {
static import(objects: ImportedCalendar[]) {
Expand Down Expand Up @@ -82,7 +82,7 @@ export default class Import {
const intervals = interval.map((i) => {
const ignore = /\+/.test(i);
const exclusive = /\!/.test(i);
const interval = i.match(/(\d+)/).first();
const interval = i.match(/(\d+)/)[0];

return {
ignore,
Expand Down
24 changes: 17 additions & 7 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,22 @@ export function areDatesEqual(date: CalDate, date2: CalDate) {
return true;
}

export function sortEventList(list: CalEvent[]): CalEvent[] {
return list.sort((a, b) => {
if (!("sort" in a) || !("sort" in b)) return 0;
if (a.sort?.timestamp === b.sort?.timestamp) {
return a.sort?.order.localeCompare(b.sort?.order);
export function compareEvents(a: CalEvent, b: CalEvent) {
if (a.sort && b.sort) {
if (a.sort.timestamp == b.sort.timestamp) {
return a.sort.order.localeCompare(b.sort.order);
}
return a.sort?.timestamp - b.sort?.timestamp;
});
return a.sort.timestamp - b.sort.timestamp;
}
if (a.date.year != b.date.year) {
return a.date.year - b.date.year;
}
if (a.date.month != b.date.month) {
return a.date.month - b.date.month;
}
return a.date.day - b.date.day;
}

export function sortEventList(list: CalEvent[]): CalEvent[] {
return list.sort((a, b) => compareEvents(a, b));
}

0 comments on commit cfb2cd9

Please sign in to comment.