Created
April 27, 2021 19:22
-
-
Save sebinsua/6db0540b7e8f5fd75cf35eb7b39a09d8 to your computer and use it in GitHub Desktop.
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
type AtRange = number | [number, number]; | |
const at = <T>(arr: T[], ...ranges: AtRange[]): (T | undefined)[] => | |
ranges.flatMap((range) => { | |
if (typeof range === "number") { | |
return range >= 0 ? arr[range] : arr[arr.length + range - 1]; | |
} | |
return range[0] >= 0 | |
? arr.slice(range[0], range[1] + 1) | |
: arr.slice(arr.length + range[0] - 1, arr.length + range[1]); | |
}); | |
console.log(at("hello world".split(""), 1, [-3, 0], -10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment