Last active
April 2, 2022 02:36
-
-
Save YuCJ/0a42afc1b578b2545195a7b688dcbab6 to your computer and use it in GitHub Desktop.
A pure version of Array.prototype.splice(). It will return a new array rather than mutate the array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A pure version of Array.prototype.splice | |
* It will return a new array rather than mutate the array | |
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice | |
* @param {Array} array The target array | |
* @param {number} start Index at which to start changing the array | |
* @param {number} deleteCount An integer indicating the number of old array elements to remove | |
* @param {any} items The elements to add to the array, beginning at the start index | |
* @returns {Array} | |
*/ | |
function pureSplice(array, start = 0, deleteCount = 0, ...items) { | |
const arrayLength = array.length | |
const _deleteCount = (deleteCount < 0) ? 0 : deleteCount | |
let _start | |
if (start < 0) { | |
if (Math.abs(start) > arrayLength) { | |
_start = 0 | |
} else { | |
_start = arrayLength + start | |
} | |
} else if (start > arrayLength) { | |
_start = arrayLength | |
} else { | |
_start = start | |
} | |
return [ | |
...array.slice(0, _start), | |
...items, | |
...array.slice((_start + _deleteCount), arrayLength), | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for creating this!