All files tuple.ts

90% Statements 54/60
100% Branches 6/6
50% Functions 2/4
90% Lines 54/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 611x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x       1x 1x 1x 1x 1x 1x  
/**
 * Copyright (c) OpenLens Authors. All rights reserved.
 * Licensed under MIT License. See LICENSE in root directory for more information.
 */
 
import { array } from "./array";
 
/**
 * A strict N-tuple of type T
 */
export type Tuple<T, N extends number> = N extends N
  ? number extends N
    ? T[]
    : TupleOfImpl<T, N, []>
  : never;
type TupleOfImpl<T, N extends number, R extends unknown[]> = R["length"] extends N
  ? R
  : TupleOfImpl<T, N, [T, ...R]>;
 
/**
 * Iterates over `sources` yielding full tuples until one of the tuple arrays
 * is empty. Then it returns a tuple with the rest of each of tuples
 * @param sources The source arrays
 * @yields A tuple of the next element from each of the sources
 * @returns The tuple of all the sources as soon as at least one of the sources is exausted
 */
function zip<T>(...sources: Tuple<T[], 0>): Iterator<Tuple<T, 0>, Tuple<T[], 0>>;
function zip<T>(...sources: Tuple<T[], 1>): Iterator<Tuple<T, 1>, Tuple<T[], 1>>;
function zip<T>(...sources: Tuple<T[], 2>): Iterator<Tuple<T, 2>, Tuple<T[], 2>>;
function* zip<T, N extends number>(...sources: Tuple<T[], N>): Iterator<Tuple<T, N>, Tuple<T[], N>> {
  const maxSafeLength = Math.min(...sources.map(source => source.length));
 
  for (let i = 0; i < maxSafeLength; i += 1) {
    yield sources.map(source => source[i]) as Tuple<T, N>;
  }
 
  return sources.map(source => source.slice(maxSafeLength)) as Tuple<T[], N>;
}
 
/**
 * Returns a `length` tuple filled with copies of `value`
 * @param length The size of the tuple
 * @param value The value for each of the tuple entries
 */
function filled<T, L extends number>(length: L, value: T): Tuple<T, L> {
  return array.filled(length, value) as Tuple<T, L>;
}
 
/**
 * A function for converting an explicit array to a tuple but without the `readonly` typing
 */
function from<T extends any[]>(...args: T): [...T] {
  return args;
}
 
export const tuple = {
  zip,
  filled,
  from,
};