From 525c6fa954572de3b8766fa7b34f3bb3b614add0 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Wed, 10 Jul 2024 00:31:33 -0400 Subject: [PATCH] impl map helper --- debug/idv.ts | 38 +++++++++++++++++++++++++------------- lib/idv.ts | 12 ++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/debug/idv.ts b/debug/idv.ts index b54354c..de17339 100644 --- a/debug/idv.ts +++ b/debug/idv.ts @@ -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), + }; +}; diff --git a/lib/idv.ts b/lib/idv.ts index a656145..ddccc28 100644 --- a/lib/idv.ts +++ b/lib/idv.ts @@ -85,6 +85,18 @@ export class Idv { : parseDistinguisher(distinguisher) ); } + + public getMap( + name: string, + parseDocument: DocumentParser + ): Record { + return Object.fromEntries( + (this.collections[name] ?? []).map(([distinguisher, document]) => [ + distinguisher, + parseDocument(document), + ]) + ); + } } export const StringFromDocument = (lines: string[]) => lines.join("\n");