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[]][]> = {}; collections: Record<string, undefined | [string, string[]][]> = {};
public static parse(input: string): Idv { 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(); 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 currentDocument: string[] | undefined = undefined;
let currentIndent: string | undefined = undefined; let currentIndent: string | undefined = undefined;
let bufferedBlankLines: string[] = []; let bufferedBlankLines: string[] = [];
@ -44,20 +51,20 @@ export class Idv {
if (matches) { if (matches) {
const [, collection, distinguisher] = matches; const [, collection, distinguisher] = matches;
if (idv.collections[collection] == undefined) { if (this.collections[collection] == undefined) {
idv.collections[collection] = []; this.collections[collection] = [];
} }
currentDocument = []; currentDocument = [];
currentIndent = undefined; currentIndent = undefined;
idv.collections[collection].push([distinguisher, currentDocument]); this.collections[collection].push([distinguisher, currentDocument]);
} else { } else {
throw new Error("Failed to parse a property"); throw new Error("Failed to parse a property");
} }
} }
}); });
return idv; return this;
} }
public getProperty<T>( public getProperty<T>(