# All files / src/helpers/matchers matchObjectProp.ts

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

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

    8x
    2x

    6x

    7x

     /**
     * check that each key value pair of the objects match
     * BE CAREFUL it works only for 1 level deep key value pairs
     * won't work for nested objects
     */

    /**
     * Matches whether given object prop contains all key/value pairs.
     * @param prop - The object prop to match.
     * @param matcher - The key/value pairs to be included in the object.
     * @returns Whether the object prop contains all key/value pairs.
     */
    export function matchObjectProp<T extends Record<string, unknown>>(
      prop: T | undefined,
      matcher: T
    ): boolean {
      if (!prop || Object.keys(matcher).length === 0) {
        return false;
      }

      return (
        Object.keys(prop).length !== 0 &&
        Object.keys(matcher).every((key) => prop[key] === matcher[key])
      );
    }