-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (42 loc) · 1013 Bytes
/
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
47
48
49
export default function slice(el, start, del, ...items){
let children = el.childNodes;
// if negative we start from the end
if (start < 0) start = children.length + start;
start = Math.max(0, start);
let sel = children[start];
let ret = [];
if (sel) {
// if deletion count is not specified
// and neither is insertion, all the
// elements following `start` are removed
if (!del && !items.length) {
del = children.length - start;
}
del = Math.max(0, del);
if (del) {
for (let i = 0; i < del; i++) {
let child = children[start + i];
if (child) {
el.removeChild(child);
ret.push(child);
}
}
}
if (items.length) {
sel = children[start];
if (sel) {
items.forEach(item => {
el.insertBefore(item, sel);
});
} else {
append();
}
}
} else {
append();
}
function append(){
items.forEach(el.appendChild, el);
}
return ret;
}