Skip to content

Instantly share code, notes, and snippets.

@jackboberg
Created December 16, 2017 09:04
Show Gist options
  • Save jackboberg/4e2cc3188d550ce1e2e0bf202546c7b1 to your computer and use it in GitHub Desktop.
Save jackboberg/4e2cc3188d550ce1e2e0bf202546c7b1 to your computer and use it in GitHub Desktop.
immutable array functions for ES6
// https://twitter.com/lukejacksonn/status/928244319760220160
module.exports = {
clone: x => [...x],
push: y => x => [...x, y],
pop: x => x.slice(0, -1),
unshift: y => x => [y, ...x],
shift: x => x.slice(1),
sort: f => x => [...x].sort(f),
delete: i => x => [...x.slice(0, i), ...x.slice(i + 1)],
splice: (s, c, ...y) => x => [...x.slice(0, s), ...y, ...x.slice(s + c)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment