Comparing version 0.16.2 to 0.17.0
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.hyperapp={})}(this,function(e){"use strict";function n(e,n){function t(e,n){return e&&r(e.tagName.toLowerCase(),{},n.call(e.childNodes,function(e){return 3===e.nodeType?e.nodeValue:t(e,n)}))}function o(e,n){for(var r in n)e[r]=n[r];return e}function i(e,n){return o(o({},e),n)}function u(e,n,r){var t={};return 0===e.length?n:(t[e[0]]=1<e.length?u(e.slice(1),n,r[e[0]]):n,i(r,t))}function f(e,n){for(var r=0;r<e.length;r++)n=n[e[r]];return n}function p(e){return"function"==typeof e}function l(e,n,r,t){for(var o in r)p(r[o])?function(r,o){n[r]=function(r){return e=f(t,k),p(r=o(r))&&p(r=r(e))&&(r=r(n)),r&&r!==e&&!r.then&&g(k=u(t,i(e,r),k)),r}}(o,r[o]):l(e[o]||(e[o]={}),n[o]={},r[o],t.concat(o))}function c(e){if(e&&e.props)return e.props.key}function s(e,n,r,t){if("key"===n);else if("style"===n)for(var o in i(t,r=r||{}))e.style[o]=null==r[o]?"":r[o];else{try{e[n]=null==r?"":r}catch(e){}p(r)||(null==r||!1===r?e.removeAttribute(n):e.setAttribute(n,r))}}function a(e,n){if("string"==typeof e)var r=document.createTextNode(e);else{var r=(n=n||"svg"===e.type)?document.createElementNS("http://www.w3.org/2000/svg",e.type):document.createElement(e.type);e.props.oncreate&&N.push(function(){e.props.oncreate(r)});for(var t=0;t<e.children.length;t++)r.appendChild(a(e.children[t],n));for(var t in e.props)s(r,t,e.props[t])}return r}function d(e,n,r){for(var t in i(n,r)){var o=r[t],u="value"===t||"checked"===t?e[t]:n[t];o!==u&&s(e,t,o,u)}r.onupdate&&N.push(function(){r.onupdate(e,n)})}function h(e,n,r){function t(){e.removeChild(n)}r&&r.onremove?r.onremove(n,t):t()}function v(e,n,r,t,o,i){if(r===t);else if(null==r)n=e.insertBefore(a(t,o),n);else if(null!=t.type&&t.type===r.type){d(n,r.props,t.props),o=o||"svg"===t.type;for(var u=t.children.length,f=r.children.length,p={},l=[],s={},y=0;y<f;y++){var g=l[y]=n.childNodes[y],m=r.children[y],w=c(m);null!=w&&(p[w]=[g,m])}for(var y=0,b=0;b<u;){var g=l[y],m=r.children[y],N=t.children[b],w=c(m);if(s[w])y++;else{var k=c(N),x=p[k]||[];null==k?(null==w&&(v(n,g,m,N,o),b++),y++):(w===k?(v(n,x[0],x[1],N,o),y++):x[0]?(n.insertBefore(x[0],g),v(n,x[0],x[1],N,o)):v(n,g,null,N,o),b++,s[k]=N)}}for(;y<f;){var m=r.children[y],w=c(m);null==w&&h(n,l[y],m.props),y++}for(var y in p){var x=p[y],A=x[1];s[A.props.key]||h(n,x[0],A.props)}}else n&&t!==n.nodeValue&&("string"==typeof t&&"string"==typeof r?n.nodeValue=t:(n=e.insertBefore(a(t,o),i=n),h(e,i,r.props)));return n}function y(r){for(m=!m,p(r=e.view(k))&&(r=r(x)),m||(w=v(n,w,b,b=r));r=N.pop();)r()}function g(){e.view&&!m&&setTimeout(y,m=!m)}var m,w=(n=n||document.body).children[0],b=t(w,[].map),N=[],k=e.state||{},x={};return g(l(k,x,e.actions,[])),x}function r(e,n){for(var r,t=[],o=[],i=arguments.length;i-- >2;)t.push(arguments[i]);for(;t.length;)if(Array.isArray(r=t.pop()))for(i=r.length;i--;)t.push(r[i]);else null==r||!0===r||!1===r||o.push("number"==typeof r?r+="":r);return"string"==typeof e?{type:e,props:n||{},children:o}:e(n||{},o)}e.app=n,e.h=r}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd||n(e.hyperapp={})}(this,function(e){"use strict";e.h=function(e,n){for(var r,t=[],o=[],i=arguments.length;i-- >2;)t.push(arguments[i]);for(;t.length;)if(Array.isArray(r=t.pop()))for(i=r.length;i--;)t.push(r[i]);else null==r||!0===r||!1===r||o.push("number"==typeof r?r+="":r);return"string"==typeof e?{tag:e,props:n||{},children:o}:e(n||{},o)},e.app=function(e,n,r){function t(e,n){return e&&{tag:e.tagName.toLowerCase(),props:{},children:n.call(e.childNodes,function(e){return 3===e.nodeType?e.nodeValue:t(e,n)})}}function o(e,n){for(var r in n)e[r]=n[r];return e}function i(e,n){return o(o({},e),n)}function u(e,n,r){var t={};return 0===e.length?n:(t[e[0]]=1<e.length?u(e.slice(1),n,r[e[0]]):n,i(r,t))}function f(e,n){for(var r=0;r<e.length;r++)n=n[e[r]];return n}function l(n,r){for(var t in n)"function"==typeof n[t]?function(t,o){n[t]=function(t){return n=f(r,e),"function"==typeof(t=o(t))&&(t=t(n)),t&&t!==n&&!t.then&&v(e=u(r,i(n,t),e)),t}}(t,n[t]):"object"==typeof n[t]&&!Array.isArray(n[t])&&l(n[t],r.concat(t))}function p(e){if(e&&e.props)return e.props.key}function c(e,n,r,t){if("key"===n);else if("style"===n)for(var o in i(t,r=r||{}))e.style[o]=null==r[o]?"":r[o];else{try{e[n]=null==r?"":r}catch(e){}"function"!=typeof r&&(null==r||!1===r?e.removeAttribute(n):e.setAttribute(n,r))}}function a(e,n){if("string"==typeof e)var r=document.createTextNode(e);else{r=(n=n||"svg"===e.tag)?document.createElementNS("http://www.w3.org/2000/svg",e.tag):document.createElement(e.tag),e.props.oncreate&&b.push(function(){e.props.oncreate(r)});for(var t=0;t<e.children.length;t++)r.appendChild(a(e.children[t],n));for(var t in e.props)c(r,t,e.props[t])}return r}function s(e,n,r){function t(){e.removeChild(n)}r&&r.onremove?r.onremove(n,t):t()}function d(e,n,r,t,o,u){if(r===t);else if(null==r)n=e.insertBefore(a(t,o),n);else if(null!=t.tag&&t.tag===r.tag){!function(e,n,r){for(var t in i(n,r)){var o=r[t],u="value"===t||"checked"===t?e[t]:n[t];o!==u&&c(e,t,o,u)}r.onupdate&&b.push(function(){r.onupdate(e,n)})}(n,r.props,t.props),o=o||"svg"===t.tag;for(var f=t.children.length,l=r.children.length,h={},v=[],g={},y=0;y<l;y++){var m=v[y]=n.childNodes[y];null!=(B=p(w=r.children[y]))&&(h[B]=[m,w])}y=0;for(var A=0;A<f;){m=v[y];var w=r.children[y],N=t.children[A];if(g[B=p(w)])y++;else{var k=p(N),x=h[k]||[];null==k?(null==B&&(d(n,m,w,N,o),A++),y++):(B===k?(d(n,x[0],x[1],N,o),y++):x[0]?(n.insertBefore(x[0],m),d(n,x[0],x[1],N,o)):d(n,m,null,N,o),A++,g[k]=N)}}for(;y<l;){var B;null==(B=p(w=r.children[y]))&&s(n,v[y],w.props),y++}for(var y in h){var C=(x=h[y])[1];g[C.props.key]||s(n,x[0],C.props)}}else n&&t!==n.nodeValue&&("string"==typeof t&&"string"==typeof r?n.nodeValue=t:(n=e.insertBefore(a(t,o),u=n),s(e,u,r.props)));return n}function h(t){for(g=!g,t=n(e),g||(y=d(r,y,m,m=t));t=b.pop();)t()}function v(){n&&!g&&setTimeout(h,g=!g)}var g,y=(r=r||document.body).children[0],m=t(y,[].map),b=[];return v(l(e,[])),e}}); | ||
//# sourceMappingURL=hyperapp.js.map |
@@ -81,5 +81,3 @@ export as namespace Hyperapp | ||
state: State | ||
) => | ||
| ((actions: Actions) => VNode<{}>) | ||
| VNode<{}> | ||
) => ((actions: Actions) => VNode<{}>) | VNode<{}> | ||
@@ -86,0 +84,0 @@ /** The props object that serves as an input to app(). |
{ | ||
"name": "hyperapp", | ||
"description": "1 KB JavaScript library for building frontend applications.", | ||
"version": "0.16.2", | ||
"version": "0.17.0", | ||
"main": "dist/hyperapp.js", | ||
@@ -26,5 +26,5 @@ "jsnext:main": "src/index.js", | ||
"bundle": "rollup -i src/index.js -o dist/hyperapp.js -m -f umd -n hyperapp", | ||
"minify": "uglifyjs dist/hyperapp.js -o dist/hyperapp.js --mangle --compress warnings=false --pure-funcs=Object.defineProperty -p relative --in-source-map dist/hyperapp.js.map --source-map dist/hyperapp.js.map", | ||
"minify": "uglifyjs dist/hyperapp.js -o dist/hyperapp.js -mc pure_funcs=Object.defineProperty --source-map includeSources,url=hyperapp.js.map", | ||
"prepare": "npm run build", | ||
"format": "prettier --semi false --write \"src/**/*.js\" \"{,test/ts/}*.{ts,tsx}\"", | ||
"format": "prettier --semi false --write {src,test}/**/*.js {,test/ts/}*.{ts,tsx}", | ||
"release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
@@ -38,7 +38,7 @@ }, | ||
"jest": "^21.2.1", | ||
"prettier": "^1.8.2", | ||
"rollup": "^0.51.2", | ||
"typescript": "^2.6.1", | ||
"uglify-js": "^2.7.5" | ||
"prettier": "^1.9.1", | ||
"rollup": "^0.52.1", | ||
"typescript": "^2.6.2", | ||
"uglify-js": "^3.2.1" | ||
} | ||
} |
172
src/index.js
@@ -1,79 +0,96 @@ | ||
export function app(props, container) { | ||
export function h(tag, props) { | ||
var node | ||
var stack = [] | ||
var children = [] | ||
for (var i = arguments.length; i-- > 2; ) { | ||
stack.push(arguments[i]) | ||
} | ||
while (stack.length) { | ||
if (Array.isArray((node = stack.pop()))) { | ||
for (var i = node.length; i--; ) { | ||
stack.push(node[i]) | ||
} | ||
} else if (null == node || true === node || false === node) { | ||
} else { | ||
children.push(typeof node === "number" ? (node += "") : node) | ||
} | ||
} | ||
return typeof tag === "string" | ||
? { | ||
tag: tag, | ||
props: props || {}, | ||
children: children | ||
} | ||
: tag(props || {}, children) | ||
} | ||
export function app(model, view, container) { | ||
var lock | ||
var root = (container = container || document.body).children[0] | ||
var node = vnode(root, [].map) | ||
var lifecycle = [] | ||
var appState = props.state || {} | ||
var appActions = {} | ||
var stack = [] | ||
repaint(init(appState, appActions, props.actions, [])) | ||
repaint(init(model, [])) | ||
return appActions | ||
return model | ||
function vnode(element, map) { | ||
return ( | ||
element && | ||
h( | ||
element.tagName.toLowerCase(), | ||
{}, | ||
map.call(element.childNodes, function(element) { | ||
return element.nodeType === 3 | ||
element && { | ||
tag: element.tagName.toLowerCase(), | ||
props: {}, | ||
children: map.call(element.childNodes, function(element) { | ||
return 3 === element.nodeType | ||
? element.nodeValue | ||
: vnode(element, map) | ||
}) | ||
) | ||
} | ||
) | ||
} | ||
function set(to, from) { | ||
for (var i in from) { | ||
to[i] = from[i] | ||
function set(target, source) { | ||
for (var i in source) { | ||
target[i] = source[i] | ||
} | ||
return to | ||
return target | ||
} | ||
function merge(to, from) { | ||
return set(set({}, to), from) | ||
function merge(target, source) { | ||
return set(set({}, target), source) | ||
} | ||
function setDeep(path, value, from) { | ||
var to = {} | ||
function setDeep(path, value, source) { | ||
var target = {} | ||
return 0 === path.length | ||
? value | ||
: ((to[path[0]] = | ||
: ((target[path[0]] = | ||
1 < path.length | ||
? setDeep(path.slice(1), value, from[path[0]]) | ||
? setDeep(path.slice(1), value, source[path[0]]) | ||
: value), | ||
merge(from, to)) | ||
merge(source, target)) | ||
} | ||
function get(path, from) { | ||
function get(path, source) { | ||
for (var i = 0; i < path.length; i++) { | ||
from = from[path[i]] | ||
source = source[path[i]] | ||
} | ||
return from | ||
return source | ||
} | ||
function isFunction(any) { | ||
return "function" === typeof any | ||
} | ||
function init(state, actions, from, path) { | ||
for (var key in from) { | ||
isFunction(from[key]) | ||
function init(source, path) { | ||
for (var key in source) { | ||
typeof source[key] === "function" | ||
? (function(key, action) { | ||
actions[key] = function(data) { | ||
state = get(path, appState) | ||
source[key] = function(data) { | ||
source = get(path, model) | ||
if ( | ||
isFunction((data = action(data))) && | ||
isFunction((data = data(state))) | ||
) { | ||
data = data(actions) | ||
if (typeof (data = action(data)) === "function") { | ||
data = data(source) | ||
} | ||
if (data && data !== state && !data.then) { | ||
repaint( | ||
(appState = setDeep(path, merge(state, data), appState)) | ||
) | ||
if (data && data !== source && !data.then) { | ||
repaint((model = setDeep(path, merge(source, data), model))) | ||
} | ||
@@ -83,9 +100,6 @@ | ||
} | ||
})(key, from[key]) | ||
: init( | ||
state[key] || (state[key] = {}), | ||
(actions[key] = {}), | ||
from[key], | ||
path.concat(key) | ||
) | ||
})(key, source[key]) | ||
: typeof source[key] === "object" && | ||
!Array.isArray(source[key]) && | ||
init(source[key], path.concat(key)) | ||
} | ||
@@ -111,3 +125,3 @@ } | ||
if (!isFunction(value)) { | ||
if (typeof value !== "function") { | ||
if (null == value || false === value) { | ||
@@ -126,8 +140,8 @@ element.removeAttribute(name) | ||
} else { | ||
var element = (isSVG = isSVG || node.type === "svg") | ||
? document.createElementNS("http://www.w3.org/2000/svg", node.type) | ||
: document.createElement(node.type) | ||
var element = (isSVG = isSVG || node.tag === "svg") | ||
? document.createElementNS("http://www.w3.org/2000/svg", node.tag) | ||
: document.createElement(node.tag) | ||
if (node.props.oncreate) { | ||
lifecycle.push(function() { | ||
stack.push(function() { | ||
node.props.oncreate(element) | ||
@@ -159,3 +173,3 @@ }) | ||
if (props.onupdate) { | ||
lifecycle.push(function() { | ||
stack.push(function() { | ||
props.onupdate(element, oldProps) | ||
@@ -182,6 +196,6 @@ }) | ||
element = parent.insertBefore(createElement(node, isSVG), element) | ||
} else if (node.type != null && node.type === oldNode.type) { | ||
} else if (node.tag != null && node.tag === oldNode.tag) { | ||
updateElement(element, oldNode.props, node.props) | ||
isSVG = isSVG || node.type === "svg" | ||
isSVG = isSVG || node.tag === "svg" | ||
@@ -276,7 +290,4 @@ var len = node.children.length | ||
lock = !lock | ||
next = view(model) | ||
if (isFunction((next = props.view(appState)))) { | ||
next = next(appActions) | ||
} | ||
if (!lock) { | ||
@@ -286,7 +297,7 @@ root = patch(container, root, node, (node = next)) | ||
while ((next = lifecycle.pop())) next() | ||
while ((next = stack.pop())) next() | ||
} | ||
function repaint() { | ||
if (props.view && !lock) { | ||
if (view && !lock) { | ||
setTimeout(render, (lock = !lock)) | ||
@@ -296,30 +307,1 @@ } | ||
} | ||
export function h(type, props) { | ||
var node | ||
var stack = [] | ||
var children = [] | ||
for (var i = arguments.length; i-- > 2; ) { | ||
stack.push(arguments[i]) | ||
} | ||
while (stack.length) { | ||
if (Array.isArray((node = stack.pop()))) { | ||
for (i = node.length; i--; ) { | ||
stack.push(node[i]) | ||
} | ||
} else if (null == node || node === true || node === false) { | ||
} else { | ||
children.push(typeof node === "number" ? (node = node + "") : node) | ||
} | ||
} | ||
return typeof type === "string" | ||
? { | ||
type: type, | ||
props: props || {}, | ||
children: children | ||
} | ||
: type(props || {}, children) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32767
368