-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyCopyWithin.html
More file actions
183 lines (170 loc) · 6.61 KB
/
myCopyWithin.html
File metadata and controls
183 lines (170 loc) · 6.61 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<script src="..\my-jsArrayMethods\simpletest.js"></script>
<script>
// The copyWithin() method shallow copies part of an array
// into another location in the same array.
// Returns the modified array, without modifying its size.
// List of unfamaliar concepts to investigate later:
// TypedArray constructor.
// TypedArray.prototype.
// TypedArray.prototype.copywithin.
// ArrayBuffer object prototype.
// DataView object prototype.
// Parameters
// target
// Zero based index at which to copy the sequence to.
// If negative, target will be counted from the end.
// If target is at or greater than arr.length, nothing will be copied.
// If target is positioned after start, the copied sequence will be trimmed to fit arr.length.
// start Optional
// Zero based index at which to start copying elements from.
// If negative, start will be counted from the end.
// If start is omitted, copyWithin will copy from the start (defaults to 0).
// end Optional
// Zero based index at which to end copying elements from.
// copyWithin copies up to but not including end.
// If negative, end will be counted from the end.
// If end is omitted, copyWithin will copy until the end (default to arr.length).
// Return value
// The modified array.
Array.prototype.myCopyWithin = function(target, optionalStart, optionalEnd) {
var arr = this;
var originalArr = []; // to be used as temp storage during execution.
var startIndex = optionalStart || 0;
var endIndex = optionalEnd || arr.length;
// copy values of input array into a temp stoarge variable.
for (var i = 0; i < arr.length; i++) {
// do not copy indexes containing holes.
if (arr.hasOwnProperty(i)) {
originalArr[i] = arr[i];
}
}
if (target < 0) {
target = arr.length + target;
}
if (target > arr.length || target < 0) {
return arr;
}
// refactored to account for negative start index.
if (startIndex < 0) {
startIndex += arr.length;
}
// refactored to account for negative end index.
if (endIndex < 0) {
endIndex += arr.length;
}
// 2/22: refactored to ensure holes remain.
for (var i = startIndex; i < endIndex; i++) {
if (target < arr.length) {
// do not copy indexes that contain holes.
if (originalArr.hasOwnProperty(i)) {
arr[target] = originalArr[i];
} else {
delete arr[target];
}
target ++;
}
}
return arr;
}
tests({
'1. It should return the array unmodified if called without any arguments.': function() {
var myArray = ['an element', 'a second element'];
myArray.myCopyWithin();
eq(myArray[0], 'an element');
eq(myArray[1], 'a second element');
},
'1a. If no startIndex, it should return the array unmodified if passed target of 0.': function() {
var myArray = ['an element', 'a second element'];
myArray.myCopyWithin(0);
eq(myArray[0], 'an element');
eq(myArray[1], 'a second element');
},
'2. It should return the same array passed in, without modifying its size.': function() {
var myArray = [1, 2, 3];
var test = myArray.myCopyWithin(1);
eq(myArray.length, test.length);
eq(myArray.myCopyWithin(2, 1), myArray);
},
'3. If no optionalStart, it should copy array elements starting with index 0, and insert them beginning at arr[target].': function() {
var myArray = [1, 2, 3];
myArray.myCopyWithin(1);
eq(myArray[0], 1);
eq(myArray[1], 1);
},
'4. If no optionalEnd, it should default final copy index to array.length.': function() {
var myArray = [1, 2, 3];
myArray.myCopyWithin(1)
},
'5. If target is >= array.length, nothing should be copied.': function() {
var myArray = [1, 2];
myArray.myCopyWithin(2);
eq(myArray[0], 1);
eq(myArray[1], 2);
},
'6. If target is negative, it should insert the copied elements beginning at an index offset from end of the array.': function() {
var myArray = [1, 2, 3];
myArray.myCopyWithin(-1);
eq(myArray[2], 1);
},
'7. If calculated target (positive or negative) is > array.length, nothing should be copied.': function() {
var myArray = [1, 2];
myArray.myCopyWithin(3);
myArray.myCopyWithin(-4);
eq(myArray[0], 1);
eq(myArray[1], 2);
},
'8. If target is after startIndex, the copied sequence should be trimmed to fit array.length.': function() {
var myArray = [1, 2, 3, 4];
myArray.myCopyWithin(2, 1);
eq(myArray.length, 4);
},
'9. If optionalStart, it should use this as index to begin copying elements.': function() {
var myArray = [1, 2, 3];
myArray.myCopyWithin(0, 1);
eq(myArray[0], 2);
eq(myArray[1], 3);
eq(myArray[2], 3);
},
'10. It should work identically on bool, undefined, NaN, null data types.': function() {
var myArray = ['string', 2, true, false, NaN, undefined, null];
myArray.myCopyWithin(0, 2);
eq(myArray[0], true);
eq(myArray[1], false);
eq(isNaN(myArray[2]), true);
eq(myArray[3], undefined);
eq(myArray[4], null);
},
// refactored to preserve holes: 2/22/19
'11. It should preserve holes in the array.': function() {
var holesArray = [0, , 2];
holesArray.myCopyWithin(0, 1);
eq(0 in holesArray, false);
},
// Original Test Modified
'12. It should work generically on an object.': function() {
var weirdObject = {length: 5, 3: 1};
Array.prototype.myCopyWithin.call(weirdObject, 0, 3);
eq(weirdObject[0], 1);
eq(weirdObject[3], 1);
eq(weirdObject.length, 5);
},
'13. It should work on nested arrays.': function() {
var nestedArray = [['nested'], 'not nested', 3];
nestedArray.myCopyWithin(2, 0);
eq(Array.isArray(nestedArray[2]), true);
},
// test added: 2/21
'14. If negative start index, it should be subtracted from array.length.': function() {
var copied = [1, 2, 3, 4, 5].myCopyWithin(2, -2, 5);
eq(copied[2], 4);
eq(copied[3], 5);
eq(copied[4], 5);
},
'15. If negative end index, it should be subtracted from array.length.': function() {
var copied = [1, 2, 3, 4, 5].myCopyWithin(2, 1, -2);
eq(copied[2], 2);
eq(copied[3], 3);
eq(copied[4], 5);
}
})
</script>