~bmp/hayom

4e35cc4390d56531be2256245eef737a3058b019 — Benjamin Pollack 7 months ago 256b9e7
extract rendering logic
3 files changed, 48 insertions(+), 16 deletions(-)

M cli.ts
M mod.ts
M test.ts
M cli.ts => cli.ts +2 -2
@@ 4,8 4,8 @@ import { loadConfig } from "./config.ts";
import {
  Entry,
  filterEntries,
  loadEntries,
  makeEntry,
  parseEntries,
  printEntry,
  saveEntries,
} from "./mod.ts";


@@ 106,7 106,7 @@ async function main() {
    } else throw e;
  }

  const entries = loadEntries(path);
  const entries = parseEntries(Deno.readTextFileSync(path));

  if (
    ["from", "f", "to", "t", "on", "count", "n"].some((arg) => arg in opts) ||

M mod.ts => mod.ts +13 -13
@@ 25,11 25,19 @@ function parseEntry(header: string, body: string): Entry {
  return { date, title, body };
}

export function loadEntries(path: string): Entry[] {
  const text = Deno.readTextFileSync(path);
  const entries = text.split(/^(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}\] .*)\n/m)
export function parseEntries(bodies: string): Entry[] {
  const entries = bodies.split(/^(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}\] .*)\n/m)
    .slice(1);
  return chunk(entries, 2).map((e) => parseEntry(e[0], e[1]));
  return chunk(entries, 2).map((e) => parseEntry(e[0], e[1].trim()));
}

export function renderEntry(entry: Entry): string {
  let text = `[${entry.date.toFormat(DATE_STRING)}] ${entry.title.trim()}\n`;
  const body = entry.body?.trim();
  if (body != null && body !== "") {
    text += body + "\n";
  }
  return text;
}

export function saveEntries(path: string, entries: Entry[]) {


@@ 45,15 53,7 @@ export function saveEntries(path: string, entries: Entry[]) {
        file.writeSync(nl);
      }
      first = false;
      file.writeSync(
        encoder.encode(
          `[${entry.date.toFormat(DATE_STRING)}] ${entry.title.trim()}\n`,
        ),
      );
      const body = entry.body?.trim();
      if (body != null && body !== "") {
        file.writeSync(encoder.encode(body + "\n"));
      }
      file.writeSync(encoder.encode(renderEntry(entry)));
    }
  } finally {
    file?.close();

M test.ts => test.ts +33 -1
@@ 1,6 1,6 @@
import { assertEquals } from "https://deno.land/std@0.118.0/testing/asserts.ts";

import { makeEntry } from "./mod.ts";
import { makeEntry, parseEntries, renderEntry } from "./mod.ts";

Deno.test("make sure we can generate simple entries", () => {
  const raw = "This is a simple test";


@@ 33,3 33,35 @@ Deno.test("test multiline entries", () => {
    "I guess so.\nBut how can you be sure?  What should we\nbe doing?!",
  );
});

Deno.test("test multiparse with a single entry", () => {
  const raw =
    "This is a simple test?!  I guess so.\nBut how can you be sure?  What should we\nbe doing?!";
  const cleanEntry = makeEntry(raw);
  const parsedEntry = parseEntries(renderEntry(cleanEntry))[0];
  assertEquals(parsedEntry.title, "This is a simple test?!");
  assertEquals(
    parsedEntry.body,
    "I guess so.\nBut how can you be sure?  What should we\nbe doing?!",
  );
});

Deno.test("round trip of multiple entries", () => {
  const multiple = `[1999-12-31 23:59] This is it!
It's about to be the new century! I can't wait!

[2000-01-01 00:00] Oh Cthulhu oh crap oh spaghetti
Everything has collapsed, society is imploding.

[2000-01-01 00:01] Every man for himself.
That means you're on your own, my little hamster.  Good luck and godspeed.
`;
  const entries = parseEntries(multiple);
  assertEquals(entries.length, 3);
  assertEquals(entries[0].title, "This is it!");
  assertEquals(
    entries[1].body,
    "Everything has collapsed, society is imploding.",
  );
  assertEquals(entries.map(renderEntry).join("\n"), multiple);
});