mostly-dom
Version:
A virtual-dom for TypeScript
69 lines • 2.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var VNode_1 = require("./VNode");
var helpers_1 = require("../helpers");
exports.h = function () {
var tagName = arguments[0]; // required
var childrenOrText = arguments[2]; // optional
var props = {};
var children;
var text;
if (childrenOrText) {
props = arguments[1];
if (isArrayLike(childrenOrText))
children = flattenArrayLike(childrenOrText);
else if (helpers_1.isPrimitive(childrenOrText))
text = String(childrenOrText);
}
else if (arguments[1]) {
var childrenOrTextOrProps = arguments[1];
if (isArrayLike(childrenOrTextOrProps))
children = flattenArrayLike(childrenOrTextOrProps);
else if (helpers_1.isPrimitive(childrenOrTextOrProps))
text = String(childrenOrTextOrProps);
else
props = childrenOrTextOrProps;
}
if (typeof tagName === 'function') {
return tagName(props, Array.isArray(children) ? children : text ? [text] : []);
}
var isSvg = tagName === 'svg';
var vNode = isSvg
? VNode_1.MostlyVNode.createSvg(tagName, props, undefined, text)
: VNode_1.MostlyVNode.create(tagName, props, undefined, text);
if (Array.isArray(children))
vNode.children = sanitizeChildren(children, vNode);
if (isSvg)
VNode_1.addSvgNamespace(vNode);
return vNode;
};
function isArrayLike(x) {
var typeOf = typeof x;
return x && typeof x.length === 'number' && typeOf !== 'function' && typeOf !== 'string';
}
function flattenArrayLike(arrayLike, arr) {
if (arr === void 0) { arr = []; }
forEach(function (x) { return (isArrayLike(x) ? flattenArrayLike(x, arr) : arr.push(x)); }, arrayLike);
return arr;
}
function forEach(fn, list) {
for (var i = 0; i < list.length; ++i)
fn(list[i]);
}
function sanitizeChildren(childrenOrText, parent) {
childrenOrText = childrenOrText.filter(Boolean); // remove possible null values
var childCount = childrenOrText.length;
var children = Array(childCount);
for (var i = 0; i < childCount; ++i) {
var vNodeOrText = childrenOrText[i];
if (helpers_1.isString(vNodeOrText))
children[i] = VNode_1.MostlyVNode.createText(vNodeOrText);
else
children[i] = vNodeOrText;
if (parent.scope && !children[i].scope)
children[i].scope = parent.scope;
children[i].parent = parent;
}
return children;
}
//# sourceMappingURL=h.js.map
;