user 6 лет назад
Родитель
Сommit
57410e1518
4 измененных файлов с 37 добавлено и 13 удалено
  1. 4 2
      lib/contact-book.d.ts
  2. 15 5
      lib/contact-book.js
  3. 1 1
      lib/contact-book.js.map
  4. 17 5
      src/contact-book.ts

+ 4 - 2
lib/contact-book.d.ts

@@ -2,6 +2,8 @@ import { ContactItem } from "./contact-item";
 export declare class ContactBook {
     readonly items: ContactItem[];
     constructor(items: ContactItem[]);
-    getPrimaryEmailAddress(): string | undefined;
-    getPrimaryPhoneNumber(): string | undefined;
+    getPrimaryEmailAddress(): string;
+    getPrimaryPhoneNumber(): string;
+    getFromPhoneNumbers(): string[];
+    getFromEmailAddresses(): string[];
 }

+ 15 - 5
lib/contact-book.js

@@ -5,22 +5,32 @@ class ContactBook {
         this.items = items;
     }
     getPrimaryEmailAddress() {
+        return this.getFromEmailAddresses()[0];
+    }
+    getPrimaryPhoneNumber() {
+        return this.getFromPhoneNumbers()[0];
+    }
+    getFromPhoneNumbers() {
+        const result = [];
         for (const item of this.items) {
             for (const address of item.canSendFromAddrs) {
-                if (address.startsWith('email:')) {
-                    return address;
+                if (address.startsWith('phone:')) {
+                    result.push(address);
                 }
             }
         }
+        return result;
     }
-    getPrimaryPhoneNumber() {
+    getFromEmailAddresses() {
+        const result = [];
         for (const item of this.items) {
             for (const address of item.canSendFromAddrs) {
-                if (address.startsWith('phone:')) {
-                    return address;
+                if (address.startsWith('email:')) {
+                    result.push(address);
                 }
             }
         }
+        return result;
     }
 }
 exports.ContactBook = ContactBook;

Разница между файлами не показана из-за своего большого размера
+ 1 - 1
lib/contact-book.js.map


+ 17 - 5
src/contact-book.ts

@@ -9,23 +9,35 @@ export class ContactBook {
   }
 
   public getPrimaryEmailAddress() {
+    return this.getFromEmailAddresses()[0];
+  }
+
+  public getPrimaryPhoneNumber() {
+    return this.getFromPhoneNumbers()[0];
+  }
+
+  public getFromPhoneNumbers() {
+    const result: string[] = [];
     for (const item of this.items) {
       for (const address of item.canSendFromAddrs) {
-        if (address.startsWith('email:')) {
-          return address;
+        if (address.startsWith('phone:')) {
+          result.push(address);
         }
       }
     }
+    return result;
   }
 
-  public getPrimaryPhoneNumber() {
+  public getFromEmailAddresses() {
+    const result: string[] = [];
     for (const item of this.items) {
       for (const address of item.canSendFromAddrs) {
-        if (address.startsWith('phone:')) {
-          return address;
+        if (address.startsWith('email:')) {
+          result.push(address);
         }
       }
     }
+    return result;
   }