]> git.codecow.com Git - libnemo.git/commitdiff
Refactor account import to reduce repeated code.
authorChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 15:23:21 +0000 (08:23 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 15:23:21 +0000 (08:23 -0700)
src/lib/account.ts

index 360af735d54d33cc512037f4663f58ae76be6059..8fb93eae3c0f566ef75d4e11120b1bab0cd54ee0 100644 (file)
@@ -148,26 +148,17 @@ export class Account {
        * @returns {Promise<Account[]>} Promise for array of new Account objects\r
        */\r
        static async import (keypairs: KeyPair[], password: Key): Promise<Account[]>\r
-       static import (input: Key | Key[] | KeyPair | KeyPair[], password?: Key): Account | Account[] | Promise<Account | Account[]> {\r
-               if (Array.isArray(input)) {\r
-                       if (this.#isKeyPairs(input) && password != null) {\r
-                               return new Promise((resolve, reject): void => {\r
-                                       this.#fromPrivate(input, password)\r
-                                               .then(r => resolve(r))\r
-                                               .catch(e => reject(e))\r
-                               })\r
-                       }\r
-                       return this.#fromPublic(input)\r
+       static import (input: Key | KeyPair | (Key | KeyPair)[], password?: Key): Account | Account[] | Promise<Account | Account[]> {\r
+               const isInputArray = Array.isArray(input)\r
+               const inputs = isInputArray ? input : [input]\r
+               if (this.#isKeyPairs(inputs) && password != null) {\r
+                       return new Promise((resolve, reject): void => {\r
+                               this.#fromPrivate(inputs, password)\r
+                                       .then(r => resolve(isInputArray ? r : r[0]))\r
+                                       .catch(e => reject(e))\r
+                       })\r
                } else {\r
-                       const inputs = [input]\r
-                       if (this.#isKeyPairs(inputs) && password != null) {\r
-                               return new Promise((resolve, reject): void => {\r
-                                       this.#fromPrivate(inputs, password)\r
-                                               .then(r => resolve(r[0]))\r
-                                               .catch(e => reject(e))\r
-                               })\r
-                       }\r
-                       return this.#fromPublic(inputs)[0]\r
+                       return isInputArray ? this.#fromPublic(inputs) : this.#fromPublic(inputs)[0]\r
                }\r
        }\r
 \r