From b173e356626e688324feaee69173fa0ccc389e29 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 23 Jul 2025 08:23:21 -0700 Subject: [PATCH] Refactor account import to reduce repeated code. --- src/lib/account.ts | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index 360af73..8fb93ea 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -148,26 +148,17 @@ export class Account { * @returns {Promise} Promise for array of new Account objects */ static async import (keypairs: KeyPair[], password: Key): Promise - static import (input: Key | Key[] | KeyPair | KeyPair[], password?: Key): Account | Account[] | Promise { - if (Array.isArray(input)) { - if (this.#isKeyPairs(input) && password != null) { - return new Promise((resolve, reject): void => { - this.#fromPrivate(input, password) - .then(r => resolve(r)) - .catch(e => reject(e)) - }) - } - return this.#fromPublic(input) + static import (input: Key | KeyPair | (Key | KeyPair)[], password?: Key): Account | Account[] | Promise { + const isInputArray = Array.isArray(input) + const inputs = isInputArray ? input : [input] + if (this.#isKeyPairs(inputs) && password != null) { + return new Promise((resolve, reject): void => { + this.#fromPrivate(inputs, password) + .then(r => resolve(isInputArray ? r : r[0])) + .catch(e => reject(e)) + }) } else { - const inputs = [input] - if (this.#isKeyPairs(inputs) && password != null) { - return new Promise((resolve, reject): void => { - this.#fromPrivate(inputs, password) - .then(r => resolve(r[0])) - .catch(e => reject(e)) - }) - } - return this.#fromPublic(inputs)[0] + return isInputArray ? this.#fromPublic(inputs) : this.#fromPublic(inputs)[0] } } -- 2.47.3