-
Notifications
You must be signed in to change notification settings - Fork 0
/
splice.js
34 lines (30 loc) · 832 Bytes
/
splice.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
module.exports = (start, deleteCount, ...items) => {
if (isNaN(start)) {
throw new TypeError(`"start" parameter must be a number.`)
} else if (start < 0) {
throw new RangeError(`"start" parameter must not be negative.`)
} else if (isNaN(deleteCount)) {
throw new TypeError(`"deleteCount" parameter must be a number.`)
} else if (deleteCount < 0) {
throw new RangeError(`"deleteCount" parameter must not be negative.`)
} else {
const end = start + deleteCount
return function * (gen) {
let i = 0
for (const value of gen) {
if (i < start) {
yield value
} else if (i === start) {
yield * items
}
if (i >= end) {
yield value
}
i += 1
}
if (start >= i) {
yield * items
}
}
}
}