Prepare output values
createPresentation() creates and formats output values. Call presentation.render(...) when one value works for every format. Use the individual renderers when JSON, tables, and CSV require different data.
Empty and text output
presentation.empty() represents a successful command with no output. presentation.text(...) accepts ready text.
import { createPresentation } from 'icore';
const presentation = createPresentation();
const noOutput = presentation.empty();
const readyText = presentation.text('cache cleared\n');
These values keep their original form when selecting table, json, or csv.
Records
Use record(...) and records(...) for the same top-level fields in JSON, tables, and CSV.
const oneJob = presentation.record({
id: 'job-42',
status: 'running'
});
const manyJobs = presentation.records([
{
id: 'job-41',
status: 'done'
},
{
id: 'job-42',
status: 'running'
}
]);
presentation.render(oneJob, 'table');
presentation.render(manyJobs, 'json');
PresentationRecord accepts arbitrary string keys. Map the required fields from a regular TypeScript interface to an object literal to pass them without a cast.
interface JobReportRow {
id: string;
status: string;
internalTraceId: string;
}
function presentJobs(rows: readonly JobReportRow[]) {
return presentation.records(rows.map((row) => ({
id: row.id,
status: row.status
})));
}
In table format, record(row) creates a vertical table with field and value columns. Use records([row]) for a horizontal table with headers from the field names. For records([]), JSON produces [] and the table and CSV render empty text. To show headers even without rows, pass them explicitly to the table or CSV renderer.
Prepared table and CSV cells
Pass ready text cells to table(...) and rows of scalar values to csv(...). The application controls column order and values.
const table = presentation.table([
['id', 'status'],
['job-41', 'done'],
['job-42', 'running']
]);
const csv = presentation.csv([
['id', 'status'],
['job-41', 'done'],
['job-42', 'running']
]);
Text-table alignment uses JavaScript string length. ANSI sequences, emoji, combining characters, tabs, and full-width characters can appear misaligned. Column alignment applies only to text tables.
Validate the requested format
Use presentation.formats, presentationFormats, and isPresentationFormat(...) for the list and validation of supported formats.
import { isPresentationFormat, presentationFormats } from 'icore';
function normalizeFormat(value: unknown) {
return isPresentationFormat(value) ? value : 'table';
}
presentation.formats;
presentationFormats;
Render an output value
presentation.render(...) calls renderPresentationResult(...). The standalone function suits tests and adapters that already have an output value.
import { renderPresentationResult } from 'icore';
const view = presentation.records([
{
id: 'job-42',
status: 'running'
}
]);
const fromFacade = presentation.render(view, 'csv');
const fromPrimitive = renderPresentationResult(view, 'csv');
Check an output value
Check an unknown result with isPresentationResult(...) before rendering.
import { isPresentationResult } from 'icore';
function renderUnknown(value: unknown): string {
if (!isPresentationResult(value)) {
throw new Error('Expected presentation result');
}
return presentation.render(value, 'table');
}
Call renderers directly
createPresentation() provides renderers for data already prepared for a specific format.
const jsonText = presentation.renderers.json.render({
ok: true
});
const tableText = presentation.renderers.table.render([
['field', 'value'],
['ok', 'true']
]);
const csvRow = presentation.renderers.csv.renderRow([
'job-42',
'running'
]);
const csvText = presentation.renderers.csv.render([
['id', 'status'],
['job-42', 'running']
]);
The same renderers are available as standalone exports.
import { renderCsv, renderCsvRow, renderJson, renderTextTable } from 'icore';
renderJson({ ok: true });
renderTextTable([
['field', 'value'],
['ok', 'true']
]);
renderCsvRow(['job-42', 'running']);
renderCsv([
['id', 'status'],
['job-42', 'running']
]);
Direct calls let JSON keep a complete nested report while tables and CSV select their own columns and format each cell. The application chooses the renderer after reading the option.
const jsonText = renderJson(report);
const tableText = renderTextTable([
['id', 'status'],
...report.orders.map((order) => [
order.id,
order.status
])
]);