All files agegate.js

95.23% Statements 20/21
84.61% Branches 11/13
100% Functions 1/1
95.23% Lines 20/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    5x 10x 10x 10x   10x 10x 10x         10x 10x 10x   5x 5x 5x   5x 5x 5x 5x 5x   5x   10x    
import { z } from 'zod';
 
export const ageGate = (ageGateType, ageGateValue, legalAge = null) => {
  const ageGateTypeValidation = z.enum([ 'yesno', 'dob' ]);
  const ageDateValidation = z.coerce.date();
  legalAge = legalAge || 21;
 
  let _type = null;
  try {
    _type = ageGateTypeValidation.parse(ageGateType.toLowerCase());
  } catch {
    return {};
  }
 
  let _passed = false;
  let { success, data: userDOB } = ageDateValidation.safeParse(ageGateValue);
  switch (_type) {
    case 'yesno':
      _passed = !!ageGateValue;
      userDOB = null;
      break;
    case 'dob':
      Eif (success) {
        const today = new Date();
        const legalAsOf = new Date(userDOB.getFullYear() + legalAge, userDOB.getMonth(), userDOB.getDate());
        const realisticDoB = userDOB.getFullYear() > 1900;
        _passed = !legalAsOf ? false : (realisticDoB && legalAsOf <= today);
      }
      break;
  }
  return { ageGateType: _type, ageGatePassed: _passed, ageGateDOB: userDOB || null };
};