Skip to content

Commit

Permalink
feat: parse journal file
Browse files Browse the repository at this point in the history
  • Loading branch information
kajyr committed Nov 5, 2021
1 parent 35f2af5 commit 97fde99
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src
rollup.config.js
tsconfig.json
import-sorter.json
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
coveragePathIgnorePatterns: ["/node_modules/", "/build/"],
moduleFileExtensions: ["js", "jsx", "ts", "tsx"],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
setupFiles: [],
testMatch: ["**/*.test.ts", "**/*.test.tsx", "**/*.test.js"],
testURL: "http://localhost/",
transform: {
"^.+\\.tsx?$": "ts-jest",
},
};
74 changes: 73 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "build/index.js",
"scripts": {
"build": "rimraf ./build && rollup -c rollup.config.js",
"watch": "rollup -w -c rollup.config.js"
"watch": "rollup -w -c rollup.config.js",
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -19,12 +20,18 @@
"homepage": "https://github.com/kajyr/pta-js#readme",
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"@types/jest": "^27.0.2",
"@types/node": "^16.11.6",
"@types/split2": "^3.2.1",
"jest": "^27.3.1",
"rimraf": "^3.0.2",
"rollup": "^2.59.0",
"rollup-plugin-typescript2": "^0.30.0",
"ts-jest": "^27.0.7",
"tslib": "^2.3.1",
"typescript": "^4.4.4"
},
"dependencies": {
"split2": "^4.1.0"
}
}
20 changes: 20 additions & 0 deletions src/__mocks__/prova.journal
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
~ Monthly
Expenses:Droga 15 EUR
Assets:Banca

2021-01-01 * Opening Balance
Assets:Banca 1000 EUR
Equity:Opening Balances

2021-06-02 Nulla
Assets:Banca 20 EUR
Expenses:Droga -20 EUR

2021-11-02 Nulla
Assets:Banca 40 EUR
Expenses:Droga -40 EUR


2021-11-04 Pino
34 EUR

14 changes: 14 additions & 0 deletions src/__mocks__/string-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Stream } from 'stream';

function mockStream(str: string) {
const stream = new Stream.Readable();

stream._read = function () {
this.push(str);
this.push(null);
};

return stream;
}

export default mockStream;
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import formatTransaction from './format-transaction';
import parse from './parse';
import { Transaction } from './types';

export { formatTransaction, Transaction };
export { formatTransaction, Transaction, parse };
112 changes: 112 additions & 0 deletions src/parse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { createReadStream } from 'fs';

import mockStream from './__mocks__/string-stream';
import parse, { parseEntryLine, parseHeaderLine } from './parse';

describe("parseHeaderLine", () => {
it("Confirmed and description", () => {
expect(parseHeaderLine("2021-11-02 * Description")).toEqual({
date: new Date("2021-11-02"),
confirmed: true,
description: "Description",
});
});

it("Not confirmed", () => {
expect(parseHeaderLine("2021-11-02 Foo")).toEqual({
date: new Date("2021-11-02"),
confirmed: false,
description: "Foo",
});
});

it("No description", () => {
expect(parseHeaderLine("2021-11-02")).toEqual({
date: new Date("2021-11-02"),
confirmed: false,
description: "",
});
});
});

describe("parseEntryLine", () => {
test("Line with commodity", () => {
expect(parseEntryLine("Assets:Crypto:Coinbase 1942.96 EUR")).toEqual({
account: "Assets:Crypto:Coinbase",
amount: "1942.96",
commodity: "EUR",
});
});
test("Line with just account", () => {
expect(parseEntryLine("Assets:Crypto:Coinbase")).toEqual({
account: "Assets:Crypto:Coinbase",
amount: undefined,
commodity: undefined,
});
});

test("Line with just value", () => {
expect(parseEntryLine("Assets:Bank 34.00")).toEqual({
account: "Assets:Bank",
amount: "34.00",
commodity: undefined,
});
});

test("Line with conversion data", () => {
expect(parseEntryLine("Assets:Crypto -8.00 LTC @ 173.41 EUR")).toEqual(
{
account: "Assets:Crypto",
amount: "-8.00",
commodity: "LTC",
conversion: { amount: "173.41", commodity: "EUR" },
}
);
});
});

describe("parse", () => {
test("it works with file streams", async () => {
const readStream = createReadStream(`src/__mocks__/prova.journal`);

const p = await parse(readStream);

expect(p.length).toBe(4);
});

test("it works with string streams", async () => {
const stream = mockStream(`
2021-11-02 * Some shopping
Assets:Crypto:Coinbase -8.00 LTC @ 173.41 EUR
Assets:Crypto:Coinbase 1382.42 EUR
Expenses:Fees:Coinbase
2021-11-02
Assets:Crypto:Coinbase -0.5 ETH @ 3899.56 EUR
Assets:Crypto:Coinbase 1942.96 EUR
Expenses:Fees:Coinbase
`);

const p = await parse(stream);

expect(p.length).toBe(2);
const [first] = p;

expect(first.date).toStrictEqual(new Date("2021-11-02"));
expect(first.confirmed).toBe(true);
expect(first.description).toBe("Some shopping");
expect(first.entries.length).toBe(3);

expect(first.entries[0]).toEqual({
account: "Assets:Crypto:Coinbase",
amount: "-8.00",
commodity: "LTC",
conversion: {
amount: "173.41",
commodity: "EUR",
},
});
});
});
Loading

0 comments on commit 97fde99

Please sign in to comment.