All files / object omit.ts

100% Statements 6/6
100% Branches 4/4
100% Functions 1/1
100% Lines 6/6

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                        3x 3x 3x 7x 4x       3x    
/**
 * Copies an object without some of its properties, without `delete`, which slows down V8.
 *
 * @template T - The type of the object.
 * @template K - The keys to leave out.
 * @param object - The source object.
 * @param keys - The keys to leave out.
 * @returns A new object with the other properties.
 * @example
 * omit({ id: 1, name: 'Pump', internalRef: 'x' }, ['internalRef']); // { id: 1, name: 'Pump' }
 */
export function omit<T extends object, K extends keyof T>(object: T, keys: readonly K[]): Omit<T, K> {
  const excluded = new Set<PropertyKey>(keys);
  const result: Record<PropertyKey, unknown> = {};
  for (const key of Reflect.ownKeys(object)) {
    if (!excluded.has(key) && Object.prototype.propertyIsEnumerable.call(object, key)) {
      result[key] = Reflect.get(object, key);
    }
  }
  // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Every other own enumerable key is copied.
  return result as Omit<T, K>;
}