Skip to content

Instantly share code, notes, and snippets.

@Tevinthuku
Last active August 14, 2019 16:23
Show Gist options
  • Save Tevinthuku/e1959fb0b044dda7f22db05917749c2c to your computer and use it in GitHub Desktop.
Save Tevinthuku/e1959fb0b044dda7f22db05917749c2c to your computer and use it in GitHub Desktop.
our very own createElement
const TEXT_ELEMENT = "TEXT";
/**
* @param {string} type - the node type
* @param {?object} configObject - the props
* @param {?...any} args - the children array
* @returns {object} - to be called by tevreact.render
*/
export function createElement(type, configObject, ...args) {
const props = Object.assign({}, configObject);
const hasChildren = args.length > 0;
const nodeChildren = hasChildren ? [...args] : [];
props.children = nodeChildren
.filter(Boolean)
.map(c => (c instanceof Object ? c : createTextElement(c)));
return { type, props };
}
/**
* @param {string} nodeValue - the text of the node
* @returns {object} - a call to createElement
*/
function createTextElement(nodeValue) {
return createElement(TEXT_ELEMENT, { nodeValue, children: [] });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment