All files / src/validators number.ts

100% Statements 31/31
100% Branches 28/28
100% Functions 14/14
100% Lines 21/21

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92          5x 5x   3x                     7x 7x   5x                     7x 7x   5x                   18x 18x   16x                   12x 12x   10x                   13x 13x   11x                   8x 8x   6x      
import { Validator } from '..';
import { isNullOrUndefined } from '../utils/is-defined';
import { isNotEqualTo } from './index';
 
export function isDecimals(decimals: number): Validator.Fn<number | null | undefined> {
  return (value: number | null | undefined): string | void => {
    if (isNullOrUndefined(value)) return;
 
    if (value.toString().length > value.toFixed(decimals).length) return `exceeds decimal count of "${decimals}"`;
  };
}
 
/**
 * Passes if the value is between or equal to the range values.
 * @param min The minimum range value
 * @param max The maximum range value
 * @returns a validator function matching the criteria
 */
export function isBetweenInclusive(min: number, max: number): Validator.Fn<number | null | undefined> {
  return (value: number | null | undefined): string | void => {
    if (isNullOrUndefined(value)) return;
 
    return isGreaterThanOrEqualTo(min)(value) ?? isLessThanOrEqualTo(max)(value);
  };
}
 
/**
 * Passes if the value is between the range values.
 * @param min The minimum range value
 * @param max The maximum range value
 * @returns a validator function matching the criteria
 */
export function isBetween(min: number, max: number): Validator.Fn<number | null | undefined> {
  return (value: number | null | undefined): string | void => {
    if (isNullOrUndefined(value)) return;
 
    return isGreaterThan(min)(value) ?? isLessThan(max)(value);
  };
}
 
/**
 * Passes if the value is greater than or equal to the minimum value.
 * @param min The minimum value
 * @returns a validator function matching the criteria
 */
export function isGreaterThanOrEqualTo(min: number): Validator.Fn<number | null | undefined> {
  return (value: number | null | undefined): string | void => {
    if (isNullOrUndefined(value)) return;
 
    if (value < min) return `is less than "${min}"`;
  };
}
 
/**
 * Passes if the value is greater than the minimum value.
 * @param min The minimum value
 * @returns a validator function matching the criteria
 */
export function isGreaterThan(min: number): Validator.Fn<number | null | undefined> {
  return (value: number | null | undefined): string | void => {
    if (isNullOrUndefined(value)) return;
 
    return isNotEqualTo(min)(value) ?? isGreaterThanOrEqualTo(min)(value);
  };
}
 
/**
 * Passes if the value is less than or equal to the maximum value.
 * @param max The maximum value
 * @returns a validator function matching the criteria
 */
export function isLessThanOrEqualTo(max: number): Validator.Fn<number | null | undefined> {
  return (value: number | null | undefined): string | void => {
    if (isNullOrUndefined(value)) return;
 
    if (value > max) return `is greater than "${max}"`;
  };
}
 
/**
 * Passes if the value is less than the maximum value.
 * @param max The maximum value
 * @returns a validator function matching the criteria
 */
export function isLessThan(max: number): Validator.Fn<number | null | undefined> {
  return (value: number | null | undefined): string | void => {
    if (isNullOrUndefined(value)) return;
 
    return isNotEqualTo(max)(value) ?? isLessThanOrEqualTo(max)(value);
  };
}