Skip to content

Instantly share code, notes, and snippets.

@beaucharman beaucharman/pureSplice.js
Last active Feb 22, 2019

Embed
What would you like to do?
An ES6 / pure function implementation of the JS splice method that returns an object of the new array and the removed elements
function safeSplice(array, start, deleteCount, ...replace) {
const removed = array.splice(start, deleteCount, ...replace)
return {
array: array,
removed: removed,
}
}
/** usage
const array = [1, 2, 3, 4, 5, 6]
const { array: newArray, removed } = safeSplice(array, 2, 2, 'foo', 'bar')
console.log(newArray, removed)
*/
@filippoitaliano

This comment has been minimized.

Copy link

filippoitaliano commented Sep 15, 2017

This function is an 'inpure function', isn't it? It modifies the array passed as parameter.

@sergiubologa

This comment has been minimized.

Copy link

sergiubologa commented Feb 21, 2018

Yes, it's not really a pure function because it modifies the array.

@YuCJ

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.