Skip to content

Commit a08bbd1

Browse files
authored
feat: strip trailing slashes from folders when applicable (#39)
1 parent a01827d commit a08bbd1

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { sanitizeFolder } from "../utils";
4+
5+
describe(`${sanitizeFolder.name}`, () => {
6+
it("should strip trailing slashes", () => {
7+
expect(sanitizeFolder("foo/")).toEqual("foo");
8+
});
9+
10+
it("should do nothing if there are no trailing slashes", () => {
11+
expect(sanitizeFolder("foo")).toEqual("foo");
12+
});
13+
14+
it("should do nothing when folder is the vault root", () => {
15+
expect(sanitizeFolder("/")).toEqual("/");
16+
});
17+
});

src/model/collection/periodic-notes.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import assert from "assert";
22
import { DateTime, DateTimeOptions, Duration, DurationLike, Interval, IntervalMaybeValid } from "luxon";
33
import { parse } from "path";
44

5+
import { assertLuxonValidity } from "@/util/luxon-utils";
6+
57
import { DateBasedCollection } from "./schema";
8+
import { sanitizeFolder } from "./utils";
69

710
/**
811
* @see {@link https://github.com/liamcain/obsidian-periodic-notes}
@@ -45,14 +48,15 @@ export class PeriodicNotes extends DateBasedCollection {
4548
) {
4649
super();
4750

48-
folder = folder.trim().replace(/\/$/, ""); // Ensure trailing slashes are removed.
51+
folder = sanitizeFolder(folder);
4952
const intervalOffset = Duration.fromDurationLike(intervalOffsetLike);
5053
const intervalDuration = Duration.fromDurationLike(intervalDurationLike);
5154

5255
assert(folder.length > 0, "folder must be non-empty");
5356
assert(dateFormat.length > 0, "dateFormat must be non-empty");
54-
assert(intervalOffset.isValid, "dateOffset must be valid");
55-
assert(intervalDuration.isValid && intervalDuration.valueOf() !== 0, "intervalDuration must be non-zero");
57+
assertLuxonValidity(intervalDuration, "intervalDuration must be valid");
58+
assert(intervalDuration.valueOf() !== 0, "intervalDuration must be non-zero");
59+
assertLuxonValidity(intervalOffset, "intervalOffset must be valid");
5660

5761
this.folder = folder;
5862
this.dateFormat = dateFormat;

src/model/collection/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param folder - the folder to sanitize.
3+
* @returns sanitized value with trailing slashes removed, if applicable.
4+
*/
5+
export function sanitizeFolder(folder: string): string {
6+
folder = folder.trim();
7+
return folder === "/" ? folder : folder.replace(/\/$/, "");
8+
}

0 commit comments

Comments
 (0)