All files mapKeys.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 4/4
100% Lines 7/7

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                                                            1x 2x 1x   1x       2x   4x 4x          
/**
 * Maps keys of `object` and keeps the same values.
 * @param object the object to map
 * @param fn the mapping function
 * @signature
 *    R.mapKeys(object, fn)
 * @example
 *    R.mapKeys({a: 1, b: 2}, (key, value) => key + value) // => { a1: 1, b2: 2 }
 * @data_first
 * @category Object
 */
export function mapKeys<T, S>(
  object: T,
  fn: (key: keyof T, value: T[keyof T]) => any
): { [x: string]: any };
 
/**
 * Maps keys of `object` and keeps the same values.
 * @param fn the mapping function
 * @signature
 *    R.mapKeys(fn)(object)
 * @example
 *    R.pipe({a: 1, b: 2}, R.mapKeys((key, value) => key + value)) // => { a1: 1, b2: 2 }
 * @data_last
 * @category Object
 */
export function mapKeys<T, S>(
  fn: (key: keyof T, value: T[keyof T]) => any
): (object: T) => { [x: string]: any };
 
export function mapKeys(arg1: any, arg2?: any): any {
  if (arguments.length === 1) {
    return (data: any) => _mapKeys(data, arg1);
  }
  return _mapKeys(arg1, arg2);
}
 
function _mapKeys(obj: any, fn: (key: string, value: any) => any) {
  return Object.keys(obj).reduce(
    (acc, key) => {
      acc[fn(key, obj[key])] = obj[key];
      return acc;
    },
    {} as any
  );
}