From 323f785c5bb04e94609f3d2d83f4e23e5aa2cf0e Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Sat, 9 Mar 2024 15:59:25 +0000 Subject: [PATCH 1/2] fix(common): remove underscore prefix from root namespace labels --- 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 ++++++++++++++++ 5 files changed, 34 insertions(+), 13 deletions(-) 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/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; +} From 379eb7b42683537c977144351b89b9ea3c6b8adc Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Sat, 9 Mar 2024 08:37:33 -0800 Subject: [PATCH 2/2] Create green-moles-camp.md --- .changeset/green-moles-camp.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/green-moles-camp.md 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.