/** * Copyright 2022 Gravwell, Inc. All rights reserved. * * Contact: [legal@gravwell.io](mailto:legal@gravwell.io) * * This software may be modified and distributed under the terms of the MIT * license. See the LICENSE file for details. */ export const lazyLast = (getLast: () => Last) => ) => any>(f: F) => (...args: DropLast>): ReturnType => f(...args, getLast()); export type DropLast> = TupleLength> extends 0 ? [] : TupleLength> extends 1 ? [] : TupleLength> extends 2 ? [Args[0]] : TupleLength> extends 3 ? [Args[0], Args[1]] : TupleLength> extends 4 ? [Args[0], Args[1], Args[2]] : TupleLength> extends 5 ? [Args[0], Args[1], Args[2], Args[3]] : Array; type TupleLength> = Tuple extends { length: infer L } ? L : never; // *NOTE: The code below is useful but not used. Also, it doesn't work with ts<3.8, so I'm commenting it out to avoid compilation issues // type Prev = [0, 0, 1, 2, 3, 4, 5][Index]; // export type GetLast> = Args[Prev>>];