Skip to content

Commit

Permalink
minor cleanup of rendering fns
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoscaz committed Oct 29, 2024
1 parent f0d09c5 commit 1f6014f
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@

import type { RenderItemsFn } from './types.js';
import type { Item, RenderItemsFn } from './types.js';

import { stringify } from 'csv-stringify/sync';
import columnify from 'columnify';

type Row = Record<string, string>;

const toRow = (item: Item, show_tags: string[]): Row => {
return show_tags.reduce((acc, tag) => {
acc[tag] = item.tags[tag];
return acc;
}, Object.create(null));
};

const toRows = (items: Item[], show_tags: string[]): Row[] => {
return items.map(item => toRow(item, show_tags));
};

export const renderTabular: RenderItemsFn = (items, show_tags) => {
const data = items.map((task) => {
const row: Record<string, any> = {};
show_tags.forEach((tag) => {
row[tag] = task.tags[tag] ?? '';
});
return row;
});
data.unshift(show_tags.reduce((acc, col) => {
const rows = toRows(items, show_tags);
rows.unshift(show_tags.reduce((acc, col) => {
acc[col] = ''.padStart(col.length, '-');
return acc;
}, {} as any));
const opts: columnify.GlobalOptions = {
return columnify(rows, {
columns: show_tags,
columnSplitter: ' | ',
headingTransform: data => data,
};
return columnify(data, opts);
headingTransform: heading => heading,
});
};

export const renderCSV: RenderItemsFn = (items, show_tags) => {
return stringify(items.map((task) => {
const row: Record<string, any> = {};
show_tags.forEach((tag) => {
row[tag] = task.tags[tag] ?? '';
});
return row;
}), { columns: show_tags, header: true });
return stringify(
toRows(items, show_tags),
{ columns: show_tags, header: true },
);
};

export const renderJSON: RenderItemsFn = (items, show_tags) => {
return JSON.stringify(items.map((task) => {
const entry: Record<string, any> = {};
show_tags.forEach((tag) => {
entry[tag] = task.tags[tag];
});
return entry;
}));
return JSON.stringify(toRows(items, show_tags));
};

0 comments on commit 1f6014f

Please sign in to comment.