Allow iterable object to be detected as an object by foundry

This commit is contained in:
Carlos Fernandez 2026-03-06 21:27:58 -05:00
parent 8727cb0dca
commit 0ace9f2b8e

View file

@ -6,25 +6,27 @@ export default class IterableTypedObjectField extends foundry.data.fields.TypedO
#elementClass; #elementClass;
initialize(value) { /** Initializes an object with an iterator. This modifies the prototype instead of */
return new IterableObject(value, this.#elementClass); initialize(values) {
} const object = Object.create(IterableObjectPrototype);
}
class IterableObject {
constructor(values, elementClass) {
for (const [key, value] of Object.entries(values)) { for (const [key, value] of Object.entries(values)) {
this[key] = new elementClass(value); object[key] = new this.#elementClass(value);
}
return object;
} }
} }
*[Symbol.iterator]() { /**
* The prototype of an iterable object.
* This allows the functionality of a class but also allows foundry.utils.getType() to return "Object" instead of "Unknown".
*/
const IterableObjectPrototype = {
[Symbol.iterator]: function*() {
for (const value of Object.values(this)) { for (const value of Object.values(this)) {
yield value; yield value;
} }
} },
map: function (func) {
map(func) {
return Array.from(this, func); return Array.from(this, func);
} }
} };