UNPKG

@webreflection/uwire

Version:

A way to create persistent fragments

37 lines (33 loc) 888 B
const ELEMENT_NODE = 1; const nodeType = 111; const remove = ({firstChild, lastChild}) => { const range = document.createRange(); range.setStartAfter(firstChild); range.setEndAfter(lastChild); range.deleteContents(); return firstChild; }; export const diffable = (node, operation) => node.nodeType === nodeType ? ((1 / operation) < 0 ? (operation ? remove(node) : node.lastChild) : (operation ? node.valueOf() : node.firstChild)) : node ; export const persistent = fragment => { const {firstChild, lastChild} = fragment; if (firstChild === lastChild) return lastChild || fragment; const {childNodes} = fragment; const nodes = [...childNodes]; return { ELEMENT_NODE, nodeType, firstChild, lastChild, valueOf() { if (childNodes.length !== nodes.length) fragment.append(...nodes); return fragment; } }; };