contact-address.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 parseEmail(search) {
  39. const ats = search.replace(/[^@]/g, '');
  40. if (ats.length === 1) {
  41. return search.trim();
  42. }
  43. else {
  44. return undefined;
  45. }
  46. }
  47. static formatPhoneNumber(phoneNumber) {
  48. if (phoneNumber.startsWith('+1') && phoneNumber.length === 12) {
  49. return '(' + phoneNumber.substring(2, 5) + ') ' + phoneNumber.substring(5, 8) + '-' + phoneNumber.substring(8, 12);
  50. }
  51. return phoneNumber;
  52. }
  53. constructor(type, address) {
  54. this.type = type;
  55. this.address = address;
  56. }
  57. matches(search) {
  58. if (this.address.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. matchesExactly(addressType, addressValue) {
  64. return (this.type === addressType) && (this.address.toLowerCase().trim() === addressValue.toLowerCase().trim());
  65. }
  66. toPrefixedString() {
  67. return `${this.type}:${this.address}`;
  68. }
  69. }
  70. exports.ContactAddress = ContactAddress;
  71. //# sourceMappingURL=contact-address.js.map