Skip to content

Commit

Permalink
moved logic for shorthands into render function
Browse files Browse the repository at this point in the history
  • Loading branch information
avigoldman committed Oct 16, 2017
1 parent 32c5098 commit 4bab62e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
14 changes: 1 addition & 13 deletions packages/heml-render/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
46 changes: 25 additions & 21 deletions packages/heml-render/src/renderElement.js
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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 */
Expand Down

0 comments on commit 4bab62e

Please sign in to comment.