/** * Creates a new instance of the passed in ctor with the Enumerable as input. * Useful for custom iterables. * @example * ```typescript * class MyCustomEnumerable extends Enumerable { * public foo(): void { * console.log('hello'); * } * } * * const items = [1, 2, 3]; * * const asCustom = from(items).to(MyCustomEnumerable); // asCustom is now a MyCustomEnumerable instance. * ``` * @typeparam TSource The type of the source iterable. * @typeparam TResult The type of the returned object. * @param src The source Iterable. * @param ctor The constructor function to create the result. * @returns A new instance of the passed in ctor with the enumerable passed as input. */ export function to(src: Iterable, ctor: new (src: Iterable) => TResult): TResult { if ((ctor as unknown) === Array) { return [...src] as unknown as TResult; } return new ctor(src); }