From 307abab341cd5b1bb2bef86f9c7335b6e6e798f0 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Mon, 11 Mar 2024 10:26:47 +0000 Subject: [PATCH] fix(common): remove underscore prefix from root namespace labels (#2400) --- .changeset/green-moles-camp.md | 5 +++++ packages/common/src/hexToResource.ts | 2 +- packages/common/src/index.ts | 2 +- packages/common/src/resourceLabel.ts | 11 ----------- packages/common/src/resourceToLabel.test.ts | 16 ++++++++++++++++ packages/common/src/resourceToLabel.ts | 16 ++++++++++++++++ 6 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 .changeset/green-moles-camp.md delete mode 100644 packages/common/src/resourceLabel.ts create mode 100644 packages/common/src/resourceToLabel.test.ts create mode 100644 packages/common/src/resourceToLabel.ts diff --git a/.changeset/green-moles-camp.md b/.changeset/green-moles-camp.md new file mode 100644 index 0000000000..991eb247b5 --- /dev/null +++ b/.changeset/green-moles-camp.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/common": patch +--- + +`resourceToLabel` now correctly returns just the resource name if its in the root namespace. diff --git a/packages/common/src/hexToResource.ts b/packages/common/src/hexToResource.ts index f95c93d15b..80a649880d 100644 --- a/packages/common/src/hexToResource.ts +++ b/packages/common/src/hexToResource.ts @@ -3,7 +3,7 @@ import { Resource } from "./common"; import { ResourceType, resourceTypes } from "./resourceTypes"; import { resourceTypeIds } from "./resourceToHex"; import { ReverseMap } from "./type-utils/common"; -import { resourceToLabel } from "./resourceLabel"; +import { resourceToLabel } from "./resourceToLabel"; const resourceTypeIdToType = Object.fromEntries( Object.entries(resourceTypeIds).map(([key, value]) => [value, key]), diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index e13d85d617..0da5bb7794 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -8,7 +8,7 @@ export * from "./getNonceManager"; export * from "./getNonceManagerId"; export * from "./hexToResource"; export * from "./readHex"; -export * from "./resourceLabel"; +export * from "./resourceToLabel"; export * from "./resourceToHex"; export * from "./resourceTypes"; export * from "./result"; diff --git a/packages/common/src/resourceLabel.ts b/packages/common/src/resourceLabel.ts deleted file mode 100644 index 83ed9b6d1b..0000000000 --- a/packages/common/src/resourceLabel.ts +++ /dev/null @@ -1,11 +0,0 @@ -export type ResourceLabel = `${namespace}__${name}`; - -export function resourceToLabel({ - namespace, - name, -}: { - readonly namespace: namespace; - readonly name: name; -}): ResourceLabel { - return `${namespace}__${name}`; -} diff --git a/packages/common/src/resourceToLabel.test.ts b/packages/common/src/resourceToLabel.test.ts new file mode 100644 index 0000000000..9a7949f46c --- /dev/null +++ b/packages/common/src/resourceToLabel.test.ts @@ -0,0 +1,16 @@ +import { describe, it, expect, expectTypeOf } from "vitest"; +import { resourceToLabel } from "./resourceToLabel"; + +describe("resourceToLabel", () => { + it("creates a human-readable label for a resource", () => { + const label = resourceToLabel({ namespace: "SomeNamespace", name: "SomeName" }); + expect(label).toBe("SomeNamespace__SomeName"); + expectTypeOf(label).toEqualTypeOf<"SomeNamespace__SomeName">(); + }); + + it("creates a human-readable label for a root resource", () => { + const label = resourceToLabel({ namespace: "", name: "SomeName" }); + expect(label).toBe("SomeName"); + expectTypeOf(label).toEqualTypeOf<"SomeName">(); + }); +}); diff --git a/packages/common/src/resourceToLabel.ts b/packages/common/src/resourceToLabel.ts new file mode 100644 index 0000000000..9079eeb290 --- /dev/null +++ b/packages/common/src/resourceToLabel.ts @@ -0,0 +1,16 @@ +const rootNamespace = ""; + +export type ResourceLabel< + namespace extends string = string, + name extends string = string, +> = namespace extends typeof rootNamespace ? name : `${namespace}__${name}`; + +export function resourceToLabel({ + namespace, + name, +}: { + readonly namespace: namespace; + readonly name: name; +}): ResourceLabel { + return (namespace === rootNamespace ? name : `${namespace}__${name}`) as ResourceLabel; +}