Skip to content

Instantly share code, notes, and snippets.

@sebinsua
Created Apr 27, 2021
Embed
What would you like to do?
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