Advertisement
Brad-Hillier

Untitled

Dec 10th, 2019
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  2. <meta content="utf-8" http-equiv="encoding">
  3. <script src = "../Tinytest/simpleTest.js"*- > </script>
  4. <script> 'use strict';
  5.  
  6. // The copyWithin() method shallow copies part of an array to another location in the same array and returns it without modifying its length.
  7.  
  8. // ----Native Syntax----
  9. // arr.copyWithin(target[, start[, end]])
  10.  
  11. // ----Parameters----
  12. // ARRAY
  13. // TARGET
  14. // the index at which to copy the sequence to
  15. // START
  16. // the index to begin copying
  17. // defaults to index 0
  18. // END
  19. // the index copied up to but not including
  20. // defaults to array.length
  21.  
  22. // ----Return Value----
  23. // the modified array
  24.  
  25. // ----Requirements----
  26. // it should return the same array, not a new one
  27.  
  28. // it should copy the sequence to target index
  29. // it should begin copying at start
  30. // it should copy upto but not including end
  31.  
  32. // it should treat target as an offset from the end of the array if it is a negative number
  33. // it should treat start as an offset from the end of the array if it is a negative number
  34. // it should treat end as an offset from the end of the array if it is a negative number
  35.  
  36. // it should round target down to the nearest integer if it has a decimal value
  37. // it should round start down to the nearest integer if it has a decimal value
  38. // it should round end down to the nearest integer if it has a decimal value
  39.  
  40. // it should round target up to the nearest integer if it has a negative decimal value
  41. // it should round start up to the nearest integer if it has a negative decimal value
  42. // it should round end up to the nearest integer if it has a negative decimal value
  43.  
  44. // it should accept objects resembling arrays
  45.  
  46. // it should not alter the length property of the object passed through
  47.  
  48. // what if length is a decimal
  49. // what if no length
  50. // what if length is negative
  51. // what if you pass in a string, a boolean, a number
  52.  
  53.  
  54. function copyWithin(array, target, start, end) {
  55. if (this) {
  56. array = this;
  57. if (typeof array === 'object') {
  58. [].unshift.call(arguments, array);
  59. }
  60. }
  61. if (typeof array !== 'object') {
  62. // convert array to object
  63. array = {}.valueOf.call(array);
  64. }
  65.  
  66. if (!start) {
  67. arguments[2] = 0;
  68. }
  69. if (!end) {
  70. arguments[3] = array.length;
  71. }
  72. for (let i = 1; i < 4; i++) {
  73. if (Number.isInteger(arguments[i]) === false) {
  74. arguments[i] = arguments[i] | 0;
  75. }
  76. if (arguments[i] < 0) {
  77. arguments[i] = array.length + arguments[i];
  78. }
  79. }
  80. target = arguments[1];
  81. start = arguments[2];
  82. end = arguments[3];
  83.  
  84. let length = array.length
  85. if (Number.isInteger(array.length) === false) {
  86. length = Math.floor(length);
  87. }
  88. let originalArray = JSON.parse(JSON.stringify(array));
  89.  
  90. for (let j = start; j < end; j++) {
  91. if (target === length) {
  92. return array;
  93. }
  94. array[target] = originalArray[j];
  95. target++;
  96. }
  97.  
  98. return array;
  99. };
  100.  
  101. tests({
  102. '1. it should return the same array, not a new one': function() {
  103. let array = ['I should be the array that gets returned']
  104. let sameArray = copyWithin(array);
  105. eq(sameArray, array);
  106. },
  107.  
  108. '2. it should copy the sequence to target index': function() {
  109. let array = [1, 2, 3]
  110. copyWithin(array, 1, 0, 2);
  111. eq(array[1], 1);
  112. },
  113. '3. it should begin copying at start': function() {
  114. let array = [1, 2, 3]
  115. copyWithin(array, 0, 1, 2);
  116. eq(array[0], 2);
  117. },
  118. '4. it should copy upto but not including end': function() {
  119. let array = [1, 2, 3]
  120. copyWithin(array, 0, 1, 2);
  121. eq(array[0], 2);
  122. eq(array[1], 2);
  123. eq(array[2], 3);
  124. },
  125.  
  126. '5. it should default start to 0': function() {
  127. let array = [1, 2, 3]
  128. copyWithin(array, 2);
  129. eq(array[2], 1);
  130. },
  131. '6. it should default end to array.length': function() {
  132. let array = [1, 2, 3]
  133. copyWithin(array, 0);
  134. eq(array[2], 3);
  135. },
  136.  
  137. '7. it should copy the elements original value if an element is overwritten before being reached': function() {
  138. let array = [1, 2, 3, 4, 5];
  139. copyWithin(array, 2);
  140. eq(array[4], 3);
  141. },
  142. '8. it should not alter the length of the array': function() {
  143. let array = [1, 2, 3];
  144. copyWithin(array, 2);
  145. eq(array.length, 3);
  146. },
  147.  
  148. '9. it should treat target as an offset from .length if it is a negative number': function() {
  149. let array = [1, 2, 3];
  150. copyWithin(array, -1);
  151. eq(array[array.length - 1], 1);
  152. },
  153. '10. it should treat start as an offset from the end of the array if it is a negative number':function() {
  154. let array = [1, 2, 3];
  155. copyWithin(array, 0, -1);
  156. eq(array[0], 3);
  157. },
  158. '11. it should treat end as an offset from the end of the array if it is a negative number': function() {
  159. let array = [1, 2, 3, 4, 5];
  160. copyWithin(array, 0, 1, -3);
  161. eq(array[0], 2);
  162. eq(array[1], 2);
  163. },
  164.  
  165. '12. it should round target down to the nearest integer if it has a decimal value': function() {
  166. let array = [1, 2, 3];
  167. copyWithin(array, 1.8);
  168. eq(array[1], 1);
  169. },
  170. '13. it should round start down to the nearest integer if it has a decimal value': function() {
  171. let array = [1, 2, 3];
  172. copyWithin(array, 0, 1.8);
  173. eq(array[0], 2);
  174. },
  175. '14. it should round end down to the nearest integer if it has a decimal value': function() {
  176. let array = [1, 2, 3, 4];
  177. copyWithin(array, 0, 1, 3.8);
  178. eq(array[1], 3);
  179. eq(array[2], 3);
  180. },
  181.  
  182. '15. it should round target up to the nearest integer if it has a negative decimal value': function() {
  183. let array = [1, 2, 3];
  184. copyWithin(array, -1.8);
  185. eq(array[2], 1);
  186. },
  187. '16. it should round start up to the nearest integer if it has a negative decimal value': function() {
  188. let array = [1, 2, 3];
  189. copyWithin(array, 0, -1.8);
  190. eq(array[0], 3);
  191. },
  192. '17. it should round end up to the nearest integer if it has a negative decimal value': function() {
  193. let array = [1, 2, 3];
  194. copyWithin(array, 0, 1, -1.8);
  195. eq(array[0], 2);
  196. eq(array[1], 2);
  197. },
  198.  
  199. '18. it should accept objects resembling arrays': function() {
  200. let obj = {0:0, 1:1, 2:2, length: 3};
  201. copyWithin(obj, 1 );
  202. eq(obj[1], 0);
  203. eq(obj[2], 1);
  204. },
  205. '19. it should treat length as the closest whole number < length, if length is a negative number': function() {
  206. let obj = {0:0, 1:1, 2:2, length: 2.8};
  207. copyWithin(obj, 0, 1);
  208. eq(JSON.stringify(obj), JSON.stringify({ 0:1, 1:1, 2:2, length: 2.8 }));
  209. },
  210. '20. it should do nothing of significance if length is 0, < 0, or NaN': function() {
  211. let obj = {0:0, 1:1, 2:2, length: 'string'};
  212. copyWithin(obj, 0, 1);
  213. eq(JSON.stringify(obj), JSON.stringify({0:0, 1:1, 2:2, length: 'string'}));
  214.  
  215. obj = {0:0, 1:1, 2:2, length: -2};
  216. copyWithin(obj, 0, 1);
  217. eq(JSON.stringify(obj), JSON.stringify({0:0, 1:1, 2:2, length: -2}));
  218.  
  219. obj = {0:0, 1:1, 2:2};
  220. copyWithin(obj, 0, 1);
  221. eq(JSON.stringify(obj), JSON.stringify({0:0, 1:1, 2:2}));
  222. },
  223. '21. it should convert array value to an object if possible': function() {
  224. eq(typeof (copyWithin.call(true)) === 'object', true);
  225. },
  226. '22. it should work with apply': function() {
  227. let obj = {0:0, 1:1, 2:2, length: 3};
  228. let arg = [1, 0, 3]
  229. copyWithin.apply(obj, arg);
  230. eq(obj[1], 0);
  231. eq(obj[2], 1);
  232. },
  233. });
  234.  
  235.  
  236.  
  237. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement