All files / src/modules date.js

100% Statements 177/177
100% Branches 32/32
100% Functions 10/10
100% Lines 177/177

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1781x 1x 22x 22x 1x 1x 13x 13x 13x 13x 13x 3x 3x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 4x 4x 4x 4x 4x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 12x 12x 12x 12x 12x 12x 12x 1x 1x 2x 2x 2x 2x 2x 6x 6x 6x 2x 2x 2x 2x 2x 1x 1x 8x 8x 8x 8x 8x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 6x 6x 6x 6x 3x 3x 6x 6x 6x 4x 6x 2x 2x 6x 6x 6x 6x 6x 1x 1x 1x 6x 6x 6x 6x 3x 3x 6x 6x 6x 4x 6x 2x 2x 6x 6x 6x 6x 6x 1x 1x 3x 3x 3x 3x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x  
export default class pureDate {
  constructor(pure) {
    this.pure = pure;
  }
 
  past(options) {
    const def = options || {};
    const { years = 1, refDate } = def;
    let date = new Date();
 
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }
 
    const range = {
      min: 1000,
      max: years * 365 * 24 * 3600 * 1000
    };
 
    let past = date.getTime();
    // some time from now to N years ago, in milliseconds
    past -= this.pure.random.number(range);
    date.setTime(past);
 
    return date;
  }
 
  future(options) {
    const def = options || {};
    const { years = 1, refDate } = def;
    let date = new Date();
 
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }
 
    const range = {
      min: 1000,
      max: years * 365 * 24 * 3600 * 1000
    };
 
    let future = date.getTime();
    // some time from now to N years later, in milliseconds
    future += this.pure.random.number(range);
    date.setTime(future);
 
    return date;
  }
 
  between(options) {
    const def = options || {};
    const { from = this.pure.date.past({ years: 5 }), to = new Date() } = def;
    const fromMilli = Date.parse(from);
    const dateOffset = this.pure.random.number(Date.parse(to) - fromMilli);
 
    return new Date(fromMilli + dateOffset);
  }
 
  arrayBetween(options) {
    const def = options || {};
    const { from, to, num = 3 } = def;
    const results = [];
 
    for (let index = 0; index < num; index += 1) {
      const newDate = this.pure.unique.exec(this.pure.date.between, [{ from, to }]);
      results.push(newDate);
    }
 
    results.sort((a, b) => new Date(a) - new Date(b));
 
    return results;
  }
 
  recent(options) {
    const def = options || {};
    const { days = 1, refDate } = def;
    let date = new Date();
 
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }
 
    const range = {
      min: 1000,
      max: days * 24 * 3600 * 1000
    };
 
    let future = date.getTime();
    // some time from now to N days ago, in milliseconds
    future -= this.pure.random.number(range);
    date.setTime(future);
 
    return date;
  }
 
  soon(options) {
    const def = options || {};
    const { days = 1, refDate } = def;
    let date = new Date();
 
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }
 
    const range = {
      min: 1000,
      max: days * 24 * 3600 * 1000
    };
 
    let future = date.getTime();
    // some time from now to N days later, in milliseconds
    future += this.pure.random.number(range);
    date.setTime(future);
 
    return date;
  }
 
  // TODO: Review context parameter and write on docs
  month(options) {
    const def = options || {};
    let type = 'wide';
 
    if (def.abbr) {
      type = 'abbr';
    }
 
    if (
      def.context &&
      typeof this.pure.registeredModules.date.month[`${type}Context`] !== 'undefined'
    ) {
      type += 'Context';
    }
 
    const source = this.pure.registeredModules.date.month[type];
 
    return this.pure.random.arrayElement(source);
  }
 
  // TODO: Review context parameter and write on docs
  weekday(options) {
    const def = options || {};
    let type = 'wide';
 
    if (def.abbr) {
      type = 'abbr';
    }
 
    if (
      def.context &&
      typeof this.pure.registeredModules.date.weekday[`${type}Context`] !== 'undefined'
    ) {
      type += 'Context';
    }
 
    const source = this.pure.registeredModules.date.weekday[type];
 
    return this.pure.random.arrayElement(source);
  }
 
  birthDay(options) {
    const def = options || {};
    let { minAge = 18, maxAge = 60 } = def;
 
    if (minAge > maxAge) {
      minAge = maxAge;
      maxAge = minAge;
    }
 
    const actualYear = new Date().getFullYear();
    const minYear = actualYear - minAge;
    const maxYear = actualYear - maxAge;
    const minDateParsed = new Date(`${minYear}-12-31`);
    const maxDateParsed = new Date(`${maxYear}-01-01`);
 
    return this.pure.date.between({ from: minDateParsed, to: maxDateParsed });
  }
}