Skip to content

Commit

Permalink
fix: significantly improve the markdown data file transition
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Sep 26, 2023
1 parent e718a1a commit 7f9b7b1
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/settings/markdown-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export enum MarkdownReason {
NONE,
NO_DATA = "No saved plugin data.",
NOT_OBJECT = "Plugin data is not an object.",
EMPTY = "Plugin data is an empty object.",
TRANSITIONED = "Previous plugin data was transitioned correctly.",
NO_VERSION = "No version information exists in plugin data.",
NO_PATCH = "No patch version infromation exists in plugin data.",
OLD_VERSION = "Plugin data is from a version prior to beta 26.",
}

export function shouldTransitionMarkdownSettings(pluginData: any) {
/** No saved plugin data in data.json */
if (pluginData == null) {
return MarkdownReason.NO_DATA;
}
/** data.json is not an object??? */
if (typeof pluginData !== "object") {
return MarkdownReason.NOT_OBJECT;
}
/** data.json is an empty object. */
if (Object.keys(pluginData)?.length == 0) {
return MarkdownReason.EMPTY;
}
/** data.json was transitioned to markdown data */
if ("transitioned" in pluginData) {
return MarkdownReason.TRANSITIONED;
}
/** finally, check the version in data.json */
if (!("version" in pluginData) || typeof pluginData.version !== "object") {
return MarkdownReason.NO_VERSION;
}

const version = pluginData.version;
/** In a post-beta 28 release. */
if ("beta" in version) {
return MarkdownReason.NONE;
}
/** No patch version to check */
if (!("patch" in version) || version.patch == null) {
return MarkdownReason.NO_PATCH;
}
if (typeof version.patch === "string") {
let [, , beta] = version.patch.match(/(\d+)(?:\-b(\d+))?/) ?? [
version.patch,
];
if (isNaN(Number(beta)) || Number(beta) < 26) {
return MarkdownReason.OLD_VERSION;
}
}

return MarkdownReason.NONE;
}
114 changes: 114 additions & 0 deletions test/settings/settings.markdown-transition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @vitest-environment happy-dom
*/
import { vi, test, expect } from "vitest";
import {
shouldTransitionMarkdownSettings,
MarkdownReason,
} from "../../src/settings/markdown-import";

test("No data", () => {
expect(shouldTransitionMarkdownSettings(null)).toBe(MarkdownReason.NO_DATA);
});
test("Not object", () => {
expect(shouldTransitionMarkdownSettings("abc")).toBe(
MarkdownReason.NOT_OBJECT
);
});
test("Empty object", () => {
expect(shouldTransitionMarkdownSettings({})).toBe(MarkdownReason.EMPTY);
});
test("Transitioned", () => {
expect(shouldTransitionMarkdownSettings({ transitioned: true })).toBe(
MarkdownReason.TRANSITIONED
);
});
test("No version", () => {
expect(shouldTransitionMarkdownSettings({ abc: "def" })).toBe(
MarkdownReason.NO_VERSION
);
expect(shouldTransitionMarkdownSettings({ version: "def" })).toBe(
MarkdownReason.NO_VERSION
);
});
test("No patch", () => {
expect(shouldTransitionMarkdownSettings({ version: {} })).toBe(
MarkdownReason.NO_PATCH
);
expect(shouldTransitionMarkdownSettings({ version: { patch: null } })).toBe(
MarkdownReason.NO_PATCH
);
});
test("Old version", () => {
expect(
shouldTransitionMarkdownSettings({ version: { patch: "0-b25" } })
).toBe(MarkdownReason.OLD_VERSION);
});

test("Good", () => {
const good = {
addToDefaultIfMissing: true,
calendars: [],
configDirectory: null,
dailyNotes: false,
dateFormat: "YYYY-MM-DD",
defaultCalendar: "ID_c9ca2a79c8c9",
eventPreview: false,
exit: {
saving: false,
event: false,
calendar: false,
},
eventFrontmatter: false,
parseDates: false,
settingsToggleState: {
calendars: true,
events: false,
advanced: true,
},
showIntercalary: false,
version: {
major: 1,
minor: 0,
patch: 0,
beta: 28,
},
debug: false,
askedToMoveFC: true,
currentCalendar: null,
deletedCalendars: [],
};
const good2 = {
addToDefaultIfMissing: true,
calendars: [],
configDirectory: null,
dailyNotes: false,
dateFormat: "YYYY-MM-DD",
defaultCalendar: "ID_c9ca2a79c8c9",
eventPreview: false,
exit: {
saving: false,
event: false,
calendar: false,
},
eventFrontmatter: false,
parseDates: false,
settingsToggleState: {
calendars: true,
events: false,
advanced: true,
},
showIntercalary: false,
version: {
major: 1,
minor: 0,
patch: "0-b27",
},
debug: false,
askedToMoveFC: true,
currentCalendar: null,
deletedCalendars: [],
};
expect(shouldTransitionMarkdownSettings(good)).toBe(MarkdownReason.NONE);
expect(shouldTransitionMarkdownSettings(good2)).toBe(MarkdownReason.NONE);
});

0 comments on commit 7f9b7b1

Please sign in to comment.