pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
20 lines (15 loc) • 683 B
text/typescript
import { getValue, getValueOr } from "./main.ts";
// prepends collection with element or string with string
export function prepend<A>(el: A, coll: A[]): A[];
export function prepend(head: string, tail: string): string;
export function prepend<A>(
head: A
): <B extends A extends string ? string | string[] : A[]>(coll: B) => B extends string ? string : B;
export function prepend(el: unknown, coll?: unknown) {
if (arguments.length === 1) {
return (_coll: unknown) => prepend(el, _coll as any) as any;
}
return coll instanceof Array
? [getValue(el), ...getValueOr([], coll!)]
: (((getValueOr("", el) as string) + (getValueOr("", coll) as string)) as any);
}