From 32e6f67216d6aeacb6de9750478e9324a1c80894 Mon Sep 17 00:00:00 2001 From: Tuhin Ghose <71938696+ghosetuhin@users.noreply.github.com> Date: Wed, 6 Dec 2023 03:26:17 -0500 Subject: [PATCH] [Maintenance] Migrating util.js and chartTheme.js to typescript (#2009) --- .../{chart-theme.js => chart-theme.ts} | 4 +- .../src/components/reports/util.js | 44 -------------- .../src/components/reports/util.ts | 57 +++++++++++++++++++ upcoming-release-notes/2009.md | 6 ++ 4 files changed, 65 insertions(+), 46 deletions(-) rename packages/desktop-client/src/components/reports/{chart-theme.js => chart-theme.ts} (96%) delete mode 100644 packages/desktop-client/src/components/reports/util.js create mode 100644 packages/desktop-client/src/components/reports/util.ts create mode 100644 upcoming-release-notes/2009.md diff --git a/packages/desktop-client/src/components/reports/chart-theme.js b/packages/desktop-client/src/components/reports/chart-theme.ts similarity index 96% rename from packages/desktop-client/src/components/reports/chart-theme.js rename to packages/desktop-client/src/components/reports/chart-theme.ts index f525d3c0cd8..b34f6f62033 100644 --- a/packages/desktop-client/src/components/reports/chart-theme.js +++ b/packages/desktop-client/src/components/reports/chart-theme.ts @@ -112,8 +112,8 @@ export const chartTheme = { }, }; -export function getColorScale(name) { - const scales = { +export function getColorScale(name: string): string[] { + const scales: Record = { grayscale: ['#cccccc', '#969696', '#636363', '#252525'], qualitative: [ '#45B29D', //Dark Teal diff --git a/packages/desktop-client/src/components/reports/util.js b/packages/desktop-client/src/components/reports/util.js deleted file mode 100644 index d6ad29ffa14..00000000000 --- a/packages/desktop-client/src/components/reports/util.js +++ /dev/null @@ -1,44 +0,0 @@ -import { runQuery } from 'loot-core/src/client/query-helpers'; - -export function fromDateRepr(date) { - return date.slice(0, 7); -} - -export async function runAll(queries, cb) { - const data = await Promise.all( - queries.map(q => { - return runQuery(q).then(({ data }) => data); - }), - ); - cb(data); -} - -export function index(data, field, mapper) { - const result = {}; - data.forEach(item => { - result[mapper ? mapper(item[field]) : item[field]] = item; - }); - return result; -} - -export function indexStack(data, fieldName, field) { - const result = {}; - data.forEach(item => { - result[item[fieldName]] = item[field]; - }); - return result; -} - -export function indexCashFlow(data, date, isTransfer) { - const results = {}; - data.forEach(item => { - const findExisting = results[item.date] - ? results[item.date][item.isTransfer] - ? results[item.date][item.isTransfer] - : 0 - : 0; - const result = { [item[isTransfer]]: item.amount + findExisting }; - results[item[date]] = { ...results[item[date]], ...result }; - }); - return results; -} diff --git a/packages/desktop-client/src/components/reports/util.ts b/packages/desktop-client/src/components/reports/util.ts new file mode 100644 index 00000000000..4d28f2ee4d4 --- /dev/null +++ b/packages/desktop-client/src/components/reports/util.ts @@ -0,0 +1,57 @@ +import { runQuery } from 'loot-core/src/client/query-helpers'; +import type { Query } from 'loot-core/src/shared/query'; + +export function fromDateRepr(date: string): string { + return date.slice(0, 7); +} + +export async function runAll( + queries: Query[], + cb: (data) => void, +): Promise { + const data = await Promise.all( + queries.map(q => { + return runQuery(q).then(({ data }) => data); + }), + ); + cb(data); +} + +export function index< + T extends Record, + K extends keyof T, +>(data: T[], field: K) { + const result: Record = {}; + data.forEach(item => { + const key = item[field]; + result[key] = item; + }); + return result; +} + +export function indexStack< + T extends Record, + K extends keyof T, +>(data: T[], fieldName: K, field: K) { + const result: Record = {}; + data.forEach(item => { + result[item[fieldName]] = item[field]; + }); + return result; +} + +export function indexCashFlow< + T extends { date: string; isTransfer: boolean; amount: number }, +>(data: T[], date: string, isTransfer: string) { + const results = {}; + data.forEach(item => { + const findExisting = results[item.date] + ? results[item.date][item.isTransfer] + ? results[item.date][item.isTransfer] + : 0 + : 0; + const result = { [item[isTransfer]]: item.amount + findExisting }; + results[item[date]] = { ...results[item[date]], ...result }; + }); + return results; +} diff --git a/upcoming-release-notes/2009.md b/upcoming-release-notes/2009.md new file mode 100644 index 00000000000..43b60f981d9 --- /dev/null +++ b/upcoming-release-notes/2009.md @@ -0,0 +1,6 @@ +--- +category: Maintenance +authors: [ghosetuhin] +--- + +Migrating the util.js and chartTheme.js files to typescript \ No newline at end of file