diff --git a/deno.json b/deno.json index 90fca06..75c8180 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@n2geoff/um", - "version": "0.4.1", + "version": "0.5.0", "exports": "./index.js", "tasks": { "dev": "deno run --watch index.js", diff --git a/dist/um.js b/dist/um.js index fe24b87..55b101a 100644 --- a/dist/um.js +++ b/dist/um.js @@ -1,32 +1,128 @@ -/** - * HTML Tag Scripting Function - * - * Generates new DOM element(s) from a tag, attributes - * - * @param {String} tag - tag name - * @param {Object|String|Array} args - attributes, text or array of child elements - * - * @returns {HTMLElement} The created DOM element(s) - */ -function h(tag, ...args) { - const el = document.createElement(tag); - - // support all scalar values as TextNodes - const isScalar = (value) => ["boolean", "string", "number"].includes(typeof value); - - args.forEach((arg) => { - if (isScalar(arg)) { - el.appendChild(document.createTextNode(arg)); - } else if (Array.isArray(arg)) { - el.append(...arg); - } else { - Object.assign(el, arg); +/*! Emerj v1.0.0 | MIT LICENSE | https://github.com/bryhoyt/emerj */ +var diff = { + attrs(elem) { + const attrs = {}; + for (let i=0; i < elem.attributes.length; i++) { + const attr = elem.attributes[i]; + attrs[attr.name] = attr.value; } - }); + return attrs; + }, + nodesByKey(parent, makeKey) { + const map = {}; + for (let j=0; j < parent.childNodes.length; j++) { + const key = makeKey(parent.childNodes[j]); + if (key) map[key] = parent.childNodes[j]; + } + return map; + }, + merge(base, modified, opts) { + /* Merge any differences between base and modified back into base. + * + * Operates only the children nodes, and does not change the root node or its + * attributes. + * + * Conceptually similar to React's reconciliation algorithm: + * https://facebook.github.io/react/docs/reconciliation.html + * + * I haven't thoroughly tested performance to compare to naive DOM updates (i.e. + * just updating the entire DOM from a string using .innerHTML), but some quick + * tests on a basic DOMs were twice as fast -- so at least it's not slower in + * a simple scenario -- and it's definitely "fast enough" for responsive UI and + * even smooth animation. + * + * The real advantage for me is not so much performance, but that state & identity + * of existing elements is preserved -- text typed into an , an open + *