All files / src/utils objectUtils.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 24 25 26                        7x 7x     7x 24x   16x       7x    
// src/utils/objectUtils.ts
 
/**
 * Creates a new object by omitting specified keys from a source object.
 * This is a generic utility function similar to what's found in libraries like Lodash.
 * @param obj The source object.
 * @param keys An array of keys to omit from the new object.
 * @returns A new object without the specified keys.
 */
export function omit<T extends object, K extends keyof T>(obj: T, keysToOmit: K[]): Omit<T, K> {
  // Using a Set for the keys to omit provides a faster lookup (O(1) on average)
  // compared to Array.prototype.includes() which is O(n).
  const keysToOmitSet = new Set(keysToOmit);
  const newObj = {} as Omit<T, K>;
 
  // Iterate over the object's own enumerable properties.
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key) && !keysToOmitSet.has(key as keyof T as K)) {
      // If the key is not in the omit set, add it to the new object.
      (newObj as Record<string, unknown>)[key] = obj[key];
    }
  }
 
  return newObj;
}