All files pick.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 3/3
100% Lines 8/8

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 471x                                                           1x 4x       4x 2x   2x   4x 4x          
import { purry } from './purry';
 
/**
 * Creates an object composed of the picked `object` properties.
 * @param object the target object
 * @param names the properties names
 * @signature R.pick(object, [prop1, prop2])
 * @example
 *    R.pick({ a: 1, b: 2, c: 3, d: 4 }, ['a', 'd']) // => { a: 1, d: 4 }
 * @data_first
 * @category Object
 */
export function pick<T extends {}, K extends keyof T>(
  object: T,
  names: K[]
): Pick<T, K>;
 
/**
 * Creates an object composed of the picked `object` properties.
 * @param names the properties names
 * @signature R.pick([prop1, prop2])(object)
 * @example
 *    R.pipe({ a: 1, b: 2, c: 3, d: 4 }, R.pick(['a', 'd'])) // => { a: 1, d: 4 }
 * @data_last
 * @category Object
 */
export function pick<T extends {}, K extends keyof T>(
  names: K[]
): (object: T) => Pick<T, K>;
 
export function pick() {
  return purry(_pick, arguments);
}
 
function _pick(object: any, names: string[]) {
  if (object == null) {
    return {};
  }
  return names.reduce(
    (acc, name) => {
      acc[name] = object[name];
      return acc;
    },
    {} as any
  );
}