Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 29, 2018
1 parent 4fd3e6b commit 3ed828b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
53 changes: 30 additions & 23 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
}
};

var mapReduce = function (arr, fn) { return arr.reduce(function (result, current) { return result + fn(current); }, ""); };

var expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g;
var escapeRE = /(?:(?:&(?:amp|gt|lt|nbsp|quot);)|"|\\|\n)/g;
var escapeMap = {
Expand Down Expand Up @@ -206,21 +208,19 @@
return root;
};

var mapReduce = function (arr, fn) { return arr.reduce(function (result, current) { return result + fn(current); }, ""); };

var generateCreate = function (element) {
switch (element.type) {
case "m-fragment":
return mapReduce(element.children, generateCreate);
break;
case "m-expression":
return ("m[" + (element.index) + "] = document.createTextNode(\"\");");
return ("m[" + (element.index) + "] = m.ct(\"\");");
break;
case "m-text":
return ("m[" + (element.index) + "] = document.createTextNode(\"" + (element.content) + "\");");
return ("m[" + (element.index) + "] = m.ct(\"" + (element.content) + "\");");
break;
default:
return ((mapReduce(element.children, generateCreate)) + "m[" + (element.index) + "] = document.createElement(\"" + (element.type) + "\");");
return ((mapReduce(element.children, generateCreate)) + "m[" + (element.index) + "] = m.ce(\"" + (element.type) + "\");");
}
};

Expand All @@ -229,20 +229,7 @@

switch (element.type) {
case "m-fragment":
var children = element.children;

var loop = function ( i ) {
var child = children[i];
var childPath = "m[" + (child.index) + "]";

if (child.type !== "m-text") {
generatedMount += mapReduce(child.children, function (grandchild) { return generateMount(grandchild, childPath); });
}

generatedMount += parent + ".parentNode.insertBefore(" + childPath + ", " + parent + ");";
};

for (var i = 0; i < children.length; i++) loop( i );
return mapReduce(element.children, function (child) { return generateMount(child, parent); });
break;
default:
var elementPath = "m[" + (element.index) + "]";
Expand All @@ -251,7 +238,7 @@
generatedMount += mapReduce(element.children, function (child) { return generateMount(child, elementPath); });
}

generatedMount += parent + ".appendChild(" + elementPath + ");";
generatedMount += "m.ma(" + elementPath + ", " + parent + ");";
}

return generatedMount;
Expand All @@ -260,7 +247,7 @@
var generateUpdate = function (element) {
switch (element.type) {
case "m-expression":
return ("m[" + (element.index) + "].textContent = " + (element.content) + ";");
return ("m.ut(m[" + (element.index) + "], " + (element.content) + ");")
break;
case "m-text":
return "";
Expand All @@ -278,6 +265,26 @@
return generate(parse(input));
};

var createElement = function (type) { return document.createElement(type); };
var createTextNode = function (content) { return document.createTextNode(content); };

var mountAppendChild = function (element, parent) {
parent.appendChild(element);
};

var updateTextContent = function (element, content) {
element.textContent = content;
};

var newM = function () {
var m = [];
m.ce = createElement;
m.ct = createTextNode;
m.ma = mountAppendChild;
m.ut = updateTextContent;
return m;
};

function Moon(root, view) {
if (typeof root === "string") {
root = document.querySelector(root);
Expand All @@ -293,12 +300,12 @@
create: view[0],
mount: view[1],
update: view[2],
m: []
m: newM()
};

instance.create();
instance.mount(root);
root.parentNode.removeChild(root);
instance.update();

return instance;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ed828b

Please sign in to comment.