beancount 0.2.4 → 0.2.6

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 (66) hide show
  1. package/build/src/classes/Node.mjs +7 -7
  2. package/build/src/classes/nodes/Blankline.mjs +2 -6
  3. package/build/src/classes/nodes/Transaction/Posting.mjs +1 -1
  4. package/build/src/genericParse.d.mts +8 -2
  5. package/build/src/genericParse.mjs +19 -20
  6. package/build/src/main.d.mts +1 -1
  7. package/build/src/parse.mjs +1 -1
  8. package/build/src/utils/SourceLocation.d.mts +27 -4
  9. package/build/src/utils/splitStringIntoSourceFragments.d.ts +1 -1
  10. package/build/src/utils/splitStringIntoSourceFragments.js +63 -11
  11. package/build/tsconfig.build.tsbuildinfo +1 -1
  12. package/package.json +7 -7
  13. package/build/src/classes/DateEntry.d.mts +0 -47
  14. package/build/src/classes/DateEntry.mjs +0 -60
  15. package/build/src/classes/Entry.d.mts +0 -92
  16. package/build/src/classes/Entry.mjs +0 -107
  17. package/build/src/classes/entryTypes/Balance.d.mts +0 -32
  18. package/build/src/classes/entryTypes/Balance.mjs +0 -50
  19. package/build/src/classes/entryTypes/Blankline.d.mts +0 -24
  20. package/build/src/classes/entryTypes/Blankline.mjs +0 -38
  21. package/build/src/classes/entryTypes/Close.d.mts +0 -20
  22. package/build/src/classes/entryTypes/Close.mjs +0 -31
  23. package/build/src/classes/entryTypes/Comment.d.mts +0 -25
  24. package/build/src/classes/entryTypes/Comment.mjs +0 -42
  25. package/build/src/classes/entryTypes/Commodity.d.mts +0 -20
  26. package/build/src/classes/entryTypes/Commodity.mjs +0 -31
  27. package/build/src/classes/entryTypes/Custom.d.mts +0 -23
  28. package/build/src/classes/entryTypes/Custom.mjs +0 -38
  29. package/build/src/classes/entryTypes/Document.d.mts +0 -22
  30. package/build/src/classes/entryTypes/Document.mjs +0 -34
  31. package/build/src/classes/entryTypes/Event.d.mts +0 -23
  32. package/build/src/classes/entryTypes/Event.mjs +0 -34
  33. package/build/src/classes/entryTypes/Include.d.mts +0 -20
  34. package/build/src/classes/entryTypes/Include.mjs +0 -31
  35. package/build/src/classes/entryTypes/Note.d.mts +0 -22
  36. package/build/src/classes/entryTypes/Note.mjs +0 -34
  37. package/build/src/classes/entryTypes/Open.d.mts +0 -27
  38. package/build/src/classes/entryTypes/Open.mjs +0 -66
  39. package/build/src/classes/entryTypes/Option.d.mts +0 -23
  40. package/build/src/classes/entryTypes/Option.mjs +0 -32
  41. package/build/src/classes/entryTypes/Pad.d.mts +0 -22
  42. package/build/src/classes/entryTypes/Pad.mjs +0 -33
  43. package/build/src/classes/entryTypes/Plugin.d.mts +0 -22
  44. package/build/src/classes/entryTypes/Plugin.mjs +0 -36
  45. package/build/src/classes/entryTypes/Poptag.d.mts +0 -21
  46. package/build/src/classes/entryTypes/Poptag.mjs +0 -34
  47. package/build/src/classes/entryTypes/Price.d.mts +0 -32
  48. package/build/src/classes/entryTypes/Price.mjs +0 -57
  49. package/build/src/classes/entryTypes/Pushtag.d.mts +0 -21
  50. package/build/src/classes/entryTypes/Pushtag.mjs +0 -34
  51. package/build/src/classes/entryTypes/Query.d.mts +0 -22
  52. package/build/src/classes/entryTypes/Query.mjs +0 -34
  53. package/build/src/classes/entryTypes/Transaction/Posting.d.mts +0 -59
  54. package/build/src/classes/entryTypes/Transaction/Posting.mjs +0 -97
  55. package/build/src/classes/entryTypes/Transaction/Tag.d.mts +0 -28
  56. package/build/src/classes/entryTypes/Transaction/Tag.mjs +0 -28
  57. package/build/src/classes/entryTypes/Transaction/index.d.mts +0 -70
  58. package/build/src/classes/entryTypes/Transaction/index.mjs +0 -193
  59. package/build/src/classes/entryTypes/index.d.mts +0 -19
  60. package/build/src/classes/entryTypes/index.mjs +0 -19
  61. package/build/src/entryTypeToClass.d.mts +0 -73
  62. package/build/src/entryTypeToClass.mjs +0 -37
  63. package/build/src/entryTypes.d.mts +0 -10
  64. package/build/src/entryTypes.mjs +0 -29
  65. package/build/src/utils/splitStringIntoUnparsedEntries.d.ts +0 -14
  66. package/build/src/utils/splitStringIntoUnparsedEntries.js +0 -48
