Comparing version 1.5.2 to 1.6.0
/// <reference types="node" /> | ||
import { Readable } from 'stream'; | ||
import { Readable } from "stream"; | ||
export declare function unique<T>(list: T[]): T[]; | ||
export declare function collect<T>(readable: Readable): Promise<T[]>; | ||
export declare const groupBy: <T, K extends string | number | symbol>(arr: T[], key: (i: T) => K) => Record<K, T[]>; |
@@ -1,8 +0,9 @@ | ||
import Formatter from './formatter'; | ||
import formatComment from './formatter/format-comment'; | ||
import formatTransaction from './formatter/format-transaction'; | ||
import parse, { ParseResult } from './parse'; | ||
import Parser from './parser'; | ||
import { isComment, isDirective, isPosting, isTransaction } from './type-guards'; | ||
import { Comment, Journal, Posting, Transaction } from './types'; | ||
import Formatter from "./formatter"; | ||
import formatComment from "./formatter/format-comment"; | ||
import formatTransaction from "./formatter/format-transaction"; | ||
import parse, { ParseResult } from "./parse"; | ||
import Parser from "./parser"; | ||
import { isComment, isDirective, isPosting, isTransaction } from "./type-guards"; | ||
import { Comment, Journal, Posting, Transaction } from "./types"; | ||
import { groupMonthly } from "./group"; | ||
/** | ||
@@ -12,2 +13,2 @@ * @deprecated | ||
declare type TransactionEntry = Posting; | ||
export { Comment, formatComment, Formatter, formatTransaction, isComment, isDirective, isPosting, isTransaction, Journal, parse, Parser, ParseResult, Posting, Transaction, TransactionEntry, }; | ||
export { Comment, formatComment, Formatter, formatTransaction, groupMonthly, isComment, isDirective, isPosting, isTransaction, Journal, parse, Parser, ParseResult, Posting, Transaction, TransactionEntry, }; |
@@ -12,3 +12,3 @@ 'use strict'; | ||
/*! ***************************************************************************** | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
@@ -105,2 +105,12 @@ | ||
function __spreadArray(to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
} | ||
function __asyncValues(o) { | ||
@@ -439,3 +449,3 @@ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
function isDate(str) { | ||
return /^\d{4}-\d{2}-\d{2}$/.test(str); | ||
return /^\d{4}-\d{2}-\d{2}$/.test(str) || /^\d{4}\/\d{2}\/\d{2}$/.test(str); | ||
} | ||
@@ -551,2 +561,81 @@ function isComment(str) { | ||
function sumPostings(trx) { | ||
return trx.entries.reduce(function (acc, entry) { | ||
if (isPosting(entry) && entry.amount) { | ||
var commodity = entry.commodity || "_"; | ||
if (!acc[commodity]) { | ||
acc[commodity] = 0; | ||
} | ||
acc[commodity] = acc[commodity] + Number(entry.amount); | ||
return acc; | ||
} | ||
return acc; | ||
}, {}); | ||
} | ||
function fillAmounts(trx) { | ||
var catchAllList = trx.entries.filter(function (e) { return isPosting(e) && !e.amount; }); | ||
if (catchAllList.length === 0) { | ||
// every posting is already filled | ||
return trx; | ||
} | ||
if (catchAllList.length > 1) { | ||
// more than one posting without amount | ||
throw new Error("Only one posting can have no amount"); | ||
} | ||
var totals = sumPostings(trx); | ||
var catchAll = catchAllList[0]; | ||
var entries = __spreadArray([], trx.entries.filter(function (e) { return e !== catchAll; }), true); | ||
for (var _i = 0, _a = Object.entries(totals); _i < _a.length; _i++) { | ||
var _b = _a[_i], commodity = _b[0], value = _b[1]; | ||
entries.push(__assign(__assign({}, catchAll), { commodity: commodity, amount: -1 * value })); | ||
} | ||
return __assign(__assign({}, trx), { entries: entries }); | ||
} | ||
function makePivot(journal) { | ||
var map = {}; | ||
for (var _i = 0, journal_1 = journal; _i < journal_1.length; _i++) { | ||
var trx = journal_1[_i]; | ||
if (!isTransaction(trx)) { | ||
continue; | ||
} | ||
var filled = fillAmounts(trx); | ||
for (var _a = 0, _b = filled.entries; _a < _b.length; _a++) { | ||
var posting = _b[_a]; | ||
if (!isPosting(posting)) { | ||
continue; | ||
} | ||
var existing = map[posting.account] || []; | ||
existing.push(__assign(__assign({}, posting), { date: new Date(trx.date), amount: Number(posting.amount) })); | ||
map[posting.account] = existing; | ||
} | ||
} | ||
return map; | ||
} | ||
function getFirstDayOfMonth(d) { | ||
var m = ("0" + (d.getMonth() + 1)).slice(-2); | ||
return "".concat(d.getFullYear(), "-").concat(m, "-01"); | ||
} | ||
function groupMonthly(journal) { | ||
var pivot = makePivot(journal); | ||
var map = {}; | ||
for (var _i = 0, _a = Object.entries(pivot); _i < _a.length; _i++) { | ||
var _b = _a[_i], key = _b[0], entries = _b[1]; | ||
var m2 = entries.reduce(function (acc, cur) { | ||
var dateKey = "".concat(getFirstDayOfMonth(cur.date)); | ||
var commodity = cur.commodity || "commodity"; | ||
if (!acc[dateKey]) { | ||
acc[dateKey] = {}; | ||
} | ||
if (!acc[dateKey][commodity]) { | ||
acc[dateKey][commodity] = 0; | ||
} | ||
acc[dateKey][commodity] = acc[dateKey][commodity] + Number(cur.amount); | ||
return acc; | ||
}, {}); | ||
map[key] = m2; | ||
} | ||
return map; | ||
} | ||
exports.Formatter = Formatter; | ||
@@ -556,2 +645,3 @@ exports.Parser = Transformer; | ||
exports.formatTransaction = formatTransaction; | ||
exports.groupMonthly = groupMonthly; | ||
exports.isComment = isComment$1; | ||
@@ -558,0 +648,0 @@ exports.isDirective = isDirective$1; |
/// <reference types="node" /> | ||
import { Transform } from 'stream'; | ||
import { JournalEntries } from '../types'; | ||
import { Transform } from "stream"; | ||
import { JournalEntries } from "../types"; | ||
declare class Transformer extends Transform { | ||
@@ -5,0 +5,0 @@ chunk: JournalEntries | undefined; |
@@ -17,2 +17,3 @@ export declare type VirtualTypes = "round" | "square" | undefined; | ||
}; | ||
export declare type TrxEntry = Posting | Comment; | ||
export declare type Transaction = { | ||
@@ -23,3 +24,3 @@ comment?: string; | ||
confirmed?: boolean; | ||
entries: (Posting | Comment)[]; | ||
entries: TrxEntry[]; | ||
}; | ||
@@ -31,3 +32,3 @@ export declare type Comment = { | ||
symbol: string; | ||
date: Date; | ||
date: Date | string; | ||
content: string; | ||
@@ -34,0 +35,0 @@ }; |
@@ -6,2 +6,19 @@ # Change Log | ||
# [1.6.0](https://github.com/kajyr/pta-tools/compare/pta-tools@1.5.2...pta-tools@1.6.0) (2022-11-07) | ||
### Bug Fixes | ||
* Directives dates can also be a string ([f117fe9](https://github.com/kajyr/pta-tools/commit/f117fe9c9492cbfce65946ee582844eb17d99fd4)) | ||
### Features | ||
* Accept dates with slashes, fix [#5](https://github.com/kajyr/pta-tools/issues/5) ([#6](https://github.com/kajyr/pta-tools/issues/6)) ([71a3308](https://github.com/kajyr/pta-tools/commit/71a3308d79cc543e37be36f6e0eeb465a6eb542d)) | ||
* Added groupMonthly function, to create a montly recap ([4474db8](https://github.com/kajyr/pta-tools/commit/4474db8bc6f9acbd418d8acfe8f558d3a20221ba)) | ||
## [1.5.2](https://github.com/kajyr/pta-tools/compare/pta-tools@1.5.1...pta-tools@1.5.2) (2022-07-13) | ||
@@ -8,0 +25,0 @@ |
{ | ||
"name": "pta-tools", | ||
"version": "1.5.2", | ||
"version": "1.6.0", | ||
"description": "Plain text accounting journal JS apis", | ||
@@ -22,9 +22,9 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"@types/node": "^16.11.6", | ||
"@types/node": "^18.8.2", | ||
"@types/split2": "^3.2.1", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.59.0", | ||
"rollup-plugin-typescript2": "^0.30.0", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.4.4" | ||
"rollup": "^2.79.1", | ||
"rollup-plugin-typescript2": "^0.34.1", | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.8.4" | ||
}, | ||
@@ -34,3 +34,3 @@ "dependencies": { | ||
}, | ||
"gitHead": "2dfd93072728dab0026b0fe575c8573e07866376" | ||
"gitHead": "ff4f311683ccf9cbcdc7abda868cf0e9d4430198" | ||
} |
@@ -26,7 +26,7 @@ # Plain text acounting node js api | ||
``` | ||
import { parse } from 'pta-journal'; | ||
import { parse } from 'pta-tools'; | ||
``` | ||
``` | ||
function parse(stream: ReadableStream): ParseResult | ||
function parse(stream: ReadableStream): Promise<ParseResult> | ||
``` | ||
@@ -43,1 +43,5 @@ | ||
Returns the transactions, the accounts and the commodities from a journal. To get the stream of the journal you can use | ||
``` | ||
const stream = fs.createReadStream('./hledger.journal'); | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72457
27
756
45