| 1 |
import type { ValidationT, Validator } from "../lib/validation"; |
| 2 |
import { |
| 3 |
array, |
| 4 |
constant, |
| 5 |
defaultValue, |
| 6 |
object, |
| 7 |
optional_string, |
| 8 |
string, |
| 9 |
tagged_union, |
| 10 |
} from "../lib/validation"; |
| 11 |
import { Amount } from "./amount"; |
| 12 |
import { Cost } from "./cost"; |
| 13 |
import type { MetadataValue } from "./metadata"; |
| 14 |
import { EntryMetadata } from "./metadata"; |
| 15 |
import { Position } from "./position"; |
| 16 |
|
| 17 |
export { Amount, Cost, EntryMetadata, Position }; |
| 18 |
|
| 19 |
/** A posting. */ |
| 20 |
export class Posting { |
| 21 |
private constructor( |
| 22 |
readonly meta: EntryMetadata, |
| 23 |
readonly account: string, |
| 24 |
readonly amount: string, |
| 25 |
) {} |
| 26 |
|
| 27 |
/** Create a new empty Posting. */ |
| 28 |
static empty(): Posting { |
| 29 |
return new Posting(new EntryMetadata(), "", ""); |
| 30 |
} |
| 31 |
|
| 32 |
is_empty(): boolean { |
| 33 |
return !this.account && !this.amount && this.meta.is_empty(); |
| 34 |
} |
| 35 |
|
| 36 |
/** Set a property and return an updated copy. */ |
| 37 |
set<T extends keyof Posting>(key: T, value: Posting[T]): Posting { |
| 38 |
const copy = new Posting(this.meta, this.account, this.amount); |
| 39 |
copy[key] = value; |
| 40 |
return copy; |
| 41 |
} |
| 42 |
|
| 43 |
private static raw_validator = object({ |
| 44 |
meta: defaultValue(EntryMetadata.validator, () => new EntryMetadata()), |
| 45 |
account: string, |
| 46 |
amount: string, |
| 47 |
}); |
| 48 |
|
| 49 |
static validator: Validator<Posting> = (json) => |
| 50 |
Posting.raw_validator(json).map( |
| 51 |
({ meta, account, amount }) => new Posting(meta, account, amount), |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** The properties that all entries share. */ |
| 56 |
export interface EntryBaseAttributes { |
| 57 |
t: string; |
| 58 |
meta: EntryMetadata; |
| 59 |
date: string; |
| 60 |
} |
| 61 |
|
| 62 |
export const entryBaseValidator = object<EntryBaseAttributes>({ |
| 63 |
t: string, |
| 64 |
meta: EntryMetadata.validator, |
| 65 |
date: string, |
| 66 |
}); |
| 67 |
|
| 68 |
abstract class EntryBase<T extends string> { |
| 69 |
constructor( |
| 70 |
readonly t: T, |
| 71 |
readonly meta: EntryMetadata, |
| 72 |
readonly date: string, |
| 73 |
) {} |
| 74 |
|
| 75 |
/** Clone. */ |
| 76 |
clone(): this { |
| 77 |
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 78 |
const copy: typeof this = Object.assign( |
| 79 |
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
| 80 |
Object.create(Object.getPrototypeOf(this)), |
| 81 |
this, |
| 82 |
); |
| 83 |
return copy; |
| 84 |
} |
| 85 |
|
| 86 |
/** Set a property and return an updated copy. */ |
| 87 |
set<T extends keyof typeof this>(key: T, value: (typeof this)[T]): this { |
| 88 |
const copy = this.clone(); |
| 89 |
copy[key] = value; |
| 90 |
return copy; |
| 91 |
} |
| 92 |
|
| 93 |
/** Set the value for a key and return an updated copy. */ |
| 94 |
set_meta(key: string, value: MetadataValue): this { |
| 95 |
const copy = this.clone(); |
| 96 |
// @ts-expect-error We can mutate it as we just created it and noone has access yet. |
| 97 |
copy.meta = this.meta.set(key, value); |
| 98 |
return copy; |
| 99 |
} |
| 100 |
|
| 101 |
/** Check whether the given entry is marked as duplicate (used in imports). */ |
| 102 |
is_duplicate(): boolean { |
| 103 |
const value = this.meta.get("__duplicate__"); |
| 104 |
return value != null && value !== false; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
interface RawAmount { |
| 109 |
readonly number: string; |
| 110 |
readonly currency: string; |
| 111 |
} |
| 112 |
|
| 113 |
/** A balance. */ |
| 114 |
export class Balance extends EntryBase<"Balance"> { |
| 115 |
private constructor( |
| 116 |
meta: EntryMetadata, |
| 117 |
date: string, |
| 118 |
readonly account: string, |
| 119 |
readonly amount: RawAmount, |
| 120 |
) { |
| 121 |
super("Balance", meta, date); |
| 122 |
} |
| 123 |
|
| 124 |
/** Create a new empty Balance entry on the date. */ |
| 125 |
static empty(date: string): Balance { |
| 126 |
return new Balance(new EntryMetadata(), date, "", { |
| 127 |
number: "", |
| 128 |
currency: "", |
| 129 |
}); |
| 130 |
} |
| 131 |
|
| 132 |
private static raw_validator = object({ |
| 133 |
t: constant("Balance"), |
| 134 |
meta: EntryMetadata.validator, |
| 135 |
date: string, |
| 136 |
account: string, |
| 137 |
amount: object({ number: string, currency: string }), |
| 138 |
}); |
| 139 |
|
| 140 |
static validator: Validator<Balance> = (json) => |
| 141 |
Balance.raw_validator(json).map( |
| 142 |
({ date, meta, account, amount }) => |
| 143 |
new Balance(meta, date, account, amount), |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** A document. */ |
| 148 |
export class Document extends EntryBase<"Document"> { |
| 149 |
private constructor( |
| 150 |
meta: EntryMetadata, |
| 151 |
date: string, |
| 152 |
readonly account: string, |
| 153 |
readonly filename: string, |
| 154 |
) { |
| 155 |
super("Document", meta, date); |
| 156 |
} |
| 157 |
|
| 158 |
private static raw_validator = object({ |
| 159 |
t: constant("Document"), |
| 160 |
meta: EntryMetadata.validator, |
| 161 |
date: string, |
| 162 |
account: string, |
| 163 |
filename: string, |
| 164 |
}); |
| 165 |
|
| 166 |
static validator: Validator<Document> = (json) => |
| 167 |
Document.raw_validator(json).map( |
| 168 |
({ date, meta, account, filename }) => |
| 169 |
new Document(meta, date, account, filename), |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
/** An event. */ |
| 174 |
export class Event extends EntryBase<"Event"> { |
| 175 |
private constructor( |
| 176 |
meta: EntryMetadata, |
| 177 |
date: string, |
| 178 |
readonly type: string, |
| 179 |
readonly description: string, |
| 180 |
) { |
| 181 |
super("Event", meta, date); |
| 182 |
} |
| 183 |
|
| 184 |
private static raw_validator = object({ |
| 185 |
t: constant("Event"), |
| 186 |
meta: EntryMetadata.validator, |
| 187 |
date: string, |
| 188 |
type: string, |
| 189 |
description: string, |
| 190 |
}); |
| 191 |
|
| 192 |
static validator: Validator<Event> = (json) => |
| 193 |
Event.raw_validator(json).map( |
| 194 |
({ meta, date, type, description }) => |
| 195 |
new Event(meta, date, type, description), |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
/** A note. */ |
| 200 |
export class Note extends EntryBase<"Note"> { |
| 201 |
private constructor( |
| 202 |
meta: EntryMetadata, |
| 203 |
date: string, |
| 204 |
readonly account: string, |
| 205 |
readonly comment: string, |
| 206 |
) { |
| 207 |
super("Note", meta, date); |
| 208 |
} |
| 209 |
|
| 210 |
/** Create a new empty Note entry on the date. */ |
| 211 |
static empty(date: string): Note { |
| 212 |
return new Note(new EntryMetadata(), date, "", ""); |
| 213 |
} |
| 214 |
|
| 215 |
private static raw_validator = object({ |
| 216 |
t: constant("Note"), |
| 217 |
meta: EntryMetadata.validator, |
| 218 |
date: string, |
| 219 |
account: string, |
| 220 |
comment: string, |
| 221 |
}); |
| 222 |
|
| 223 |
static validator: Validator<Note> = (json) => |
| 224 |
Note.raw_validator(json).map( |
| 225 |
({ meta, date, account, comment }) => |
| 226 |
new Note(meta, date, account, comment), |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
const TAGS_RE = /(?:^|\s)#([A-Za-z0-9\-_/.]+)/g; |
| 231 |
const LINKS_RE = /(?:^|\s)\^([A-Za-z0-9\-_/.]+)/g; |
| 232 |
|
| 233 |
/** A transaction. */ |
| 234 |
export class Transaction extends EntryBase<"Transaction"> { |
| 235 |
private constructor( |
| 236 |
meta: EntryMetadata, |
| 237 |
date: string, |
| 238 |
readonly flag: string, |
| 239 |
readonly payee: string, |
| 240 |
readonly narration: string, |
| 241 |
readonly tags: string[], |
| 242 |
readonly links: string[], |
| 243 |
readonly postings: readonly Posting[], |
| 244 |
) { |
| 245 |
super("Transaction", meta, date); |
| 246 |
} |
| 247 |
|
| 248 |
/** Create a new empty Transaction entry on the date. */ |
| 249 |
static empty(date: string): Transaction { |
| 250 |
return new Transaction(new EntryMetadata(), date, "*", "", "", [], [], []); |
| 251 |
} |
| 252 |
|
| 253 |
/** Combine narration, tags, and links for a single `<input>`. */ |
| 254 |
get_narration_tags_links(): string { |
| 255 |
let val = this.narration; |
| 256 |
if (this.tags.length) { |
| 257 |
val += ` ${this.tags.map((t) => `#${t}`).join(" ")}`; |
| 258 |
} |
| 259 |
if (this.links.length) { |
| 260 |
val += ` ${this.links.map((t) => `^${t}`).join(" ")}`; |
| 261 |
} |
| 262 |
return val; |
| 263 |
} |
| 264 |
|
| 265 |
/** Set narration, tags, and links from a single string. */ |
| 266 |
set_narration_tags_links(value: string): Transaction { |
| 267 |
const tags = [...value.matchAll(TAGS_RE)].map((a) => a[1] ?? ""); |
| 268 |
const links = [...value.matchAll(LINKS_RE)].map((a) => a[1] ?? ""); |
| 269 |
const narration = value |
| 270 |
.replaceAll(TAGS_RE, "") |
| 271 |
.replaceAll(LINKS_RE, "") |
| 272 |
.trim(); |
| 273 |
return new Transaction( |
| 274 |
this.meta, |
| 275 |
this.date, |
| 276 |
this.flag, |
| 277 |
this.payee, |
| 278 |
narration, |
| 279 |
tags, |
| 280 |
links, |
| 281 |
this.postings, |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
toString(): string { |
| 286 |
const postings = this.postings.map((p) => ` ${p.account} ${p.amount}`); |
| 287 |
return ( |
| 288 |
`${this.date} ${this.flag} "${this.payee}" "${this.narration}"` + |
| 289 |
`\n${postings.join("\n")}` |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
toJSON(): ValidationT<typeof Transaction.raw_validator> { |
| 294 |
return { |
| 295 |
t: this.t, |
| 296 |
meta: this.meta, |
| 297 |
date: this.date, |
| 298 |
flag: this.flag, |
| 299 |
payee: this.payee, |
| 300 |
narration: this.narration, |
| 301 |
tags: this.tags, |
| 302 |
links: this.links, |
| 303 |
postings: this.postings.filter((p) => !p.is_empty()), |
| 304 |
}; |
| 305 |
} |
| 306 |
|
| 307 |
private static raw_validator = object({ |
| 308 |
t: constant("Transaction"), |
| 309 |
meta: EntryMetadata.validator, |
| 310 |
date: string, |
| 311 |
flag: string, |
| 312 |
payee: optional_string, |
| 313 |
narration: optional_string, |
| 314 |
tags: array(string), |
| 315 |
links: array(string), |
| 316 |
postings: array(Posting.validator), |
| 317 |
}); |
| 318 |
|
| 319 |
static validator: Validator<Transaction> = (json) => |
| 320 |
Transaction.raw_validator(json).map( |
| 321 |
({ meta, date, flag, payee, narration, tags, links, postings }) => |
| 322 |
new Transaction( |
| 323 |
meta, |
| 324 |
date, |
| 325 |
flag, |
| 326 |
payee, |
| 327 |
narration, |
| 328 |
tags, |
| 329 |
links, |
| 330 |
postings, |
| 331 |
), |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
/** Validate an Entry. */ |
| 336 |
export const entryValidator = tagged_union("t", { |
| 337 |
Balance: Balance.validator, |
| 338 |
Document: Document.validator, |
| 339 |
Event: Event.validator, |
| 340 |
Note: Note.validator, |
| 341 |
Transaction: Transaction.validator, |
| 342 |
}); |
| 343 |
|
| 344 |
/** A Beancount entry, currently only supports some of the types. */ |
| 345 |
export type Entry = ValidationT<typeof entryValidator>; |