All files / src objects.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 3/3
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                                        4x       35x 35x 35x 63x   44x     35x    
/**
 * Performs a key comparison between two objects, deleting from the first where
 * the keys exist in the second.
 *
 * Can be used to remove unwanted component prop values. For example:
 *
 * ```tsx
 * function Item(props) {
 *   const { children, className, ...rest } = props;
 *   return (
 *     <div
 *       {...objectKeyFilter(rest, Item.propKeys)}
 *       className={classNames('dp-item', className)}
 *     >
 *       {children}
 *     </div>
 *   );
 * }
 * ```
 */
export function objectKeyFilter<T extends Record<string, unknown>>(
  obj1: T,
  obj2: Record<string, unknown>
): Partial<T> {
  const obj2Keys = Object.keys(obj2);
  const newProps: Partial<T> = { ...obj1 };
  Object.keys(newProps)
    .filter((key) => obj2Keys.indexOf(key) !== -1)
    .forEach((key) => {
      delete (newProps as Record<string, unknown>)[key];
    });
 
  return newProps;
}