contact-address.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 isValidUserHash(search) {
  39. if (search.match(/Qm[A-HJ-NP-Za-km-z1-9]{44,45}/)) {
  40. return true;
  41. }
  42. }
  43. static isValidEmailAddress(search) {
  44. const ats = search.replace(/[^@]/g, '');
  45. return ats.length === 1;
  46. }
  47. static parseEmail(search) {
  48. const ats = search.replace(/[^@]/g, '');
  49. if (ats.length === 1) {
  50. return search.trim();
  51. }
  52. else {
  53. return undefined;
  54. }
  55. }
  56. static formatAddress(type, address) {
  57. if (type === 'phone' && ContactAddress.isValidPhoneNumber(address)) {
  58. return ContactAddress.formatPhoneNumber(ContactAddress.parsePhoneNumber(address) || address);
  59. }
  60. return address;
  61. }
  62. static formatPhoneNumber(phoneNumber) {
  63. if (phoneNumber.startsWith('+1') && phoneNumber.length === 12) {
  64. return '(' + phoneNumber.substring(2, 5) + ') ' + phoneNumber.substring(5, 8) + '-' + phoneNumber.substring(8, 12);
  65. }
  66. return phoneNumber;
  67. }
  68. constructor(type, address) {
  69. this.type = type;
  70. this.address = address;
  71. }
  72. matches(search) {
  73. if (this.address.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. matchesExactly(addressType, addressValue) {
  79. return (this.type === addressType) && (this.address.toLowerCase().trim() === addressValue.toLowerCase().trim());
  80. }
  81. toPrefixedString() {
  82. return `${this.type}:${this.address}`;
  83. }
  84. formattedAddress() {
  85. return ContactAddress.formatAddress(this.type, this.address);
  86. }
  87. }
  88. exports.ContactAddress = ContactAddress;
  89. //# sourceMappingURL=contact-address.js.map