beancount 0.0.1 → 0.0.3
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 +8 -7
- package/build/src/classes/DateEntry.d.mts +22 -3
- package/build/src/classes/DateEntry.mjs +20 -1
- package/build/src/classes/Entry.d.mts +52 -2
- package/build/src/classes/Entry.mjs +48 -0
- package/build/src/classes/ParseResult.d.mts +29 -3
- package/build/src/classes/ParseResult.mjs +45 -4
- package/build/src/classes/Value.d.mts +39 -0
- package/build/src/classes/Value.mjs +34 -0
- package/build/src/classes/entryTypes/Balance.d.mts +19 -0
- package/build/src/classes/entryTypes/Balance.mjs +16 -0
- package/build/src/classes/entryTypes/Blankline.d.mts +16 -0
- package/build/src/classes/entryTypes/Blankline.mjs +16 -0
- package/build/src/classes/entryTypes/Close.d.mts +12 -0
- package/build/src/classes/entryTypes/Close.mjs +11 -0
- package/build/src/classes/entryTypes/Comment.d.mts +17 -0
- package/build/src/classes/entryTypes/Comment.mjs +17 -0
- package/build/src/classes/entryTypes/Commodity.d.mts +12 -0
- package/build/src/classes/entryTypes/Commodity.mjs +11 -0
- package/build/src/classes/entryTypes/Custom.d.mts +13 -0
- package/build/src/classes/entryTypes/Custom.mjs +11 -0
- package/build/src/classes/entryTypes/Document.d.mts +13 -0
- package/build/src/classes/entryTypes/Document.mjs +11 -0
- package/build/src/classes/entryTypes/Event.d.mts +13 -0
- package/build/src/classes/entryTypes/Event.mjs +11 -0
- package/build/src/classes/entryTypes/Include.d.mts +12 -0
- package/build/src/classes/entryTypes/Include.mjs +11 -0
- package/build/src/classes/entryTypes/Note.d.mts +13 -0
- package/build/src/classes/entryTypes/Note.mjs +11 -0
- package/build/src/classes/entryTypes/Open.d.mts +15 -0
- package/build/src/classes/entryTypes/Open.mjs +12 -0
- package/build/src/classes/entryTypes/Option.d.mts +13 -0
- package/build/src/classes/entryTypes/Option.mjs +11 -0
- package/build/src/classes/entryTypes/Pad.d.mts +13 -0
- package/build/src/classes/entryTypes/Pad.mjs +11 -0
- package/build/src/classes/entryTypes/Plugin.d.mts +13 -0
- package/build/src/classes/entryTypes/Plugin.mjs +11 -0
- package/build/src/classes/entryTypes/Poptag.d.mts +12 -0
- package/build/src/classes/entryTypes/Poptag.mjs +11 -0
- package/build/src/classes/entryTypes/Price.d.mts +18 -0
- package/build/src/classes/entryTypes/Price.mjs +15 -0
- package/build/src/classes/entryTypes/Pushtag.d.mts +12 -0
- package/build/src/classes/entryTypes/Pushtag.mjs +11 -0
- package/build/src/classes/entryTypes/Query.d.mts +13 -0
- package/build/src/classes/entryTypes/Query.mjs +11 -0
- package/build/src/classes/entryTypes/Transaction/Posting.d.mts +41 -0
- package/build/src/classes/entryTypes/Transaction/Posting.mjs +33 -0
- package/build/src/classes/entryTypes/Transaction/Tag.d.mts +19 -0
- package/build/src/classes/entryTypes/Transaction/Tag.mjs +17 -0
- package/build/src/classes/entryTypes/Transaction/index.d.mts +42 -1
- package/build/src/classes/entryTypes/Transaction/index.mjs +69 -5
- package/build/src/entryTypeToClass.d.mts +59 -0
- package/build/src/entryTypeToClass.mjs +37 -0
- package/build/src/genericParse.d.mts +39 -2
- package/build/src/genericParse.mjs +13 -0
- package/build/src/main.d.mts +58 -0
- package/build/src/main.mjs +55 -0
- package/build/src/parse.d.mts +76 -23
- package/build/src/parse.mjs +69 -22
- package/build/src/utils/countChar.d.mts +7 -0
- package/build/src/utils/countChar.mjs +7 -0
- package/build/src/utils/formatPrice.d.mts +7 -2
- package/build/src/utils/formatPrice.mjs +7 -2
- package/build/src/utils/parseMetadata.d.mts +12 -0
- package/build/src/utils/parseMetadata.mjs +8 -0
- package/build/src/utils/simpleParseLine.d.mts +7 -0
- package/build/src/utils/simpleParseLine.mjs +7 -0
- package/build/src/utils/stringAwareParseLine.d.mts +7 -0
- package/build/src/utils/stringAwareParseLine.mjs +7 -0
- package/build/src/utils/stringAwareSplitLine.d.mts +9 -0
- package/build/src/utils/stringAwareSplitLine.mjs +9 -0
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ npm install beancount
|
|
|
19
19
|
## Quick Start
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
|
-
import { parse } from 'beancount'
|
|
22
|
+
import { parse, ParseResult } from 'beancount'
|
|
23
23
|
|
|
24
24
|
const beancountContent = `
|
|
25
25
|
2024-01-01 open Assets:Checking USD
|
|
@@ -37,6 +37,13 @@ console.log(result.entries.length) // 2
|
|
|
37
37
|
// Convert back to string
|
|
38
38
|
console.log(result.toString())
|
|
39
39
|
|
|
40
|
+
// Convert to JSON
|
|
41
|
+
const resultJSON = JSON.stringify(result)
|
|
42
|
+
console.log(resultJSON)
|
|
43
|
+
|
|
44
|
+
// Convert back to parsed entries
|
|
45
|
+
console.log(ParseResult.fromJSON(result))
|
|
46
|
+
|
|
40
47
|
// Or with formatted output (aligned columns)
|
|
41
48
|
// only partially implemented at this point
|
|
42
49
|
console.log(result.toFormattedString())
|
|
@@ -46,12 +53,6 @@ console.log(result.toFormattedString())
|
|
|
46
53
|
|
|
47
54
|
Full API documentation is available at https://Boelensman1.github.io/node-beancount/
|
|
48
55
|
|
|
49
|
-
You can also generate documentation locally with:
|
|
50
|
-
|
|
51
|
-
```bash
|
|
52
|
-
command make docs
|
|
53
|
-
```
|
|
54
|
-
|
|
55
56
|
## Contributing
|
|
56
57
|
|
|
57
58
|
This project uses Make for task orchestration. Common commands:
|
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
2
|
import { Entry } from './Entry.mjs';
|
|
3
3
|
import { Value } from './Value.mjs';
|
|
4
|
-
import type {
|
|
5
|
-
|
|
4
|
+
import type { BeancountEntryType } from '../entryTypeToClass.mjs';
|
|
5
|
+
/**
|
|
6
|
+
* Union type of all Beancount entry types that include a date field.
|
|
7
|
+
* Excludes non-dated directives 'include', 'option', and 'plugin'.
|
|
8
|
+
*/
|
|
9
|
+
export type BeancountDateEntryType = Exclude<BeancountEntryType, 'include' | 'option' | 'plugin'>;
|
|
6
10
|
/**
|
|
7
11
|
* Abstract base class for all Beancount entry types that have a date.
|
|
8
12
|
*
|
|
9
13
|
* Child classes must implement static `fromGenericParseResult` method
|
|
10
14
|
*/
|
|
11
15
|
export declare abstract class DateEntry extends Entry {
|
|
12
|
-
|
|
16
|
+
/** The type of this dated entry */
|
|
17
|
+
abstract type: BeancountDateEntryType;
|
|
18
|
+
/** The date of this entry as a Temporal.PlainDate object */
|
|
13
19
|
date: Temporal.PlainDate;
|
|
20
|
+
/** Optional metadata key-value pairs associated with this entry */
|
|
14
21
|
metadata?: Record<string, Value>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new DateEntry instance.
|
|
24
|
+
* @param obj - Object containing entry properties
|
|
25
|
+
* @param obj.date - The date string in YYYY-MM-DD format
|
|
26
|
+
*/
|
|
15
27
|
constructor(obj: {
|
|
16
28
|
date: string;
|
|
17
29
|
[key: string]: unknown;
|
|
@@ -19,7 +31,14 @@ export declare abstract class DateEntry extends Entry {
|
|
|
19
31
|
/**
|
|
20
32
|
* Returns the common prefix for all DateEntry toString methods.
|
|
21
33
|
* Format: "YYYY-MM-DD <type>"
|
|
34
|
+
* @returns The formatted date and type prefix string
|
|
22
35
|
*/
|
|
23
36
|
protected getDateTypePrefix(): string;
|
|
37
|
+
/**
|
|
38
|
+
* Converts metadata and comment to a formatted string.
|
|
39
|
+
* If metadata exists, each key-value pair is formatted on separate indented lines.
|
|
40
|
+
*
|
|
41
|
+
* @returns The formatted metadata and comment string, or empty string if neither exists
|
|
42
|
+
*/
|
|
24
43
|
getMetaDataString(): string;
|
|
25
44
|
}
|
|
@@ -1,25 +1,44 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
2
|
import { Entry } from './Entry.mjs';
|
|
3
|
+
import { Value } from './Value.mjs';
|
|
3
4
|
/**
|
|
4
5
|
* Abstract base class for all Beancount entry types that have a date.
|
|
5
6
|
*
|
|
6
7
|
* Child classes must implement static `fromGenericParseResult` method
|
|
7
8
|
*/
|
|
8
9
|
export class DateEntry extends Entry {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new DateEntry instance.
|
|
12
|
+
* @param obj - Object containing entry properties
|
|
13
|
+
* @param obj.date - The date string in YYYY-MM-DD format
|
|
14
|
+
*/
|
|
9
15
|
constructor(obj) {
|
|
10
|
-
const { date, ...props } = obj;
|
|
16
|
+
const { date, metadata, ...props } = obj;
|
|
11
17
|
super(props);
|
|
12
18
|
if (date) {
|
|
13
19
|
this.date = Temporal.PlainDate.from(date, { overflow: 'reject' });
|
|
14
20
|
}
|
|
21
|
+
if (metadata) {
|
|
22
|
+
this.metadata = Object.fromEntries(Object.entries(metadata).map(([key, val]) => [
|
|
23
|
+
key,
|
|
24
|
+
new Value(val),
|
|
25
|
+
]));
|
|
26
|
+
}
|
|
15
27
|
}
|
|
16
28
|
/**
|
|
17
29
|
* Returns the common prefix for all DateEntry toString methods.
|
|
18
30
|
* Format: "YYYY-MM-DD <type>"
|
|
31
|
+
* @returns The formatted date and type prefix string
|
|
19
32
|
*/
|
|
20
33
|
getDateTypePrefix() {
|
|
21
34
|
return `${this.date.toJSON()} ${this.type}`;
|
|
22
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Converts metadata and comment to a formatted string.
|
|
38
|
+
* If metadata exists, each key-value pair is formatted on separate indented lines.
|
|
39
|
+
*
|
|
40
|
+
* @returns The formatted metadata and comment string, or empty string if neither exists
|
|
41
|
+
*/
|
|
23
42
|
getMetaDataString() {
|
|
24
43
|
const comment = this.comment ? ` ; ${this.comment}` : '';
|
|
25
44
|
if (!this.metadata) {
|
|
@@ -1,14 +1,23 @@
|
|
|
1
|
+
import type { BeancountEntryType } from '../entryTypeToClass.mjs';
|
|
1
2
|
import { genericParse } from '../../src/genericParse.mjs';
|
|
2
|
-
import type { EntryType } from '../parse.mjs';
|
|
3
3
|
/**
|
|
4
4
|
* Type helper for Entry class constructors that enforce the static factory methods.
|
|
5
5
|
* Child classes must implement static fromGenericParseResult and fromObject methods to create instances.
|
|
6
6
|
* Note: The constructor is protected, so only the static factory methods are part of the public API.
|
|
7
7
|
*/
|
|
8
8
|
export interface EntryConstructor<T extends Entry> {
|
|
9
|
+
/**
|
|
10
|
+
* Creates an Entry instance from a generic parse result.
|
|
11
|
+
* @param parsedStart - The result from genericParse containing parsed entry data
|
|
12
|
+
* @returns A new instance of the Entry subclass
|
|
13
|
+
*/
|
|
9
14
|
fromGenericParseResult(parsedStart: ReturnType<typeof genericParse>): T;
|
|
10
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Options for formatting Entry output.
|
|
18
|
+
*/
|
|
11
19
|
export interface FormatOptions {
|
|
20
|
+
/** The column position where currency symbols should be aligned */
|
|
12
21
|
currencyColumn: number;
|
|
13
22
|
}
|
|
14
23
|
/**
|
|
@@ -17,14 +26,55 @@ export interface FormatOptions {
|
|
|
17
26
|
* Child classes must implement static `fromGenericParseResult` method
|
|
18
27
|
*/
|
|
19
28
|
export declare abstract class Entry {
|
|
29
|
+
/** Optional comment text associated with this entry */
|
|
20
30
|
comment?: string;
|
|
21
|
-
|
|
31
|
+
/** The type of this entry (e.g., 'open', 'close', 'transaction', 'comment', 'blankline') */
|
|
32
|
+
abstract type: BeancountEntryType | 'comment' | 'blankline';
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new Entry instance.
|
|
35
|
+
* @param obj - Object containing entry properties
|
|
36
|
+
*/
|
|
22
37
|
constructor(obj: Record<string, unknown>);
|
|
38
|
+
/**
|
|
39
|
+
* Creates an Entry instance from a Beancount string.
|
|
40
|
+
* Parses the input string and constructs the appropriate Entry subclass.
|
|
41
|
+
*
|
|
42
|
+
* @param input - A single Beancount entry as a string
|
|
43
|
+
* @returns A new instance of the Entry subclass
|
|
44
|
+
*/
|
|
23
45
|
static fromString<T extends Entry>(this: EntryConstructor<T>, input: string): T;
|
|
46
|
+
/**
|
|
47
|
+
* Creates an Entry instance from JSON data.
|
|
48
|
+
* Calls parseJSON to allow subclasses to transform the data before construction.
|
|
49
|
+
*
|
|
50
|
+
* @param jsonString - JSON data representing an entry
|
|
51
|
+
* @returns A new instance of the Entry subclass
|
|
52
|
+
* @remarks **Warning:** No validation is performed on the JSON input. We assume the JSON is valid and well-formed.
|
|
53
|
+
*/
|
|
54
|
+
static fromJSON<T extends Entry>(this: new (obj: any) => T, jsonString: string): T;
|
|
55
|
+
/**
|
|
56
|
+
* Converts this entry to a formatted string with aligned columns.
|
|
57
|
+
* Default implementation delegates to toString(). Subclasses can override for custom formatting.
|
|
58
|
+
*
|
|
59
|
+
* @param _formatOptions - Formatting options (unused in base implementation)
|
|
60
|
+
* @returns The formatted string representation of this entry
|
|
61
|
+
*/
|
|
24
62
|
toFormattedString(_formatOptions: FormatOptions): string;
|
|
63
|
+
/**
|
|
64
|
+
* Transforms JSON data before creating an Entry instance.
|
|
65
|
+
* Default implementation returns the input unchanged.
|
|
66
|
+
* Subclasses can override this to handle custom deserialization logic
|
|
67
|
+
* (e.g., converting nested objects, handling dates, etc.).
|
|
68
|
+
*
|
|
69
|
+
* @param json - The JSON data to transform
|
|
70
|
+
* @returns The transformed data ready for the constructor
|
|
71
|
+
*/
|
|
72
|
+
protected parseJSON(json: Record<string, unknown>): Record<string, unknown>;
|
|
25
73
|
}
|
|
26
74
|
/**
|
|
27
75
|
* Type assertion helper to ensure a class conforms to EntryConstructor.
|
|
28
76
|
* Usage: assertEntryConstructor(MyEntryClass)
|
|
77
|
+
* @param _ctor - The constructor to validate (unused at runtime)
|
|
78
|
+
* @internal
|
|
29
79
|
*/
|
|
30
80
|
export declare function assertEntryConstructor<T extends Entry>(_ctor: EntryConstructor<T>): void;
|
|
@@ -6,9 +6,20 @@ import { stringAwareSplitLine } from '../utils/stringAwareSplitLine.mjs';
|
|
|
6
6
|
* Child classes must implement static `fromGenericParseResult` method
|
|
7
7
|
*/
|
|
8
8
|
export class Entry {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new Entry instance.
|
|
11
|
+
* @param obj - Object containing entry properties
|
|
12
|
+
*/
|
|
9
13
|
constructor(obj) {
|
|
10
14
|
Object.assign(this, obj);
|
|
11
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates an Entry instance from a Beancount string.
|
|
18
|
+
* Parses the input string and constructs the appropriate Entry subclass.
|
|
19
|
+
*
|
|
20
|
+
* @param input - A single Beancount entry as a string
|
|
21
|
+
* @returns A new instance of the Entry subclass
|
|
22
|
+
*/
|
|
12
23
|
static fromString(input) {
|
|
13
24
|
const unparsedEntry = stringAwareSplitLine(input);
|
|
14
25
|
const genericParseResult = genericParse(unparsedEntry);
|
|
@@ -18,15 +29,52 @@ export class Entry {
|
|
|
18
29
|
}
|
|
19
30
|
return result;
|
|
20
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates an Entry instance from JSON data.
|
|
34
|
+
* Calls parseJSON to allow subclasses to transform the data before construction.
|
|
35
|
+
*
|
|
36
|
+
* @param jsonString - JSON data representing an entry
|
|
37
|
+
* @returns A new instance of the Entry subclass
|
|
38
|
+
* @remarks **Warning:** No validation is performed on the JSON input. We assume the JSON is valid and well-formed.
|
|
39
|
+
*/
|
|
40
|
+
static fromJSON(jsonString) {
|
|
41
|
+
const json = JSON.parse(jsonString);
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
const instance = new this({});
|
|
44
|
+
const transformedData = instance.parseJSON(json);
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
+
return new this(transformedData);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Converts this entry to a formatted string with aligned columns.
|
|
50
|
+
* Default implementation delegates to toString(). Subclasses can override for custom formatting.
|
|
51
|
+
*
|
|
52
|
+
* @param _formatOptions - Formatting options (unused in base implementation)
|
|
53
|
+
* @returns The formatted string representation of this entry
|
|
54
|
+
*/
|
|
21
55
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
22
56
|
toFormattedString(_formatOptions) {
|
|
23
57
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
24
58
|
return this.toString();
|
|
25
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Transforms JSON data before creating an Entry instance.
|
|
62
|
+
* Default implementation returns the input unchanged.
|
|
63
|
+
* Subclasses can override this to handle custom deserialization logic
|
|
64
|
+
* (e.g., converting nested objects, handling dates, etc.).
|
|
65
|
+
*
|
|
66
|
+
* @param json - The JSON data to transform
|
|
67
|
+
* @returns The transformed data ready for the constructor
|
|
68
|
+
*/
|
|
69
|
+
parseJSON(json) {
|
|
70
|
+
return json;
|
|
71
|
+
}
|
|
26
72
|
}
|
|
27
73
|
/**
|
|
28
74
|
* Type assertion helper to ensure a class conforms to EntryConstructor.
|
|
29
75
|
* Usage: assertEntryConstructor(MyEntryClass)
|
|
76
|
+
* @param _ctor - The constructor to validate (unused at runtime)
|
|
77
|
+
* @internal
|
|
30
78
|
*/
|
|
31
79
|
export function assertEntryConstructor(
|
|
32
80
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -1,9 +1,35 @@
|
|
|
1
1
|
import { Entry } from './Entry.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Container class for parsed Beancount entries.
|
|
4
|
+
* Provides methods for converting entries back to string format.
|
|
5
|
+
*/
|
|
2
6
|
export declare class ParseResult {
|
|
3
7
|
entries: Entry[];
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new ParseResult instance.
|
|
10
|
+
* @param entries - Array of parsed Entry objects
|
|
11
|
+
*/
|
|
4
12
|
constructor(entries: Entry[]);
|
|
13
|
+
/**
|
|
14
|
+
* Converts all entries to their string representation.
|
|
15
|
+
* Each entry is converted using its toString() method and joined with newlines.
|
|
16
|
+
* @returns The complete Beancount file content as a string
|
|
17
|
+
*/
|
|
5
18
|
toString(): string;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Converts all entries to a formatted string with aligned columns.
|
|
21
|
+
* Uses each entry's toFormattedString() method for consistent formatting.
|
|
22
|
+
*
|
|
23
|
+
* @returns The formatted Beancount file content as a string
|
|
24
|
+
*/
|
|
25
|
+
toFormattedString(): string;
|
|
26
|
+
/**
|
|
27
|
+
* Creates an ParseResult instance from JSON data.
|
|
28
|
+
* Calls parseJSON to allow subclasses to transform the data before construction.
|
|
29
|
+
*
|
|
30
|
+
* @param jsonString - JSON data representing an ParseResult
|
|
31
|
+
* @remarks **Warning:** No validation is performed on the JSON input. We assume the JSON is valid and well-formed.
|
|
32
|
+
* @returns A new instance of ParseResult loaded with the data in the JSON
|
|
33
|
+
*/
|
|
34
|
+
static fromJSON(jsonString: string): ParseResult;
|
|
9
35
|
}
|
|
@@ -1,19 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { entryTypeToClass } from '../entryTypeToClass.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Container class for parsed Beancount entries.
|
|
4
|
+
* Provides methods for converting entries back to string format.
|
|
5
|
+
*/
|
|
4
6
|
export class ParseResult {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new ParseResult instance.
|
|
9
|
+
* @param entries - Array of parsed Entry objects
|
|
10
|
+
*/
|
|
5
11
|
constructor(entries) {
|
|
6
12
|
this.entries = entries;
|
|
7
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Converts all entries to their string representation.
|
|
16
|
+
* Each entry is converted using its toString() method and joined with newlines.
|
|
17
|
+
* @returns The complete Beancount file content as a string
|
|
18
|
+
*/
|
|
8
19
|
toString() {
|
|
9
20
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
10
21
|
return this.entries.map((e) => e.toString()).join('\n');
|
|
11
22
|
}
|
|
12
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Converts all entries to a formatted string with aligned columns.
|
|
25
|
+
* Uses each entry's toFormattedString() method for consistent formatting.
|
|
26
|
+
*
|
|
27
|
+
* @returns The formatted Beancount file content as a string
|
|
28
|
+
*/
|
|
29
|
+
toFormattedString() {
|
|
13
30
|
// TODO: calculate currencyColumn
|
|
14
31
|
const currencyColumn = 59;
|
|
15
32
|
return this.entries
|
|
16
33
|
.map((e) => e.toFormattedString({ currencyColumn }))
|
|
17
34
|
.join('\n');
|
|
18
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates an ParseResult instance from JSON data.
|
|
38
|
+
* Calls parseJSON to allow subclasses to transform the data before construction.
|
|
39
|
+
*
|
|
40
|
+
* @param jsonString - JSON data representing an ParseResult
|
|
41
|
+
* @remarks **Warning:** No validation is performed on the JSON input. We assume the JSON is valid and well-formed.
|
|
42
|
+
* @returns A new instance of ParseResult loaded with the data in the JSON
|
|
43
|
+
*/
|
|
44
|
+
static fromJSON(jsonString) {
|
|
45
|
+
const json = JSON.parse(jsonString);
|
|
46
|
+
const jsonEntries = json.entries;
|
|
47
|
+
const entries = jsonEntries.map((jsonEntry) => {
|
|
48
|
+
const { type } = jsonEntry;
|
|
49
|
+
const EntryClass = entryTypeToClass[type];
|
|
50
|
+
if (!EntryClass) {
|
|
51
|
+
throw new Error(`No entryclass found for type ${type}`);
|
|
52
|
+
}
|
|
53
|
+
// Type assertion needed because TypeScript can't verify that all entry classes
|
|
54
|
+
// in the union type have compatible constructor signatures
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
56
|
+
return EntryClass.fromJSON(JSON.stringify(jsonEntry));
|
|
57
|
+
});
|
|
58
|
+
return new ParseResult(entries);
|
|
59
|
+
}
|
|
19
60
|
}
|
|
@@ -1,12 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Union type of all possible value types in Beancount metadata and expressions.
|
|
3
|
+
*/
|
|
1
4
|
export type ValueType = 'string' | 'date' | 'boolean' | 'amount' | 'numbers';
|
|
5
|
+
/**
|
|
6
|
+
* Removes surrounding double quotes from a string value.
|
|
7
|
+
* @param val - The string to parse (potentially with quotes)
|
|
8
|
+
* @returns The string without leading or trailing quotes
|
|
9
|
+
*/
|
|
2
10
|
export declare const parseString: (val: string) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Represents a typed value in Beancount (used in metadata, expressions, etc.).
|
|
13
|
+
* Handles string, boolean, date, amount, and number values.
|
|
14
|
+
*/
|
|
3
15
|
export declare class Value {
|
|
16
|
+
/** The type of this value */
|
|
4
17
|
type: ValueType;
|
|
18
|
+
/** The actual value data */
|
|
5
19
|
value: unknown;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Value instance.
|
|
22
|
+
* @param obj - Object containing type and value properties
|
|
23
|
+
* @param obj.type - The value type
|
|
24
|
+
* @param obj.value - The value data
|
|
25
|
+
*/
|
|
6
26
|
constructor(obj: {
|
|
7
27
|
type: ValueType;
|
|
8
28
|
value: unknown;
|
|
9
29
|
});
|
|
30
|
+
/**
|
|
31
|
+
* Parses a string into a Value object with appropriate type detection.
|
|
32
|
+
* Detects and handles:
|
|
33
|
+
* - Quoted strings
|
|
34
|
+
* - Boolean values (TRUE/FALSE, case-insensitive)
|
|
35
|
+
* - Amounts (fallback for other values)
|
|
36
|
+
*
|
|
37
|
+
* @param input - The string to parse
|
|
38
|
+
* @returns A new Value instance with detected type
|
|
39
|
+
*/
|
|
10
40
|
static fromString(input: string): Value;
|
|
41
|
+
/**
|
|
42
|
+
* Converts this value to its string representation.
|
|
43
|
+
* Formats based on type:
|
|
44
|
+
* - Booleans as TRUE/FALSE
|
|
45
|
+
* - Strings with surrounding quotes
|
|
46
|
+
* - Other types as their string representation
|
|
47
|
+
*
|
|
48
|
+
* @returns The formatted string representation
|
|
49
|
+
*/
|
|
11
50
|
toString(): string;
|
|
12
51
|
}
|
|
@@ -1,10 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes surrounding double quotes from a string value.
|
|
3
|
+
* @param val - The string to parse (potentially with quotes)
|
|
4
|
+
* @returns The string without leading or trailing quotes
|
|
5
|
+
*/
|
|
1
6
|
export const parseString = (val) => {
|
|
2
7
|
return val.replace(/^"|"$/g, '');
|
|
3
8
|
};
|
|
9
|
+
/**
|
|
10
|
+
* Represents a typed value in Beancount (used in metadata, expressions, etc.).
|
|
11
|
+
* Handles string, boolean, date, amount, and number values.
|
|
12
|
+
*/
|
|
4
13
|
export class Value {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new Value instance.
|
|
16
|
+
* @param obj - Object containing type and value properties
|
|
17
|
+
* @param obj.type - The value type
|
|
18
|
+
* @param obj.value - The value data
|
|
19
|
+
*/
|
|
5
20
|
constructor(obj) {
|
|
6
21
|
Object.assign(this, obj);
|
|
7
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Parses a string into a Value object with appropriate type detection.
|
|
25
|
+
* Detects and handles:
|
|
26
|
+
* - Quoted strings
|
|
27
|
+
* - Boolean values (TRUE/FALSE, case-insensitive)
|
|
28
|
+
* - Amounts (fallback for other values)
|
|
29
|
+
*
|
|
30
|
+
* @param input - The string to parse
|
|
31
|
+
* @returns A new Value instance with detected type
|
|
32
|
+
*/
|
|
8
33
|
static fromString(input) {
|
|
9
34
|
if (input.startsWith('"') && input.endsWith('"')) {
|
|
10
35
|
// string!
|
|
@@ -19,6 +44,15 @@ export class Value {
|
|
|
19
44
|
// TODO: more parsing
|
|
20
45
|
return new Value({ type: 'amount', value: input });
|
|
21
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Converts this value to its string representation.
|
|
49
|
+
* Formats based on type:
|
|
50
|
+
* - Booleans as TRUE/FALSE
|
|
51
|
+
* - Strings with surrounding quotes
|
|
52
|
+
* - Other types as their string representation
|
|
53
|
+
*
|
|
54
|
+
* @returns The formatted string representation
|
|
55
|
+
*/
|
|
22
56
|
toString() {
|
|
23
57
|
switch (this.type) {
|
|
24
58
|
case 'boolean':
|
|
@@ -1,13 +1,32 @@
|
|
|
1
1
|
import type { GenericParseResultWithDate } from '../../genericParse.mjs';
|
|
2
2
|
import { FormatOptions } from '../Entry.mjs';
|
|
3
3
|
import { DateEntry } from '../DateEntry.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a Balance assertion entry.
|
|
6
|
+
* Balance directives assert that an account has a specific balance at a given date.
|
|
7
|
+
*/
|
|
4
8
|
export declare class Balance extends DateEntry {
|
|
9
|
+
/** @inheritdoc */
|
|
5
10
|
type: "balance";
|
|
11
|
+
/** The account name for the balance assertion */
|
|
6
12
|
account: string;
|
|
13
|
+
/** The expected amount */
|
|
7
14
|
amount: string;
|
|
15
|
+
/** The currency of the amount */
|
|
8
16
|
currency: string;
|
|
17
|
+
/**
|
|
18
|
+
* Gets the formatted price string (amount + currency).
|
|
19
|
+
* @returns The formatted price string
|
|
20
|
+
*/
|
|
9
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
|
+
*/
|
|
10
27
|
static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Balance;
|
|
28
|
+
/** @inheritdoc */
|
|
11
29
|
toString(): string;
|
|
30
|
+
/** @inheritdoc */
|
|
12
31
|
toFormattedString(formatOptions: FormatOptions): string;
|
|
13
32
|
}
|
|
@@ -2,14 +2,28 @@ import { assertEntryConstructor } from '../Entry.mjs';
|
|
|
2
2
|
import { DateEntry } from '../DateEntry.mjs';
|
|
3
3
|
import { simpleParseLine } from '../../utils/simpleParseLine.mjs';
|
|
4
4
|
import { formatPrice } from '../../utils/formatPrice.mjs';
|
|
5
|
+
/**
|
|
6
|
+
* Represents a Balance assertion entry.
|
|
7
|
+
* Balance directives assert that an account has a specific balance at a given date.
|
|
8
|
+
*/
|
|
5
9
|
export class Balance extends DateEntry {
|
|
6
10
|
constructor() {
|
|
7
11
|
super(...arguments);
|
|
12
|
+
/** @inheritdoc */
|
|
8
13
|
this.type = 'balance';
|
|
9
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Gets the formatted price string (amount + currency).
|
|
17
|
+
* @returns The formatted price string
|
|
18
|
+
*/
|
|
10
19
|
get price() {
|
|
11
20
|
return formatPrice(this.amount, this.currency);
|
|
12
21
|
}
|
|
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
|
+
*/
|
|
13
27
|
static fromGenericParseResult(genericParseResult) {
|
|
14
28
|
const [account, amount, currency] = simpleParseLine(genericParseResult.header);
|
|
15
29
|
return new Balance({
|
|
@@ -19,9 +33,11 @@ export class Balance extends DateEntry {
|
|
|
19
33
|
currency,
|
|
20
34
|
});
|
|
21
35
|
}
|
|
36
|
+
/** @inheritdoc */
|
|
22
37
|
toString() {
|
|
23
38
|
return this.toFormattedString({ currencyColumn: 0 });
|
|
24
39
|
}
|
|
40
|
+
/** @inheritdoc */
|
|
25
41
|
toFormattedString(formatOptions) {
|
|
26
42
|
const firstPart = `${this.getDateTypePrefix()} ${this.account}`;
|
|
27
43
|
const paddingLength = formatOptions.currencyColumn - firstPart.length - this.amount.length - 2; // not sure what this is for
|
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import type { GenericParseResult } from '../../genericParse.mjs';
|
|
2
2
|
import { Entry, EntryConstructor } from '../Entry.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a blank line in a Beancount file.
|
|
5
|
+
* Blank lines are used to separate entries for readability.
|
|
6
|
+
*/
|
|
3
7
|
export declare class Blankline extends Entry {
|
|
8
|
+
/** @inheritdoc */
|
|
4
9
|
type: "blankline";
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Blankline instance from a generic parse result.
|
|
12
|
+
* @param _genericParseResult - Unused, blank lines have no content
|
|
13
|
+
* @returns A new Blankline instance
|
|
14
|
+
*/
|
|
5
15
|
static fromGenericParseResult(_genericParseResult: GenericParseResult): Blankline;
|
|
16
|
+
/** @inheritdoc */
|
|
6
17
|
toString(): string;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a Blankline instance from a string.
|
|
20
|
+
* @param _input - Unused, blank lines have no content
|
|
21
|
+
* @returns A new Blankline instance
|
|
22
|
+
*/
|
|
7
23
|
static fromString<T extends Entry>(this: EntryConstructor<T>, _input: string): T;
|
|
8
24
|
}
|
|
@@ -1,17 +1,33 @@
|
|
|
1
1
|
import { assertEntryConstructor, Entry } from '../Entry.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a blank line in a Beancount file.
|
|
4
|
+
* Blank lines are used to separate entries for readability.
|
|
5
|
+
*/
|
|
2
6
|
export class Blankline extends Entry {
|
|
3
7
|
constructor() {
|
|
4
8
|
super(...arguments);
|
|
9
|
+
/** @inheritdoc */
|
|
5
10
|
this.type = 'blankline';
|
|
6
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a Blankline instance from a generic parse result.
|
|
14
|
+
* @param _genericParseResult - Unused, blank lines have no content
|
|
15
|
+
* @returns A new Blankline instance
|
|
16
|
+
*/
|
|
7
17
|
static fromGenericParseResult(
|
|
8
18
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
9
19
|
_genericParseResult) {
|
|
10
20
|
return new Blankline({});
|
|
11
21
|
}
|
|
22
|
+
/** @inheritdoc */
|
|
12
23
|
toString() {
|
|
13
24
|
return '';
|
|
14
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates a Blankline instance from a string.
|
|
28
|
+
* @param _input - Unused, blank lines have no content
|
|
29
|
+
* @returns A new Blankline instance
|
|
30
|
+
*/
|
|
15
31
|
static fromString(
|
|
16
32
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
17
33
|
_input) {
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import type { GenericParseResultWithDate } from '../../genericParse.mjs';
|
|
2
2
|
import { DateEntry } from '../DateEntry.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a Close entry that closes an account.
|
|
5
|
+
* Close directives mark the end of an account's lifespan.
|
|
6
|
+
*/
|
|
3
7
|
export declare class Close extends DateEntry {
|
|
8
|
+
/** @inheritdoc */
|
|
4
9
|
type: "close";
|
|
10
|
+
/** The account name being closed */
|
|
5
11
|
account: string;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a Close instance from a generic parse result.
|
|
14
|
+
* @param genericParseResult - The parsed close entry data
|
|
15
|
+
* @returns A new Close instance
|
|
16
|
+
*/
|
|
6
17
|
static fromGenericParseResult(genericParseResult: GenericParseResultWithDate): Close;
|
|
18
|
+
/** @inheritdoc */
|
|
7
19
|
toString(): string;
|
|
8
20
|
}
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { assertEntryConstructor } from '../Entry.mjs';
|
|
2
2
|
import { DateEntry } from '../DateEntry.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a Close entry that closes an account.
|
|
5
|
+
* Close directives mark the end of an account's lifespan.
|
|
6
|
+
*/
|
|
3
7
|
export class Close extends DateEntry {
|
|
4
8
|
constructor() {
|
|
5
9
|
super(...arguments);
|
|
10
|
+
/** @inheritdoc */
|
|
6
11
|
this.type = 'close';
|
|
7
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Creates a Close instance from a generic parse result.
|
|
15
|
+
* @param genericParseResult - The parsed close entry data
|
|
16
|
+
* @returns A new Close instance
|
|
17
|
+
*/
|
|
8
18
|
static fromGenericParseResult(genericParseResult) {
|
|
9
19
|
const account = genericParseResult.header.trim();
|
|
10
20
|
return new Close({
|
|
@@ -12,6 +22,7 @@ export class Close extends DateEntry {
|
|
|
12
22
|
account,
|
|
13
23
|
});
|
|
14
24
|
}
|
|
25
|
+
/** @inheritdoc */
|
|
15
26
|
toString() {
|
|
16
27
|
return `${this.getDateTypePrefix()} ${this.account}${this.getMetaDataString()}`;
|
|
17
28
|
}
|