All files / helpers/validators timeString.js

100% Statements 38/38
100% Branches 31/31
100% Functions 2/2
100% Lines 29/29

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  10x 10x 9x 9x 9x   9x 1x   8x   7x 5x 4x   3x   1x         13x 13x 12x   12x 12x   12x 9x   6x 6x 6x 5x   4x 4x 4x   3x   1x                
function validateTimeString(timeString) {
  try {
    const components = timeString.split(':');
    const hours = parseInt(components[0]);
    const minutes = parseInt(components[1]);
    const seconds = parseInt(components[2]);
 
    if (typeof hours === 'undefined' || typeof minutes === 'undefined')
      return false;
 
    if (isNaN(hours) || isNaN(minutes) || isNaN(seconds)) return false;
 
    if (hours < 0 || hours > 23) return false;
    if (minutes < 0 || minutes > 60) return false;
    if (seconds < 0 || seconds > 60) return false;
 
    return true;
  } catch (error) {
    return false;
  }
}
 
function validateTimeSequence(startTime, endTime) {
  try {
    const startComponents = startTime.split(':');
    const endComponents = endTime.split(':');
 
    const startHour = startComponents[0];
    const endHour = endComponents[0];
 
    if (startHour < endHour) return true;
    if (startHour > endHour) return false;
 
    const startMinutes = startComponents[1];
    const endMinutes = endComponents[1];
    if (startMinutes < endMinutes) return true;
    if (startMinutes > endMinutes) return false;
 
    const startSeconds = startComponents[2];
    const endSeconds = endComponents[2];
    if (startSeconds < endSeconds) return true;
 
    return false;
  } catch (error) {
    return false;
  }
}
 
export default {
  validateTimeString,
  validateTimeSequence,
};