diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/html.js | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/util/html.js b/src/util/html.js index 338df71b..bdb385b5 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -55,7 +55,7 @@ export function tag(tagName, ...args) { } if (attrs) { - const attrString = attributes(args[0]); + const attrString = attributes(attrs); if (attrString) { openTag = `${tagName} ${attrString}`; } @@ -121,3 +121,19 @@ export function attributes(attribs) { ) .join(' '); } + +// Ensures the passed value is an array of elements, for usage in [...spread] +// syntax. This may be used when it's not guaranteed whether the return value of +// an external function is one child or an array, or in combination with +// conditionals, e.g. fragment(cond && [x, y, z]). +export function fragment(childOrChildren) { + if (!childOrChildren) { + return []; + } + + if (Array.isArray(childOrChildren)) { + return childOrChildren; + } + + return [childOrChildren]; +} |