Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): remove underscore prefix from root namespace labels #2400

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/green-moles-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/common": patch
---

`resourceToLabel` now correctly returns just the resource name if its in the root namespace.
2 changes: 1 addition & 1 deletion packages/common/src/hexToResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
11 changes: 0 additions & 11 deletions packages/common/src/resourceLabel.ts

This file was deleted.

16 changes: 16 additions & 0 deletions packages/common/src/resourceToLabel.test.ts
Original file line number Diff line number Diff line change
@@ -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">();
});
});
16 changes: 16 additions & 0 deletions packages/common/src/resourceToLabel.ts
Original file line number Diff line number Diff line change
@@ -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 extends string, name extends string>({
namespace,
name,
}: {
readonly namespace: namespace;
readonly name: name;
}): ResourceLabel<namespace, name> {
return (namespace === rootNamespace ? name : `${namespace}__${name}`) as ResourceLabel<namespace, name>;
}
Loading