60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { Result } from "@repo/rdx-utils";
|
|
import { z } from "zod/v4";
|
|
import { translateZodValidationError } from "../helpers";
|
|
import { ValueObject } from "./value-object";
|
|
|
|
interface NameProps {
|
|
value: string;
|
|
}
|
|
|
|
export class Name extends ValueObject<NameProps> {
|
|
private static readonly MAX_LENGTH = 255;
|
|
|
|
protected static validate(value: string) {
|
|
const schema = z
|
|
.string()
|
|
.trim()
|
|
.max(Name.MAX_LENGTH, { message: `Name must be at most ${Name.MAX_LENGTH} characters long` });
|
|
return schema.safeParse(value);
|
|
}
|
|
|
|
static create(value: string) {
|
|
const valueIsValid = Name.validate(value);
|
|
|
|
if (!valueIsValid.success) {
|
|
return Result.fail(translateZodValidationError("Name creation failed", valueIsValid.error));
|
|
}
|
|
return Result.ok(new Name({ value }));
|
|
}
|
|
|
|
static generateAcronym(name: string): string {
|
|
const words = name.split(" ").map((word) => word[0].toUpperCase());
|
|
let acronym = words.join("");
|
|
|
|
// Asegurarse de que tenga 4 caracteres, recortando o añadiendo letras
|
|
if (acronym.length > 4) {
|
|
acronym = acronym.slice(0, 4);
|
|
} else if (acronym.length < 4) {
|
|
acronym = acronym.padEnd(4, "X"); // Se completa con 'X' si es necesario
|
|
}
|
|
|
|
return acronym;
|
|
}
|
|
|
|
getAcronym(): string {
|
|
return Name.generateAcronym(this.toString());
|
|
}
|
|
|
|
getProps(): string {
|
|
return this.props.value;
|
|
}
|
|
|
|
toPrimitive() {
|
|
return this.getProps();
|
|
}
|
|
|
|
toString() {
|
|
return String(this.props.value);
|
|
}
|
|
}
|