@@ -1,66 +0,0 @@
1
- import { assertEntryConstructor } from '../Entry.mjs';
2
- import { DateEntry } from '../DateEntry.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 entry that declares a new account.
8
- * Open directives define the beginning of an account's lifespan.
9
- */
10
- export class Open extends DateEntry {
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 entry 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 EntryConstructor pattern
66
- assertEntryConstructor(Open);
@@ -1,23 +0,0 @@
1
- import type { GenericParseResult } from '../../genericParse.mjs';
2
- import { Value } from '../Value.mjs';
3
- import { Entry } from '../Entry.mjs';
4
- /**
5
- * Represents an Option entry that sets a Beancount configuration option.
6
- * Option directives configure various aspects of Beancount's behavior.
7
- */
8
- export declare class Option extends Entry {
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 entry data
18
- * @returns A new Option instance
19
- */
20
- static fromGenericParseResult(genericParseResult: GenericParseResult): Option;
21
- /** @inheritdoc */
22
- toString(): string;
23
- }
@@ -1,32 +0,0 @@
1
- import { parseString, Value } from '../Value.mjs';
2
- import { stringAwareParseLine } from '../../utils/stringAwareParseLine.mjs';
3
- import { assertEntryConstructor, Entry } from '../Entry.mjs';
4
- /**
5
- * Represents an Option entry that sets a Beancount configuration option.
6
- * Option directives configure various aspects of Beancount's behavior.
7
- */
8
- export class Option extends Entry {
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 entry 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 EntryConstructor pattern
32
- assertEntryConstructor(Option);
@@ -1,22 +0,0 @@
1
- import type { GenericParseResultWithDate } from '../../genericParse.mjs';
2
- import { DateEntry } from '../DateEntry.mjs';
3
- /**
4
- * Represents a Pad entry that automatically balances accounts.
5
- * Pad directives insert transactions to bring an account to a specific balance.
6
- */
7
- export declare class Pad extends DateEntry {
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 entry data
17
- * @returns A new Pad instance
18
- */
19
- static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Pad;
20
- /** @inheritdoc */
21
- toString(): string;
22
- }
@@ -1,33 +0,0 @@
1
- import { assertEntryConstructor } from '../Entry.mjs';
2
- import { DateEntry } from '../DateEntry.mjs';
3
- import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
4
- /**
5
- * Represents a Pad entry that automatically balances accounts.
6
- * Pad directives insert transactions to bring an account to a specific balance.
7
- */
8
- export class Pad extends DateEntry {
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 entry 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 EntryConstructor pattern
33
- assertEntryConstructor(Pad);
@@ -1,22 +0,0 @@
1
- import type { GenericParseResult } from '../../genericParse.mjs';
2
- import { Entry } from '../Entry.mjs';
3
- /**
4
- * Represents a Plugin entry that loads a Beancount plugin.
5
- * Plugin directives enable plugins to process the ledger.
6
- */
7
- export declare class Plugin extends Entry {
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 entry data
17
- * @returns A new Plugin instance
18
- */
19
- static fromGenericParseResult(genericParseResult: GenericParseResult): Plugin;
20
- /** @inheritdoc */
21
- toString(): string;
22
- }
@@ -1,36 +0,0 @@
1
- import { parseString } from '../Value.mjs';
2
- import { stringAwareParseLine } from '../../utils/stringAwareParseLine.mjs';
3
- import { assertEntryConstructor, Entry } from '../Entry.mjs';
4
- /**
5
- * Represents a Plugin entry that loads a Beancount plugin.
6
- * Plugin directives enable plugins to process the ledger.
7
- */
8
- export class Plugin extends Entry {
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 entry 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 EntryConstructor pattern
36
- assertEntryConstructor(Plugin);
@@ -1,21 +0,0 @@
1
- import type { GenericParseResult } from '../../genericParse.mjs';
2
- import { Entry } from '../Entry.mjs';
3
- import { Tag } from './Transaction/Tag.mjs';
4
- /**
5
- * Represents a Poptag entry 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 Entry {
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 entry data
16
- * @returns A new Poptag instance
17
- */
18
- static fromGenericParseResult(genericParseResult: GenericParseResult): Poptag;
19
- /** @inheritdoc */
20
- toString(): string;
21
- }
@@ -1,34 +0,0 @@
1
- import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
2
- import { assertEntryConstructor, Entry } from '../Entry.mjs';
3
- import { Tag } from './Transaction/Tag.mjs';
4
- /**
5
- * Represents a Poptag entry 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 Entry {
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 entry 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 EntryConstructor pattern
34
- assertEntryConstructor(Poptag);
@@ -1,32 +0,0 @@
1
- import type { GenericParseResultWithDate } from '../../genericParse.mjs';
2
- import { DateEntry } from '../DateEntry.mjs';
3
- import { FormatOptions } from '../ParseResult.mjs';
4
- /**
5
- * Represents a Price entry 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 DateEntry {
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 entry 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
- }
@@ -1,57 +0,0 @@
1
- import { assertEntryConstructor } from '../Entry.mjs';
2
- import { DateEntry } from '../DateEntry.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 entry 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 DateEntry {
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 entry 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 EntryConstructor pattern
57
- assertEntryConstructor(Price);
@@ -1,21 +0,0 @@
1
- import type { GenericParseResult } from '../../genericParse.mjs';
2
- import { Entry } from '../Entry.mjs';
3
- import { Tag } from './Transaction/Tag.mjs';
4
- /**
5
- * Represents a Pushtag entry 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 Entry {
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 entry data
16
- * @returns A new Pushtag instance
17
- */
18
- static fromGenericParseResult(genericParseResult: GenericParseResult): Pushtag;
19
- /** @inheritdoc */
20
- toString(): string;
21
- }
@@ -1,34 +0,0 @@
1
- import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
2
- import { assertEntryConstructor, Entry } from '../Entry.mjs';
3
- import { Tag } from './Transaction/Tag.mjs';
4
- /**
5
- * Represents a Pushtag entry 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 Entry {
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 entry 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 EntryConstructor pattern
34
- assertEntryConstructor(Pushtag);
@@ -1,22 +0,0 @@
1
- import type { GenericParseResultWithDate } from '../../genericParse.mjs';
2
- import { DateEntry } from '../DateEntry.mjs';
3
- /**
4
- * Represents a Query entry that defines a named SQL query.
5
- * Query directives allow defining reusable queries in the Beancount file.
6
- */
7
- export declare class Query extends DateEntry {
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 entry data
17
- * @returns A new Query instance
18
- */
19
- static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Query;
20
- /** @inheritdoc */
21
- toString(): string;
22
- }
@@ -1,34 +0,0 @@
1
- import { assertEntryConstructor } from '../Entry.mjs';
2
- import { DateEntry } from '../DateEntry.mjs';
3
- import { stringAwareParseLine } from '../../utils/stringAwareParseLine.mjs';
4
- import { parseString } from '../Value.mjs';
5
- /**
6
- * Represents a Query entry that defines a named SQL query.
7
- * Query directives allow defining reusable queries in the Beancount file.
8
- */
9
- export class Query extends DateEntry {
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 entry 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 EntryConstructor pattern
34
- assertEntryConstructor(Query);
@@ -1,59 +0,0 @@
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
- }
@@ -1,97 +0,0 @@
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
- }