Skip to content

Commit

Permalink
fix: Removed space after colons in account names.
Browse files Browse the repository at this point in the history
Sometimes the android keyboard / autosuggestion adds it.
  • Loading branch information
kajyr committed Nov 29, 2021
1 parent 00d8755 commit d9916cb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
12 changes: 0 additions & 12 deletions frontend/src/helpers/clear-transaction.ts

This file was deleted.

33 changes: 33 additions & 0 deletions frontend/src/helpers/fix-transaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Transaction } from 'pta-journal';

import { clearTransaction, fixColons } from './fix-transaction';

describe("clearTransaction", () => {
test("Clears empty postings", () => {
const trx: Transaction = {
comment: "comment",
date: new Date("2019-01-01"),
description: "string",
entries: [
{ account: "", amount: "" },
{ account: "A", amount: "" },
{ account: "", amount: "123" },
],
};

expect(clearTransaction(trx).entries.length).toBe(2);
});
});

describe("fixColons", () => {
test("Clears empty spaces in accounts", () => {
const trx: Transaction = {
comment: "comment",
date: new Date("2019-01-01"),
description: "string",
entries: [{ account: "Expenses: Groceries", amount: "" }],
};

expect(fixColons(trx).entries[0].account).toBe("Expenses:Groceries");
});
});
32 changes: 32 additions & 0 deletions frontend/src/helpers/fix-transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import dayjs from 'dayjs';
import { Transaction } from 'pta-journal';

type FixTransaction = (trx: Transaction) => Transaction;

// Removes empty entries
export const clearTransaction: FixTransaction = (trx) => {
return {
...trx,
entries: trx.entries.filter((e) => !(e.account == "" && e.amount === "")),
};
};

// Removes empty entries
export const fixColons: FixTransaction = (trx) => {
return {
...trx,
entries: trx.entries.map((e) => ({
...e,
account: e.account.replace(/:\s+/g, ":"),
})),
};
};

const compose =
(...functions) =>
(args) =>
functions.reduceRight((arg, fn) => fn(arg), args);

const fixer: FixTransaction = compose(clearTransaction, fixColons);

export default fixer;
4 changes: 2 additions & 2 deletions frontend/src/pages/dashboard/confirmation-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC } from 'react';

import clearTransaction from 'helpers/clear-transaction';
import fixer from 'helpers/fix-transaction';

import { Button, Code, Group, Modal } from '@mantine/core';

Expand All @@ -11,7 +11,7 @@ const ConfirmationModal: FC<{
onCancel: () => void;
onConfirm: (data: Transaction) => void;
}> = ({ data, onCancel, onConfirm }) => {
const clean = clearTransaction(data);
const clean = fixer(data);
const trxstr = formatTransaction(clean);
return (
<Modal opened={true} onClose={onCancel} title="Confirm transaction">
Expand Down

0 comments on commit d9916cb

Please sign in to comment.