Created
March 24, 2017 17:12
-
-
Save razdvapoka/5553a162927a78dbd9f41d698a91a17a to your computer and use it in GitHub Desktop.
slice that ignores array boundaries
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
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