@weborigami/origami 0.5.7 → 0.5.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/origami",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "description": "Web Origami language, CLI, framework, and server",
5
5
  "type": "module",
6
6
  "repository": {
@@ -17,10 +17,10 @@
17
17
  "typescript": "5.9.2"
18
18
  },
19
19
  "dependencies": {
20
- "@weborigami/async-tree": "0.5.7",
20
+ "@weborigami/async-tree": "0.5.8",
21
21
  "@weborigami/json-feed-to-rss": "1.0.0",
22
- "@weborigami/language": "0.5.7",
23
- "@weborigami/types": "0.5.7",
22
+ "@weborigami/language": "0.5.8",
23
+ "@weborigami/types": "0.5.8",
24
24
  "css-tree": "3.1.0",
25
25
  "graphviz-wasm": "3.0.2",
26
26
  "highlight.js": "11.11.1",
@@ -29,6 +29,7 @@ export { default as repeat } from "./repeat.js";
29
29
  export { default as shell } from "./shell.js";
30
30
  export { default as slash } from "./slash.js";
31
31
  export { default as string } from "./string.js";
32
+ export { default as tsv } from "./tsv.js";
32
33
  export { default as unpack } from "./unpack.js";
33
34
  export { default as yaml } from "./yaml.js";
34
35
  export { default as yamlParse } from "./yamlParse.js";
@@ -0,0 +1,43 @@
1
+ import { isUnpackable, toPlainValue } from "@weborigami/async-tree";
2
+ import { EOL } from "node:os";
3
+
4
+ /**
5
+ * Render the object as text in TSV (tab-separated values) format.
6
+ *
7
+ * The object should a treelike object such as an array. The output will include
8
+ * a header row with field names taken from the first item in the tree/array.
9
+ *
10
+ * Lines will end with EOL character(s) appropriate for the operating system.
11
+ *
12
+ * @param {any} object
13
+ */
14
+ export default async function tsv(object) {
15
+ if (object == null) {
16
+ return "";
17
+ }
18
+ if (isUnpackable(object)) {
19
+ object = await object.unpack();
20
+ }
21
+ const value = await toPlainValue(object);
22
+ const array = Array.isArray(value) ? value : Object.values(value);
23
+ const text = formatTsv(array);
24
+ return text;
25
+ }
26
+
27
+ function formatTsv(array) {
28
+ if (!array || array.length === 0) {
29
+ return "";
30
+ }
31
+
32
+ // Extract header fields from the first object.
33
+ const headerFields = Object.keys(array[0]);
34
+ const headerRow = headerFields.join("\t");
35
+
36
+ // Map through each object and generate a data row.
37
+ const dataRows = array.map((row) => {
38
+ return headerFields.map((field) => row[field] ?? "").join("\t");
39
+ });
40
+
41
+ // Concatenate header and data rows, joining and ending with EOL.
42
+ return [headerRow, ...dataRows].join(EOL) + EOL;
43
+ }