Skip to content

Commit

Permalink
[Maintenance] Migrating util.js and chartTheme.js to typescript (#2009)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghosetuhin authored Dec 6, 2023
1 parent 7174fcc commit cde4551
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export const chartTheme = {
},
};

export function getColorScale(name) {
const scales = {
export function getColorScale(name: string): string[] {
const scales: Record<string, string[]> = {
grayscale: ['#cccccc', '#969696', '#636363', '#252525'],
qualitative: [
'#45B29D', //Dark Teal
Expand Down
44 changes: 0 additions & 44 deletions packages/desktop-client/src/components/reports/util.js

This file was deleted.

57 changes: 57 additions & 0 deletions packages/desktop-client/src/components/reports/util.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const data = await Promise.all(
queries.map(q => {
return runQuery(q).then(({ data }) => data);
}),
);
cb(data);
}

export function index<
T extends Record<string, string | number>,
K extends keyof T,
>(data: T[], field: K) {
const result: Record<string | number, T> = {};
data.forEach(item => {
const key = item[field];
result[key] = item;
});
return result;
}

export function indexStack<
T extends Record<string, string | number>,
K extends keyof T,
>(data: T[], fieldName: K, field: K) {
const result: Record<string | number, T[K]> = {};
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;
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/2009.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [ghosetuhin]
---

Migrating the util.js and chartTheme.js files to typescript

0 comments on commit cde4551

Please sign in to comment.