Skip to content

Instantly share code, notes, and snippets.

@razdvapoka
Created March 24, 2017 17:12
Show Gist options
  • Save razdvapoka/5553a162927a78dbd9f41d698a91a17a to your computer and use it in GitHub Desktop.
Save razdvapoka/5553a162927a78dbd9f41d698a91a17a to your computer and use it in GitHub Desktop.
slice that ignores array boundaries
const slice = (array, from, to) => {
let realFrom = from < 0 ? array.length + from : from
let realTo = to > array.length ? to - array.length : to
return (realFrom <= realTo
? array.slice(realFrom, realTo)
: array.slice(realFrom, array.length).concat(array.slice(0, realTo))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment