Skip to content

Commit

Permalink
Add Not query fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Aug 30, 2024
1 parent 9d191bb commit 4a82f7a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/stash/src/actions/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { runQuery } from "./runQuery";
import { defineStore } from "@latticexyz/store";
import { Stash, StoreRecords, getQueryConfig } from "../common";
import { setRecord } from "./setRecord";
import { In, Matches, NotIn, NotMatches } from "../queryFragments";
import { In, Matches, Not } from "../queryFragments";
import { Hex } from "viem";

describe("runQuery", () => {
Expand Down Expand Up @@ -84,7 +84,7 @@ describe("runQuery", () => {
});

it("should return all keys that are in Position but not Health", () => {
const result = runQuery({ stash, query: [In(Position), NotIn(Health)] });
const result = runQuery({ stash, query: [In(Position), Not(In(Health))] });
attest(result).snap({
keys: {
"0x0": { player: "0x0" },
Expand All @@ -95,7 +95,7 @@ describe("runQuery", () => {
});

it("should return all keys that don't include a gold item in the Inventory table", () => {
const result = runQuery({ stash, query: [NotMatches(Inventory, { item: "0xgold" })] });
const result = runQuery({ stash, query: [Not(Matches(Inventory, { item: "0xgold" }))] });
attest(result).snap({
keys: {
"0x0|0xsilver": { player: "0x0", item: "0xsilver" },
Expand Down
2 changes: 1 addition & 1 deletion packages/stash/src/apiEquality.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
import { createStash } from "./createStash";
import { attest } from "@ark/attest";
import { BoundTable } from "./actions/getTable";
import { DefaultActions } from "./decorators/default";
import { DefaultActions } from "./decorators/defaultActions";
import { defineTable } from "@latticexyz/store/config/v2";

describe("stash actions, bound table", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/stash/src/boundTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { attest } from "@arktype/attest";
import { createStash } from "./createStash";
import { BoundTable } from "./actions/getTable";
import { Stash } from "./common";
import { DefaultActions } from "./decorators/default";
import { DefaultActions } from "./decorators/defaultActions";
import { defineTable } from "@latticexyz/store/config/v2";

describe("BoundTable", () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/stash/src/queryFragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,17 @@ export function NotMatches<table extends Table>(
Object.fromEntries(Object.entries(getKeys({ stash, table })).filter(([key]) => pass(stash, key)));
return { table, pass, getInitialKeys };
}

/**
* Inverses a given query fragment (`In` becomes `NotIn`, `Matches` becomes `NotMatches` etc.)
* @param queryFragment
*/
export function Not<table extends Table>(queryFragment: QueryFragment<table>): QueryFragment<table> {
const pass = (stash: Stash, encodedKey: string) => !queryFragment.pass(stash, encodedKey);
const getInitialKeys = (stash: Stash) => {
const allKeys = getKeys({ stash, table: queryFragment.table });
const notInitialKeys = queryFragment.getInitialKeys(stash);
return Object.fromEntries(Object.entries(allKeys).filter(([key]) => !(key in notInitialKeys)));
};
return { table: queryFragment.table, pass, getInitialKeys };
}

0 comments on commit 4a82f7a

Please sign in to comment.