contact-address.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 parseEmail(search) {
  28. const ats = search.replace(/[^@]/g, '');
  29. if (ats.length === 1) {
  30. return search.trim();
  31. }
  32. else {
  33. return undefined;
  34. }
  35. }
  36. static formatPhoneNumber(phoneNumber) {
  37. if (phoneNumber.startsWith('+1') && phoneNumber.length === 12) {
  38. return '(' + phoneNumber.substring(2, 5) + ') ' + phoneNumber.substring(5, 8) + '-' + phoneNumber.substring(8, 12);
  39. }
  40. return phoneNumber;
  41. }
  42. constructor(type, address) {
  43. this.type = type;
  44. this.address = address;
  45. }
  46. matches(search) {
  47. if (this.address.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. matchesExactly(addressType, addressValue) {
  53. return (this.type === addressType) && (this.address.toLowerCase().trim() === addressValue.toLowerCase().trim());
  54. }
  55. toPrefixedString() {
  56. return `${this.type}:${this.address}`;
  57. }
  58. }
  59. exports.ContactAddress = ContactAddress;
  60. //# sourceMappingURL=contact-address.js.map