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

feat(core): entity-type-label pipe for easier display of human-readable labels #1927

Merged
merged 3 commits into from
Jul 17, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { EntityTypeLabelPipe } from "./entity-type-label.pipe";
import { EntityRegistry } from "../../entity/database-entity.decorator";
import { Entity } from "../../entity/model/entity";

describe("EntityTypeLabelPipe", () => {
class TestEntity extends Entity {
static ENTITY_TYPE = "TestEntity";
static label = "test record";
static labelPlural = "test records";
}

let pipe: EntityTypeLabelPipe;

beforeEach(() => {
const entityRegistry = new EntityRegistry();
entityRegistry.add(TestEntity.ENTITY_TYPE, TestEntity);
pipe = new EntityTypeLabelPipe(entityRegistry);
});

it("returns entity-type label for a valid type/prefix", () => {
expect(pipe.transform(TestEntity.ENTITY_TYPE)).toBe(TestEntity.label);
});

it("returns entity-type label plural if flag given", () => {
expect(pipe.transform(TestEntity.ENTITY_TYPE, true)).toBe(
TestEntity.labelPlural
);
});

it("throws error on invalid entity type", () => {
expect(() => pipe.transform("unknown type")).toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Pipe, PipeTransform } from "@angular/core";
import { EntityRegistry } from "../../entity/database-entity.decorator";

/**
* Transform an entity type key to its human-readable label.
*/
@Pipe({
name: "entityTypeLabel",
standalone: true,
})
export class EntityTypeLabelPipe implements PipeTransform {
constructor(private entityTypes: EntityRegistry) {}

transform(value: string, plural: boolean = false): string {
const entity = this.entityTypes.get(value);
return plural ? entity?.labelPlural : entity?.label;
}
}