All files / src/validators eq.ts

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

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                                          10x   3x 2x 6x 3x    
import { Validator } from "../types";
 
/**
 * Checks whether an actual value equals to expected.
 * Accepts optional comparator as a second argument, where you can define custom comparison logic.
 * By default `Object.is` is used as comparator.
 *
 * @example
 *
 * ```ts
 * import { eq } from 'compose-validators';
 *
 * const validate = eq('production');
 *
 * validate('development') // => { eq: 'production' }
 * validate('production') // => {}
 * ```
 *
 * @param expected expected value
 * @param comparator custom comparator (`Object.is` is used by default)
 */
export const eq = <T>(
  expected: T,
  comparator: (expected: T, actual: any) => boolean = Object.is
): Validator<T> => (value: T) => {
  if (comparator(expected, value)) return {};
  return { eq: expected };
};