Skip to content

Commit

Permalink
removes unused utility code
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Jan 21, 2022
1 parent d595848 commit 62c62ac
Showing 1 changed file with 2 additions and 144 deletions.
146 changes: 2 additions & 144 deletions src/util/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Spell, Monster, Trait } from "@types";
import type { Monster, Trait } from "@types";
import { parseYaml } from "obsidian";

export function toTitleCase(str: string): string {
Expand All @@ -11,157 +11,15 @@ export function getMod(arg0: number) {
return `${mod >= 0 ? "+" : "-"}${Math.abs(mod)}`;
}

type YamlTrait = [string, ...{ [key: string]: any }[]];
/** Parse Yaml-Defined Trait to Trait */
export function parseTrait(arg: YamlTrait): Trait {
if (!arg || !(arg instanceof Array)) return;
if (!arg.length) return;

const name = arg[0];
const desc = arg
.slice(1)
.map((d) =>
typeof d === "string"
? d
: `${Object.keys(d)[0]}: ${Object.values(d)[0]}`
)
.join(", ")
.trim();

return {
name: name,
desc: desc
};
}

export function getParamsFromSource(source: string): Monster {
let params = parseYaml(source);

const traits: Trait[] = [];
const actions: Trait[] = [];
const reactions: Trait[] = [];
const legendary_actions: Trait[] = [];
try {
(params?.traits || []).forEach((trait: YamlTrait) => {
let t = parseTrait(trait);
if (!t) return;
traits.push(t);
});
} catch (e) {
throw new Error("There was an error parsing the provided traits.");
}
try {
(params?.actions || []).forEach((trait: YamlTrait) => {
let t = parseTrait(trait);
if (!t) return;
actions.push(t);
});
} catch (e) {
throw new Error("There was an error parsing the provided actions.");
}
try {
(params?.reactions || []).forEach((trait: YamlTrait) => {
let t = parseTrait(trait);
if (!t) return;
reactions.push(t);
});
} catch (e) {
throw new Error("There was an error parsing the provided reactions.");
}
try {
(params?.legendary_actions || []).forEach((trait: YamlTrait) => {
let t = parseTrait(trait);
if (!t) return;
legendary_actions.push(t);
});
} catch (e) {
throw new Error(
"There was an error parsing the provided legendary actions."
);
}

//replace escapes
params = JSON.parse(JSON.stringify(params).replace(/\\/g, ""));

return { ...params, traits, actions, reactions, legendary_actions };
return params ?? {};
}

export function traitMapFrom(traits: Trait[] = []): Map<string, Trait> {
return new Map(traits.map((t) => [t.name, t]));
}
export function spellArrayFrom(arg0: Trait): Spell[] {
const { desc } = arg0;
if (!desc) return [];

return desc
.split("\n")
.filter((d) => d.trim().length)
.map((d) => d.trim())
.map((spell) => {
if (/^•\s?([\d\w\s\(\)]+):/.test(spell)) {
const [, level, spellList] = spell.match(
/^•\s?([\d\w\s\(\)]+):\s?([\s\S]+?)$/
);
return {
[level]: spellList
};
}
return spell;
});
}

export function getColumns(contentEl: HTMLElement) {
let width = contentEl?.getBoundingClientRect()?.width || 400;
return Math.floor((width - 20) / 400);
}

export function catchError(
target: any,
name: string,
descriptor: PropertyDescriptor
) {
const original = descriptor.value;
if (typeof original === "function") {
descriptor.value = function (...args: any[]) {
try {
return original.apply(this, args);
} catch (e) {
//throw error here
renderError(
this.contentEl.parentElement,
`Error in ${name}: ${e.message}`
);
}
};
}
}

export function catchErrorAsync(
target: any,
name: string,
descriptor: PropertyDescriptor
) {
const original = descriptor.value;
if (typeof original === "function") {
descriptor.value = async function (...args: any[]) {
try {
return await original.apply(this, args);
} catch (e) {
//throw error here
renderError(
this.contentEl.parentElement,
`Error in ${name}: ${e.message}`
);
}
};
}
}

export function renderError(el: HTMLElement, error: string): void {
let pre = createEl("pre");
pre.setText(`\`\`\`statblock
There was an error rendering the statblock:
${error}
\`\`\``);
el.replaceWith(pre);
}

0 comments on commit 62c62ac

Please sign in to comment.