beancount 0.1.0 → 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 +15 -16
- 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 +2 -2
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm install beancount
|
|
|
14
14
|
|
|
15
15
|
- **Full Beancount Support** - All directives supported: transactions, open/close, balance, pad, note, document, price, event, query, custom, commodity, include, option, plugin, pushtag/poptag
|
|
16
16
|
- **Full Transaction Support** - Tags, links, metadata, costs, price annotations, and postings
|
|
17
|
-
- **Type-Safe** - Complete TypeScript types
|
|
17
|
+
- **Type-Safe** - Complete TypeScript types
|
|
18
18
|
- **Platform Agnostic** - Works in Node.js, browsers, Deno, and other JavaScript runtimes
|
|
19
19
|
- **Round-Trip Parsing** - Parse to objects and serialize back to text
|
|
20
20
|
- **Recursive File Parsing** - Option to automatically follow and merge `include` directives with circular reference protection
|
|
@@ -24,7 +24,7 @@ npm install beancount
|
|
|
24
24
|
## Quick Start
|
|
25
25
|
|
|
26
26
|
```typescript
|
|
27
|
-
import { parse
|
|
27
|
+
import { parse } from './main.mjs'
|
|
28
28
|
|
|
29
29
|
const beancountContent = `
|
|
30
30
|
2024-01-01 open Assets:Checking USD
|
|
@@ -36,22 +36,21 @@ const beancountContent = `
|
|
|
36
36
|
|
|
37
37
|
const result = parse(beancountContent)
|
|
38
38
|
|
|
39
|
-
// Access parsed
|
|
40
|
-
console.log(result.
|
|
39
|
+
// Access parsed directives
|
|
40
|
+
console.log(result.nodes.length) // 5
|
|
41
|
+
console.log(result.nodes)
|
|
41
42
|
|
|
42
|
-
//
|
|
43
|
-
|
|
43
|
+
// Edit parsed directives
|
|
44
|
+
result.transactions[0].narration = 'Trip to the supermarket'
|
|
45
|
+
result.transactions[0].postings.forEach((p) => (p.currency = 'EUR'))
|
|
44
46
|
|
|
45
|
-
//
|
|
46
|
-
const resultJSON = JSON.stringify(result)
|
|
47
|
-
console.log(resultJSON)
|
|
48
|
-
|
|
49
|
-
// Convert back to parsed entries
|
|
50
|
-
console.log(ParseResult.fromJSON(result))
|
|
51
|
-
|
|
52
|
-
// Or with formatted output (aligned columns)
|
|
53
|
-
// only partially implemented at this point
|
|
47
|
+
// Output formatted
|
|
54
48
|
console.log(result.toFormattedString())
|
|
49
|
+
/* outputs:
|
|
50
|
+
2024-01-15 * "Grocery Store" "Trip to the supermarket"
|
|
51
|
+
Assets:Checking -50.00 EUR
|
|
52
|
+
Expenses:Food 50.00 EUR
|
|
53
|
+
*/
|
|
55
54
|
```
|
|
56
55
|
|
|
57
56
|
## Parsing Files
|
|
@@ -68,7 +67,7 @@ const result = await parseFile('/path/to/ledger.beancount')
|
|
|
68
67
|
const result = await parseFile('/path/to/main.beancount', { recurse: true })
|
|
69
68
|
```
|
|
70
69
|
|
|
71
|
-
When `recurse: true`, the parser follows all `include` directives and merges the
|
|
70
|
+
When `recurse: true`, the parser follows all `include` directives and merges the directives from included files into the result. Circular includes are handled gracefully (each file is only parsed once).
|
|
72
71
|
|
|
73
72
|
## Browser Usage
|
|
74
73
|
|
package/build/src/benchmark.mjs
CHANGED
|
@@ -25,4 +25,4 @@ console.log(`Average parse time: ${avgDuration.toFixed(3)} ms`);
|
|
|
25
25
|
console.log(`Min: ${minDuration.toFixed(3)} ms, Max: ${maxDuration.toFixed(3)} ms`);
|
|
26
26
|
console.log(`Average throughput: ${(fileSizeKB / avgDurationSeconds / 1000).toFixed(2)} MB/s`);
|
|
27
27
|
console.log('---');
|
|
28
|
-
console.log(`Parsed ${lastResult.
|
|
28
|
+
console.log(`Parsed ${lastResult.nodes.length} items`);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Temporal } from '@js-temporal/polyfill';
|
|
2
|
+
import { Node } from './Node.mjs';
|
|
3
|
+
import { Value } from './Value.mjs';
|
|
4
|
+
import type { DatedDirectiveNodeType } from '../nodeTypeToClass.mjs';
|
|
5
|
+
/**
|
|
6
|
+
* Abstract base class for all nodes that correspond to dated Beancount directives
|
|
7
|
+
* like transaction and open.
|
|
8
|
+
*
|
|
9
|
+
* Child classes must implement static `fromGenericParseResult` method
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class DatedNode extends Node {
|
|
12
|
+
/** The type of this dated node */
|
|
13
|
+
abstract type: DatedDirectiveNodeType;
|
|
14
|
+
/** The date of this node as a Temporal.PlainDate object */
|
|
15
|
+
date: Temporal.PlainDate;
|
|
16
|
+
/** Optional metadata key-value pairs associated with this node */
|
|
17
|
+
metadata?: Record<string, Value>;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new DatedNode instance.
|
|
20
|
+
* @param obj - Object containing node properties
|
|
21
|
+
* @param obj.date - The date string in YYYY-MM-DD format
|
|
22
|
+
*/
|
|
23
|
+
constructor(obj: {
|
|
24
|
+
date: string | Temporal.PlainDate;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Returns the common prefix for all DatedNode toString methods.
|
|
29
|
+
* Format: "YYYY-MM-DD <type>"
|
|
30
|
+
* @returns The formatted date and type prefix string
|
|
31
|
+
*/
|
|
32
|
+
protected getDateTypePrefix(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Converts metadata and comment to a formatted string.
|
|
35
|
+
* If metadata exists, each key-value pair is formatted on separate indented lines.
|
|
36
|
+
*
|
|
37
|
+
* @returns The formatted metadata and comment string, or empty string if neither exists
|
|
38
|
+
*/
|
|
39
|
+
getMetaDataString(): string;
|
|
40
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Temporal } from '@js-temporal/polyfill';
|
|
2
|
+
import { Node } from './Node.mjs';
|
|
3
|
+
import { Value } from './Value.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for all nodes that correspond to dated Beancount directives
|
|
6
|
+
* like transaction and open.
|
|
7
|
+
*
|
|
8
|
+
* Child classes must implement static `fromGenericParseResult` method
|
|
9
|
+
*/
|
|
10
|
+
export class DatedNode extends Node {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new DatedNode instance.
|
|
13
|
+
* @param obj - Object containing node properties
|
|
14
|
+
* @param obj.date - The date string in YYYY-MM-DD format
|
|
15
|
+
*/
|
|
16
|
+
constructor(obj) {
|
|
17
|
+
const { date, metadata, ...props } = obj;
|
|
18
|
+
super(props);
|
|
19
|
+
if (date) {
|
|
20
|
+
if (date instanceof Temporal.PlainDate) {
|
|
21
|
+
this.date = date;
|
|
22
|
+
}
|
|
23
|
+
else if (typeof date === 'string') {
|
|
24
|
+
this.date = Temporal.PlainDate.from(date, { overflow: 'reject' });
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error('Could not parse date, should be either string of Temporal.PlainDate');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (metadata) {
|
|
31
|
+
this.metadata = Object.fromEntries(Object.entries(metadata).map(([key, val]) => [
|
|
32
|
+
key,
|
|
33
|
+
new Value(val),
|
|
34
|
+
]));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns the common prefix for all DatedNode toString methods.
|
|
39
|
+
* Format: "YYYY-MM-DD <type>"
|
|
40
|
+
* @returns The formatted date and type prefix string
|
|
41
|
+
*/
|
|
42
|
+
getDateTypePrefix() {
|
|
43
|
+
return `${this.date.toJSON()} ${this.type}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Converts metadata and comment to a formatted string.
|
|
47
|
+
* If metadata exists, each key-value pair is formatted on separate indented lines.
|
|
48
|
+
*
|
|
49
|
+
* @returns The formatted metadata and comment string, or empty string if neither exists
|
|
50
|
+
*/
|
|
51
|
+
getMetaDataString() {
|
|
52
|
+
const comment = this.comment ? ` ; ${this.comment}` : '';
|
|
53
|
+
if (!this.metadata) {
|
|
54
|
+
return comment;
|
|
55
|
+
}
|
|
56
|
+
return (`${comment}\n` +
|
|
57
|
+
Object.entries(this.metadata)
|
|
58
|
+
.map(([key, val]) => ` ${key}: ${val.toString()}`)
|
|
59
|
+
.join('\n'));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { BeancountDirectiveNodeType } from '../nodeTypeToClass.mjs';
|
|
2
|
+
import { genericParse } from '../genericParse.mjs';
|
|
3
|
+
import { FormatOptions } from './ParseResult.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Type helper for Node class constructors that enforce the static factory methods.
|
|
6
|
+
* Child classes must implement static fromGenericParseResult and fromObject methods to create instances.
|
|
7
|
+
* Note: The constructor is protected, so only the static factory methods are part of the public API.
|
|
8
|
+
*/
|
|
9
|
+
export interface NodeConstructor<T extends Node> {
|
|
10
|
+
/**
|
|
11
|
+
* Creates an Node instance from a generic parse result.
|
|
12
|
+
* @param parsedStart - The result from genericParse containing parsed node data
|
|
13
|
+
* @returns A new instance of the Node subclass
|
|
14
|
+
*/
|
|
15
|
+
fromGenericParseResult(parsedStart: ReturnType<typeof genericParse>): T;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Abstract base class for all nodes.
|
|
19
|
+
*
|
|
20
|
+
* Child classes must implement static `fromGenericParseResult` method
|
|
21
|
+
*/
|
|
22
|
+
export declare abstract class Node {
|
|
23
|
+
/** Optional comment text associated with this node */
|
|
24
|
+
comment?: string;
|
|
25
|
+
/** Internal metadata key-value pairs associated with this node.
|
|
26
|
+
* These can be anything, are not used in the output, and are meant to be used
|
|
27
|
+
* to allow your pipeline to keep track of an internal property */
|
|
28
|
+
internalMetadata: Record<string, unknown>;
|
|
29
|
+
/** The type of this node (e.g., 'open', 'close', 'transaction', 'comment', 'blankline') */
|
|
30
|
+
abstract type: BeancountDirectiveNodeType | 'comment' | 'blankline';
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new Node instance.
|
|
33
|
+
* @param obj - Object containing node properties
|
|
34
|
+
*/
|
|
35
|
+
constructor(obj: Record<string, unknown>);
|
|
36
|
+
/**
|
|
37
|
+
* Creates an Node instance from a Beancount string.
|
|
38
|
+
* Parses the input string and constructs the appropriate Node subclass.
|
|
39
|
+
*
|
|
40
|
+
* @param input - A single unparsed node as a string
|
|
41
|
+
* @returns A new instance of the Node subclass
|
|
42
|
+
*/
|
|
43
|
+
static fromString<T extends Node>(this: NodeConstructor<T>, input: string): T;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an Node instance from JSON data.
|
|
46
|
+
* Calls fromJSONData to allow subclasses to transform the data before construction.
|
|
47
|
+
*
|
|
48
|
+
* @param jsonString - JSON data representing an node
|
|
49
|
+
* @returns A new instance of the Node subclass
|
|
50
|
+
* @remarks **Warning:** No validation is performed on the JSON input. We assume the JSON is valid and well-formed.
|
|
51
|
+
*/
|
|
52
|
+
static fromJSON<T extends Node>(this: new (obj: any) => T, jsonString: string): T;
|
|
53
|
+
/**
|
|
54
|
+
* Creates an Node instance from JSON data.
|
|
55
|
+
* Calls fromJSONData to allow subclasses to transform the data before construction.
|
|
56
|
+
*
|
|
57
|
+
* @param jsonData - object representing an node
|
|
58
|
+
* @returns A new instance of the Node subclass
|
|
59
|
+
* @remarks **Warning:** No validation is performed on the input. We assume the input is valid and well-formed.
|
|
60
|
+
*/
|
|
61
|
+
static fromJSONData<T extends Node>(this: new (obj: any) => T, jsonData: Record<string, unknown>): T;
|
|
62
|
+
/**
|
|
63
|
+
* Converts this node to a formatted string with aligned columns.
|
|
64
|
+
* Default implementation delegates to toString(). Subclasses can override for custom formatting.
|
|
65
|
+
*
|
|
66
|
+
* @param _formatOptions - Formatting options (unused in base implementation)
|
|
67
|
+
* @returns The formatted string representation of this node
|
|
68
|
+
*/
|
|
69
|
+
toFormattedString(_formatOptions?: FormatOptions): string;
|
|
70
|
+
/**
|
|
71
|
+
* Transforms JSON data before creating an Node instance.
|
|
72
|
+
* Default implementation returns the input unchanged.
|
|
73
|
+
* Subclasses can override this to handle custom deserialization logic
|
|
74
|
+
* (e.g., converting nested objects, handling dates, etc.).
|
|
75
|
+
*
|
|
76
|
+
* @param json - The JSON data to transform
|
|
77
|
+
* @returns The transformed data ready for the constructor
|
|
78
|
+
*/
|
|
79
|
+
protected parseJSONData(json: Record<string, unknown>): Record<string, unknown>;
|
|
80
|
+
/**
|
|
81
|
+
* Converts an node to a JSON-serializable object.
|
|
82
|
+
* @returns A JSON-serializable representation of this node
|
|
83
|
+
*/
|
|
84
|
+
toJSON(): Record<string, unknown>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Type assertion helper to ensure a class conforms to NodeConstructor.
|
|
88
|
+
* Usage: assertNodeConstructor(MyNodeClass)
|
|
89
|
+
* @param _ctor - The constructor to validate (unused at runtime)
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
export declare function assertNodeConstructor<T extends Node>(_ctor: NodeConstructor<T>): void;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { genericParse } from '../genericParse.mjs';
|
|
2
|
+
import { stringAwareSplitLine } from '../utils/stringAwareSplitLine.mjs';
|
|
3
|
+
import { defaultFormatOptions } from './ParseResult.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for all nodes.
|
|
6
|
+
*
|
|
7
|
+
* Child classes must implement static `fromGenericParseResult` method
|
|
8
|
+
*/
|
|
9
|
+
export class Node {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new Node instance.
|
|
12
|
+
* @param obj - Object containing node properties
|
|
13
|
+
*/
|
|
14
|
+
constructor(obj) {
|
|
15
|
+
/** Internal metadata key-value pairs associated with this node.
|
|
16
|
+
* These can be anything, are not used in the output, and are meant to be used
|
|
17
|
+
* to allow your pipeline to keep track of an internal property */
|
|
18
|
+
this.internalMetadata = {};
|
|
19
|
+
Object.assign(this, obj);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates an Node instance from a Beancount string.
|
|
23
|
+
* Parses the input string and constructs the appropriate Node subclass.
|
|
24
|
+
*
|
|
25
|
+
* @param input - A single unparsed node as a string
|
|
26
|
+
* @returns A new instance of the Node subclass
|
|
27
|
+
*/
|
|
28
|
+
static fromString(input) {
|
|
29
|
+
const sourceFragment = stringAwareSplitLine(input);
|
|
30
|
+
const genericParseResult = genericParse(sourceFragment);
|
|
31
|
+
const result = this.fromGenericParseResult(genericParseResult);
|
|
32
|
+
if (result.type !== genericParseResult.type) {
|
|
33
|
+
console.warn('Parse result type is not equal to requested object type, make sure the correct class is initiated');
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates an Node instance from JSON data.
|
|
39
|
+
* Calls fromJSONData to allow subclasses to transform the data before construction.
|
|
40
|
+
*
|
|
41
|
+
* @param jsonString - JSON data representing an node
|
|
42
|
+
* @returns A new instance of the Node subclass
|
|
43
|
+
* @remarks **Warning:** No validation is performed on the JSON input. We assume the JSON is valid and well-formed.
|
|
44
|
+
*/
|
|
45
|
+
static fromJSON(jsonString) {
|
|
46
|
+
const json = JSON.parse(jsonString);
|
|
47
|
+
// @ts-expect-error Not sure how to type this correctly
|
|
48
|
+
return this.fromJSONData(json); // eslint-disable-line
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates an Node instance from JSON data.
|
|
52
|
+
* Calls fromJSONData to allow subclasses to transform the data before construction.
|
|
53
|
+
*
|
|
54
|
+
* @param jsonData - object representing an node
|
|
55
|
+
* @returns A new instance of the Node subclass
|
|
56
|
+
* @remarks **Warning:** No validation is performed on the input. We assume the input is valid and well-formed.
|
|
57
|
+
*/
|
|
58
|
+
static fromJSONData(jsonData) {
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
const instance = new this({});
|
|
61
|
+
const transformedData = instance.parseJSONData(jsonData);
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
+
return new this(transformedData);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Converts this node to a formatted string with aligned columns.
|
|
67
|
+
* Default implementation delegates to toString(). Subclasses can override for custom formatting.
|
|
68
|
+
*
|
|
69
|
+
* @param _formatOptions - Formatting options (unused in base implementation)
|
|
70
|
+
* @returns The formatted string representation of this node
|
|
71
|
+
*/
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
73
|
+
toFormattedString(_formatOptions = defaultFormatOptions) {
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
75
|
+
return this.toString();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Transforms JSON data before creating an Node instance.
|
|
79
|
+
* Default implementation returns the input unchanged.
|
|
80
|
+
* Subclasses can override this to handle custom deserialization logic
|
|
81
|
+
* (e.g., converting nested objects, handling dates, etc.).
|
|
82
|
+
*
|
|
83
|
+
* @param json - The JSON data to transform
|
|
84
|
+
* @returns The transformed data ready for the constructor
|
|
85
|
+
*/
|
|
86
|
+
parseJSONData(json) {
|
|
87
|
+
return json;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Converts an node to a JSON-serializable object.
|
|
91
|
+
* @returns A JSON-serializable representation of this node
|
|
92
|
+
*/
|
|
93
|
+
toJSON() {
|
|
94
|
+
return {
|
|
95
|
+
...this,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Type assertion helper to ensure a class conforms to NodeConstructor.
|
|
101
|
+
* Usage: assertNodeConstructor(MyNodeClass)
|
|
102
|
+
* @param _ctor - The constructor to validate (unused at runtime)
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
export function assertNodeConstructor(
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
107
|
+
_ctor) { }
|
|
@@ -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;
|