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.
- package/README.md +51 -22
- package/build/src/benchmark.mjs +1 -1
- package/build/src/classes/DatedNode.d.mts +40 -0
- package/build/src/classes/DatedNode.mjs +61 -0
- package/build/src/classes/Node.d.mts +92 -0
- package/build/src/classes/Node.mjs +107 -0
- package/build/src/classes/ParseResult.d.mts +75 -75
- package/build/src/classes/ParseResult.mjs +96 -98
- package/build/src/classes/nodes/Balance.d.mts +32 -0
- package/build/src/classes/nodes/Balance.mjs +50 -0
- package/build/src/classes/nodes/Blankline.d.mts +23 -0
- package/build/src/classes/nodes/Blankline.mjs +37 -0
- package/build/src/classes/nodes/Close.d.mts +20 -0
- package/build/src/classes/nodes/Close.mjs +31 -0
- package/build/src/classes/nodes/Comment.d.mts +25 -0
- package/build/src/classes/nodes/Comment.mjs +42 -0
- package/build/src/classes/nodes/Commodity.d.mts +20 -0
- package/build/src/classes/nodes/Commodity.mjs +31 -0
- package/build/src/classes/nodes/Custom.d.mts +23 -0
- package/build/src/classes/nodes/Custom.mjs +38 -0
- package/build/src/classes/nodes/Document.d.mts +22 -0
- package/build/src/classes/nodes/Document.mjs +34 -0
- package/build/src/classes/nodes/Event.d.mts +23 -0
- package/build/src/classes/nodes/Event.mjs +34 -0
- package/build/src/classes/nodes/Include.d.mts +20 -0
- package/build/src/classes/nodes/Include.mjs +31 -0
- package/build/src/classes/nodes/Note.d.mts +22 -0
- package/build/src/classes/nodes/Note.mjs +34 -0
- package/build/src/classes/nodes/Open.d.mts +27 -0
- package/build/src/classes/nodes/Open.mjs +66 -0
- package/build/src/classes/nodes/Option.d.mts +23 -0
- package/build/src/classes/nodes/Option.mjs +32 -0
- package/build/src/classes/nodes/Pad.d.mts +22 -0
- package/build/src/classes/nodes/Pad.mjs +33 -0
- package/build/src/classes/nodes/Plugin.d.mts +22 -0
- package/build/src/classes/nodes/Plugin.mjs +36 -0
- package/build/src/classes/nodes/Poptag.d.mts +21 -0
- package/build/src/classes/nodes/Poptag.mjs +34 -0
- package/build/src/classes/nodes/Price.d.mts +32 -0
- package/build/src/classes/nodes/Price.mjs +57 -0
- package/build/src/classes/nodes/Pushtag.d.mts +21 -0
- package/build/src/classes/nodes/Pushtag.mjs +34 -0
- package/build/src/classes/nodes/Query.d.mts +22 -0
- package/build/src/classes/nodes/Query.mjs +34 -0
- package/build/src/classes/nodes/Transaction/Posting.d.mts +59 -0
- package/build/src/classes/nodes/Transaction/Posting.mjs +97 -0
- package/build/src/classes/nodes/Transaction/Tag.d.mts +28 -0
- package/build/src/classes/nodes/Transaction/Tag.mjs +28 -0
- package/build/src/classes/nodes/Transaction/index.d.mts +70 -0
- package/build/src/classes/nodes/Transaction/index.mjs +193 -0
- package/build/src/classes/nodes/index.d.mts +19 -0
- package/build/src/classes/nodes/index.mjs +19 -0
- package/build/src/cli.mjs +4 -4
- package/build/src/deserialize.d.mts +54 -54
- package/build/src/deserialize.mjs +89 -89
- package/build/src/directiveTypes.d.mts +10 -0
- package/build/src/directiveTypes.mjs +29 -0
- package/build/src/genericParse.d.mts +27 -20
- package/build/src/genericParse.mjs +30 -30
- package/build/src/main.d.mts +30 -31
- package/build/src/main.mjs +30 -30
- package/build/src/nodeTypeToClass.d.mts +81 -0
- package/build/src/nodeTypeToClass.mjs +37 -0
- package/build/src/parse.d.mts +16 -16
- package/build/src/parse.mjs +37 -37
- package/build/src/parseFile.d.mts +2 -2
- package/build/src/parseFile.mjs +11 -11
- package/build/src/utils/splitStringIntoSourceFragments.d.ts +14 -0
- package/build/src/utils/splitStringIntoSourceFragments.js +48 -0
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +7 -7
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type { Transaction } from './
|
|
5
|
-
import type { Balance } from './
|
|
6
|
-
import type { Close } from './
|
|
7
|
-
import type { Commodity } from './
|
|
8
|
-
import type { Custom } from './
|
|
9
|
-
import type { Document } from './
|
|
10
|
-
import type { Event } from './
|
|
11
|
-
import type { Include } from './
|
|
12
|
-
import type { Note } from './
|
|
13
|
-
import type { Open } from './
|
|
14
|
-
import type { Option } from './
|
|
15
|
-
import type { Pad } from './
|
|
16
|
-
import type { Plugin } from './
|
|
17
|
-
import type { Poptag } from './
|
|
18
|
-
import type { Price } from './
|
|
19
|
-
import type { Pushtag } from './
|
|
20
|
-
import type { Query } from './
|
|
21
|
-
import type { Comment } from './
|
|
22
|
-
import type { Blankline } from './
|
|
2
|
+
import { NodeType } from '../nodeTypeToClass.mjs';
|
|
3
|
+
import { Node } from './Node.mjs';
|
|
4
|
+
import type { Transaction } from './nodes/Transaction/index.mjs';
|
|
5
|
+
import type { Balance } from './nodes/Balance.mjs';
|
|
6
|
+
import type { Close } from './nodes/Close.mjs';
|
|
7
|
+
import type { Commodity } from './nodes/Commodity.mjs';
|
|
8
|
+
import type { Custom } from './nodes/Custom.mjs';
|
|
9
|
+
import type { Document } from './nodes/Document.mjs';
|
|
10
|
+
import type { Event } from './nodes/Event.mjs';
|
|
11
|
+
import type { Include } from './nodes/Include.mjs';
|
|
12
|
+
import type { Note } from './nodes/Note.mjs';
|
|
13
|
+
import type { Open } from './nodes/Open.mjs';
|
|
14
|
+
import type { Option } from './nodes/Option.mjs';
|
|
15
|
+
import type { Pad } from './nodes/Pad.mjs';
|
|
16
|
+
import type { Plugin } from './nodes/Plugin.mjs';
|
|
17
|
+
import type { Poptag } from './nodes/Poptag.mjs';
|
|
18
|
+
import type { Price } from './nodes/Price.mjs';
|
|
19
|
+
import type { Pushtag } from './nodes/Pushtag.mjs';
|
|
20
|
+
import type { Query } from './nodes/Query.mjs';
|
|
21
|
+
import type { Comment } from './nodes/Comment.mjs';
|
|
22
|
+
import type { Blankline } from './nodes/Blankline.mjs';
|
|
23
23
|
export interface ParseResultObj {
|
|
24
|
-
|
|
25
|
-
type:
|
|
24
|
+
nodes: {
|
|
25
|
+
type: NodeType;
|
|
26
26
|
}[];
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
@@ -47,115 +47,115 @@ export interface CalculateCurrencyColumnOptions {
|
|
|
47
47
|
minPadding?: number;
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
* Container class for
|
|
51
|
-
* Provides methods for converting
|
|
50
|
+
* Container class for the result of a parse.
|
|
51
|
+
* Provides methods for converting node back to string format.
|
|
52
52
|
*/
|
|
53
53
|
export declare class ParseResult {
|
|
54
|
-
|
|
54
|
+
nodes: Node[];
|
|
55
55
|
/**
|
|
56
56
|
* Creates a new ParseResult instance.
|
|
57
|
-
* @param
|
|
57
|
+
* @param nodes - Array of parsed nodes
|
|
58
58
|
*/
|
|
59
|
-
constructor(
|
|
59
|
+
constructor(nodes: Node[]);
|
|
60
60
|
/**
|
|
61
|
-
* Gets all transaction
|
|
62
|
-
* @returns Array of
|
|
61
|
+
* Gets all transaction nodes from the parsed nodes.
|
|
62
|
+
* @returns Array of nodes that correspond to transaction directives
|
|
63
63
|
*/
|
|
64
64
|
get transactions(): Transaction[];
|
|
65
65
|
/**
|
|
66
|
-
* Gets all balance
|
|
67
|
-
* @returns Array of
|
|
66
|
+
* Gets all balance nodes from the parsed nodes.
|
|
67
|
+
* @returns Array of nodes that correspond to balance directives
|
|
68
68
|
*/
|
|
69
69
|
get balance(): Balance[];
|
|
70
70
|
/**
|
|
71
|
-
* Gets all close
|
|
72
|
-
* @returns Array of
|
|
71
|
+
* Gets all close nodes from the parsed nodes.
|
|
72
|
+
* @returns Array of nodes that correspond to close directives
|
|
73
73
|
*/
|
|
74
74
|
get close(): Close[];
|
|
75
75
|
/**
|
|
76
|
-
* Gets all commodity
|
|
77
|
-
* @returns Array of
|
|
76
|
+
* Gets all commodity nodes from the parsed nodes.
|
|
77
|
+
* @returns Array of nodes that correspond to commodity directives
|
|
78
78
|
*/
|
|
79
79
|
get commodity(): Commodity[];
|
|
80
80
|
/**
|
|
81
|
-
* Gets all custom
|
|
82
|
-
* @returns Array of
|
|
81
|
+
* Gets all custom nodes from the parsed nodes.
|
|
82
|
+
* @returns Array of nodes that correspond to custom directives
|
|
83
83
|
*/
|
|
84
84
|
get custom(): Custom[];
|
|
85
85
|
/**
|
|
86
|
-
* Gets all document
|
|
87
|
-
* @returns Array of
|
|
86
|
+
* Gets all document nodes from the parsed nodes.
|
|
87
|
+
* @returns Array of nodes that correspond to document directives
|
|
88
88
|
*/
|
|
89
89
|
get document(): Document[];
|
|
90
90
|
/**
|
|
91
|
-
* Gets all event
|
|
92
|
-
* @returns Array of
|
|
91
|
+
* Gets all event nodes from the parsed nodes.
|
|
92
|
+
* @returns Array of nodes that correspond to event directives
|
|
93
93
|
*/
|
|
94
94
|
get event(): Event[];
|
|
95
95
|
/**
|
|
96
|
-
* Gets all include
|
|
97
|
-
* @returns Array of
|
|
96
|
+
* Gets all include nodes from the parsed nodes.
|
|
97
|
+
* @returns Array of nodes that correspond to include directives
|
|
98
98
|
*/
|
|
99
99
|
get include(): Include[];
|
|
100
100
|
/**
|
|
101
|
-
* Gets all note
|
|
102
|
-
* @returns Array of
|
|
101
|
+
* Gets all note nodes from the parsed nodes.
|
|
102
|
+
* @returns Array of nodes that correspond to note directives
|
|
103
103
|
*/
|
|
104
104
|
get note(): Note[];
|
|
105
105
|
/**
|
|
106
|
-
* Gets all open
|
|
107
|
-
* @returns Array of
|
|
106
|
+
* Gets all open nodes from the parsed nodes.
|
|
107
|
+
* @returns Array of nodes that correspond to open directives
|
|
108
108
|
*/
|
|
109
109
|
get open(): Open[];
|
|
110
110
|
/**
|
|
111
|
-
* Gets all option
|
|
112
|
-
* @returns Array of
|
|
111
|
+
* Gets all option nodes from the parsed nodes.
|
|
112
|
+
* @returns Array of nodes that correspond to option directives
|
|
113
113
|
*/
|
|
114
114
|
get option(): Option[];
|
|
115
115
|
/**
|
|
116
|
-
* Gets all pad
|
|
117
|
-
* @returns Array of
|
|
116
|
+
* Gets all pad nodes from the parsed nodes.
|
|
117
|
+
* @returns Array of nodes that correspond to pad directives
|
|
118
118
|
*/
|
|
119
119
|
get pad(): Pad[];
|
|
120
120
|
/**
|
|
121
|
-
* Gets all plugin
|
|
122
|
-
* @returns Array of
|
|
121
|
+
* Gets all plugin nodes from the parsed nodes.
|
|
122
|
+
* @returns Array of nodes that correspond to plugin directives
|
|
123
123
|
*/
|
|
124
124
|
get plugin(): Plugin[];
|
|
125
125
|
/**
|
|
126
|
-
* Gets all poptag
|
|
127
|
-
* @returns Array of
|
|
126
|
+
* Gets all poptag nodes from the parsed nodes.
|
|
127
|
+
* @returns Array of nodes that correspond to poptag directives
|
|
128
128
|
*/
|
|
129
129
|
get poptag(): Poptag[];
|
|
130
130
|
/**
|
|
131
|
-
* Gets all price
|
|
132
|
-
* @returns Array of
|
|
131
|
+
* Gets all price nodes from the parsed nodes.
|
|
132
|
+
* @returns Array of nodes that correspond to price directives
|
|
133
133
|
*/
|
|
134
134
|
get price(): Price[];
|
|
135
135
|
/**
|
|
136
|
-
* Gets all pushtag
|
|
137
|
-
* @returns Array of
|
|
136
|
+
* Gets all pushtag nodes from the parsed nodes.
|
|
137
|
+
* @returns Array of nodes that correspond to pushtag directives
|
|
138
138
|
*/
|
|
139
139
|
get pushtag(): Pushtag[];
|
|
140
140
|
/**
|
|
141
|
-
* Gets all query
|
|
142
|
-
* @returns Array of
|
|
141
|
+
* Gets all query nodes from the parsed nodes.
|
|
142
|
+
* @returns Array of nodes that correspond to query directives
|
|
143
143
|
*/
|
|
144
144
|
get query(): Query[];
|
|
145
145
|
/**
|
|
146
|
-
* Gets all comment
|
|
147
|
-
* @returns Array of
|
|
146
|
+
* Gets all comment nodes from the parsed nodes.
|
|
147
|
+
* @returns Array of nodes that correspond to a comments
|
|
148
148
|
*/
|
|
149
149
|
get comment(): Comment[];
|
|
150
150
|
/**
|
|
151
|
-
* Gets all blankline
|
|
152
|
-
* @returns Array of
|
|
151
|
+
* Gets all blankline nodes from the parsed nodes.
|
|
152
|
+
* @returns Array of nodes that correspond to a blank line
|
|
153
153
|
*/
|
|
154
154
|
get blankline(): Blankline[];
|
|
155
155
|
/**
|
|
156
|
-
* Gets all unique account names used across all
|
|
156
|
+
* Gets all unique account names used across all directives.
|
|
157
157
|
* Extracts accounts from transactions (via postings), open, close,
|
|
158
|
-
* balance, pad, note, and document
|
|
158
|
+
* balance, pad, note, and document nodes.
|
|
159
159
|
* @returns Set of unique account names
|
|
160
160
|
*/
|
|
161
161
|
get accounts(): Set<string>;
|
|
@@ -170,14 +170,14 @@ export declare class ParseResult {
|
|
|
170
170
|
*/
|
|
171
171
|
accountsActiveAt(date: Temporal.PlainDate): Set<string>;
|
|
172
172
|
/**
|
|
173
|
-
* Converts all
|
|
174
|
-
* Each
|
|
173
|
+
* Converts all nodes to their string representation.
|
|
174
|
+
* Each node is converted using its toString() method and joined with newlines.
|
|
175
175
|
* @returns The complete Beancount file content as a string
|
|
176
176
|
*/
|
|
177
177
|
toString(): string;
|
|
178
178
|
/**
|
|
179
|
-
* Converts all
|
|
180
|
-
* Uses each
|
|
179
|
+
* Converts all nodes to a formatted string with aligned columns.
|
|
180
|
+
* Uses each node's toFormattedString() method for consistent formatting.
|
|
181
181
|
* @param formatOptions - Formatting options
|
|
182
182
|
*
|
|
183
183
|
* @returns The formatted Beancount file content as a string
|
|
@@ -194,11 +194,11 @@ export declare class ParseResult {
|
|
|
194
194
|
static fromJSON(jsonString: string): ParseResult;
|
|
195
195
|
/**
|
|
196
196
|
* Creates a ParseResult instance from a plain JavaScript object.
|
|
197
|
-
* Deserializes each
|
|
197
|
+
* Deserializes each node by mapping it to the appropriate Node class based on its type.
|
|
198
198
|
*
|
|
199
199
|
* @param obj - Plain object representation of a ParseResult
|
|
200
|
-
* @returns A new ParseResult instance with deserialized
|
|
201
|
-
* @throws {Error} If an
|
|
200
|
+
* @returns A new ParseResult instance with deserialized nodes
|
|
201
|
+
* @throws {Error} If an node has an unknown type with no corresponding node class
|
|
202
202
|
* @remarks **Warning:** No validation is performed on the input object. We assume the object is valid and well-formed.
|
|
203
203
|
*/
|
|
204
204
|
static fromJSONData(obj: ParseResultObj): ParseResult;
|
|
@@ -1,165 +1,165 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
|
-
import {
|
|
2
|
+
import { nodeTypeToClass } from '../nodeTypeToClass.mjs';
|
|
3
3
|
export const defaultFormatOptions = {
|
|
4
4
|
currencyColumn: 59,
|
|
5
5
|
};
|
|
6
6
|
/**
|
|
7
|
-
* Container class for
|
|
8
|
-
* Provides methods for converting
|
|
7
|
+
* Container class for the result of a parse.
|
|
8
|
+
* Provides methods for converting node back to string format.
|
|
9
9
|
*/
|
|
10
10
|
export class ParseResult {
|
|
11
11
|
/**
|
|
12
12
|
* Creates a new ParseResult instance.
|
|
13
|
-
* @param
|
|
13
|
+
* @param nodes - Array of parsed nodes
|
|
14
14
|
*/
|
|
15
|
-
constructor(
|
|
16
|
-
this.
|
|
15
|
+
constructor(nodes) {
|
|
16
|
+
this.nodes = nodes;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* Gets all transaction
|
|
20
|
-
* @returns Array of
|
|
19
|
+
* Gets all transaction nodes from the parsed nodes.
|
|
20
|
+
* @returns Array of nodes that correspond to transaction directives
|
|
21
21
|
*/
|
|
22
22
|
get transactions() {
|
|
23
|
-
return this.
|
|
23
|
+
return this.nodes.filter((node) => node.type === 'transaction');
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
* Gets all balance
|
|
27
|
-
* @returns Array of
|
|
26
|
+
* Gets all balance nodes from the parsed nodes.
|
|
27
|
+
* @returns Array of nodes that correspond to balance directives
|
|
28
28
|
*/
|
|
29
29
|
get balance() {
|
|
30
|
-
return this.
|
|
30
|
+
return this.nodes.filter((node) => node.type === 'balance');
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
|
-
* Gets all close
|
|
34
|
-
* @returns Array of
|
|
33
|
+
* Gets all close nodes from the parsed nodes.
|
|
34
|
+
* @returns Array of nodes that correspond to close directives
|
|
35
35
|
*/
|
|
36
36
|
get close() {
|
|
37
|
-
return this.
|
|
37
|
+
return this.nodes.filter((node) => node.type === 'close');
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
|
-
* Gets all commodity
|
|
41
|
-
* @returns Array of
|
|
40
|
+
* Gets all commodity nodes from the parsed nodes.
|
|
41
|
+
* @returns Array of nodes that correspond to commodity directives
|
|
42
42
|
*/
|
|
43
43
|
get commodity() {
|
|
44
|
-
return this.
|
|
44
|
+
return this.nodes.filter((node) => node.type === 'commodity');
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
|
-
* Gets all custom
|
|
48
|
-
* @returns Array of
|
|
47
|
+
* Gets all custom nodes from the parsed nodes.
|
|
48
|
+
* @returns Array of nodes that correspond to custom directives
|
|
49
49
|
*/
|
|
50
50
|
get custom() {
|
|
51
|
-
return this.
|
|
51
|
+
return this.nodes.filter((node) => node.type === 'custom');
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* Gets all document
|
|
55
|
-
* @returns Array of
|
|
54
|
+
* Gets all document nodes from the parsed nodes.
|
|
55
|
+
* @returns Array of nodes that correspond to document directives
|
|
56
56
|
*/
|
|
57
57
|
get document() {
|
|
58
|
-
return this.
|
|
58
|
+
return this.nodes.filter((node) => node.type === 'document');
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
|
-
* Gets all event
|
|
62
|
-
* @returns Array of
|
|
61
|
+
* Gets all event nodes from the parsed nodes.
|
|
62
|
+
* @returns Array of nodes that correspond to event directives
|
|
63
63
|
*/
|
|
64
64
|
get event() {
|
|
65
|
-
return this.
|
|
65
|
+
return this.nodes.filter((node) => node.type === 'event');
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
|
-
* Gets all include
|
|
69
|
-
* @returns Array of
|
|
68
|
+
* Gets all include nodes from the parsed nodes.
|
|
69
|
+
* @returns Array of nodes that correspond to include directives
|
|
70
70
|
*/
|
|
71
71
|
get include() {
|
|
72
|
-
return this.
|
|
72
|
+
return this.nodes.filter((node) => node.type === 'include');
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
|
-
* Gets all note
|
|
76
|
-
* @returns Array of
|
|
75
|
+
* Gets all note nodes from the parsed nodes.
|
|
76
|
+
* @returns Array of nodes that correspond to note directives
|
|
77
77
|
*/
|
|
78
78
|
get note() {
|
|
79
|
-
return this.
|
|
79
|
+
return this.nodes.filter((node) => node.type === 'note');
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
* Gets all open
|
|
83
|
-
* @returns Array of
|
|
82
|
+
* Gets all open nodes from the parsed nodes.
|
|
83
|
+
* @returns Array of nodes that correspond to open directives
|
|
84
84
|
*/
|
|
85
85
|
get open() {
|
|
86
|
-
return this.
|
|
86
|
+
return this.nodes.filter((node) => node.type === 'open');
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
|
-
* Gets all option
|
|
90
|
-
* @returns Array of
|
|
89
|
+
* Gets all option nodes from the parsed nodes.
|
|
90
|
+
* @returns Array of nodes that correspond to option directives
|
|
91
91
|
*/
|
|
92
92
|
get option() {
|
|
93
|
-
return this.
|
|
93
|
+
return this.nodes.filter((node) => node.type === 'option');
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
|
-
* Gets all pad
|
|
97
|
-
* @returns Array of
|
|
96
|
+
* Gets all pad nodes from the parsed nodes.
|
|
97
|
+
* @returns Array of nodes that correspond to pad directives
|
|
98
98
|
*/
|
|
99
99
|
get pad() {
|
|
100
|
-
return this.
|
|
100
|
+
return this.nodes.filter((node) => node.type === 'pad');
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
|
-
* Gets all plugin
|
|
104
|
-
* @returns Array of
|
|
103
|
+
* Gets all plugin nodes from the parsed nodes.
|
|
104
|
+
* @returns Array of nodes that correspond to plugin directives
|
|
105
105
|
*/
|
|
106
106
|
get plugin() {
|
|
107
|
-
return this.
|
|
107
|
+
return this.nodes.filter((node) => node.type === 'plugin');
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
|
-
* Gets all poptag
|
|
111
|
-
* @returns Array of
|
|
110
|
+
* Gets all poptag nodes from the parsed nodes.
|
|
111
|
+
* @returns Array of nodes that correspond to poptag directives
|
|
112
112
|
*/
|
|
113
113
|
get poptag() {
|
|
114
|
-
return this.
|
|
114
|
+
return this.nodes.filter((node) => node.type === 'poptag');
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
|
-
* Gets all price
|
|
118
|
-
* @returns Array of
|
|
117
|
+
* Gets all price nodes from the parsed nodes.
|
|
118
|
+
* @returns Array of nodes that correspond to price directives
|
|
119
119
|
*/
|
|
120
120
|
get price() {
|
|
121
|
-
return this.
|
|
121
|
+
return this.nodes.filter((node) => node.type === 'price');
|
|
122
122
|
}
|
|
123
123
|
/**
|
|
124
|
-
* Gets all pushtag
|
|
125
|
-
* @returns Array of
|
|
124
|
+
* Gets all pushtag nodes from the parsed nodes.
|
|
125
|
+
* @returns Array of nodes that correspond to pushtag directives
|
|
126
126
|
*/
|
|
127
127
|
get pushtag() {
|
|
128
|
-
return this.
|
|
128
|
+
return this.nodes.filter((node) => node.type === 'pushtag');
|
|
129
129
|
}
|
|
130
130
|
/**
|
|
131
|
-
* Gets all query
|
|
132
|
-
* @returns Array of
|
|
131
|
+
* Gets all query nodes from the parsed nodes.
|
|
132
|
+
* @returns Array of nodes that correspond to query directives
|
|
133
133
|
*/
|
|
134
134
|
get query() {
|
|
135
|
-
return this.
|
|
135
|
+
return this.nodes.filter((node) => node.type === 'query');
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
|
-
* Gets all comment
|
|
139
|
-
* @returns Array of
|
|
138
|
+
* Gets all comment nodes from the parsed nodes.
|
|
139
|
+
* @returns Array of nodes that correspond to a comments
|
|
140
140
|
*/
|
|
141
141
|
get comment() {
|
|
142
|
-
return this.
|
|
142
|
+
return this.nodes.filter((node) => node.type === 'comment');
|
|
143
143
|
}
|
|
144
144
|
/**
|
|
145
|
-
* Gets all blankline
|
|
146
|
-
* @returns Array of
|
|
145
|
+
* Gets all blankline nodes from the parsed nodes.
|
|
146
|
+
* @returns Array of nodes that correspond to a blank line
|
|
147
147
|
*/
|
|
148
148
|
get blankline() {
|
|
149
|
-
return this.
|
|
149
|
+
return this.nodes.filter((node) => node.type === 'blankline');
|
|
150
150
|
}
|
|
151
151
|
/**
|
|
152
|
-
* Gets all unique account names used across all
|
|
152
|
+
* Gets all unique account names used across all directives.
|
|
153
153
|
* Extracts accounts from transactions (via postings), open, close,
|
|
154
|
-
* balance, pad, note, and document
|
|
154
|
+
* balance, pad, note, and document nodes.
|
|
155
155
|
* @returns Set of unique account names
|
|
156
156
|
*/
|
|
157
157
|
get accounts() {
|
|
158
158
|
const accountSet = new Set();
|
|
159
|
-
for (const
|
|
160
|
-
switch (
|
|
159
|
+
for (const node of this.nodes) {
|
|
160
|
+
switch (node.type) {
|
|
161
161
|
case 'transaction':
|
|
162
|
-
for (const posting of
|
|
162
|
+
for (const posting of node.postings) {
|
|
163
163
|
accountSet.add(posting.account);
|
|
164
164
|
}
|
|
165
165
|
break;
|
|
@@ -168,10 +168,10 @@ export class ParseResult {
|
|
|
168
168
|
case 'balance':
|
|
169
169
|
case 'note':
|
|
170
170
|
case 'document':
|
|
171
|
-
accountSet.add(
|
|
171
|
+
accountSet.add(node.account);
|
|
172
172
|
break;
|
|
173
173
|
case 'pad': {
|
|
174
|
-
const pad =
|
|
174
|
+
const pad = node;
|
|
175
175
|
accountSet.add(pad.account);
|
|
176
176
|
accountSet.add(pad.accountPad);
|
|
177
177
|
break;
|
|
@@ -192,13 +192,13 @@ export class ParseResult {
|
|
|
192
192
|
accountsActiveAt(date) {
|
|
193
193
|
const openedAccounts = new Map();
|
|
194
194
|
const closedAccounts = new Map();
|
|
195
|
-
for (const
|
|
196
|
-
if (
|
|
197
|
-
const open =
|
|
195
|
+
for (const node of this.nodes) {
|
|
196
|
+
if (node.type === 'open') {
|
|
197
|
+
const open = node;
|
|
198
198
|
openedAccounts.set(open.account, open.date);
|
|
199
199
|
}
|
|
200
|
-
else if (
|
|
201
|
-
const close =
|
|
200
|
+
else if (node.type === 'close') {
|
|
201
|
+
const close = node;
|
|
202
202
|
closedAccounts.set(close.account, close.date);
|
|
203
203
|
}
|
|
204
204
|
}
|
|
@@ -214,25 +214,23 @@ export class ParseResult {
|
|
|
214
214
|
return activeSet;
|
|
215
215
|
}
|
|
216
216
|
/**
|
|
217
|
-
* Converts all
|
|
218
|
-
* Each
|
|
217
|
+
* Converts all nodes to their string representation.
|
|
218
|
+
* Each node is converted using its toString() method and joined with newlines.
|
|
219
219
|
* @returns The complete Beancount file content as a string
|
|
220
220
|
*/
|
|
221
221
|
toString() {
|
|
222
222
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
223
|
-
return this.
|
|
223
|
+
return this.nodes.map((e) => e.toString()).join('\n');
|
|
224
224
|
}
|
|
225
225
|
/**
|
|
226
|
-
* Converts all
|
|
227
|
-
* Uses each
|
|
226
|
+
* Converts all nodes to a formatted string with aligned columns.
|
|
227
|
+
* Uses each node's toFormattedString() method for consistent formatting.
|
|
228
228
|
* @param formatOptions - Formatting options
|
|
229
229
|
*
|
|
230
230
|
* @returns The formatted Beancount file content as a string
|
|
231
231
|
*/
|
|
232
232
|
toFormattedString(formatOptions = defaultFormatOptions) {
|
|
233
|
-
return this.
|
|
234
|
-
.map((e) => e.toFormattedString(formatOptions))
|
|
235
|
-
.join('\n');
|
|
233
|
+
return this.nodes.map((e) => e.toFormattedString(formatOptions)).join('\n');
|
|
236
234
|
}
|
|
237
235
|
/**
|
|
238
236
|
* Creates an ParseResult instance from JSON data.
|
|
@@ -247,27 +245,27 @@ export class ParseResult {
|
|
|
247
245
|
}
|
|
248
246
|
/**
|
|
249
247
|
* Creates a ParseResult instance from a plain JavaScript object.
|
|
250
|
-
* Deserializes each
|
|
248
|
+
* Deserializes each node by mapping it to the appropriate Node class based on its type.
|
|
251
249
|
*
|
|
252
250
|
* @param obj - Plain object representation of a ParseResult
|
|
253
|
-
* @returns A new ParseResult instance with deserialized
|
|
254
|
-
* @throws {Error} If an
|
|
251
|
+
* @returns A new ParseResult instance with deserialized nodes
|
|
252
|
+
* @throws {Error} If an node has an unknown type with no corresponding node class
|
|
255
253
|
* @remarks **Warning:** No validation is performed on the input object. We assume the object is valid and well-formed.
|
|
256
254
|
*/
|
|
257
255
|
static fromJSONData(obj) {
|
|
258
|
-
const
|
|
259
|
-
const
|
|
260
|
-
const { type } =
|
|
261
|
-
const
|
|
262
|
-
if (!
|
|
263
|
-
throw new Error(`No
|
|
256
|
+
const nodeObjects = obj.nodes;
|
|
257
|
+
const nodes = nodeObjects.map((nodeObj) => {
|
|
258
|
+
const { type } = nodeObj;
|
|
259
|
+
const NodeClass = nodeTypeToClass[type];
|
|
260
|
+
if (!NodeClass) {
|
|
261
|
+
throw new Error(`No class found for type ${type} while creating nodes`);
|
|
264
262
|
}
|
|
265
|
-
// Type assertion needed because TypeScript can't verify that all
|
|
263
|
+
// Type assertion needed because TypeScript can't verify that all node classes
|
|
266
264
|
// in the union type have compatible constructor signatures
|
|
267
265
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
268
|
-
return
|
|
266
|
+
return NodeClass.fromJSONData(nodeObj);
|
|
269
267
|
});
|
|
270
|
-
return new ParseResult(
|
|
268
|
+
return new ParseResult(nodes);
|
|
271
269
|
}
|
|
272
270
|
/**
|
|
273
271
|
* Calculates the optimal currency column position for formatting.
|
|
@@ -303,10 +301,10 @@ export class ParseResult {
|
|
|
303
301
|
if (transactions.length === 0) {
|
|
304
302
|
return defaultFormatOptions.currencyColumn;
|
|
305
303
|
}
|
|
306
|
-
// Extract all postings
|
|
304
|
+
// Extract all postings from the transactions
|
|
307
305
|
const allPostings = [];
|
|
308
|
-
for (const
|
|
309
|
-
allPostings.push(...
|
|
306
|
+
for (const transaction of transactions) {
|
|
307
|
+
allPostings.push(...transaction.postings);
|
|
310
308
|
}
|
|
311
309
|
// Edge case: No postings
|
|
312
310
|
if (allPostings.length === 0) {
|
|
@@ -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 Balance assertion node.
|
|
6
|
+
* Balance directives assert that an account has a specific balance at a given date.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Balance extends DatedNode {
|
|
9
|
+
/** @inheritdoc */
|
|
10
|
+
type: "balance";
|
|
11
|
+
/** The account name for the balance assertion */
|
|
12
|
+
account: string;
|
|
13
|
+
/** The expected amount */
|
|
14
|
+
amount: string;
|
|
15
|
+
/** The currency of the amount */
|
|
16
|
+
currency: 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 Balance instance from a generic parse result.
|
|
24
|
+
* @param genericParseResult - The parsed balance entry data
|
|
25
|
+
* @returns A new Balance instance
|
|
26
|
+
*/
|
|
27
|
+
static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Balance;
|
|
28
|
+
/** @inheritdoc */
|
|
29
|
+
toString(): string;
|
|
30
|
+
/** @inheritdoc */
|
|
31
|
+
toFormattedString(formatOptions?: FormatOptions): string;
|
|
32
|
+
}
|