diff --git a/src/fails.js b/src/fails.js index 52d8c36..d258046 100644 --- a/src/fails.js +++ b/src/fails.js @@ -13,7 +13,7 @@ class BaseFail { */ _getFlags(actor) { const midiFlags = actor?.flags["midi-qol"] || {}; - return flattenObject(midiFlags); + return foundry.utils.flattenObject(midiFlags); } get failKeys() { diff --git a/src/messages.js b/src/messages.js index 20b79db..c317b41 100644 --- a/src/messages.js +++ b/src/messages.js @@ -30,7 +30,7 @@ class BaseMessage { debug("checking for message effects"); // get any existing messages - const messages = getProperty(options, "dialogOptions.adv-reminder.messages") ?? []; + const messages = foundry.utils.getProperty(options, "dialogOptions.adv-reminder.messages") ?? []; // get messages from the actor and merge const keys = this.messageKeys; @@ -50,8 +50,8 @@ class BaseMessage { if (messages.length > 0) { debug("messages found:", messages); - setProperty(options, "dialogOptions.adv-reminder.messages", messages); - setProperty(options, "dialogOptions.adv-reminder.rollData", this.actor.getRollData()); + foundry.utils.setProperty(options, "dialogOptions.adv-reminder.messages", messages); + foundry.utils.setProperty(options, "dialogOptions.adv-reminder.rollData", this.actor.getRollData()); } } } diff --git a/src/module.js b/src/module.js index e6a0aba..befc199 100644 --- a/src/module.js +++ b/src/module.js @@ -32,10 +32,10 @@ function applyMidiCustom(actor, change) { ]; if (supportedKeys.some((k) => change.key.startsWith(k))) { // update the actor - if (typeof change.value !== "string") setProperty(actor, change.key, change.value); - else if (["true", "1"].includes(change.value.trim())) setProperty(actor, change.key, true); - else if (["false", "0"].includes(change.value.trim())) setProperty(actor, change.key, false); - else setProperty(actor, change.key, change.value); + if (typeof change.value !== "string") foundry.utils.setProperty(actor, change.key, change.value); + else if (["true", "1"].includes(change.value.trim())) foundry.utils.setProperty(actor, change.key, true); + else if (["false", "0"].includes(change.value.trim())) foundry.utils.setProperty(actor, change.key, false); + else foundry.utils.setProperty(actor, change.key, change.value); } } diff --git a/src/reminders.js b/src/reminders.js index 935738a..679077d 100644 --- a/src/reminders.js +++ b/src/reminders.js @@ -1,4 +1,4 @@ -import { debug, isEmpty } from "./util.js"; +import { debug } from "./util.js"; class BaseReminder { constructor(actor) { @@ -15,7 +15,7 @@ class BaseReminder { */ _getFlags(actor) { const midiFlags = actor?.flags["midi-qol"] || {}; - return flattenObject(midiFlags); + return foundry.utils.flattenObject(midiFlags); } _message() { @@ -345,7 +345,7 @@ export class CriticalReminder extends BaseReminder { // get the Range directly from the actor's flags if (targetActor) { const grantsCriticalRange = - getProperty(targetActor, "flags.midi-qol.grants.critical.range") || -Infinity; + foundry.utils.getProperty(targetActor, "flags.midi-qol.grants.critical.range") || -Infinity; this._adjustRange(distanceFn, grantsCriticalRange); } } diff --git a/src/sources.js b/src/sources.js index 452f47e..23925a0 100644 --- a/src/sources.js +++ b/src/sources.js @@ -80,9 +80,9 @@ const SourceMixin = (superclass) => debug("advantageLabels", advantageLabels, "disadvantageLabels", disadvantageLabels); const merge = (newLabels, key) => { if (newLabels.length) { - const labels = getProperty(options, key); + const labels = foundry.utils.getProperty(options, key); if (labels) newLabels.push(...labels); - setProperty(options, key, newLabels); + foundry.utils.setProperty(options, key, newLabels); } }; merge(advantageLabels, "dialogOptions.adv-reminder.advantageLabels"); @@ -156,9 +156,9 @@ export class CriticalSource extends SourceMixin(CriticalReminder) { debug("criticalLabels", criticalLabels, "normalLabels", normalLabels); const merge = (newLabels, key) => { if (newLabels.length) { - const labels = getProperty(options, key); + const labels = foundry.utils.getProperty(options, key); if (labels) newLabels.push(...labels); - setProperty(options, key, newLabels); + foundry.utils.setProperty(options, key, newLabels); } }; merge(criticalLabels, "dialogOptions.adv-reminder.criticalLabels"); diff --git a/src/util.js b/src/util.js index df6797d..b2cbb97 100644 --- a/src/util.js +++ b/src/util.js @@ -29,15 +29,6 @@ export function getTarget() { return [...game.user.targets][0]?.actor; } -/** - * Test if an object is empty. - * @param {object} obj object to test - * @returns true if an object is empty, false otherwise - */ -export function isEmpty(obj) { - return !Object.keys(obj).length; -} - export function getDistanceToTargetFn(speaker) { return () => { const controlledTokenDoc = game.scenes.get(speaker.scene).tokens.get(speaker.token); diff --git a/test/common.js b/test/common.js index 19ccdd7..cbd59af 100644 --- a/test/common.js +++ b/test/common.js @@ -7,7 +7,7 @@ export default function commonTestInit() { hasConditionEffect: () => false, system: {}, }; - keys.forEach((k) => setProperty(actor, k, true)); + keys.forEach((k) => foundry.utils.setProperty(actor, k, true)); return actor; }; @@ -34,7 +34,10 @@ export default function commonTestInit() { toObject: {value: toObject} }); - globalThis.setProperty = (object, key, value) => { + globalThis.foundry = {}; + globalThis.foundry.utils = {}; + + globalThis.foundry.utils.setProperty = (object, key, value) => { // split the key into parts, removing the last one const parts = key.split("."); const lastProp = parts.pop(); @@ -47,12 +50,12 @@ export default function commonTestInit() { lastObj[lastProp] = value; }; - globalThis.getProperty = (object, key) => { + globalThis.foundry.utils.getProperty = (object, key) => { if (!key) return undefined; if (key in object) return object[key]; let target = object; for (let p of key.split(".")) { - getType(target); + foundry.utils.getType(target); if (!(typeof target === "object")) return undefined; if (p in target) target = target[p]; else return undefined; @@ -60,16 +63,16 @@ export default function commonTestInit() { return target; }; - globalThis.flattenObject = (obj, _d = 0) => { + globalThis.foundry.utils.flattenObject = (obj, _d = 0) => { const flat = {}; if (_d > 100) { throw new Error("Maximum depth exceeded"); } for (let [k, v] of Object.entries(obj)) { - let t = getType(v); + let t = foundry.utils.getType(v); if (t === "Object") { - if (isEmpty(v)) flat[k] = v; - let inner = flattenObject(v, _d + 1); + if (foundry.utils.isEmpty(v)) flat[k] = v; + let inner = foundry.utils.flattenObject(v, _d + 1); for (let [ik, iv] of Object.entries(inner)) { flat[`${k}.${ik}`] = iv; } @@ -78,7 +81,7 @@ export default function commonTestInit() { return flat; }; - globalThis.getType = (variable) => { + globalThis.foundry.utils.getType = (variable) => { // Primitive types, handled with simple typeof check const typeOf = typeof variable; if (typeOf !== "object") return typeOf; @@ -106,15 +109,15 @@ export default function commonTestInit() { return "Object"; }; - globalThis.isEmpty = (value) => { - const t = getType(value); + globalThis.foundry.utils.isEmpty = (value) => { + const t = foundry.utils.getType(value); switch (t) { case "undefined": return true; case "Array": return !value.length; case "Object": - return getType(value) === "Object" && !Object.keys(value).length; + return foundry.utils.getType(value) === "Object" && !Object.keys(value).length; case "Set": case "Map": return !value.size;