Change signature for Property pattern helpers
This commit is contained in:
parent
63c41f07cb
commit
6c6c9386be
2 changed files with 13 additions and 24 deletions
|
@ -1,5 +1,5 @@
|
|||
import { h } from "../lib/html";
|
||||
import { Idv, StringFromDocument } from "../lib/idv";
|
||||
import { Idv, StringProperty } from "../lib/idv";
|
||||
|
||||
export function IdvDebug() {
|
||||
const textarea = h(
|
||||
|
@ -51,8 +51,8 @@ User: tirga
|
|||
|
||||
const UserFromDocument = (idv: Idv) => {
|
||||
return {
|
||||
shell: idv.getProperty("Shell", String, StringFromDocument),
|
||||
groups: idv.getList("Group", String, StringFromDocument),
|
||||
banner: idv.getProperty("Banner", String, StringFromDocument),
|
||||
shell: idv.getProperty("Shell", StringProperty),
|
||||
groups: idv.getList("Group", StringProperty),
|
||||
banner: idv.getProperty("Banner", StringProperty),
|
||||
};
|
||||
};
|
||||
|
|
29
lib/idv.ts
29
lib/idv.ts
|
@ -1,5 +1,6 @@
|
|||
export type DistinguisherParser<T> = (distinguisher: string) => T;
|
||||
export type DocumentParser<T> = (document: string[]) => T;
|
||||
export type UnionParser<T> = (distinguisher: string, document: string[]) => T;
|
||||
export type IdvConverter<T> = (idv: Idv) => T;
|
||||
|
||||
const LEADING_WHITESPACE = /^([ \t]+)/;
|
||||
|
@ -58,6 +59,7 @@ export class Idv {
|
|||
|
||||
currentDocument = [];
|
||||
currentIndent = undefined;
|
||||
// TODO: implement backslash escaping in the distinguisher
|
||||
this.collections[collection].push([distinguisher, currentDocument]);
|
||||
} else {
|
||||
throw new Error("Failed to parse a property");
|
||||
|
@ -68,29 +70,14 @@ export class Idv {
|
|||
return this;
|
||||
}
|
||||
|
||||
public getProperty<T>(
|
||||
name: string,
|
||||
parseDistinguisher: DistinguisherParser<T>,
|
||||
parseDocument: DocumentParser<T>
|
||||
): T | undefined {
|
||||
public getProperty<T>(name: string, parse: UnionParser<T>): T | undefined {
|
||||
const firstEntry = this.collections[name]?.[0];
|
||||
return (
|
||||
firstEntry &&
|
||||
(firstEntry[1].length > 0
|
||||
? parseDocument(firstEntry[1])
|
||||
: parseDistinguisher(firstEntry[0]))
|
||||
);
|
||||
return firstEntry && parse(firstEntry[0], firstEntry[1]);
|
||||
}
|
||||
|
||||
public getList<T>(
|
||||
name: string,
|
||||
parseDistinguisher: DistinguisherParser<T>,
|
||||
parseDocument: DocumentParser<T>
|
||||
): T[] {
|
||||
public getList<T>(name: string, parse: UnionParser<T>): T[] {
|
||||
return (this.collections[name] ?? []).map(([distinguisher, document]) =>
|
||||
document.length > 0
|
||||
? parseDocument(document)
|
||||
: parseDistinguisher(distinguisher)
|
||||
parse(distinguisher, document)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -131,4 +118,6 @@ export class Idv {
|
|||
}
|
||||
}
|
||||
|
||||
export const StringFromDocument = (lines: string[]) => lines.join("\n");
|
||||
// TODO: implement backslash-escaping in the document?
|
||||
export const StringProperty = (distinguisher: string, lines: string[]) =>
|
||||
lines.length == 0 ? distinguisher : lines.join("\n");
|
||||
|
|
Loading…
Add table
Reference in a new issue