Skip to content

Commit

Permalink
Merge pull request #69 from kaelad02/v12compat
Browse files Browse the repository at this point in the history
fix deprecation warnings of util functions for Foundry v12
  • Loading branch information
kaelad02 authored May 21, 2024
2 parents a49c3bd + f1f3a81 commit 47611e6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/fails.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BaseFail {
*/
_getFlags(actor) {
const midiFlags = actor?.flags["midi-qol"] || {};
return flattenObject(midiFlags);
return foundry.utils.flattenObject(midiFlags);
}

get failKeys() {
Expand Down
6 changes: 3 additions & 3 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/reminders.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debug, isEmpty } from "./util.js";
import { debug } from "./util.js";

class BaseReminder {
constructor(actor) {
Expand All @@ -15,7 +15,7 @@ class BaseReminder {
*/
_getFlags(actor) {
const midiFlags = actor?.flags["midi-qol"] || {};
return flattenObject(midiFlags);
return foundry.utils.flattenObject(midiFlags);
}

_message() {
Expand Down Expand Up @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
9 changes: 0 additions & 9 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 15 additions & 12 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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();
Expand All @@ -47,29 +50,29 @@ 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;
}
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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 47611e6

Please sign in to comment.