UNPKG

@platform/cell.typesystem

Version:

The 'strongly typed sheets' system of the CellOS.

40 lines (39 loc) 1.53 kB
import { coord } from '../common'; import { TypeTarget } from '../TypeSystem.core/TypeTarget'; export function objectToCells(input) { var _a; const types = Array.isArray(input) ? input : ((_a = input) === null || _a === void 0 ? void 0 : _a.columns) || []; const api = { row(index, data) { const cells = {}; Object.keys(data) .map(key => { const value = data[key]; const type = types.find(type => type.prop === key); return { key, value, type }; }) .filter(({ type }) => Boolean(type)) .forEach(({ value, type }) => { const target = TypeTarget.parse(type.target); const cell = {}; if (target.path === 'value') { cell.value = value; } if (target.path.startsWith('props:')) { const path = target.path.substring('props:'.length); cell.props = cell.props || {}; cell.props[path] = value; } const pos = coord.cell.toCell(`${type.column}${index + 1}`); cells[pos.key] = cell; }); return cells; }, rows(index, items) { let cells = {}; (items || []).forEach((item, i) => (cells = Object.assign(Object.assign({}, cells), api.row(index + i, item)))); return cells; }, }; return api; }