Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
- <meta content="utf-8" http-equiv="encoding">
- <script src = "../Tinytest/simpleTest.js"*- > </script>
- <script> 'use strict';
- // The copyWithin() method shallow copies part of an array to another location in the same array and returns it without modifying its length.
- // ----Native Syntax----
- // arr.copyWithin(target[, start[, end]])
- // ----Parameters----
- // ARRAY
- // TARGET
- // the index at which to copy the sequence to
- // START
- // the index to begin copying
- // defaults to index 0
- // END
- // the index copied up to but not including
- // defaults to array.length
- // ----Return Value----
- // the modified array
- // ----Requirements----
- // it should return the same array, not a new one
- // it should copy the sequence to target index
- // it should begin copying at start
- // it should copy upto but not including end
- // it should treat target as an offset from the end of the array if it is a negative number
- // it should treat start as an offset from the end of the array if it is a negative number
- // it should treat end as an offset from the end of the array if it is a negative number
- // it should round target down to the nearest integer if it has a decimal value
- // it should round start down to the nearest integer if it has a decimal value
- // it should round end down to the nearest integer if it has a decimal value
- // it should round target up to the nearest integer if it has a negative decimal value
- // it should round start up to the nearest integer if it has a negative decimal value
- // it should round end up to the nearest integer if it has a negative decimal value
- // it should accept objects resembling arrays
- // it should not alter the length property of the object passed through
- // what if length is a decimal
- // what if no length
- // what if length is negative
- // what if you pass in a string, a boolean, a number
- function copyWithin(array, target, start, end) {
- if (this) {
- array = this;
- if (typeof array === 'object') {
- [].unshift.call(arguments, array);
- }
- }
- if (typeof array !== 'object') {
- // convert array to object
- array = {}.valueOf.call(array);
- }
- if (!start) {
- arguments[2] = 0;
- }
- if (!end) {
- arguments[3] = array.length;
- }
- for (let i = 1; i < 4; i++) {
- if (Number.isInteger(arguments[i]) === false) {
- arguments[i] = arguments[i] | 0;
- }
- if (arguments[i] < 0) {
- arguments[i] = array.length + arguments[i];
- }
- }
- target = arguments[1];
- start = arguments[2];
- end = arguments[3];
- let length = array.length
- if (Number.isInteger(array.length) === false) {
- length = Math.floor(length);
- }
- let originalArray = JSON.parse(JSON.stringify(array));
- for (let j = start; j < end; j++) {
- if (target === length) {
- return array;
- }
- array[target] = originalArray[j];
- target++;
- }
- return array;
- };
- tests({
- '1. it should return the same array, not a new one': function() {
- let array = ['I should be the array that gets returned']
- let sameArray = copyWithin(array);
- eq(sameArray, array);
- },
- '2. it should copy the sequence to target index': function() {
- let array = [1, 2, 3]
- copyWithin(array, 1, 0, 2);
- eq(array[1], 1);
- },
- '3. it should begin copying at start': function() {
- let array = [1, 2, 3]
- copyWithin(array, 0, 1, 2);
- eq(array[0], 2);
- },
- '4. it should copy upto but not including end': function() {
- let array = [1, 2, 3]
- copyWithin(array, 0, 1, 2);
- eq(array[0], 2);
- eq(array[1], 2);
- eq(array[2], 3);
- },
- '5. it should default start to 0': function() {
- let array = [1, 2, 3]
- copyWithin(array, 2);
- eq(array[2], 1);
- },
- '6. it should default end to array.length': function() {
- let array = [1, 2, 3]
- copyWithin(array, 0);
- eq(array[2], 3);
- },
- '7. it should copy the elements original value if an element is overwritten before being reached': function() {
- let array = [1, 2, 3, 4, 5];
- copyWithin(array, 2);
- eq(array[4], 3);
- },
- '8. it should not alter the length of the array': function() {
- let array = [1, 2, 3];
- copyWithin(array, 2);
- eq(array.length, 3);
- },
- '9. it should treat target as an offset from .length if it is a negative number': function() {
- let array = [1, 2, 3];
- copyWithin(array, -1);
- eq(array[array.length - 1], 1);
- },
- '10. it should treat start as an offset from the end of the array if it is a negative number':function() {
- let array = [1, 2, 3];
- copyWithin(array, 0, -1);
- eq(array[0], 3);
- },
- '11. it should treat end as an offset from the end of the array if it is a negative number': function() {
- let array = [1, 2, 3, 4, 5];
- copyWithin(array, 0, 1, -3);
- eq(array[0], 2);
- eq(array[1], 2);
- },
- '12. it should round target down to the nearest integer if it has a decimal value': function() {
- let array = [1, 2, 3];
- copyWithin(array, 1.8);
- eq(array[1], 1);
- },
- '13. it should round start down to the nearest integer if it has a decimal value': function() {
- let array = [1, 2, 3];
- copyWithin(array, 0, 1.8);
- eq(array[0], 2);
- },
- '14. it should round end down to the nearest integer if it has a decimal value': function() {
- let array = [1, 2, 3, 4];
- copyWithin(array, 0, 1, 3.8);
- eq(array[1], 3);
- eq(array[2], 3);
- },
- '15. it should round target up to the nearest integer if it has a negative decimal value': function() {
- let array = [1, 2, 3];
- copyWithin(array, -1.8);
- eq(array[2], 1);
- },
- '16. it should round start up to the nearest integer if it has a negative decimal value': function() {
- let array = [1, 2, 3];
- copyWithin(array, 0, -1.8);
- eq(array[0], 3);
- },
- '17. it should round end up to the nearest integer if it has a negative decimal value': function() {
- let array = [1, 2, 3];
- copyWithin(array, 0, 1, -1.8);
- eq(array[0], 2);
- eq(array[1], 2);
- },
- '18. it should accept objects resembling arrays': function() {
- let obj = {0:0, 1:1, 2:2, length: 3};
- copyWithin(obj, 1 );
- eq(obj[1], 0);
- eq(obj[2], 1);
- },
- '19. it should treat length as the closest whole number < length, if length is a negative number': function() {
- let obj = {0:0, 1:1, 2:2, length: 2.8};
- copyWithin(obj, 0, 1);
- eq(JSON.stringify(obj), JSON.stringify({ 0:1, 1:1, 2:2, length: 2.8 }));
- },
- '20. it should do nothing of significance if length is 0, < 0, or NaN': function() {
- let obj = {0:0, 1:1, 2:2, length: 'string'};
- copyWithin(obj, 0, 1);
- eq(JSON.stringify(obj), JSON.stringify({0:0, 1:1, 2:2, length: 'string'}));
- obj = {0:0, 1:1, 2:2, length: -2};
- copyWithin(obj, 0, 1);
- eq(JSON.stringify(obj), JSON.stringify({0:0, 1:1, 2:2, length: -2}));
- obj = {0:0, 1:1, 2:2};
- copyWithin(obj, 0, 1);
- eq(JSON.stringify(obj), JSON.stringify({0:0, 1:1, 2:2}));
- },
- '21. it should convert array value to an object if possible': function() {
- eq(typeof (copyWithin.call(true)) === 'object', true);
- },
- '22. it should work with apply': function() {
- let obj = {0:0, 1:1, 2:2, length: 3};
- let arg = [1, 0, 3]
- copyWithin.apply(obj, arg);
- eq(obj[1], 0);
- eq(obj[2], 1);
- },
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement