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

Add a property aggregating all names of a NamespacedValue #2656

Merged
merged 1 commit into from
Sep 9, 2022
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 spec/unit/NamespacedValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ describe("NamespacedValue", () => {
const ns = new NamespacedValue("stable", "unstable");
expect(ns.name).toBe(ns.stable);
expect(ns.altName).toBe(ns.unstable);
expect(ns.names).toEqual([ns.stable, ns.unstable]);
});

it("should return unstable if there is no stable", () => {
const ns = new NamespacedValue(null, "unstable");
expect(ns.name).toBe(ns.unstable);
expect(ns.altName).toBeFalsy();
expect(ns.names).toEqual([ns.unstable]);
});

it("should have a falsey unstable if needed", () => {
const ns = new NamespacedValue("stable", null);
expect(ns.name).toBe(ns.stable);
expect(ns.altName).toBeFalsy();
expect(ns.names).toEqual([ns.stable]);
});

it("should match against either stable or unstable", () => {
Expand All @@ -58,12 +61,14 @@ describe("UnstableValue", () => {
const ns = new UnstableValue("stable", "unstable");
expect(ns.name).toBe(ns.unstable);
expect(ns.altName).toBe(ns.stable);
expect(ns.names).toEqual([ns.unstable, ns.stable]);
});

it("should return unstable if there is no stable", () => {
const ns = new UnstableValue(null, "unstable");
expect(ns.name).toBe(ns.unstable);
expect(ns.altName).toBeFalsy();
expect(ns.names).toEqual([ns.unstable]);
});

it("should not permit falsey unstable values", () => {
Expand Down
7 changes: 7 additions & 0 deletions src/NamespacedValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export class NamespacedValue<S extends string, U extends string> {
return this.unstable;
}

public get names(): (U | S)[] {
const names = [this.name];
const altName = this.altName;
if (altName) names.push(altName);
return names;
}

public matches(val: string): boolean {
return this.name === val || this.altName === val;
}
Expand Down