Refactor Idv to support merging documents
This commit is contained in:
parent
525c6fa954
commit
dc19a5ed9e
1 changed files with 15 additions and 8 deletions
23
lib/idv.ts
23
lib/idv.ts
|
@ -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>(
|
||||
|
|
Loading…
Add table
Reference in a new issue