Skip to content

Commit e1b42a6

Browse files
More compression tricks
1 parent 59dc3ef commit e1b42a6

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<br/>
33
<img src="./assets/logo.png"/>
44
<br/>
5-
<div><b>An extremely lightweight (0.37kb), experimental, front-end library </b></div>
5+
<div><b>An extremely lightweight (0.4kb gzipped), experimental, front-end library </b></div>
66
<br/>
77
<a href="https://github.com/nguyenphuminh/reval/blob/master/LICENSE.md"><img src="https://img.shields.io/badge/license-MIT-orange.svg"/></a>
88
<a href="https://www.npmjs.com/package/revaljs"><img src="https://img.shields.io/npm/v/revaljs.svg?sanitize=true" alt="Version"></a>

examples/counter/reval.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ let c = item => item && a(item.render);
88
// Utility to get the html element of a component/element
99
let e = item => c(item) ? item.e : item;
1010

11+
// Utility to remove child el from parent el
12+
let r = (parent, child) => parent.removeChild(child);
13+
1114
// Utility to mount a component/element to another component/element
1215
let m = (target, child, before, replace) => {
1316
// Check if target is a component, we would have to use target.e
@@ -31,7 +34,7 @@ let m = (target, child, before, replace) => {
3134
c(child) ?
3235
(
3336
// Get appropriate handler and remove element from old parent if the parent exists
34-
(handler = child.m ? (child.m.removeChild(child.e), child.onremount) : child.onmount),
37+
(handler = child.m ? (r(child.m, child.e), child.onremount) : child.onmount),
3538

3639
// Re-render
3740
mount(child.e = child.render()),
@@ -56,7 +59,7 @@ let Reval = {
5659
let newTarget = e(target);
5760

5861
// Unmount based on whether child is a component or an element
59-
(c(child)) ? (newTarget.removeChild(child.e), delete child.m, a(child.onunmount) && child.onunmount()) : newTarget.removeChild(child);
62+
(c(child)) ? (r(newTarget, child.e), delete child.m, a(child.onunmount) && child.onunmount()) : r(newTarget, child);
6063
},
6164

6265
// Utility to re-render component with new state
@@ -74,15 +77,14 @@ let Reval = {
7477
let newEl = document.createElement(tag);
7578

7679
// Mount child components and child elements onto newEl
77-
(Array.isArray(body) ? body : [body]).forEach(child => m(newEl, child));
80+
[].concat(body).map(child => m(newEl, child));
7881

7982
// Set attributes or event handlers for our new element
80-
Object.entries(attr).forEach(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
83+
Object.entries(attr).map(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
8184

8285
return newEl;
8386
}
8487
}
8588

86-
if (typeof module == "object") {
87-
module.exports = Reval;
88-
}
89+
// Export for use in Node
90+
typeof module == "object" && (module.exports = Reval);

examples/shopping cart/reval.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ let c = item => item && a(item.render);
88
// Utility to get the html element of a component/element
99
let e = item => c(item) ? item.e : item;
1010

11+
// Utility to remove child el from parent el
12+
let r = (parent, child) => parent.removeChild(child);
13+
1114
// Utility to mount a component/element to another component/element
1215
let m = (target, child, before, replace) => {
1316
// Check if target is a component, we would have to use target.e
@@ -31,7 +34,7 @@ let m = (target, child, before, replace) => {
3134
c(child) ?
3235
(
3336
// Get appropriate handler and remove element from old parent if the parent exists
34-
(handler = child.m ? (child.m.removeChild(child.e), child.onremount) : child.onmount),
37+
(handler = child.m ? (r(child.m, child.e), child.onremount) : child.onmount),
3538

3639
// Re-render
3740
mount(child.e = child.render()),
@@ -56,7 +59,7 @@ let Reval = {
5659
let newTarget = e(target);
5760

5861
// Unmount based on whether child is a component or an element
59-
(c(child)) ? (newTarget.removeChild(child.e), delete child.m, a(child.onunmount) && child.onunmount()) : newTarget.removeChild(child);
62+
(c(child)) ? (r(newTarget, child.e), delete child.m, a(child.onunmount) && child.onunmount()) : r(newTarget, child);
6063
},
6164

6265
// Utility to re-render component with new state
@@ -74,15 +77,14 @@ let Reval = {
7477
let newEl = document.createElement(tag);
7578

7679
// Mount child components and child elements onto newEl
77-
(Array.isArray(body) ? body : [body]).forEach(child => m(newEl, child));
80+
[].concat(body).map(child => m(newEl, child));
7881

7982
// Set attributes or event handlers for our new element
80-
Object.entries(attr).forEach(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
83+
Object.entries(attr).map(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
8184

8285
return newEl;
8386
}
8487
}
8588

86-
if (typeof module == "object") {
87-
module.exports = Reval;
88-
}
89+
// Export for use in Node
90+
typeof module == "object" && (module.exports = Reval);

examples/todo/reval.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ let c = item => item && a(item.render);
88
// Utility to get the html element of a component/element
99
let e = item => c(item) ? item.e : item;
1010

11+
// Utility to remove child el from parent el
12+
let r = (parent, child) => parent.removeChild(child);
13+
1114
// Utility to mount a component/element to another component/element
1215
let m = (target, child, before, replace) => {
1316
// Check if target is a component, we would have to use target.e
@@ -31,7 +34,7 @@ let m = (target, child, before, replace) => {
3134
c(child) ?
3235
(
3336
// Get appropriate handler and remove element from old parent if the parent exists
34-
(handler = child.m ? (child.m.removeChild(child.e), child.onremount) : child.onmount),
37+
(handler = child.m ? (r(child.m, child.e), child.onremount) : child.onmount),
3538

3639
// Re-render
3740
mount(child.e = child.render()),
@@ -56,7 +59,7 @@ let Reval = {
5659
let newTarget = e(target);
5760

5861
// Unmount based on whether child is a component or an element
59-
(c(child)) ? (newTarget.removeChild(child.e), delete child.m, a(child.onunmount) && child.onunmount()) : newTarget.removeChild(child);
62+
(c(child)) ? (r(newTarget, child.e), delete child.m, a(child.onunmount) && child.onunmount()) : r(newTarget, child);
6063
},
6164

6265
// Utility to re-render component with new state
@@ -74,15 +77,14 @@ let Reval = {
7477
let newEl = document.createElement(tag);
7578

7679
// Mount child components and child elements onto newEl
77-
(Array.isArray(body) ? body : [body]).forEach(child => m(newEl, child));
80+
[].concat(body).map(child => m(newEl, child));
7881

7982
// Set attributes or event handlers for our new element
80-
Object.entries(attr).forEach(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
83+
Object.entries(attr).map(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
8184

8285
return newEl;
8386
}
8487
}
8588

86-
if (typeof module == "object") {
87-
module.exports = Reval;
88-
}
89+
// Export for use in Node
90+
typeof module == "object" && (module.exports = Reval);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "revaljs",
3-
"version": "0.17.0",
3+
"version": "0.18.0",
44
"description": "React-based front-end library code golf",
55
"main": "reval.min.js",
66
"scripts": {

reval.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ let c = item => item && a(item.render);
88
// Utility to get the html element of a component/element
99
let e = item => c(item) ? item.e : item;
1010

11+
// Utility to remove child el from parent el
12+
let r = (parent, child) => parent.removeChild(child);
13+
1114
// Utility to mount a component/element to another component/element
1215
let m = (target, child, before, replace) => {
1316
// Check if target is a component, we would have to use target.e
@@ -31,7 +34,7 @@ let m = (target, child, before, replace) => {
3134
c(child) ?
3235
(
3336
// Get appropriate handler and remove element from old parent if the parent exists
34-
(handler = child.m ? (child.m.removeChild(child.e), child.onremount) : child.onmount),
37+
(handler = child.m ? (r(child.m, child.e), child.onremount) : child.onmount),
3538

3639
// Re-render
3740
mount(child.e = child.render()),
@@ -56,7 +59,7 @@ let Reval = {
5659
let newTarget = e(target);
5760

5861
// Unmount based on whether child is a component or an element
59-
(c(child)) ? (newTarget.removeChild(child.e), delete child.m, a(child.onunmount) && child.onunmount()) : newTarget.removeChild(child);
62+
(c(child)) ? (r(newTarget, child.e), delete child.m, a(child.onunmount) && child.onunmount()) : r(newTarget, child);
6063
},
6164

6265
// Utility to re-render component with new state
@@ -74,15 +77,14 @@ let Reval = {
7477
let newEl = document.createElement(tag);
7578

7679
// Mount child components and child elements onto newEl
77-
(Array.isArray(body) ? body : [body]).forEach(child => m(newEl, child));
80+
[].concat(body).map(child => m(newEl, child));
7881

7982
// Set attributes or event handlers for our new element
80-
Object.entries(attr).forEach(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
83+
Object.entries(attr).map(([attrName, attrVal]) => a(attrVal) ? (newEl[attrName] = attrVal) : newEl.setAttribute(attrName, attrVal));
8184

8285
return newEl;
8386
}
8487
}
8588

86-
if (typeof module == "object") {
87-
module.exports = Reval;
88-
}
89+
// Export for use in Node
90+
typeof module == "object" && (module.exports = Reval);

reval.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)