{"record":{"id":"5351e09a1508bc00","repo":"phaserjs/phaser","slug":"supplied-index-out-of-bounds","errorCode":null,"errorMessage":"Supplied index out of bounds","messagePattern":"Supplied index out of bounds","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/utils/array/MoveTo.js","lineNumber":26,"sourceCode":" * Moves an element in an array to a new position within the same array.\n * The array is modified in-place.\n *\n * @function Phaser.Utils.Array.MoveTo\n * @since 3.4.0\n *\n * @param {array} array - The array.\n * @param {*} item - The element to move.\n * @param {number} index - The new index that the element will be moved to.\n *\n * @return {*} The element that was moved.\n */\nvar MoveTo = function (array, item, index)\n{\n    var currentIndex = array.indexOf(item);\n\n    if (currentIndex === -1 || index < 0 || index >= array.length)\n    {\n        throw new Error('Supplied index out of bounds');\n    }\n\n    if (currentIndex !== index)\n    {\n        //  Remove\n        array.splice(currentIndex, 1);\n\n        //  Add in new location\n        array.splice(index, 0, item);\n    }\n\n    return item;\n};\n\nmodule.exports = MoveTo;\n","sourceCodeStart":8,"sourceCodeEnd":42,"githubUrl":"https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/utils/array/MoveTo.js#L8-L42","documentation":"Thrown by Phaser.Utils.Array.MoveTo when the item is not in the array (indexOf === -1) OR the target index is outside [0, array.length-1]. Note the message ('Supplied index out of bounds') is shared by two distinct failure modes: a missing item and an out-of-range target index. MoveTo relocates an element to an absolute index via two splices, so both the source position and the destination must be valid.","triggerScenarios":"Calling MoveTo(arr, item, index) where item is not present, or index < 0, or index >= arr.length. Edge case: calling on an empty array (length 0) makes any index out of bounds. Edge case: index === arr.length is rejected even though splice technically tolerates it, because the guard uses >=.","commonSituations":"Computing the target index from UI state that can exceed the list length after a deletion. Off-by-one when moving to the end (using arr.length instead of arr.length - 1). Calling MoveTo on an item that was just removed in the same handler. Passing a fractional or NaN index (NaN fails the < 0 check is false but NaN >= length is also false, so NaN slips through unless length is 0; the splice then behaves unexpectedly, but a non-integer index still violates intent).","solutions":["Clamp the target index: `idx = Phaser.Math.Clamp(idx, 0, arr.length - 1)` before calling.","Verify the item is present: `if (arr.indexOf(item) === -1) return;`.","For appending to the end, use arr.push after removing the item, rather than MoveTo with index === length.","Handle empty arrays explicitly before computing an index."],"exampleFix":"// before\nPhaser.Utils.Array.MoveTo(arr, item, arr.length); // arr.length is out of bounds\n\n// after\nvar idx = Phaser.Math.Clamp(targetIndex, 0, arr.length - 1);\nif (arr.indexOf(item) !== -1) {\n  Phaser.Utils.Array.MoveTo(arr, item, idx);\n}","handlingStrategy":"validation","validationCode":"function safeMoveTo(arr, item, index) {\n  if (arr.indexOf(item) === -1) return item;\n  if (arr.length === 0) return item;\n  var idx = Phaser.Math.Clamp(index, 0, arr.length - 1);\n  return Phaser.Utils.Array.MoveTo(arr, item, idx);\n}","typeGuard":"function isValidMoveTo(arr, item, index) {\n  return arr.indexOf(item) !== -1 && index >= 0 && index < arr.length;\n}","tryCatchPattern":"try {\n  Phaser.Utils.Array.MoveTo(arr, item, targetIndex);\n} catch (e) {\n  if (e.message === 'Supplied index out of bounds') {\n    var idx = Phaser.Math.Clamp(targetIndex, 0, Math.max(0, arr.length - 1));\n    if (arr.indexOf(item) !== -1) Phaser.Utils.Array.MoveTo(arr, item, idx);\n  } else { throw e; }\n}","preventionTips":["Clamp the target index to [0, length-1] before calling.","To append, remove then push instead of using index === length.","Confirm item membership with indexOf first.","Guard against empty arrays before computing any index."],"tags":["array","reorder","index","bounds","utils"],"backgroundTag":null,"analyzedSha":"41be1e462bc600064e498cba370bfa8c5c055a22","analyzedAt":"2026-08-13T04:23:39.729Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}