@webreflection/uwire
Version:
A way to create persistent fragments
40 lines (36 loc) • 950 B
JavaScript
;
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;
};
const diffable = (node, operation) => node.nodeType === nodeType ?
((1 / operation) < 0 ?
(operation ? remove(node) : node.lastChild) :
(operation ? node.valueOf() : node.firstChild)) :
node
;
exports.diffable = diffable;
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;
}
};
};
exports.persistent = persistent;