beancount 0.0.31 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +51 -22
  2. package/build/src/benchmark.mjs +1 -1
  3. package/build/src/classes/DatedNode.d.mts +40 -0
  4. package/build/src/classes/DatedNode.mjs +61 -0
  5. package/build/src/classes/Node.d.mts +92 -0
  6. package/build/src/classes/Node.mjs +107 -0
  7. package/build/src/classes/ParseResult.d.mts +75 -75
  8. package/build/src/classes/ParseResult.mjs +96 -98
  9. package/build/src/classes/nodes/Balance.d.mts +32 -0
  10. package/build/src/classes/nodes/Balance.mjs +50 -0
  11. package/build/src/classes/nodes/Blankline.d.mts +23 -0
  12. package/build/src/classes/nodes/Blankline.mjs +37 -0
  13. package/build/src/classes/nodes/Close.d.mts +20 -0
  14. package/build/src/classes/nodes/Close.mjs +31 -0
  15. package/build/src/classes/nodes/Comment.d.mts +25 -0
  16. package/build/src/classes/nodes/Comment.mjs +42 -0
  17. package/build/src/classes/nodes/Commodity.d.mts +20 -0
  18. package/build/src/classes/nodes/Commodity.mjs +31 -0
  19. package/build/src/classes/nodes/Custom.d.mts +23 -0
  20. package/build/src/classes/nodes/Custom.mjs +38 -0
  21. package/build/src/classes/nodes/Document.d.mts +22 -0
  22. package/build/src/classes/nodes/Document.mjs +34 -0
  23. package/build/src/classes/nodes/Event.d.mts +23 -0
  24. package/build/src/classes/nodes/Event.mjs +34 -0
  25. package/build/src/classes/nodes/Include.d.mts +20 -0
  26. package/build/src/classes/nodes/Include.mjs +31 -0
  27. package/build/src/classes/nodes/Note.d.mts +22 -0
  28. package/build/src/classes/nodes/Note.mjs +34 -0
  29. package/build/src/classes/nodes/Open.d.mts +27 -0
  30. package/build/src/classes/nodes/Open.mjs +66 -0
  31. package/build/src/classes/nodes/Option.d.mts +23 -0
  32. package/build/src/classes/nodes/Option.mjs +32 -0
  33. package/build/src/classes/nodes/Pad.d.mts +22 -0
  34. package/build/src/classes/nodes/Pad.mjs +33 -0
  35. package/build/src/classes/nodes/Plugin.d.mts +22 -0
  36. package/build/src/classes/nodes/Plugin.mjs +36 -0
  37. package/build/src/classes/nodes/Poptag.d.mts +21 -0
  38. package/build/src/classes/nodes/Poptag.mjs +34 -0
  39. package/build/src/classes/nodes/Price.d.mts +32 -0
  40. package/build/src/classes/nodes/Price.mjs +57 -0
  41. package/build/src/classes/nodes/Pushtag.d.mts +21 -0
  42. package/build/src/classes/nodes/Pushtag.mjs +34 -0
  43. package/build/src/classes/nodes/Query.d.mts +22 -0
  44. package/build/src/classes/nodes/Query.mjs +34 -0
  45. package/build/src/classes/nodes/Transaction/Posting.d.mts +59 -0
  46. package/build/src/classes/nodes/Transaction/Posting.mjs +97 -0
  47. package/build/src/classes/nodes/Transaction/Tag.d.mts +28 -0
  48. package/build/src/classes/nodes/Transaction/Tag.mjs +28 -0
  49. package/build/src/classes/nodes/Transaction/index.d.mts +70 -0
  50. package/build/src/classes/nodes/Transaction/index.mjs +193 -0
  51. package/build/src/classes/nodes/index.d.mts +19 -0
  52. package/build/src/classes/nodes/index.mjs +19 -0
  53. package/build/src/cli.mjs +4 -4
  54. package/build/src/deserialize.d.mts +54 -54
  55. package/build/src/deserialize.mjs +89 -89
  56. package/build/src/directiveTypes.d.mts +10 -0
  57. package/build/src/directiveTypes.mjs +29 -0
  58. package/build/src/genericParse.d.mts +27 -20
  59. package/build/src/genericParse.mjs +30 -30
  60. package/build/src/main.d.mts +30 -31
  61. package/build/src/main.mjs +30 -30
  62. package/build/src/nodeTypeToClass.d.mts +81 -0
  63. package/build/src/nodeTypeToClass.mjs +37 -0
  64. package/build/src/parse.d.mts +16 -16
  65. package/build/src/parse.mjs +37 -37
  66. package/build/src/parseFile.d.mts +2 -2
  67. package/build/src/parseFile.mjs +11 -11
  68. package/build/src/utils/splitStringIntoSourceFragments.d.ts +14 -0
  69. package/build/src/utils/splitStringIntoSourceFragments.js +48 -0
  70. package/build/tsconfig.build.tsbuildinfo +1 -1
  71. package/package.json +7 -7
