doctable
Version:
A library to help with parsing and transforming tables of documents.
30 lines (24 loc) • 664 B
JavaScript
/**
* Rendering - IE: Data Templating.
*/
const { entries } = Object;
const { result } = require("@laufire/utils").collection;
/* Exports */
const render = (document, template) => { //Note: The template is the second param, as it makes edits easier.
//TODO: Enhancement: Compile and cache the render for each view.
const view = {};
entries(template).forEach(([key, value]) => {
const ret = (rendererForType[typeof value] || result)(document, value);
if(ret !== undefined) view[key] = ret;
});
return view;
};
/* Helpers */
const rendererForType = {
object: render,
function: (data, value) => value(data),
};
module.exports = {
result,
render,
};