autopreso 0.1.1

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.
@@ -0,0 +1,48 @@
1
+ export function formatLineNumberedWhiteboard(elements) {
2
+ if (!Array.isArray(elements) || elements.length === 0) return "(empty whiteboard)";
3
+
4
+ const width = Math.max(3, String(elements.length).length);
5
+ return elements
6
+ .map((element, index) => `${String(index + 1).padStart(width, "0")}: ${JSON.stringify(element)}`)
7
+ .join("\n");
8
+ }
9
+
10
+ export function applyWhiteboardEditOperations(elements, operations) {
11
+ const nextElements = [...elements];
12
+
13
+ for (const operation of operations) {
14
+ if (operation.type === "replace") {
15
+ assertLineInRange(operation.line, nextElements.length, `Cannot replace line ${operation.line}`);
16
+ nextElements[operation.line - 1] = operation.element;
17
+ continue;
18
+ }
19
+
20
+ if (operation.type === "insert_after") {
21
+ assertLineInInsertRange(operation.line, nextElements.length, `Cannot insert after line ${operation.line}`);
22
+ nextElements.splice(operation.line, 0, operation.element);
23
+ continue;
24
+ }
25
+
26
+ if (operation.type === "delete") {
27
+ assertLineInRange(operation.line, nextElements.length, `Cannot delete line ${operation.line}`);
28
+ nextElements.splice(operation.line - 1, 1);
29
+ continue;
30
+ }
31
+
32
+ throw new Error(`Unknown whiteboard edit operation "${operation.type}".`);
33
+ }
34
+
35
+ return nextElements;
36
+ }
37
+
38
+ function assertLineInRange(line, lineCount, message) {
39
+ if (!Number.isInteger(line) || line < 1 || line > lineCount) {
40
+ throw new Error(`${message}; whiteboard has ${lineCount} line${lineCount === 1 ? "" : "s"}.`);
41
+ }
42
+ }
43
+
44
+ function assertLineInInsertRange(line, lineCount, message) {
45
+ if (!Number.isInteger(line) || line < 0 || line > lineCount) {
46
+ throw new Error(`${message}; whiteboard has ${lineCount} line${lineCount === 1 ? "" : "s"}.`);
47
+ }
48
+ }