user преди 6 години
родител
ревизия
84d98d78d8
променени са 11 файла, в които са добавени 111 реда и са изтрити 27 реда
  1. 3 0
      lib/contact-address.d.ts
  2. 29 0
      lib/contact-address.js
  3. 1 1
      lib/contact-address.js.map
  4. 5 4
      lib/contact-book.js
  5. 1 1
      lib/contact-book.js.map
  6. 2 1
      lib/contact-item.d.ts
  7. 14 6
      lib/contact-item.js
  8. 1 1
      lib/contact-item.js.map
  9. 32 0
      src/contact-address.ts
  10. 7 6
      src/contact-book.ts
  11. 16 7
      src/contact-item.ts

+ 3 - 0
lib/contact-address.d.ts

@@ -1,5 +1,8 @@
 export declare class ContactAddress {
     static fromPrefixedString(prefixed: string): ContactAddress;
+    static parsePhoneNumber(search: string): string | undefined;
+    static parseEmail(search: string): string | undefined;
+    static formatPhoneNumber(phoneNumber: string): string;
     type: string;
     address: string;
     constructor(type: string, address: string);

+ 29 - 0
lib/contact-address.js

@@ -10,6 +10,35 @@ class ContactAddress {
         const unprefixed = components.join(':');
         return new ContactAddress(type, unprefixed);
     }
+    static parsePhoneNumber(search) {
+        const remains = search.replace(/[0-9 +()-]/g, '');
+        if (remains !== '') {
+            return undefined;
+        }
+        const digits = search.replace(/[^0-9]/g, '');
+        if (digits.length > 16) {
+            return undefined;
+        }
+        if (!search.startsWith('+') && digits.length === 10) {
+            return '+1' + digits;
+        }
+        return '+' + digits;
+    }
+    static parseEmail(search) {
+        const ats = search.replace(/[^@]/g, '');
+        if (ats.length === 1) {
+            return search.trim();
+        }
+        else {
+            return undefined;
+        }
+    }
+    static formatPhoneNumber(phoneNumber) {
+        if (phoneNumber.startsWith('+1') && phoneNumber.length === 12) {
+            return '(' + phoneNumber.substring(2, 5) + ') ' + phoneNumber.substring(5, 8) + '-' + phoneNumber.substring(8, 12);
+        }
+        return phoneNumber;
+    }
     constructor(type, address) {
         this.type = type;
         this.address = address;

Файловите разлики са ограничени, защото са твърде много
+ 1 - 1
lib/contact-address.js.map


+ 5 - 4
lib/contact-book.js

@@ -1,5 +1,6 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
+const contact_address_1 = require("./contact-address");
 class ContactBook {
     constructor(items) {
         this.items = items;
@@ -14,8 +15,8 @@ class ContactBook {
         const result = [];
         for (const item of this.items) {
             for (const address of item.canSendFromAddrs) {
-                if (address.startsWith('phone:')) {
-                    result.push(address);
+                if (address.type === 'phone') {
+                    result.push(contact_address_1.ContactAddress.formatPhoneNumber(address.address));
                 }
             }
         }
@@ -25,8 +26,8 @@ class ContactBook {
         const result = [];
         for (const item of this.items) {
             for (const address of item.canSendFromAddrs) {
-                if (address.startsWith('email:')) {
-                    result.push(address);
+                if (address.type === 'email') {
+                    result.push(address.address);
                 }
             }
         }

Файловите разлики са ограничени, защото са твърде много
+ 1 - 1
lib/contact-book.js.map


+ 2 - 1
lib/contact-item.d.ts

@@ -8,13 +8,14 @@ export declare class ContactItem {
     groups: string[];
     names: string[];
     notes: string;
-    canSendFromAddrs: string[];
+    canSendFromAddrs: ContactAddress[];
     constructor(props: ContactParams);
     matchesGroup(group: string): boolean;
     getFirstEmail(): string | undefined;
     getFirstPhone(): string | undefined;
     search(search: string): boolean;
     matchesAddressExactly(addressType: string, addressValue: string): boolean;
+    generateName(): string;
     getName(): string;
     getData(): ContactParams;
 }

+ 14 - 6
lib/contact-item.js

@@ -6,10 +6,13 @@ class ContactItem {
         this.data = props;
         this.hash = props.hash;
         this.id = props.id;
-        this.canSendFromAddrs = props.canSendFromAddrs || [];
+        this.canSendFromAddrs = (props.canSendFromAddrs || []).map(addrString => contact_address_1.ContactAddress.fromPrefixedString(addrString));
         this.addrs = (props.addrs || []).map(addrString => contact_address_1.ContactAddress.fromPrefixedString(addrString));
         this.groups = props.groups || [];
         this.names = props.names || [];
+        if (this.names.length === 0) {
+            this.names.push(this.generateName());
+        }
         this.notes = props.notes || '';
     }
     matchesGroup(group) {
@@ -47,10 +50,7 @@ class ContactItem {
         }
         return false;
     }
-    getName() {
-        if (this.names.length > 0) {
-            return this.names[0];
-        }
+    generateName() {
         const firstEmail = this.getFirstEmail();
         if (firstEmail != null) {
             return firstEmail;
@@ -61,10 +61,18 @@ class ContactItem {
         }
         return 'New contact ' + this.id.substring(0, 8);
     }
+    getName() {
+        if (this.names.length > 0) {
+            return this.names[0];
+        }
+        else {
+            return '';
+        }
+    }
     getData() {
         return {
             addrs: this.addrs.map(addr => addr.toPrefixedString()),
-            canSendFromAddrs: this.canSendFromAddrs,
+            canSendFromAddrs: this.canSendFromAddrs.map(addr => addr.toPrefixedString()),
             groups: this.groups,
             hash: this.hash,
             id: this.id,

Файловите разлики са ограничени, защото са твърде много
+ 1 - 1
lib/contact-item.js.map


+ 32 - 0
src/contact-address.ts

@@ -10,6 +10,38 @@ export class ContactAddress {
     return new ContactAddress(type, unprefixed);
   }
 
+  public static parsePhoneNumber(search: string) {
+    const remains = search.replace(/[0-9 +()-]/g, '');
+    if (remains !== '') {
+      return undefined;
+    }
+
+    const digits = search.replace(/[^0-9]/g, '');
+    if (digits.length > 16) {
+      return undefined;
+    }
+    if (!search.startsWith('+') && digits.length === 10) {
+      return '+1' + digits;
+    }
+    return '+' + digits;
+  }
+
+  public static parseEmail(search: string) {
+    const ats = search.replace(/[^@]/g, '');
+    if (ats.length === 1) {
+      return search.trim();
+    } else {
+      return undefined;
+    }
+  }
+
+  public static formatPhoneNumber(phoneNumber: string): string {
+    if (phoneNumber.startsWith('+1') && phoneNumber.length === 12) {
+      return '(' + phoneNumber.substring(2, 5) + ') ' + phoneNumber.substring(5, 8) + '-' + phoneNumber.substring(8, 12);
+    }
+    return phoneNumber;
+  }
+
   public type: string;
   public address: string;
   

+ 7 - 6
src/contact-book.ts

@@ -1,4 +1,5 @@
 import { ContactItem } from "./contact-item";
+import { ContactAddress } from "./contact-address";
 
 export class ContactBook {
 
@@ -16,24 +17,24 @@ export class ContactBook {
     return this.getFromPhoneNumbers()[0];
   }
 
-  public getFromPhoneNumbers() {
+  public getFromPhoneNumbers(): string[] {
     const result: string[] = [];
     for (const item of this.items) {
       for (const address of item.canSendFromAddrs) {
-        if (address.startsWith('phone:')) {
-          result.push(address);
+        if (address.type === 'phone') {
+          result.push(ContactAddress.formatPhoneNumber(address.address));
         }
       }
     }
     return result;
   }
 
-  public getFromEmailAddresses() {
+  public getFromEmailAddresses(): string[] {
     const result: string[] = [];
     for (const item of this.items) {
       for (const address of item.canSendFromAddrs) {
-        if (address.startsWith('email:')) {
-          result.push(address);
+        if (address.type === 'email') {
+          result.push(address.address);
         }
       }
     }

+ 16 - 7
src/contact-item.ts

@@ -10,17 +10,20 @@ export class ContactItem {
   public groups: string[];
   public names: string[];
   public notes: string;
-  public canSendFromAddrs: string[];
+  public canSendFromAddrs: ContactAddress[];
   
   
   constructor(props: ContactParams) {
     this.data = props;
     this.hash = props.hash;
     this.id = props.id;
-    this.canSendFromAddrs = props.canSendFromAddrs || [];
+    this.canSendFromAddrs = (props.canSendFromAddrs || []).map(addrString => ContactAddress.fromPrefixedString(addrString));
     this.addrs = (props.addrs || []).map(addrString => ContactAddress.fromPrefixedString(addrString));
     this.groups = props.groups || [];
     this.names = props.names || [];
+    if (this.names.length === 0) {
+      this.names.push(this.generateName());
+    }
     this.notes = props.notes || '';
   }
 
@@ -64,10 +67,7 @@ export class ContactItem {
     return false;
   }
 
-  public getName(): string {
-    if (this.names.length > 0) {
-      return this.names[0];
-    }
+  public generateName() {
     const firstEmail = this.getFirstEmail();
     if (firstEmail != null) {
       return firstEmail;
@@ -79,10 +79,19 @@ export class ContactItem {
     return 'New contact ' + this.id.substring(0,8);
   }
 
+  public getName(): string {
+    if (this.names.length > 0) {
+      return this.names[0];
+    } else {
+      return '';
+    }
+    
+  }
+
   public getData(): ContactParams {
     return {
       addrs: this.addrs.map(addr => addr.toPrefixedString()),
-      canSendFromAddrs: this.canSendFromAddrs,
+      canSendFromAddrs: this.canSendFromAddrs.map(addr => addr.toPrefixedString()),
       groups: this.groups,
       hash: this.hash,
       id: this.id,