Comparing version 1.4.0 to 1.5.1
@@ -161,2 +161,8 @@ 'use strict'; | ||
} | ||
function formatAmountCommodity(amount, commodity) { | ||
if (commodity) { | ||
return "".concat(amount, " ").concat(commodity); | ||
} | ||
return amount.toString(); | ||
} | ||
function formatPosting(posting, lineWidth) { | ||
@@ -166,9 +172,12 @@ if (lineWidth === void 0) { lineWidth = SUGGESTED_LINE_WIDTH; } | ||
var str = formatAccountName(posting); | ||
var needsSpaces = !!(posting.amount || posting.balance); | ||
if (needsSpaces) { | ||
str = "".concat(str).concat(spaces(sepCount, 2)); | ||
} | ||
if (posting.amount) { | ||
var rebalance = posting.is_rebalance ? "= " : ""; | ||
str = "".concat(str).concat(spaces(sepCount, 2)).concat(rebalance).concat(posting.amount); | ||
if (posting.commodity) { | ||
str = "".concat(str, " ").concat(posting.commodity); | ||
} | ||
str = "".concat(str).concat(formatAmountCommodity(posting.amount, posting.commodity)); | ||
} | ||
if (posting.balance) { | ||
str = "".concat(str, " = ").concat(formatAmountCommodity(posting.balance.amount, posting.balance.commodity)); | ||
} | ||
return str; | ||
@@ -335,2 +344,34 @@ } | ||
/** | ||
* Balance Assertion: | ||
* Assets:Cash $-20.00 = $500.00 | ||
* | ||
* Balance assignments: | ||
* | ||
* Assets:Cash = $500.00 | ||
* | ||
* @see See https://www.ledger-cli.org/3.0/doc/ledger3.html#Balance-assertions | ||
*/ | ||
function isBalanceLine(line) { | ||
return line.indexOf("=") > -1; | ||
} | ||
function parseBalance(account, values, is_virtual) { | ||
var _a = values.split(/\s*=\s*/), trx = _a[0], bal = _a[1]; | ||
var amount = getAmount(trx); | ||
var commodity = getCommodity(trx); | ||
var p = { account: account, amount: amount, commodity: commodity }; | ||
var bal_amount = getAmount(bal); | ||
if (bal_amount) { | ||
var balance = { | ||
amount: bal_amount, | ||
commodity: getCommodity(bal), | ||
}; | ||
p.balance = balance; | ||
} | ||
if (!!is_virtual) { | ||
p.is_virtual = is_virtual; | ||
} | ||
return p; | ||
} | ||
function getAmount(str) { | ||
@@ -353,7 +394,2 @@ var matches = str.match(/[\-\+]?[0-9]*[\.0-9]+/); | ||
} | ||
function parseRebalance(account, values, is_virtual) { | ||
var amount = getAmount(values); | ||
var commodity = getCommodity(values); | ||
return { account: account, is_rebalance: true, amount: amount, commodity: commodity, is_virtual: is_virtual }; | ||
} | ||
function filterBrackets(str) { | ||
@@ -378,5 +414,4 @@ return str.replace(/[\[\]()]/g, ""); | ||
var values = matches[2]; | ||
if (values.indexOf("=") > -1) { | ||
// Rebalance | ||
return parseRebalance(account, values, is_virtual); | ||
if (isBalanceLine(values)) { | ||
return parseBalance(account, values, is_virtual); | ||
} | ||
@@ -383,0 +418,0 @@ if (values.indexOf("@") === -1) { |
@@ -1,2 +0,2 @@ | ||
import { Posting } from "../types"; | ||
import { Posting } from '../types'; | ||
export declare function getAmount(str: string): string | undefined; | ||
@@ -3,0 +3,0 @@ /** |
export declare type VirtualTypes = "round" | "square" | undefined; | ||
export declare type AmountType = string | number; | ||
export declare type Posting = { | ||
account: string; | ||
amount?: string | number; | ||
amount?: AmountType; | ||
commodity?: string; | ||
conversion?: { | ||
amount: string; | ||
amount: AmountType; | ||
commodity: string; | ||
}; | ||
is_rebalance?: boolean; | ||
balance?: { | ||
amount: AmountType; | ||
commodity?: string; | ||
}; | ||
is_virtual?: VirtualTypes; | ||
@@ -12,0 +16,0 @@ }; |
@@ -6,2 +6,24 @@ # Change Log | ||
## [1.5.1](https://github.com/kajyr/pta-tools/compare/pta-tools@1.5.0...pta-tools@1.5.1) (2022-01-01) | ||
### Bug Fixes | ||
* removed file ([72926eb](https://github.com/kajyr/pta-tools/commit/72926ebc4e3ca1b2ce00cca1d75a0dfb4fab30eb)) | ||
# [1.5.0](https://github.com/kajyr/pta-tools/compare/pta-tools@1.4.0...pta-tools@1.5.0) (2022-01-01) | ||
### Features | ||
* Support for balance assertions and balance assignements ([f1fe19d](https://github.com/kajyr/pta-tools/commit/f1fe19d601ca124675722aef2d0952b23e2e7f91)) | ||
# [1.4.0](https://github.com/kajyr/pta-tools/compare/pta-tools@1.3.0...pta-tools@1.4.0) (2021-12-15) | ||
@@ -8,0 +30,0 @@ |
{ | ||
"name": "pta-tools", | ||
"version": "1.4.0", | ||
"version": "1.5.1", | ||
"description": "Plain text accounting journal JS apis", | ||
@@ -33,3 +33,3 @@ "main": "build/index.js", | ||
}, | ||
"gitHead": "4f553f8e66e374c307c977b8009c5e6011ac7a16" | ||
"gitHead": "79cf8517883f2d526cd9db0ef2275ca3269a36ea" | ||
} |
Sorry, the diff of this file is not supported yet
57892
25
661