diff --git a/packages/loot-core/src/server/importers/ynab5.ts b/packages/loot-core/src/server/importers/ynab5.ts index f1ee2279173..04c8f21af70 100644 --- a/packages/loot-core/src/server/importers/ynab5.ts +++ b/packages/loot-core/src/server/importers/ynab5.ts @@ -39,17 +39,11 @@ 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 - ) { + if (findIdByName(data.category_groups, 'Internal Master Category')) { if ( ynabIncomeCategories.some(ynabIncomeCategory => equalsIgnoreCase(cat.name, ynabIncomeCategory), @@ -61,9 +55,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 +130,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 +253,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 +332,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; +}