UNPKG

@excentone/spfx-value-types

Version:

Contain common value types used when developing SharePoint Framework (SPFx) Web components.

124 lines (122 loc) 4.81 kB
import { ValueObject } from "../ValueObject"; /** * Represents an electronic mail address, following RFC standards. */ export class EmailAddress extends ValueObject { /** * Checks whether the `inputString` is a valid email address, based on RFC email format standard. * @param inputString The input string to check. * @constant EMAIL_FORMAT The regex format for validating the input string. * @returns `true` if the `inputString` is a valid e-mail address; otherwise `false`. */ static isValid(inputString) { return inputString && inputString.length ? EmailAddress.EMAIL_FORMAT.retest(inputString) : false; } /** Attempts to create a new `EmailAddress` instance from the input string. * * This will return `null` if the input string is not valid (i.e. `null`/`undefined`/empty/invalid format). */ static tryParse(inputString) { return this.isValid(inputString) ? new EmailAddress(inputString) : null; } /** * `RegExp` for username part of the email address. */ static USERNAME_FORMAT = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*/g; /** * `RegExp` for domain part of the email address. */ static DOMAIN_FORMAT = /(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g; /** * `RegExp` for the email address. */ static EMAIL_FORMAT = new RegExp(`(?<username>${EmailAddress.USERNAME_FORMAT.source})@(?<domain>${EmailAddress.DOMAIN_FORMAT.source})`, 'g'); /** The delimiters to check when generating the initials */ static USERNAME_DELIMITERS = ['.', '_', '-']; /** * Creates a new instance of `EmailAddress` with the specified string value. */ constructor(value) { super(value ? value.trim() : value); this.initializeParts(); } _username; _domain; validate() { if (!(this.value && this.value.length)) { throw new ReferenceError("Value cannot be null."); } if (!EmailAddress.EMAIL_FORMAT.retest(this.value)) { throw new EvalError("Value is not a valid email address"); } } /** * The part of the email address before the`@`character. */ get username() { return this._username; } /** * The part of the email address after the`@`character. */ get domain() { return this._domain; } /** * Returns the initials based on the `username` format. * * This checks the `username` part of the e-mail address for the delimiters specified in `EmailAddress.USERNAME_DELIMITERS`. * If exists, it will split the username by the first delimiter identified and returns the first letter of the first 2 words in the resulting array after the split operation. * Otherwise, it will just return the first 2 letters of the username. */ get initials() { let _initials = this.username.substring(0, 2); const delimiter = EmailAddress.USERNAME_DELIMITERS .find(d => this.username.includes(d, 1)); if (delimiter && delimiter.length) { const splitted = this.username.split(delimiter); console.log(splitted); if (splitted.length > 1) _initials = splitted[0][0] + splitted[1][0]; } return _initials; } /** * Creates a new instance of `EmailAddress` with the new `username` part. * * @param username The new username to be replaced to the old one. * @throws If the `username` parameter is null or empty. */ withUsername(username) { return this.createNewInstance('username', username, EmailAddress.USERNAME_FORMAT); } /** * Creates a new instance of `EmailAddress` with the new `domain` part. * * @param domain The new domain to be replaced to the old one. * @throws If the `domain` parameter is null or empty. */ withDomain(domain) { return this.createNewInstance('domain', domain, EmailAddress.DOMAIN_FORMAT); } initializeParts() { const result = EmailAddress.EMAIL_FORMAT.reexec(this.value); this._username = result.groups && result.groups.username; this._domain = result.groups && result.groups.domain; } createNewInstance(name, inputStr, regex) { if (inputStr && inputStr.length) { const newValue = this.value.replace(regex.resetIndex(), inputStr); return new EmailAddress(newValue); } throw new Error(`Parameter cannot be empty: ${name}`); } } String.prototype.asEmailAddress = function () { return EmailAddress.tryParse(this); }; String.prototype.isEmailAddress = function () { return EmailAddress.isValid(this); }; export { EmailAddress as Email }; //# sourceMappingURL=EmailAddress.js.map