From 4bab62eda4dde060172fada517dd85fa6233f7e4 Mon Sep 17 00:00:00 2001 From: Avi Goldman Date: Mon, 16 Oct 2017 00:16:45 -0700 Subject: [PATCH] moved logic for shorthands into render function --- packages/heml-render/src/index.js | 14 +------ packages/heml-render/src/renderElement.js | 46 ++++++++++++----------- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/packages/heml-render/src/index.js b/packages/heml-render/src/index.js index c4b977f..1e6c789 100644 --- a/packages/heml-render/src/index.js +++ b/packages/heml-render/src/index.js @@ -72,18 +72,6 @@ async function renderElements (elements, globals) { const renderedValue = await Promise.resolve(renderElement(element, attrs, contents)) - switch (renderedValue) { - case false: - $node.remove() - break - - case true: - // do nothing - break - - default: - $node.replaceWith(renderedValue.trim()) - break - } + $node.replaceWith(renderedValue.trim()) } } diff --git a/packages/heml-render/src/renderElement.js b/packages/heml-render/src/renderElement.js index e99b913..e9259e2 100644 --- a/packages/heml-render/src/renderElement.js +++ b/packages/heml-render/src/renderElement.js @@ -1,10 +1,15 @@ import stringifyAttributes from 'stringify-attributes' import isPromise from 'is-promise' -import { isString, isArray, defaults, mapValues, castArray } from 'lodash' +import { isPlainObject, defaults, mapValues, castArray, compact } from 'lodash' import selfClosingHtmlTags from 'html-tags/void' export default function (name, attrs, contents) { - if (!isString(name)) { + if (!name || (isPlainObject(name) && !name.render)) { + throw new Error(`name must be a HEML element or HTML tag name (.e.g 'td'). Received: ${JSON.stringify(name)}`) + } + + if (isPlainObject(name) && name.render) { + /** set the defaults and massage attribute values */ attrs = defaults({}, attrs, name.defaultAttrs || {}) attrs = mapValues(attrs, (value, name) => { if ((value === '' && name !== 'class') || value === 'true' || value === 'on') { return true } @@ -14,29 +19,28 @@ export default function (name, attrs, contents) { return value }) - /** custom elements can return promises, arrays, strings */ + /** + * custom elements can return promises, arrays, or strings + * + * we will: + * 1. check for the shorthands and render on that + * 2. return a string synchronously if we can + * 3. return a string in a promise + */ const renderResults = castArray(name.render(attrs, contents)) - /** we want to return synchronously if we can */ - if (!isPromise(renderResults) && renderResults.filter(isPromise).length === 0) { return renderResults.join('') } + /** 1. catch shorthands for rerendering the element */ + if (renderResults.length === 1 && renderResults[0] === true) { + return renderResults(name.tagName, attrs, contents) + } + + /** 2. we want to return synchronously if we can */ + if (renderResults.filter(isPromise).length === 0) { + return compact(renderResults).join('') + } /** otherwise, combine the array of promises/strings into a single string */ - return Promise.resolve(name.render(attrs, contents)) - .then((results) => { - if (isArray(results)) { - return Promise.all(results).then((results) => { - let str = '' - - results.forEach(function (part) { - str += part - }) - - return str - }) - } - - return results - }) + return Promise.all(renderResults).then((results) => compact(results).join('')) } /** if we have a regular ol element go ahead and convert it to a string */