pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
15 lines (11 loc) • 441 B
text/typescript
import { getValueOr } from "./main.ts";
export function reverse(str: string): string;
export function reverse<A>(coll: A[]): A[];
export function reverse(coll: any[] | string) {
const theColl = getValueOr([], coll);
const reverted: any[] = new Array(theColl.length);
for (let i = 0, j = theColl.length - 1; j >= 0; ++i, --j) {
reverted[j] = theColl[i];
}
return typeof theColl === "string" ? reverted.join("") : reverted;
}