contact-item.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const contact_address_1 = require("./contact-address");
  4. class ContactItem {
  5. constructor(props) {
  6. this.data = props;
  7. this.hash = props.hash;
  8. this.id = props.id;
  9. this.canSendFromAddrs = (props.canSendFromAddrs || []).map(addrString => contact_address_1.ContactAddress.fromPrefixedString(addrString));
  10. this.addrs = (props.addrs || []).map(addrString => contact_address_1.ContactAddress.fromPrefixedString(addrString));
  11. this.groups = props.groups || [];
  12. this.names = props.names || [];
  13. if (this.names.length === 0) {
  14. this.names.push(this.generateName());
  15. }
  16. this.notes = props.notes || '';
  17. }
  18. matchesGroup(group) {
  19. if (this.groups.filter(x => x === group.trim())[0].length > 0) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. getFirstEmail() {
  25. const first = this.addrs.filter(addr => addr.type === 'email')[0];
  26. if (first == null) {
  27. return undefined;
  28. }
  29. return first.address;
  30. }
  31. getFirstPhone() {
  32. const first = this.addrs.filter(addr => addr.type === 'phone')[0];
  33. if (first == null) {
  34. return undefined;
  35. }
  36. return first.address;
  37. }
  38. search(search) {
  39. if (this.names.find(i => i.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0)) {
  40. return true;
  41. }
  42. if (this.addrs.find(addr => addr.matches(search))) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. getMatchingAddresses(search) {
  48. return this.addrs.filter(addr => addr.matches(search));
  49. }
  50. matchesAddressExactly(addressType, addressValue) {
  51. if (this.addrs.find(addr => addr.matchesExactly(addressType, addressValue))) {
  52. return true;
  53. }
  54. return false;
  55. }
  56. generateName() {
  57. const firstEmail = this.getFirstEmail();
  58. if (firstEmail != null) {
  59. return firstEmail;
  60. }
  61. const firstPhone = this.getFirstPhone();
  62. if (firstPhone != null) {
  63. return firstPhone;
  64. }
  65. return 'New contact ' + this.id.substring(0, 8);
  66. }
  67. getName() {
  68. if (this.names.length > 0) {
  69. return this.names[0];
  70. }
  71. else {
  72. return '';
  73. }
  74. }
  75. getData() {
  76. return {
  77. addrs: this.addrs.map(addr => addr.toPrefixedString()),
  78. canSendFromAddrs: this.canSendFromAddrs.map(addr => addr.toPrefixedString()),
  79. groups: this.groups,
  80. hash: this.hash,
  81. id: this.id,
  82. names: this.names,
  83. notes: this.notes
  84. };
  85. }
  86. }
  87. exports.ContactItem = ContactItem;
  88. //# sourceMappingURL=contact-item.js.map