/
index.js
46 lines (42 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
const {slice} = require('uarray');
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 {childNodes} = fragment;
const {length} = childNodes;
if (length < 2)
return length ? childNodes[0] : fragment;
const nodes = slice.call(childNodes, 0);
const firstChild = nodes[0];
const lastChild = nodes[length - 1];
return {
ELEMENT_NODE,
nodeType,
firstChild,
lastChild,
valueOf() {
if (childNodes.length !== length) {
let i = 0;
while (i < length)
fragment.appendChild(nodes[i++]);
}
return fragment;
}
};
};
exports.persistent = persistent;