@timqi/pier 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -69,3 +69,24 @@ export class SenderPrefix {
69
69
  }
70
70
  /** Put the prefix above the message, or hand the message back untouched. */
71
71
  export const withPrefix = (prefix, text) => prefix ? `${prefix}\n${text}` : text;
72
+ // Only the shapes `next()` emits: `who`, `when`, or both, and — because
73
+ // `withPrefix` always joins with one — a newline after them. Anything else, a
74
+ // markdown link opening the message or a human typing `[14:23] on my way`, is
75
+ // body text and must come back untouched.
76
+ const HEADER = /^\[(?:([^\n[\]<>]*)<([^\n[\]<>]+)>)? ?((?:\d{4}-\d{2}-\d{2} )?\d{1,2}:\d{2})?\]\n/;
77
+ /**
78
+ * Read back a header this module wrote. The prefix is a token-saving device for
79
+ * the model, not something a human should have to read: a surface showing a
80
+ * stored message can render the speaker as it likes and the body without it.
81
+ */
82
+ export function splitSpeaker(text) {
83
+ const m = HEADER.exec(text);
84
+ if (!m?.[2] && !m?.[3])
85
+ return { text };
86
+ return {
87
+ ...(m[1] ? { name: m[1] } : {}),
88
+ ...(m[2] ? { id: m[2] } : {}),
89
+ ...(m[3] ? { when: m[3] } : {}),
90
+ text: text.slice(m[0].length),
91
+ };
92
+ }