From 010f4d03ae44c22275a77be2409a1f35197db14c Mon Sep 17 00:00:00 2001 From: stefanhall Date: Tue, 9 Jan 2024 09:31:13 +1300 Subject: [PATCH] extract findById into sreusable function to 'simplify' usage --- .../loot-core/src/server/importers/ynab5.ts | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/loot-core/src/server/importers/ynab5.ts b/packages/loot-core/src/server/importers/ynab5.ts index f1ee2279173..3e76b70dfff 100644 --- a/packages/loot-core/src/server/importers/ynab5.ts +++ b/packages/loot-core/src/server/importers/ynab5.ts @@ -39,16 +39,13 @@ async function importCategories( // so it's already handled. const categories = await actual.getCategories(); - const incomeCatId = categories.find(cat => - equalsIgnoreCase(cat.name, 'Income'), - ).id; + const incomeCatId = findIdByName(categories, 'Income'); const ynabIncomeCategories = ['To be Budgeted', 'Inflow: Ready to Assign']; function checkSpecialCat(cat) { if ( - data.category_groups.find(group => - equalsIgnoreCase(group.name, 'Internal Master Category'), - ).id + cat.category_group_id === + findIdByName(data.category_groups, 'Internal Master Category') ) { if ( ynabIncomeCategories.some(ynabIncomeCategory => @@ -61,9 +58,7 @@ async function importCategories( } } else if ( cat.category_group_id === - data.category_groups.find(group => - equalsIgnoreCase(group.name, 'Credit Card Payments'), - ).id + findIdByName(data.category_groups, 'Credit Card Payments') ) { return 'creditCard'; } @@ -138,16 +133,10 @@ async function importTransactions( ) { const payees = await actual.getPayees(); const categories = await actual.getCategories(); - const incomeCatId = categories.find(cat => - equalsIgnoreCase(cat.name, 'Income'), - ).id; - const startingBalanceCatId = categories.find(cat => - equalsIgnoreCase(cat.name, 'Starting Balances'), - ).id; //better way to do it? + const incomeCatId = findIdByName(categories, 'Income'); + const startingBalanceCatId = findIdByName(categories, 'Starting Balances'); //better way to do it? - const startingPayeeYNAB = data.payees.find(payee => - equalsIgnoreCase(payee.name, 'Starting Balance'), - ).id; + const startingPayeeYNAB = findIdByName(data.payees, 'Starting Balance'); const transactionsGrouped = groupBy(data.transactions, 'account_id'); const subtransactionsGrouped = groupBy( @@ -267,12 +256,14 @@ async function importBudgets( const budgets = sortByKey(data.months, 'month'); - const internalCatIdYnab = data.category_groups.find(group => - equalsIgnoreCase(group.name, 'Internal Master Category'), - ).id; - const creditcardCatIdYnab = data.category_groups.find(group => - equalsIgnoreCase(group.name, 'Credit Card Payments'), - ).id; + const internalCatIdYnab = findIdByName( + data.category_groups, + 'Internal Master Category', + ); + const creditcardCatIdYnab = findIdByName( + data.category_groups, + 'Credit Card Payments', + ); await actual.batchBudgetUpdates(async () => { for (const budget of budgets) { @@ -344,3 +335,11 @@ function equalsIgnoreCase(stringa: string, stringb: string): boolean { }) === 0 ); } + +function findByNameIgnoreCase(categories: YNAB5.CategoryGroup[], name: string) { + return categories.find(cat => equalsIgnoreCase(cat.name, name)); +} + +function findIdByName(categories: YNAB5.CategoryGroup[], name: string) { + return findByNameIgnoreCase(categories, name).id; +}