Guest User

Untitled

a guest
Jan 17th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. var arr = [ 'a', 'b', 'c', 'd', 'e'];
  2.  
  3. Array.prototype.move = function (old_index, new_index) {
  4. if (new_index >= this.length) {
  5. var k = new_index - this.length;
  6. while ((k--) + 1) {
  7. this.push(undefined);
  8. }
  9. }
  10. this.splice(new_index, 0, this.splice(old_index, 1)[0]);
  11. return this; // for testing purposes
  12. };
  13.  
  14. Array.prototype.move = function (old_index, new_index) {
  15. while (old_index < 0) {
  16. old_index += this.length;
  17. }
  18. while (new_index < 0) {
  19. new_index += this.length;
  20. }
  21. if (new_index >= this.length) {
  22. var k = new_index - this.length;
  23. while ((k--) + 1) {
  24. this.push(undefined);
  25. }
  26. }
  27. this.splice(new_index, 0, this.splice(old_index, 1)[0]);
  28. return this; // for testing purposes
  29. };
  30.  
  31. Array.prototype.move = function(from, to) {
  32. this.splice(to, 0, this.splice(from, 1)[0]);
  33. };
  34.  
  35. Array.prototype.move2 = function(pos1, pos2) {
  36. // local variables
  37. var i, tmp;
  38. // cast input parameters to integers
  39. pos1 = parseInt(pos1, 10);
  40. pos2 = parseInt(pos2, 10);
  41. // if positions are different and inside array
  42. if (pos1 !== pos2 && 0 <= pos1 && pos1 <= this.length && 0 <= pos2 && pos2 <= this.length) {
  43. // save element from position 1
  44. tmp = this[pos1];
  45. // move element down and shift other elements up
  46. if (pos1 < pos2) {
  47. for (i = pos1; i < pos2; i++) {
  48. this[i] = this[i + 1];
  49. }
  50. }
  51. // move element up and shift other elements down
  52. else {
  53. for (i = pos1; i > pos2; i--) {
  54. this[i] = this[i - 1];
  55. }
  56. }
  57. // put element from position 1 to destination
  58. this[pos2] = tmp;
  59. }
  60. }
  61.  
  62. function arraymove(arr, fromIndex, toIndex) {
  63. var element = arr[fromIndex];
  64. arr.splice(fromIndex, 1);
  65. arr.splice(toIndex, 0, element);
  66. }
  67.  
  68. Array.prototype.move = function(from,to){
  69. this.splice(to,0,this.splice(from,1)[0]);
  70. return this;
  71. };
  72.  
  73. var arr = [ 'a', 'b', 'c', 'd', 'e'];
  74. arr.move(3,1);//["a", "d", "b", "c", "e"]
  75.  
  76.  
  77. var arr = [ 'a', 'b', 'c', 'd', 'e'];
  78. arr.move(0,2);//["b", "c", "a", "d", "e"]
  79.  
  80. alert(arr.move(0,2).join(','));
  81.  
  82. function move(array, from, to) {
  83. if( to === from ) return array;
  84.  
  85. var target = array[from];
  86. var increment = to < from ? -1 : 1;
  87.  
  88. for(var k = from; k != to; k += increment){
  89. array[k] = array[k + increment];
  90. }
  91. array[to] = target;
  92. return array;
  93. }
  94.  
  95. ({}) == ({}); // false
  96.  
  97. function moveObjectAtIndex(array, sourceIndex, destIndex) {
  98. var placeholder = {};
  99. // remove the object from its initial position and
  100. // plant the placeholder object in its place to
  101. // keep the array length constant
  102. var objectToMove = array.splice(sourceIndex, 1, placeholder)[0];
  103. // place the object in the desired position
  104. array.splice(destIndex, 0, objectToMove);
  105. // take out the temporary object
  106. array.splice(array.indexOf(placeholder), 1);
  107. }
  108.  
  109. function move(array, oldIndex, newIndex) {
  110. if (newIndex >= array.length) {
  111. newIndex = array.length - 1;
  112. }
  113. array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
  114. return array;
  115. }
  116.  
  117. describe('ArrayHelper', function () {
  118. it('Move right', function () {
  119. let array = [1, 2, 3];
  120. arrayHelper.move(array, 0, 1);
  121. assert.equal(array[0], 2);
  122. assert.equal(array[1], 1);
  123. assert.equal(array[2], 3);
  124. })
  125. it('Move left', function () {
  126. let array = [1, 2, 3];
  127. arrayHelper.move(array, 1, 0);
  128. assert.equal(array[0], 2);
  129. assert.equal(array[1], 1);
  130. assert.equal(array[2], 3);
  131. });
  132. it('Move out of bounds to the left', function () {
  133. let array = [1, 2, 3];
  134. arrayHelper.move(array, 1, -2);
  135. assert.equal(array[0], 2);
  136. assert.equal(array[1], 1);
  137. assert.equal(array[2], 3);
  138. });
  139. it('Move out of bounds to the right', function () {
  140. let array = [1, 2, 3];
  141. arrayHelper.move(array, 1, 4);
  142. assert.equal(array[0], 1);
  143. assert.equal(array[1], 3);
  144. assert.equal(array[2], 2);
  145. });
  146. });
  147.  
  148. var arr = [ 'a', 'b', 'c', 'd', 'e'];
  149. var arr2 = arr.slice(0,1).concat( ['d'] ).concat( arr.slice(2,4) ).concat( arr.slice(4) );
  150.  
  151. function magicFunction (targetArray, indexFrom, indexTo) {
  152.  
  153. targetElement = targetArray[indexFrom];
  154. magicIncrement = (indexTo - indexFrom) / Math.abs (indexTo - indexFrom);
  155.  
  156. for (Element = indexFrom; Element != indexTo; Element += magicIncrement){
  157. targetArray[Element] = targetArray[Element + magicIncrement];
  158. }
  159.  
  160. targetArray[indexTo] = targetElement;
  161.  
  162. }
  163.  
  164. Array.prototype.immutableMove = function (old_index, new_index) {
  165. var copy = Object.assign([], this);
  166. if (new_index >= copy.length) {
  167. var k = new_index - copy.length;
  168. while ((k--) + 1) {
  169. copy.push(undefined);
  170. }
  171. }
  172. copy.splice(new_index, 0, copy.splice(old_index, 1)[0]);
  173. return copy;
  174. };
  175.  
  176. Array.prototype.moveUp = function (value, by) {
  177. var index = this.indexOf(value),
  178. newPos = index - (by || 1);
  179.  
  180. if (index === -1)
  181. throw new Error("Element not found in array");
  182.  
  183. if (newPos < 0)
  184. newPos = 0;
  185.  
  186. this.splice(index, 1);
  187. this.splice(newPos, 0, value);
  188. };
  189.  
  190. Array.prototype.moveDown = function (value, by) {
  191. var index = this.indexOf(value),
  192. newPos = index + (by || 1);
  193.  
  194. if (index === -1)
  195. throw new Error("Element not found in array");
  196.  
  197. if (newPos >= this.length)
  198. newPos = this.length;
  199.  
  200. this.splice(index, 1);
  201. this.splice(newPos, 0, value);
  202. };
  203.  
  204.  
  205.  
  206. var arr = ['banana', 'curyWurst', 'pc', 'remembaHaruMembaru'];
  207.  
  208. alert('withiout changes= '+arr[0]+' ||| '+arr[1]+' ||| '+arr[2]+' ||| '+arr[3]);
  209. arr.moveDown(arr[2]);
  210.  
  211.  
  212. alert('third word moved down= '+arr[0] + ' ||| ' + arr[1] + ' ||| ' + arr[2] + ' ||| ' + arr[3]);
  213. arr.moveUp(arr[2]);
  214. alert('third word moved up= '+arr[0] + ' ||| ' + arr[1] + ' ||| ' + arr[2] + ' ||| ' + arr[3]);
  215.  
  216. array.move(index, howMany, toIndex);
  217.  
  218. array = ["a", "b", "c", "d", "e", "f", "g"];
  219.  
  220. array.move(3, 2, 1); // returns ["d","e"]
  221.  
  222. array; // returns ["a", "d", "e", "b", "c", "f", "g"]
  223.  
  224. Array.prototype.move || Object.defineProperty(Array.prototype, "move", {
  225. value: function (index, howMany, toIndex) {
  226. var
  227. array = this,
  228. index = parseInt(index) || 0,
  229. index = index < 0 ? array.length + index : index,
  230. toIndex = parseInt(toIndex) || 0,
  231. toIndex = toIndex < 0 ? array.length + toIndex : toIndex,
  232. toIndex = toIndex <= index ? toIndex : toIndex <= index + howMany ? index : toIndex - howMany,
  233. moved;
  234.  
  235. array.splice.apply(array, [toIndex, 0].concat(moved = array.splice(index, howMany)));
  236.  
  237. return moved;
  238. }
  239. });
  240.  
  241. function ArrayMove(array, from, to) {
  242. if ( Math.abs(from - to) > 60) {
  243. array.splice(to, 0, array.splice(from, 1)[0]);
  244. } else {
  245. // works better when we are not moving things very far
  246. var target = array[from];
  247. var inc = (to - from) / Math.abs(to - from);
  248. var current = from;
  249. for (; current != to; current += inc) {
  250. array[current] = array[current + inc];
  251. }
  252. array[to] = target;
  253. }
  254. }
  255.  
  256. Array.prototype.move = function (old_index, new_index) {
  257. console.log(old_index + " " + new_index);
  258. while (old_index < 0) {
  259. old_index += this.length;
  260. }
  261. while (new_index < 0) {
  262. new_index += this.length;
  263. }
  264. if (new_index >= this.length) {
  265. new_index = new_index % this.length;
  266. }
  267. this.splice(new_index, 0, this.splice(old_index, 1)[0]);
  268. return this; // for testing purposes
  269. };
  270.  
  271. const moveItemInArrayFromIndexToIndex = (array, fromIndex, toIndex) => {
  272. const newArray = [...array];
  273.  
  274. if (fromIndex === toIndex) return newArray;
  275.  
  276. const target = newArray[fromIndex];
  277. const inc = toIndex < fromIndex ? -1 : 1;
  278.  
  279. for (let i = fromIndex; i !== toIndex; i += inc) {
  280. newArray[i] = newArray[i + inc];
  281. }
  282.  
  283. newArray[toIndex] = target;
  284.  
  285. return newArray;
  286. };
Add Comment
Please, Sign In to add comment