@@ -0,0 +1,66 @@
1
+ import { assertNodeConstructor } from '../Node.mjs';
2
+ import { DatedNode } from '../DatedNode.mjs';
3
+ import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
4
+ import { parseString } from '../Value.mjs';
5
+ import { defaultFormatOptions } from '../ParseResult.mjs';
6
+ /**
7
+ * Represents an Open node that declares a new account.
8
+ * Open directives define the beginning of an account's lifespan.
9
+ */
10
+ export class Open extends DatedNode {
11
+ constructor() {
12
+ super(...arguments);
13
+ /** @inheritdoc */
14
+ this.type = 'open';
15
+ }
16
+ /**
17
+ * Creates an Open instance from a generic parse result.
18
+ * @param genericParseResult - The parsed open node data
19
+ * @returns A new Open instance
20
+ */
21
+ static fromGenericParseResult(genericParseResult) {
22
+ const [account, ...other] = simpleParseLine(genericParseResult.header);
23
+ let constraintCurrencies, bookingMethod;
24
+ if (other.length === 2) {
25
+ constraintCurrencies = other[0];
26
+ bookingMethod = parseString(other[1]);
27
+ }
28
+ else if (other.length === 1) {
29
+ if (other[0].startsWith('"') && other[0].endsWith('"')) {
30
+ bookingMethod = parseString(other[0]);
31
+ }
32
+ else {
33
+ constraintCurrencies = other[0];
34
+ }
35
+ }
36
+ return new Open({
37
+ ...genericParseResult.props,
38
+ account,
39
+ constraintCurrencies: constraintCurrencies?.split(','),
40
+ bookingMethod,
41
+ });
42
+ }
43
+ /** @inheritdoc */
44
+ toString() {
45
+ return this.toFormattedString({ currencyColumn: 0 });
46
+ }
47
+ /** @inheritdoc */
48
+ toFormattedString(formatOptions = defaultFormatOptions) {
49
+ const parts = [`${this.getDateTypePrefix()} ${this.account}`];
50
+ if (this.constraintCurrencies !== undefined) {
51
+ const paddingLength = formatOptions.currencyColumn -
52
+ parts.join(' ').length -
53
+ this.constraintCurrencies.join(',').length;
54
+ if (paddingLength > 0) {
55
+ parts.push(' '.repeat(paddingLength));
56
+ }
57
+ parts.push(this.constraintCurrencies.join(','));
58
+ }
59
+ if (this.bookingMethod !== undefined) {
60
+ parts.push(`"${this.bookingMethod}"`);
61
+ }
62
+ return parts.join(' ') + this.getMetaDataString();
63
+ }
64
+ }
65
+ // Ensure class conforms to NodeConstructor pattern
66
+ assertNodeConstructor(Open);
@@ -0,0 +1,23 @@
1
+ import type { GenericParseResult } from '../../genericParse.mjs';
2
+ import { Value } from '../Value.mjs';
3
+ import { Node } from '../Node.mjs';
4
+ /**
5
+ * Represents an Option node that sets a Beancount configuration option.
6
+ * Option directives configure various aspects of Beancount's behavior.
7
+ */
8
+ export declare class Option extends Node {
9
+ /** @inheritdoc */
10
+ type: "option";
11
+ /** The option name */
12
+ name: string;
13
+ /** The option value */
14
+ value: Value;
15
+ /**
16
+ * Creates an Option instance from a generic parse result.
17
+ * @param genericParseResult - The parsed option node data
18
+ * @returns A new Option instance
19
+ */
20
+ static fromGenericParseResult(genericParseResult: GenericParseResult): Option;
21
+ /** @inheritdoc */
22
+ toString(): string;
23
+ }
@@ -0,0 +1,32 @@
1
+ import { parseString, Value } from '../Value.mjs';
2
+ import { stringAwareParseLine } from '../../utils/stringAwareParseLine.mjs';
3
+ import { assertNodeConstructor, Node } from '../Node.mjs';
4
+ /**
5
+ * Represents an Option node that sets a Beancount configuration option.
6
+ * Option directives configure various aspects of Beancount's behavior.
7
+ */
8
+ export class Option extends Node {
9
+ constructor() {
10
+ super(...arguments);
11
+ /** @inheritdoc */
12
+ this.type = 'option';
13
+ }
14
+ /**
15
+ * Creates an Option instance from a generic parse result.
16
+ * @param genericParseResult - The parsed option node data
17
+ * @returns A new Option instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult) {
20
+ const [name, value] = stringAwareParseLine(genericParseResult.header);
21
+ return new Option({
22
+ name: parseString(name),
23
+ value: Value.fromString(value),
24
+ });
25
+ }
26
+ /** @inheritdoc */
27
+ toString() {
28
+ return `${this.type} "${this.name}" ${this.value.toString()}`;
29
+ }
30
+ }
31
+ // Ensure class conforms to NodeConstructor pattern
32
+ assertNodeConstructor(Option);
@@ -0,0 +1,22 @@
1
+ import type { GenericParseResultWithDate } from '../../genericParse.mjs';
2
+ import { DatedNode } from '../DatedNode.mjs';
3
+ /**
4
+ * Represents a Pad node that automatically balances accounts.
5
+ * Pad directives insert transactions to bring an account to a specific balance.
6
+ */
7
+ export declare class Pad extends DatedNode {
8
+ /** @inheritdoc */
9
+ type: "pad";
10
+ /** The account to be padded/balanced */
11
+ account: string;
12
+ /** The source account to use for padding */
13
+ accountPad: string;
14
+ /**
15
+ * Creates a Pad instance from a generic parse result.
16
+ * @param genericParseResult - The parsed pad node data
17
+ * @returns A new Pad instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Pad;
20
+ /** @inheritdoc */
21
+ toString(): string;
22
+ }
@@ -0,0 +1,33 @@
1
+ import { assertNodeConstructor } from '../Node.mjs';
2
+ import { DatedNode } from '../DatedNode.mjs';
3
+ import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
4
+ /**
5
+ * Represents a Pad node that automatically balances accounts.
6
+ * Pad directives insert transactions to bring an account to a specific balance.
7
+ */
8
+ export class Pad extends DatedNode {
9
+ constructor() {
10
+ super(...arguments);
11
+ /** @inheritdoc */
12
+ this.type = 'pad';
13
+ }
14
+ /**
15
+ * Creates a Pad instance from a generic parse result.
16
+ * @param genericParseResult - The parsed pad node data
17
+ * @returns A new Pad instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult) {
20
+ const [account, accountPad] = simpleParseLine(genericParseResult.header);
21
+ return new Pad({
22
+ ...genericParseResult.props,
23
+ account,
24
+ accountPad,
25
+ });
26
+ }
27
+ /** @inheritdoc */
28
+ toString() {
29
+ return `${this.getDateTypePrefix()} ${this.account} ${this.accountPad}${this.getMetaDataString()}`;
30
+ }
31
+ }
32
+ // Ensure class conforms to NodeConstructor pattern
33
+ assertNodeConstructor(Pad);
@@ -0,0 +1,22 @@
1
+ import type { GenericParseResult } from '../../genericParse.mjs';
2
+ import { Node } from '../Node.mjs';
3
+ /**
4
+ * Represents a Plugin node that loads a Beancount plugin.
5
+ * Plugin directives enable plugins to process the ledger.
6
+ */
7
+ export declare class Plugin extends Node {
8
+ /** @inheritdoc */
9
+ type: "plugin";
10
+ /** The Python module name of the plugin */
11
+ moduleName: string;
12
+ /** Optional configuration string for the plugin */
13
+ config?: string;
14
+ /**
15
+ * Creates a Plugin instance from a generic parse result.
16
+ * @param genericParseResult - The parsed plugin node data
17
+ * @returns A new Plugin instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult: GenericParseResult): Plugin;
20
+ /** @inheritdoc */
21
+ toString(): string;
22
+ }
@@ -0,0 +1,36 @@
1
+ import { parseString } from '../Value.mjs';
2
+ import { stringAwareParseLine } from '../../utils/stringAwareParseLine.mjs';
3
+ import { assertNodeConstructor, Node } from '../Node.mjs';
4
+ /**
5
+ * Represents a Plugin node that loads a Beancount plugin.
6
+ * Plugin directives enable plugins to process the ledger.
7
+ */
8
+ export class Plugin extends Node {
9
+ constructor() {
10
+ super(...arguments);
11
+ /** @inheritdoc */
12
+ this.type = 'plugin';
13
+ }
14
+ /**
15
+ * Creates a Plugin instance from a generic parse result.
16
+ * @param genericParseResult - The parsed plugin node data
17
+ * @returns A new Plugin instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult) {
20
+ const [moduleName, config] = stringAwareParseLine(genericParseResult.header);
21
+ return new Plugin({
22
+ moduleName: parseString(moduleName),
23
+ config: config ? parseString(config) : undefined,
24
+ });
25
+ }
26
+ /** @inheritdoc */
27
+ toString() {
28
+ const parts = [`${this.type} "${this.moduleName}"`];
29
+ if (this.config !== undefined) {
30
+ parts.push(`"${this.config}"`);
31
+ }
32
+ return parts.join(' ');
33
+ }
34
+ }
35
+ // Ensure class conforms to NodeConstructor pattern
36
+ assertNodeConstructor(Plugin);
@@ -0,0 +1,21 @@
1
+ import type { GenericParseResult } from '../../genericParse.mjs';
2
+ import { Node } from '../Node.mjs';
3
+ import { Tag } from './Transaction/Tag.mjs';
4
+ /**
5
+ * Represents a Poptag node that removes a tag from the tag stack.
6
+ * Transactions after this will no longer inherit the popped tag.
7
+ */
8
+ export declare class Poptag extends Node {
9
+ /** @inheritdoc */
10
+ type: "poptag";
11
+ /** The tag being popped from the stack */
12
+ tag: Tag;
13
+ /**
14
+ * Creates a Poptag instance from a generic parse result.
15
+ * @param genericParseResult - The parsed poptag node data
16
+ * @returns A new Poptag instance
17
+ */
18
+ static fromGenericParseResult(genericParseResult: GenericParseResult): Poptag;
19
+ /** @inheritdoc */
20
+ toString(): string;
21
+ }
@@ -0,0 +1,34 @@
1
+ import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
2
+ import { assertNodeConstructor, Node } from '../Node.mjs';
3
+ import { Tag } from './Transaction/Tag.mjs';
4
+ /**
5
+ * Represents a Poptag node that removes a tag from the tag stack.
6
+ * Transactions after this will no longer inherit the popped tag.
7
+ */
8
+ export class Poptag extends Node {
9
+ constructor() {
10
+ super(...arguments);
11
+ /** @inheritdoc */
12
+ this.type = 'poptag';
13
+ }
14
+ /**
15
+ * Creates a Poptag instance from a generic parse result.
16
+ * @param genericParseResult - The parsed poptag node data
17
+ * @returns A new Poptag instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult) {
20
+ const [tagContent] = simpleParseLine(genericParseResult.header);
21
+ return new Poptag({
22
+ tag: new Tag({
23
+ content: tagContent.trim().replace(/^#/, ''),
24
+ fromStack: true,
25
+ }),
26
+ });
27
+ }
28
+ /** @inheritdoc */
29
+ toString() {
30
+ return `poptag #${this.tag.content}`;
31
+ }
32
+ }
33
+ // Ensure class conforms to NodeConstructor pattern
34
+ assertNodeConstructor(Poptag);
@@ -0,0 +1,32 @@
1
+ import type { GenericParseResultWithDate } from '../../genericParse.mjs';
2
+ import { DatedNode } from '../DatedNode.mjs';
3
+ import { FormatOptions } from '../ParseResult.mjs';
4
+ /**
5
+ * Represents a Price node that records the price of a commodity.
6
+ * Price directives establish the exchange rate between a commodity and a currency.
7
+ */
8
+ export declare class Price extends DatedNode {
9
+ /** @inheritdoc */
10
+ type: "price";
11
+ /** The commodity being priced */
12
+ commodity: string;
13
+ /** The currency the price is expressed in */
14
+ currency: string;
15
+ /** The price amount */
16
+ amount: string;
17
+ /**
18
+ * Gets the formatted price string (amount + currency).
19
+ * @returns The formatted price string
20
+ */
21
+ get price(): string | undefined;
22
+ /**
23
+ * Creates a Price instance from a generic parse result.
24
+ * @param genericParseResult - The parsed price node data
25
+ * @returns A new Price instance
26
+ */
27
+ static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Price;
28
+ /** @inheritdoc */
29
+ toString(): string;
30
+ /** @inheritdoc */
31
+ toFormattedString(formatOptions?: FormatOptions): string;
32
+ }
@@ -0,0 +1,57 @@
1
+ import { assertNodeConstructor } from '../Node.mjs';
2
+ import { DatedNode } from '../DatedNode.mjs';
3
+ import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
4
+ import { formatPrice } from '../../utils/formatPrice.mjs';
5
+ import { defaultFormatOptions } from '../ParseResult.mjs';
6
+ /**
7
+ * Represents a Price node that records the price of a commodity.
8
+ * Price directives establish the exchange rate between a commodity and a currency.
9
+ */
10
+ export class Price extends DatedNode {
11
+ constructor() {
12
+ super(...arguments);
13
+ /** @inheritdoc */
14
+ this.type = 'price';
15
+ }
16
+ /**
17
+ * Gets the formatted price string (amount + currency).
18
+ * @returns The formatted price string
19
+ */
20
+ get price() {
21
+ return formatPrice(this.amount, this.currency);
22
+ }
23
+ /**
24
+ * Creates a Price instance from a generic parse result.
25
+ * @param genericParseResult - The parsed price node data
26
+ * @returns A new Price instance
27
+ */
28
+ static fromGenericParseResult(genericParseResult) {
29
+ const [commodity, amount, currency] = simpleParseLine(genericParseResult.header);
30
+ return new Price({
31
+ ...genericParseResult.props,
32
+ commodity,
33
+ currency,
34
+ amount,
35
+ });
36
+ }
37
+ /** @inheritdoc */
38
+ toString() {
39
+ return this.toFormattedString({ currencyColumn: 0 });
40
+ }
41
+ /** @inheritdoc */
42
+ toFormattedString(formatOptions = defaultFormatOptions) {
43
+ const parts = [this.getDateTypePrefix(), this.commodity];
44
+ const paddingLength = formatOptions.currencyColumn -
45
+ parts.join(' ').length -
46
+ this.amount.length -
47
+ 2 - // indent
48
+ 2; // spacing
49
+ if (paddingLength > 0) {
50
+ parts.push(' '.repeat(paddingLength));
51
+ }
52
+ parts.push(this.amount, `${this.currency}${this.getMetaDataString()}`);
53
+ return parts.join(' ');
54
+ }
55
+ }
56
+ // Ensure class conforms to NodeConstructor pattern
57
+ assertNodeConstructor(Price);
@@ -0,0 +1,21 @@
1
+ import type { GenericParseResult } from '../../genericParse.mjs';
2
+ import { Node } from '../Node.mjs';
3
+ import { Tag } from './Transaction/Tag.mjs';
4
+ /**
5
+ * Represents a Pushtag node that pushes a tag onto the tag stack.
6
+ * Subsequent transactions will automatically inherit this tag until it's popped.
7
+ */
8
+ export declare class Pushtag extends Node {
9
+ /** @inheritdoc */
10
+ type: "pushtag";
11
+ /** The tag being pushed onto the stack */
12
+ tag: Tag;
13
+ /**
14
+ * Creates a Pushtag instance from a generic parse result.
15
+ * @param genericParseResult - The parsed pushtag node data
16
+ * @returns A new Pushtag instance
17
+ */
18
+ static fromGenericParseResult(genericParseResult: GenericParseResult): Pushtag;
19
+ /** @inheritdoc */
20
+ toString(): string;
21
+ }
@@ -0,0 +1,34 @@
1
+ import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
2
+ import { assertNodeConstructor, Node } from '../Node.mjs';
3
+ import { Tag } from './Transaction/Tag.mjs';
4
+ /**
5
+ * Represents a Pushtag node that pushes a tag onto the tag stack.
6
+ * Subsequent transactions will automatically inherit this tag until it's popped.
7
+ */
8
+ export class Pushtag extends Node {
9
+ constructor() {
10
+ super(...arguments);
11
+ /** @inheritdoc */
12
+ this.type = 'pushtag';
13
+ }
14
+ /**
15
+ * Creates a Pushtag instance from a generic parse result.
16
+ * @param genericParseResult - The parsed pushtag node data
17
+ * @returns A new Pushtag instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult) {
20
+ const [tagContent] = simpleParseLine(genericParseResult.header);
21
+ return new Pushtag({
22
+ tag: new Tag({
23
+ content: tagContent.trim().replace(/^#/, ''),
24
+ fromStack: true,
25
+ }),
26
+ });
27
+ }
28
+ /** @inheritdoc */
29
+ toString() {
30
+ return `${this.type} #${this.tag.content}`;
31
+ }
32
+ }
33
+ // Ensure class conforms to NodeConstructor pattern
34
+ assertNodeConstructor(Pushtag);
@@ -0,0 +1,22 @@
1
+ import type { GenericParseResultWithDate } from '../../genericParse.mjs';
2
+ import { DatedNode } from '../DatedNode.mjs';
3
+ /**
4
+ * Represents a Query node that defines a named SQL query.
5
+ * Query directives allow defining reusable queries in the Beancount file.
6
+ */
7
+ export declare class Query extends DatedNode {
8
+ /** @inheritdoc */
9
+ type: "query";
10
+ /** The name of the query */
11
+ name: string;
12
+ /** The SQL query contents */
13
+ sqlContents: string;
14
+ /**
15
+ * Creates a Query instance from a generic parse result.
16
+ * @param genericParseResult - The parsed query node data
17
+ * @returns A new Query instance
18
+ */
19
+ static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Query;
20
+ /** @inheritdoc */
21
+ toString(): string;
22
+ }
@@ -0,0 +1,34 @@
1
+ import { assertNodeConstructor } from '../Node.mjs';
2
+ import { DatedNode } from '../DatedNode.mjs';
3
+ import { stringAwareParseLine } from '../../utils/stringAwareParseLine.mjs';
4
+ import { parseString } from '../Value.mjs';
5
+ /**
6
+ * Represents a Query node that defines a named SQL query.
7
+ * Query directives allow defining reusable queries in the Beancount file.
8
+ */
9
+ export class Query extends DatedNode {
10
+ constructor() {
11
+ super(...arguments);
12
+ /** @inheritdoc */
13
+ this.type = 'query';
14
+ }
15
+ /**
16
+ * Creates a Query instance from a generic parse result.
17
+ * @param genericParseResult - The parsed query node data
18
+ * @returns A new Query instance
19
+ */
20
+ static fromGenericParseResult(genericParseResult) {
21
+ const [name, sqlContents] = stringAwareParseLine(genericParseResult.header);
22
+ return new Query({
23
+ ...genericParseResult.props,
24
+ name: parseString(name),
25
+ sqlContents: parseString(sqlContents).trim(),
26
+ });
27
+ }
28
+ /** @inheritdoc */
29
+ toString() {
30
+ return `${this.getDateTypePrefix()} "${this.name}" "${this.sqlContents}"${this.getMetaDataString()}`;
31
+ }
32
+ }
33
+ // Ensure class conforms to NodeConstructor pattern
34
+ assertNodeConstructor(Query);
@@ -0,0 +1,59 @@
1
+ import { FormatOptions } from '../../ParseResult.mjs';
2
+ /**
3
+ * Represents a single posting (account movement) within a transaction.
4
+ * Each posting records an amount moving to/from an account.
5
+ */
6
+ export declare class Posting {
7
+ /** Optional posting flag (e.g., '*' for cleared) */
8
+ flag?: string;
9
+ /** The account name for this posting */
10
+ account: string;
11
+ /** The amount as a string (may be omitted for auto-calculated postings) */
12
+ amount?: string;
13
+ /** The currency code for the amount */
14
+ currency?: string;
15
+ /** Optional cost specification (e.g., for currency conversions) */
16
+ cost?: string;
17
+ /** Currency for the price annotation */
18
+ priceCurrency?: string;
19
+ /** Amount for the price annotation */
20
+ priceAmount?: string;
21
+ /** Optional comment for this posting */
22
+ comment?: string;
23
+ /** When a cost is given, defines the amount of at signs (1 for unit price, 2 for total price) */
24
+ atSigns?: number;
25
+ /**
26
+ * Creates a new Posting instance.
27
+ * @param obj - Object containing posting properties
28
+ */
29
+ constructor(obj: Record<string, unknown>);
30
+ /**
31
+ * Parses a posting line string into a Posting instance.
32
+ * Expected format: [Flag] Account [Amount Currency] [{Cost}] [@ PriceAmount PriceCurrency] [; Comment]
33
+ *
34
+ * @param unparsedline - The posting line string to parse
35
+ * @returns A new Posting instance
36
+ * @throws {Error} If the posting line cannot be parsed
37
+ */
38
+ static fromString(unparsedline: string): Posting;
39
+ /**
40
+ * Gets the formatted price string combining amount, currency, cost, and price annotation.
41
+ * @returns The formatted price string, or undefined if no price components exist
42
+ */
43
+ get price(): string | undefined;
44
+ /**
45
+ * Converts this posting to a string representation.
46
+ * Delegates to toFormattedString with zero currency column alignment.
47
+ *
48
+ * @returns The string representation of this posting
49
+ */
50
+ toString(): string;
51
+ /**
52
+ * Converts this posting to a formatted string with aligned currency column.
53
+ * Adds padding before the price to align at the specified column.
54
+ *
55
+ * @param formatOptions - Formatting options including currency column position
56
+ * @returns The formatted string representation of this posting
57
+ */
58
+ toFormattedString(formatOptions?: FormatOptions): string;
59
+ }
@@ -0,0 +1,97 @@
1
+ import { currencyPattern } from '../../../patterns.mjs';
2
+ import { formatPrice } from '../../../utils/formatPrice.mjs';
3
+ import { defaultFormatOptions } from '../../ParseResult.mjs';
4
+ /**
5
+ * Represents a single posting (account movement) within a transaction.
6
+ * Each posting records an amount moving to/from an account.
7
+ */
8
+ export class Posting {
9
+ /**
10
+ * Creates a new Posting instance.
11
+ * @param obj - Object containing posting properties
12
+ */
13
+ constructor(obj) {
14
+ Object.assign(this, obj);
15
+ }
16
+ /**
17
+ * Parses a posting line string into a Posting instance.
18
+ * Expected format: [Flag] Account [Amount Currency] [{Cost}] [@ PriceAmount PriceCurrency] [; Comment]
19
+ *
20
+ * @param unparsedline - The posting line string to parse
21
+ * @returns A new Posting instance
22
+ * @throws {Error} If the posting line cannot be parsed
23
+ */
24
+ static fromString(unparsedline) {
25
+ // [Flag] Account Amount [Currency] [{Cost}] [@ Price]
26
+ const flagPattern = `([^ ]) +`;
27
+ const accountPattern = `([^ ]*)`;
28
+ const amountPattern = `([^A-Z;]*)`;
29
+ const costPattern = `{(.*)}`;
30
+ const pricePattern = `+(@|@@) +(?:(\\d+\\.?\\d*) ${currencyPattern})`;
31
+ const amountCurrenyCostPattern = `${amountPattern}(?: +${currencyPattern})?(?: +${costPattern})?(?: ${pricePattern})?`;
32
+ const commentPattern = `( *;.*)?`;
33
+ const pattern = `^(?:${flagPattern})?${accountPattern}(?: +${amountCurrenyCostPattern})?${commentPattern}$`;
34
+ const matches = RegExp(pattern).exec(unparsedline);
35
+ if (!matches) {
36
+ throw new Error(`Could not parse posting: ${unparsedline}`);
37
+ }
38
+ const [, flag, account, amount, currency, cost, atSigns, priceAmount, priceCurrency, comment,] = matches;
39
+ return new Posting({
40
+ flag: flag,
41
+ account: account?.trim(),
42
+ amount: amount?.trim().length > 0 ? amount.trim() : undefined,
43
+ currency: currency?.trim(),
44
+ cost: cost?.trim(),
45
+ priceAmount: priceAmount?.trim(),
46
+ priceCurrency: priceCurrency?.trim(),
47
+ atSigns: atSigns ? atSigns.length : undefined,
48
+ comment: comment?.replace(/^ *;/, '').trim(),
49
+ });
50
+ }
51
+ /**
52
+ * Gets the formatted price string combining amount, currency, cost, and price annotation.
53
+ * @returns The formatted price string, or undefined if no price components exist
54
+ */
55
+ get price() {
56
+ return formatPrice(this.amount, this.currency, this.cost, this.priceAmount, this.priceCurrency, this.atSigns);
57
+ }
58
+ /**
59
+ * Converts this posting to a string representation.
60
+ * Delegates to toFormattedString with zero currency column alignment.
61
+ *
62
+ * @returns The string representation of this posting
63
+ */
64
+ toString() {
65
+ return this.toFormattedString({ currencyColumn: 0 });
66
+ }
67
+ /**
68
+ * Converts this posting to a formatted string with aligned currency column.
69
+ * Adds padding before the price to align at the specified column.
70
+ *
71
+ * @param formatOptions - Formatting options including currency column position
72
+ * @returns The formatted string representation of this posting
73
+ */
74
+ toFormattedString(formatOptions = defaultFormatOptions) {
75
+ const parts = [];
76
+ if (this.flag !== undefined) {
77
+ parts.push(this.flag);
78
+ }
79
+ parts.push(this.account);
80
+ if (this.price !== undefined) {
81
+ const paddingLength = formatOptions.currencyColumn -
82
+ parts.join(' ').length -
83
+ this.amount.length -
84
+ 2 - // indent
85
+ 2 - // spacing
86
+ 2; // not sure what this is for
87
+ if (paddingLength > 0) {
88
+ parts.push(' '.repeat(paddingLength));
89
+ }
90
+ parts.push(this.price);
91
+ }
92
+ if (this.comment !== undefined) {
93
+ parts.push(';', this.comment);
94
+ }
95
+ return parts.join(' ');
96
+ }
97
+ }