Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions scripts/quick-db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class QuickDB {
}

has(key) {
return !!GET.call(world, `${this.#identifier}${key}`);
return !!(
GET.call(world, `${this.#identifier}${key}`) &&
GET.call(world, `${this.#identifier}${key}`) !== undefined
);
}

get(key) {
Expand All @@ -46,38 +49,55 @@ class QuickDB {
}

keys() {
return this.#UIDX("keys");
return Array.from(this.#UIDX("keys"));
}

values() {
return this.#UIDX("values");
return Array.from(this.#UIDX("values"));
}

entries() {
return this.#UIDX("entries");
return Array.from(this.#UIDX("entries"));
}

#UIDX(type) {
const ids = IDS.call(world);
const result = [];

for (const id of ids) {
if (!id.startsWith(this.#identifier)) continue;

const key = id.replace(this.#identifier, "");
if (type === "keys") {
result.push(key);
} else if (type === "values") {
const value = JSON.parse(this.get(key));
result.push(value);
} else if (type === "entries") {
const value = JSON.parse(this.get(key));
result.push([key, value]);
const ids = this.getIds();
let u_idx = 0;
const len = ids.length;

return function* () {
while (u_idx < len) {
const id = ids[u_idx];
const key = id.split(this.#identifier)[1];
const value = this.get(key);
switch (type) {
case "key":
yield key;
break;
case "value":
yield this.has(key) ? JSON.parse(value) : undefined;
break;
case "entries":
yield [key, JSON.parse(value)];
break;
}
u_idx++;
}
}
}.bind(this)();
}

return result;
getIds() {
return world
.getDynamicPropertyIds()
.filter((id) => id.startsWith(this.#identifier));
}

clear() {
for (const id of this.getIds()) {
this.delete(id.replace(this.#identifier,""));
}
}
}

export default QuickDB;
export { QuickDB };
Loading