contact-address.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. export class ContactAddress {
  2. public static fromPrefixedString(prefixed: string) {
  3. const components = prefixed.split(/:/);
  4. const type = components.shift();
  5. if (type == null) {
  6. throw new Error('Invalid input: ' + prefixed);
  7. }
  8. const unprefixed = components.join(':');
  9. return new ContactAddress(type, unprefixed);
  10. }
  11. public static parsePhoneNumber(search: string) {
  12. const remains = search.replace(/[0-9 +()-]/g, '');
  13. if (remains !== '') {
  14. return undefined;
  15. }
  16. const digits = search.replace(/[^0-9]/g, '');
  17. if (digits.length > 16) {
  18. return undefined;
  19. }
  20. if (!search.startsWith('+') && digits.length === 10) {
  21. return '+1' + digits;
  22. }
  23. return '+' + digits;
  24. }
  25. public static isValidPhoneNumber(search: string) {
  26. const remains = search.replace(/[0-9 +()-]/g, '');
  27. if (remains !== '') {
  28. return false;
  29. }
  30. const digits = search.replace(/[^0-9]/g, '');
  31. if (digits.length > 16 || digits.length < 3) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. public static isValidUserHash(search: string) {
  37. if (search.match(/Qm[A-HJ-NP-Za-km-z1-9]{44,45}/)) {
  38. return true;
  39. }
  40. }
  41. public static isValidEmailAddress(search: string) {
  42. const ats = search.replace(/[^@]/g, '');
  43. return ats.length === 1;
  44. }
  45. public static parseEmail(search: string) {
  46. const ats = search.replace(/[^@]/g, '');
  47. if (ats.length === 1) {
  48. return search.trim();
  49. } else {
  50. return undefined;
  51. }
  52. }
  53. public static formatAddress(type: string, address: string) {
  54. if (type === 'phone' && ContactAddress.isValidPhoneNumber(address)) {
  55. return ContactAddress.formatPhoneNumber(ContactAddress.parsePhoneNumber(address) || address);
  56. }
  57. return address;
  58. }
  59. public static formatPhoneNumber(phoneNumber: string): string {
  60. if (phoneNumber.startsWith('+1') && phoneNumber.length === 12) {
  61. return '(' + phoneNumber.substring(2, 5) + ') ' + phoneNumber.substring(5, 8) + '-' + phoneNumber.substring(8, 12);
  62. }
  63. return phoneNumber;
  64. }
  65. public type: string;
  66. public address: string;
  67. constructor(type: string, address: string) {
  68. this.type = type;
  69. this.address = address;
  70. }
  71. public matches(search: string): boolean {
  72. if (this.address.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0) {
  73. return true;
  74. }
  75. return false;
  76. }
  77. public matchesExactly(addressType: string, addressValue: string) {
  78. return (this.type === addressType) && (this.address.toLowerCase().trim() === addressValue.toLowerCase().trim());
  79. }
  80. public toPrefixedString() {
  81. return `${this.type}:${this.address}`;
  82. }
  83. public formattedAddress() {
  84. return ContactAddress.formatAddress(this.type, this.address);
  85. }
  86. }