1 | var isArray = require('./isArray')
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function copyWithin (array, target, start, end) {
|
13 | if (isArray(array) && array.copyWithin) {
|
14 | return array.copyWithin(target, start, end)
|
15 | }
|
16 | var replaceIndex, replaceArray
|
17 | var targetIndex = target >> 0
|
18 | var startIndex = start >> 0
|
19 | var len = array.length
|
20 | var endIndex = arguments.length > 3 ? end >> 0 : len
|
21 | if (targetIndex < len) {
|
22 | targetIndex = targetIndex >= 0 ? targetIndex : len + targetIndex
|
23 | if (targetIndex >= 0) {
|
24 | startIndex = startIndex >= 0 ? startIndex : len + startIndex
|
25 | endIndex = endIndex >= 0 ? endIndex : len + endIndex
|
26 | if (startIndex < endIndex) {
|
27 | for (replaceIndex = 0, replaceArray = array.slice(startIndex, endIndex); targetIndex < len; targetIndex++) {
|
28 | if (replaceArray.length <= replaceIndex) {
|
29 | break
|
30 | }
|
31 | array[targetIndex] = replaceArray[replaceIndex++]
|
32 | }
|
33 | }
|
34 | }
|
35 | }
|
36 | return array
|
37 | }
|
38 |
|
39 | module.exports = copyWithin
|