All files / generator DateGenerator.ts

64.28% Statements 9/14
46.15% Branches 6/13
100% Functions 2/2
64.28% Lines 9/14

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          109x       117x     117x   11x       1x   1x   104x       104x                 104x          
import { BaseGenerator, type GeneratorOptions } from './BaseGenerator'
import type { DateOptions } from '../schema/data'
 
export default class DateGenerator extends BaseGenerator {
  constructor(opts: GeneratorOptions) {
    super(opts)
  }
 
  generate(options: Partial<DateOptions> = {}): string {
    const g = this.generator
 
    // when has the highest priority
    switch (options.when) {
    case 'recent':
      return g.date.recent({ refDate: this.refDate }).toISOString()
    case 'soon':
      return g.date.soon({ refDate: this.refDate }).toISOString()
    case 'past':
      return g.date.past({ refDate: this.refDate }).toISOString()
    case 'future':
      return g.date.future({ refDate: this.refDate }).toISOString()
    default:
      break
    }
 
    // between next
    Iif (options.between) {
      const start = new Date(options.between.start)
      const end = new Date(options.between.end)
      if (!isNaN(start.getTime()) && !isNaN(end.getTime()) && start < end) {
        return g.date.between({ from: start, to: end }).toISOString()
      }
    }
 
    // fallback to recent date
    return g.date.recent({ refDate: this.refDate }).toISOString()
 
  }
 
}