diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b943dbc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "deno.enable": true +} \ No newline at end of file diff --git a/example.ts b/example.ts new file mode 100755 index 0000000..7c42052 --- /dev/null +++ b/example.ts @@ -0,0 +1,38 @@ +#!/bin/env -S deno run + +import { extr } from "./mod.ts" + +const date = ["month", " ", "day", " ", "h", ":", "m", ":", "s"] as const; +const service = extr("", ...date, " ", "device", " ", "service", "[", "pid", "]: ", "line"); +const kernel_ = extr("", ...date, " ", "device", " kernel: ", "line"); + +const lines = [ + "août 06 09:25:18 navis systemd[1]: Finished Load Kernel Module configfs.", + "août 06 09:25:18 navis systemd[1]: modprobe@drm.service: Succeeded.", + "août 06 09:25:18 navis systemd[1]: Finished Load Kernel Module drm.", + "août 06 09:25:18 navis systemd[1]: Mounting Kernel Configuration File System...", + "août 06 09:25:18 navis kernel: fuse: init (API version 7.32)", + "août 06 09:25:18 navis systemd[1]: modprobe@fuse.service: Succeeded.", + "août 06 09:25:18 navis systemd[1]: Finished Load Kernel Module fuse.", + "août 06 09:25:18 navis systemd[1]: Mounted Kernel Configuration File System.", + "août 06 09:25:18 navis systemd[1]: Mounting FUSE Control File System...", + "août 06 09:25:18 navis systemd[1]: Mounted FUSE Control File System.", + "août 06 09:25:18 navis kernel: EXT4-fs (sda4): re-mounted. Opts: errors=remount-ro", + "août 06 09:25:18 navis systemd[1]: Finished Remount Root and Kernel File Systems.", + "août 06 09:25:18 navis systemd[1]: Condition check resulted in Rebuild Hardware Database being skipped.", + "août 06 09:25:18 navis systemd[1]: Condition check resulted in Platform Persistent Storage Archival being skipped.", + "août 06 09:25:18 navis systemd[1]: Starting Load/Save Random Seed...", + "août 06 09:25:18 navis systemd[1]: Starting Create System Users...", + "août 06 09:25:18 navis kernel: lp: driver loaded but no devices found", + "août 06 09:25:18 navis kernel: ppdev: user-space parallel port driver", + "août 06 09:25:18 navis systemd[1]: Finished Load/Save Random Seed.", + "août 06 09:25:18 navis systemd[1]: Finished Load Kernel Modules.", + "août 06 09:25:18 navis systemd[1]: Condition check resulted in First Boot Complete being skipped.", + "août 06 09:25:18 navis systemd[1]: Starting Apply Kernel Variables...", + "août 06 09:25:18 navis systemd[1]: Finished Create System Users.", +]; +const mapped = lines.map(line => service.or(kernel_).get(line)); +const failed = mapped.filter(m => m === null).length; +const content = mapped.filter(m => m !== null).map(m => "service" in m! ? [m.service, m.line] : ["kernel", m!.line]); + +console.log({ failed, content }) diff --git a/lib/rule.ts b/lib/rule.ts index b18da3c..3f950a7 100644 --- a/lib/rule.ts +++ b/lib/rule.ts @@ -17,18 +17,26 @@ export class Rule { if (text.length > 0) return null; else return {} as StructOfArr; } - if (!text.includes(first_sep.word)) return null; - let [val, rest] = text.split(first_sep.word, 2); + let [val, rest] = split_once(text, first_sep.word); + if (rest === null) return null; const result = {} as Record; for (const [key, sep] of zip(this.vars, this.separators.slice(1))) { if (sep.kind === "end") { result[key] = rest; break; } - if (!rest.includes(sep.word)) return null; - [val, rest] = rest.split(sep.word, 2); + [val, rest] = split_once(rest, sep.word); + if (rest === null) return null; result[key] = val; } return result as StructOfArr; } } + +function split_once(text: string, separator: string) { + const cursor = text.indexOf(separator); + if (cursor === -1) return [text, null] as const; + const left = text.slice(0, cursor); + const right = text.slice(cursor + separator.length); + return [left, right] as const; +} diff --git a/mod.ts b/mod.ts index eff9d90..b7107de 100644 --- a/mod.ts +++ b/mod.ts @@ -1 +1 @@ -import { extr } from "./lib/lib.ts" +export { extr } from "./lib/lib.ts"