All files numberHandlers.js

100% Statements 37/37
100% Branches 14/14
100% Functions 7/7
100% Lines 30/30
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                          6x   6x   3x 3x       6x 6x   3x 3x       2x 2x 2x   1x 1x       2x 2x 2x   1x 1x       2x 2x   1x 1x       2x 2x   1x 1x       2x 2x   1x 1x    
// @ts-check
 
import is from 'is_js';
import { NAME_PLACEHOLDER, throwError } from './helpers';
 
/**
 * This function will check if the value greater than the min
 * It will throw Error if not valid, and return nothing if valid.
 * @export
 * @param {object} value
 * @param {object} schema
 */
export function minHandler(value, schema) {
  const isValid = is.above(value * 1, schema.min * 1);
 
  if (isValid) return;
 
  const errorText = `${NAME_PLACEHOLDER} should be greater than ${schema.min}. Current: ${value}`;
  throwError(value, errorText);
}
 
export function maxHandler(value, schema) {
  const isValid = is.under(value * 1, schema.max * 1);
  if (isValid) return;
 
  const errorText = `${NAME_PLACEHOLDER} should be less than ${schema.max}. Current: ${value}`;
  throwError(value, errorText);
}
 
export function equalHandler(value, schema) {
  const target = schema.equal * 1;
  const isValid = is.equal(value * 1, target);
  if (isValid) return;
 
  const errorText = `${NAME_PLACEHOLDER} should equal to ${target}.`;
  throwError(value, errorText);
}
 
export function notEqualHandler(value, schema) {
  const target = schema.notEqual * 1;
  const isValid = is.not.equal(value * 1, target);
  if (isValid) return;
 
  const errorText = `${NAME_PLACEHOLDER} should not equal to ${target}.`;
  throwError(value, errorText);
}
 
export function isPositiveHandler(value, schema) {
  const isValid = is.positive(value * 1);
  if (isValid) return;
 
  const errorText = `${NAME_PLACEHOLDER} should be positive.`;
  throwError(value, errorText);
}
 
export function isNegativeHandler(value, schema) {
  const isValid = is.negative(value * 1);
  if (isValid) return;
 
  const errorText = `${NAME_PLACEHOLDER} should be negative.`;
  throwError(value, errorText);
}
 
export function isIntHandler(value, schema) {
  const isValid = is.integer(value * 1);
  if (isValid) return;
 
  const errorText = `${NAME_PLACEHOLDER} should be integer.`;
  throwError(value, errorText);
}