impl map helper

This commit is contained in:
Tangent Wantwight 2024-07-10 00:31:33 -04:00
parent 2f9bb73107
commit 525c6fa954
2 changed files with 37 additions and 13 deletions

View file

@ -12,15 +12,21 @@ export function IdvDebug() {
},
},
`# Idv Testing Ground
Uid: 0
Shell: tclsh
Group: users
Group: sudo
Banner:
+------------------+
|Welcome to Debian!|
+------------------+
#`
User: tangent128
Uid: 10000
Shell: tclsh
Group: users
Group: sudo
Banner:
+------------------+
|Welcome to Debian!|
+------------------+
User: tirga
Uid: 10101
Shell: bash
Group: users
`
);
const pre = h("pre");
@ -30,10 +36,7 @@ Banner:
const idv = Idv.parse(textarea.value);
pre.textContent = JSON.stringify(
{
shell: idv.getProperty("Shell", String, StringFromDocument),
groups: idv.getList("Group", String, StringFromDocument),
},
idv.getMap("User", UserFromDocument),
null,
2
);
@ -45,3 +48,12 @@ Banner:
return [textarea, pre];
}
const UserFromDocument = (lines: string[]) => {
const idv = Idv.parseLines(lines);
return {
shell: idv.getProperty("Shell", String, StringFromDocument),
groups: idv.getList("Group", String, StringFromDocument),
banner: idv.getProperty("Banner", String, StringFromDocument),
};
};

View file

@ -85,6 +85,18 @@ export class Idv {
: parseDistinguisher(distinguisher)
);
}
public getMap<T>(
name: string,
parseDocument: DocumentParser<T>
): Record<string, T> {
return Object.fromEntries(
(this.collections[name] ?? []).map(([distinguisher, document]) => [
distinguisher,
parseDocument(document),
])
);
}
}
export const StringFromDocument = (lines: string[]) => lines.join("\n");