Comparing version 1.3.0 to 1.4.0
@@ -1,4 +0,5 @@ | ||
import { Transaction } from '../types'; | ||
import { Posting, Transaction } from '../types'; | ||
export declare const INDENT = " "; | ||
export declare function formatPosting(posting: Posting, lineWidth?: number): string; | ||
declare function formatTransaction(trx: Transaction): string; | ||
export default formatTransaction; |
@@ -129,7 +129,2 @@ 'use strict'; | ||
function formatDirective(d) { | ||
return "".concat(d.symbol, " ").concat(d.data, "\n\n"); | ||
} | ||
var INDENT = " "; | ||
function formatDate(date) { | ||
@@ -143,13 +138,33 @@ var d = new Date(date), month = "" + (d.getMonth() + 1), day = "" + d.getDate(), year = d.getFullYear(); | ||
} | ||
function formatCommentInline(comment) { | ||
return "; ".concat(comment.message); | ||
function formatDirective(d) { | ||
return "".concat(d.symbol, " ").concat(formatDate(d.date), " ").concat(d.content, "\n\n"); | ||
} | ||
var spaces = function (num, min) { | ||
if (min === void 0) { min = 0; } | ||
return Array(Math.max(num, min)).join(" "); | ||
return " ".repeat(Math.max(num, min)); | ||
}; | ||
function formatPosting(posting) { | ||
var str = "".concat(posting.account); | ||
var INDENT = " "; | ||
var SUGGESTED_LINE_WIDTH = 35; | ||
function formatCommentInline(comment) { | ||
return "; ".concat(comment.message); | ||
} | ||
function formatAccountName(posting) { | ||
if (posting.is_virtual === "round") { | ||
return "(".concat(posting.account, ")"); | ||
} | ||
if (posting.is_virtual === "square") { | ||
return "[".concat(posting.account, "]"); | ||
} | ||
return posting.account; | ||
} | ||
function formatPosting(posting, lineWidth) { | ||
if (lineWidth === void 0) { lineWidth = SUGGESTED_LINE_WIDTH; } | ||
var sepCount = lineWidth - getPostingTextWidth(posting); | ||
var str = formatAccountName(posting); | ||
if (posting.amount) { | ||
str = "".concat(str, " ").concat(spaces(30 - posting.account.length - posting.amount.toString().length, 2)).concat(posting.amount); | ||
var rebalance = posting.is_rebalance ? "= " : ""; | ||
str = "".concat(str).concat(spaces(sepCount, 2)).concat(rebalance).concat(posting.amount); | ||
if (posting.commodity) { | ||
@@ -173,8 +188,20 @@ str = "".concat(str, " ").concat(posting.commodity); | ||
} | ||
function getPostingTextWidth(entry) { | ||
if (!isPosting(entry)) { | ||
return 0; | ||
} | ||
var acc_len = formatAccountName(entry).length; | ||
var amount_len = (entry.amount || "").toString().length; | ||
var commodity_len = entry.commodity ? entry.commodity.length + 1 : 0; | ||
return acc_len + amount_len + commodity_len; | ||
} | ||
function formatTransaction(trx) { | ||
var str = formatHeader(trx); | ||
var widths = trx.entries.map(function (entry) { return getPostingTextWidth(entry) + 3; }); | ||
widths.push(SUGGESTED_LINE_WIDTH); | ||
var max = Math.max.apply(null, widths); | ||
for (var _i = 0, _a = trx.entries; _i < _a.length; _i++) { | ||
var line = _a[_i]; | ||
if (isPosting(line)) { | ||
str = "".concat(str).concat(INDENT).concat(formatPosting(line), "\n"); | ||
str = "".concat(str).concat(INDENT).concat(formatPosting(line, max), "\n"); | ||
} | ||
@@ -293,2 +320,7 @@ else if (isComment$1(line)) { | ||
function parseDirective(str) { | ||
var _a = str.split(/\s+/), symbol = _a[0], date = _a[1], list = _a.slice(2); | ||
return { symbol: symbol, date: new Date(date), content: list.join(" ") }; | ||
} | ||
function parseHeaderLine(str) { | ||
@@ -322,6 +354,6 @@ var _a = str.split(/\s+;\s+/), main = _a[0], comment = _a[1]; | ||
} | ||
function parseRebalance(account, values) { | ||
function parseRebalance(account, values, is_virtual) { | ||
var amount = getAmount(values); | ||
var commodity = getCommodity(values); | ||
return { account: account, is_rebalance: true, amount: amount, commodity: commodity }; | ||
return { account: account, is_rebalance: true, amount: amount, commodity: commodity, is_virtual: is_virtual }; | ||
} | ||
@@ -331,12 +363,21 @@ function filterBrackets(str) { | ||
} | ||
function isVirtual(account) { | ||
if (account.startsWith("[")) { | ||
return "square"; | ||
} | ||
if (account.startsWith("(")) { | ||
return "round"; | ||
} | ||
} | ||
function parsePosting(str) { | ||
var matches = str.match(/^(.+?)\s{2,}(.+)$/); | ||
if (!matches) { | ||
return { account: filterBrackets(str) }; | ||
return { account: filterBrackets(str), is_virtual: isVirtual(str) }; | ||
} | ||
var account = filterBrackets(matches[1]); | ||
var is_virtual = isVirtual(matches[1]); | ||
var values = matches[2]; | ||
if (values.indexOf("=") > -1) { | ||
// Rebalance | ||
return parseRebalance(account, values); | ||
return parseRebalance(account, values, is_virtual); | ||
} | ||
@@ -347,3 +388,3 @@ if (values.indexOf("@") === -1) { | ||
var commodity_1 = getCommodity(values); | ||
return { amount: amount_1, account: account, commodity: commodity_1 }; | ||
return { amount: amount_1, account: account, commodity: commodity_1, is_virtual: is_virtual }; | ||
} | ||
@@ -357,2 +398,3 @@ var _a = values.split(/\s*@\s*/), ams = _a[0], conversion = _a[1]; | ||
commodity: commodity, | ||
is_virtual: is_virtual, | ||
}; | ||
@@ -387,5 +429,2 @@ var c_amount = getAmount(conversion); | ||
} | ||
function mkDirective(str) { | ||
return { symbol: "P", data: str.replace(/^P\s*/, "") }; | ||
} | ||
function mkTransaction(other) { | ||
@@ -411,3 +450,4 @@ if (other === void 0) { other = {}; } | ||
var broken = trimmed.split(/\s+/); | ||
var isIndented = line.startsWith(INDENT); | ||
// at least 2 spaces, even if it's not proper indented with other entries | ||
var isIndented = line.startsWith(" "); | ||
if (trimmed === "") { | ||
@@ -423,3 +463,2 @@ callback(); | ||
this.chunk.entries.push(mkComment(trimmed)); | ||
this.chunk.comment = clearComment(trimmed); | ||
} | ||
@@ -430,3 +469,4 @@ else { | ||
this.clearChunk(); | ||
this.chunk = { message: clearComment(trimmed) }; | ||
// a comment is considered a one-liner | ||
this.push(mkComment(trimmed)); | ||
} | ||
@@ -439,3 +479,5 @@ callback(); | ||
this.clearChunk(); | ||
this.chunk = mkDirective(trimmed); | ||
// also directives are one liners | ||
// so we don't need to store them in the chunker | ||
this.push(parseDirective(trimmed)); | ||
callback(); | ||
@@ -442,0 +484,0 @@ return; |
@@ -1,2 +0,2 @@ | ||
import { Posting } from '../types'; | ||
import { Posting } from "../types"; | ||
export declare function getAmount(str: string): string | undefined; | ||
@@ -3,0 +3,0 @@ /** |
@@ -0,1 +1,2 @@ | ||
export declare type VirtualTypes = "round" | "square" | undefined; | ||
export declare type Posting = { | ||
@@ -10,2 +11,3 @@ account: string; | ||
is_rebalance?: boolean; | ||
is_virtual?: VirtualTypes; | ||
}; | ||
@@ -23,6 +25,7 @@ export declare type Transaction = { | ||
export declare type Directive = { | ||
symbol: "P"; | ||
data: string; | ||
symbol: string; | ||
date: Date; | ||
content: string; | ||
}; | ||
export declare type JournalEntries = Transaction | Comment | Directive; | ||
export declare type Journal = JournalEntries[]; |
@@ -6,2 +6,21 @@ # Change Log | ||
# [1.4.0](https://github.com/kajyr/pta-tools/compare/pta-tools@1.3.0...pta-tools@1.4.0) (2021-12-15) | ||
### Bug Fixes | ||
* Duplicate match of comments in postings ([ae94d49](https://github.com/kajyr/pta-tools/commit/ae94d497998eec50d18dbfad399157de768bccbc)) | ||
* spaces in formatPostings ([ae330ea](https://github.com/kajyr/pta-tools/commit/ae330ea44c50aaf940a327c39fd5a3302cf85ef5)) | ||
### Features | ||
* directives date matching ([be559e5](https://github.com/kajyr/pta-tools/commit/be559e5d81aa40cc67c180194d866241e5c751db)) | ||
* Support for formatting rebalance postings ([4285dac](https://github.com/kajyr/pta-tools/commit/4285dac585d328ae4c9cdfb0223a5324ff03fda9)) | ||
* Support for formatting virtual postings ([467676e](https://github.com/kajyr/pta-tools/commit/467676e0d03c027ca486d809a260c438eb77aad3)) | ||
# [1.3.0](https://github.com/kajyr/pta-tools/compare/pta-tools@1.2.0...pta-tools@1.3.0) (2021-12-14) | ||
@@ -8,0 +27,0 @@ |
{ | ||
"name": "pta-tools", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Plain text accounting journal JS apis", | ||
@@ -33,3 +33,3 @@ "main": "build/index.js", | ||
}, | ||
"gitHead": "455242001217161aece418d16d57888245963022" | ||
"gitHead": "4f553f8e66e374c307c977b8009c5e6011ac7a16" | ||
} |
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
53951
24
609