contact-address.js 2.7 KB

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