Skip to content

Main content

Table as Document Source Code

Browse the implementation files for Table as Document.
tool.ts typescript
import { parseNumericTable } from '../../matrix/as-table/tool';
import { createToolDocument, type ToolDocument } from '../tool';

export const tableAsDocument = (
	input: unknown
): ToolDocument | null => {
	const table = parseNumericTable(input);
	return table === null
		? null
		: createToolDocument([
				{
					columns: table.schema.fields.map(({ id, label }) => ({
						id,
						label
					})),
					kind: 'table',
					rows: table.rows
				}
			]);
};

export const formatTableAsDocument = (
	input: unknown
): string => {
	const document = tableAsDocument(input);
	return document === null
		? ''
		: JSON.stringify(document, null, 2);
};

export const Tool: TypeTool = {
	name: 'Table as Document',
	slug: 'document/from-table',
	intro:
		'Convert a structured numeric table into a Tool.io document table block.',
	tool: formatTableAsDocument,
	i: [
		{
			example:
				'{"kind":"tool.io/table","rows":[[1]],"schema":{"fields":[{"dataType":{"bits":64,"kind":"float"},"id":"column-1","label":"Column 1","nullable":false}]},"version":1}',
			label: 'Table',
			placeholder: 'Enter structured table JSON',
			render: 'textarea',
			type: 'text'
		}
	],
	o: [
		{
			example:
				'{\n  "blocks": [{ "kind": "table", ... }],\n  "kind": "document",\n  "version": 1\n}',
			label: 'Document',
			placeholder: 'Document will appear here...',
			render: 'textarea',
			type: 'text'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Table as Document.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools