All files / src/utils bind.ts

100% Statements 9/9
75% Branches 3/4
100% Functions 3/3
100% Lines 9/9

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      1088x       1088x     2608x         1088x     41x       2391x     2391x 110x     2391x                      
type Function = (...args: any[]) => any;
 
export function bind<T extends bind.BindMap, V>(object: T, thisBind: V): bind.Bound<T, V> {
  const output: T = {
    ...object,
  };
 
  for (const key in output) {
    // Typescript is acting really weird here and not typeguarding this correctly.
    // Probably worth looking into, but this enables nested objects to retain their type info.
    output[key] = (
      typeof output[key] === 'function' ? bind.fn(output[key], thisBind) : bind(output[key], thisBind)
    ) as (typeof output)[typeof key];
  }
 
  return output as bind.Bound<T, V>;
}
 
export namespace bind {
  // This is getting marked as untested for some reason. Just ignore it.
  /* node:coverage ignore next */
  export function fn<F extends Function, V>(fn: F, thisBind: V): F {
    const output = fn.bind(thisBind);
 
    // Forward any namespace properties as well
    for (const key in fn) {
      output[key] = fn[key];
    }
 
    return output;
  }
 
  export type Bound<T, V> = {
    [key in keyof T]: T[key] extends Function ? T[key] : Bound<T[key], V>;
  } & V;
 
  export type BindMap = {
    [key: string]: Function | BindMap;
  };
}