52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { moment } from "obsidian";
|
|
import { adjectives } from "adjectives.js";
|
|
import { capitalize, sample } from "lodash-es";
|
|
import { nouns } from "nouns.js";
|
|
|
|
interface FormatOptions {
|
|
ext?: string;
|
|
}
|
|
|
|
export function format(str: string, options?: FormatOptions): string {
|
|
let formatted = str.trim();
|
|
|
|
let extension = options?.ext ?? "";
|
|
if (extension.length > 0 && !extension.startsWith(".")) {
|
|
extension = `.${extension}`;
|
|
}
|
|
|
|
// Replace {N} with a random noun
|
|
formatted = formatted.replace(/{N}/g, () => {
|
|
return capitalize(sample(nouns));
|
|
});
|
|
|
|
// Replace {n} with a random noun (lower case)
|
|
formatted = formatted.replace(/{n}/g, () => {
|
|
return sample(nouns);
|
|
});
|
|
|
|
// Replace {A} with a random adjective
|
|
formatted = formatted.replace(/{A}/g, () => {
|
|
return capitalize(sample(adjectives));
|
|
});
|
|
|
|
// Replace {a} with a random adjective (lower case)
|
|
formatted = formatted.replace(/{a}/g, () => {
|
|
return sample(adjectives);
|
|
});
|
|
|
|
// Replace {MW} with the current month week
|
|
const monthWeek = `${Math.ceil(new Date().getDate() / 7)}`;
|
|
formatted = formatted.replace(/{MW}/g, monthWeek);
|
|
|
|
// Replace {time <moment format>} with the moment-formatted string
|
|
formatted = formatted.replace(
|
|
/{time (.+?)}/g,
|
|
(_: string, pattern: string) => {
|
|
return moment().format(pattern);
|
|
}
|
|
);
|
|
|
|
return formatted + extension;
|
|
}
|