Skip to content

Commit

Permalink
self-review
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Mar 13, 2024
1 parent f6af3c6 commit b5d6902
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
13 changes: 4 additions & 9 deletions packages/store/ts/config/v2/generics.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
export type get<input, key, defaultValue = undefined> = key extends keyof input ? input[key] : defaultValue;
export type get<input, key> = key extends keyof input ? input[key] : undefined;

export function get<input, key extends PropertyKey, defaultValue = undefined>(
input: input,
key: key,
defaultValue: defaultValue = undefined as defaultValue,
): get<input, key, defaultValue> {
return (typeof input === "object" && input != null && hasOwnKey(input, key) ? input[key] : defaultValue) as get<
export function get<input, key extends PropertyKey>(input: input, key: key): get<input, key> {
return (typeof input === "object" && input != null && hasOwnKey(input, key) ? input[key] : undefined) as get<
input,
key,
defaultValue
key
>;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/store/ts/config/v2/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function scopeWithEnums<enums, scope extends AbiTypeScope = AbiTypeScope>
export type extendedScope<input> = scopeWithEnums<get<input, "enums">, scopeWithUserTypes<get<input, "userTypes">>>;

export function extendedScope<input>(input: input): extendedScope<input> {
return scopeWithEnums(get(input, "enums"), scopeWithUserTypes(get(input, "userTypes", undefined)));
return scopeWithEnums(get(input, "enums"), scopeWithUserTypes(get(input, "userTypes")));
}

export type validateStoreConfig<input> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/world/ts/config/v2/world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe("resolveWorldConfig", () => {
namespace: "",
} as const;

attest<typeof expected>(config).snap(expected);
attest<typeof expected>(config).equals(expected);
});

it("should resolve namespaced table config with user types and enums", () => {
Expand Down
16 changes: 8 additions & 8 deletions packages/world/ts/config/v2/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ export type resolveWorldConfig<input> = evaluate<
>;

export function resolveWorldConfig<const input>(input: validateWorldConfig<input>): resolveWorldConfig<input> {
const namespaces = get(input, "namespaces", {});
const namespaces = get(input, "namespaces") ?? {};
const scope = extendedScope(input);

const namespacedTables = Object.fromEntries(
Object.entries(namespaces)
.map(([namespaceKey, namespace]) => {
const tables = get(namespace, "tables", {});
const tables = get(namespace, "tables") ?? {};
return Object.entries(tables).map(([tableKey, table]) => [`${namespaceKey}__${tableKey}`, table]);
})
.flat(),
Expand All @@ -88,15 +88,15 @@ export function resolveWorldConfig<const input>(input: validateWorldConfig<input
const resolvedNamespaces = Object.fromEntries(
Object.entries(namespaces).map(([namespaceKey, namespace]) => [
namespaceKey,
{ tables: resolveStoreTablesConfig(get(namespace, "tables", {}), scope) },
{ tables: resolveStoreTablesConfig(get(namespace, "tables") ?? {}, scope) },
]),
);

return {
tables: resolveStoreTablesConfig({ ...get(input, "tables", {}), ...namespacedTables }, scope),
tables: resolveStoreTablesConfig({ ...(get(input, "tables") ?? {}), ...namespacedTables }, scope),
namespaces: resolvedNamespaces,
userTypes: get(input, "userTypes", {}),
enums: get(input, "enums", {}),
namespace: get(input, "namespace", ""),
} as unknown as resolveWorldConfig<input>;
userTypes: get(input, "userTypes") ?? {},
enums: get(input, "enums") ?? {},
namespace: get(input, "namespace") ?? "",
} as resolveWorldConfig<input>;
}

0 comments on commit b5d6902

Please sign in to comment.