@type-ddd/username
Version:
This package provides TypeScript type definitions for handling User Name in Domain-Driven Design contexts
151 lines (150 loc) • 4.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserName = void 0;
const rich_domain_1 = require("rich-domain");
class UserName extends rich_domain_1.ValueObject {
constructor(props) {
super(props);
}
/**
* @returns capitalized full name
*/
value() {
return this.props.trim();
}
/**
*
* @returns capitalize full name as string
*/
static capitalize(fullName) {
const names = fullName.split(' ').filter((name) => {
return name.length > 1;
});
const capitalizedName = (name) => {
return name[0].toUpperCase() + name.slice?.(1)?.toLowerCase();
};
const capitalized = [];
for (const name of names) {
const lowerCaseName = capitalizedName(name);
capitalized.push(lowerCaseName);
}
return capitalized.toString().replace(/,/g, ' ');
}
/**
* @description get upper case name from instance
* @returns upperCase full name as string
*/
upperCase() {
return this.props.toUpperCase();
}
/**
* @description get lower case name from instance
* @returns lowerCase full name as string
*/
lowerCase() {
return this.props.toLowerCase();
}
/**
*
* @returns check if has a second name
*/
hasMiddleName() {
return this.props.split(' ').length > 2;
}
title(title) {
return {
firstName: () => title + ' ' + this.firstName(),
fullName: () => title + ' ' + this.value(),
lastName: () => title + ' ' + this.lastName(),
middleName: () => title + ' ' + this.middleName(),
};
}
/**
*
* @returns check if has last name `first middle last`
*/
hasLastName() {
return this.props.split(' ').length >= 2;
}
/**
*
* @returns first name
*/
firstName(title = '') {
return (title + ' ' + this.props.split(' ')[0])?.trim();
}
/**
*
* @returns middle name if it has more than 2 names, else returns a empty string
*/
middleName() {
if (!this.hasMiddleName()) {
return '';
}
return this.props.split(' ')[1]?.trim();
}
/**
*
* @returns last name if exists else return the name
*/
lastName() {
const names = this.props.split(' ');
return names.at(-1)?.trim();
}
/**
* @returns initials as string
* @param separator as string char to separate letters
* @default separator (empty)
* @example
* for a name "Thomas A. Anderson" = "TAA"
*/
initials(separator = '') {
const names = this.props.split(' ');
const letters = names.map((name) => name[0]);
const value = this.util.string(letters.toString());
const initials = value.replace(',').to(separator);
return initials;
}
/**
*
* @param value value as string
* @returns instance of UserName or throw an error
*/
static init(value) {
const isValidValue = UserName.isValidProps(value);
if (!isValidValue)
throw new Error(UserName.MESSAGE);
const capitalized = UserName.capitalize(value);
return new UserName(capitalized);
}
/**
* @description check name length min(2) max(40)
* @param value name as string
* @returns true if provided value is valid and false if not
*/
static isValid(value) {
return this.isValidProps(value);
}
/**
* @description check name length min(2) max(40)
* @param value name as string
* @returns true if provided value is valid and false if not
*/
static isValidProps(value) {
const { string } = this.validator;
return string(value).hasLengthBetween(UserName.MIN_LENGTH, UserName.MAX_LENGTH);
}
static create(value) {
const isValidValue = UserName.isValidProps(value);
if (!isValidValue) {
return rich_domain_1.Result.fail(UserName.MESSAGE);
}
const capitalized = UserName.capitalize(value);
return rich_domain_1.Result.Ok(new UserName(capitalized));
}
}
exports.UserName = UserName;
UserName.MAX_LENGTH = 82;
UserName.MIN_LENGTH = 2;
UserName.MESSAGE = `Invalid name length. Must has min ${UserName.MIN_LENGTH} and max ${UserName.MAX_LENGTH} chars`;
exports.default = UserName;