| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- export class ContactAddress {
- public static fromPrefixedString(prefixed: string) {
- const components = prefixed.split(/:/);
- const type = components.shift();
- if (type == null) {
- throw new Error('Invalid input: ' + prefixed);
- }
- const unprefixed = components.join(':');
- return new ContactAddress(type, unprefixed);
- }
- public static parsePhoneNumber(search: string) {
- const remains = search.replace(/[0-9 +()-]/g, '');
- if (remains !== '') {
- return undefined;
- }
- const digits = search.replace(/[^0-9]/g, '');
- if (digits.length > 16) {
- return undefined;
- }
- if (!search.startsWith('+') && digits.length === 10) {
- return '+1' + digits;
- }
- return '+' + digits;
- }
- public static isValidPhoneNumber(search: string) {
- const remains = search.replace(/[0-9 +()-]/g, '');
- if (remains !== '') {
- return false;
- }
- const digits = search.replace(/[^0-9]/g, '');
- if (digits.length > 16 || digits.length < 3) {
- return false;
- }
- return true;
- }
- public static isValidUserHash(search: string) {
- if (search.match(/Qm[A-HJ-NP-Za-km-z1-9]{44,45}/)) {
- return true;
- }
- }
- public static isValidEmailAddress(search: string) {
- const ats = search.replace(/[^@]/g, '');
- return ats.length === 1;
- }
- public static parseEmail(search: string) {
- const ats = search.replace(/[^@]/g, '');
- if (ats.length === 1) {
- return search.trim();
- } else {
- return undefined;
- }
- }
- public static formatAddress(type: string, address: string) {
- if (type === 'phone' && ContactAddress.isValidPhoneNumber(address)) {
- return ContactAddress.formatPhoneNumber(ContactAddress.parsePhoneNumber(address) || address);
- }
- return address;
- }
- public static formatPhoneNumber(phoneNumber: string): string {
- if (phoneNumber.startsWith('+1') && phoneNumber.length === 12) {
- return '(' + phoneNumber.substring(2, 5) + ') ' + phoneNumber.substring(5, 8) + '-' + phoneNumber.substring(8, 12);
- }
- return phoneNumber;
- }
- public type: string;
- public address: string;
-
- constructor(type: string, address: string) {
- this.type = type;
- this.address = address;
- }
- public matches(search: string): boolean {
- if (this.address.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0) {
- return true;
- }
- return false;
- }
- public matchesExactly(addressType: string, addressValue: string) {
- return (this.type === addressType) && (this.address.toLowerCase().trim() === addressValue.toLowerCase().trim());
- }
- public toPrefixedString() {
- return `${this.type}:${this.address}`;
- }
- public formattedAddress() {
- return ContactAddress.formatAddress(this.type, this.address);
- }
- }
|