Refactor Idv to support merging documents

This commit is contained in:
Tangent Wantwight 2024-07-14 21:33:01 -04:00
parent 525c6fa954
commit dc19a5ed9e

View file

@ -8,11 +8,18 @@ export class Idv {
collections: Record<string, undefined | [string, string[]][]> = {};
public static parse(input: string): Idv {
const lines = input.split("\n").map((line) => line.trimEnd());
return Idv.parseLines(lines);
}
static parseLines(input: string[]): Idv {
const idv = new Idv();
return idv.import(input);
}
public static parseLines(input: string[]): Idv {
const idv = new Idv();
return idv.importLines(input);
}
public import(input: string): Idv {
const lines = input.split("\n").map((line) => line.trimEnd());
return this.importLines(lines);
}
public importLines(input: string[]): Idv {
let currentDocument: string[] | undefined = undefined;
let currentIndent: string | undefined = undefined;
let bufferedBlankLines: string[] = [];
@ -44,20 +51,20 @@ export class Idv {
if (matches) {
const [, collection, distinguisher] = matches;
if (idv.collections[collection] == undefined) {
idv.collections[collection] = [];
if (this.collections[collection] == undefined) {
this.collections[collection] = [];
}
currentDocument = [];
currentIndent = undefined;
idv.collections[collection].push([distinguisher, currentDocument]);
this.collections[collection].push([distinguisher, currentDocument]);
} else {
throw new Error("Failed to parse a property");
}
}
});
return idv;
return this;
}
public getProperty<T>(