Socialify

Folder ..

Viewing date.ts
207 lines (178 loc) • 4.6 KB

  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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import type { Faker } from '.';

export class _Date {
  constructor(private readonly faker: Faker) {
    // Bind `this` so namespaced is working correctly
    for (const name of Object.getOwnPropertyNames(_Date.prototype)) {
      if (name === 'constructor' || typeof this[name] !== 'function') {
        continue;
      }
      this[name] = this[name].bind(this);
    }
  }

  /**
   * past
   *
   * @method faker.date.past
   * @param years
   * @param refDate
   */
  past(years?: number, refDate?: string): Date {
    let date = new Date();
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }

    const range = {
      min: 1000,
      max: (years || 1) * 365 * 24 * 3600 * 1000,
    };

    let past = date.getTime();
    past -= this.faker.datatype.number(range); // some time from now to N years ago, in milliseconds
    date.setTime(past);

    return date;
  }

  /**
   * future
   *
   * @method faker.date.future
   * @param years
   * @param refDate
   */
  future(years?: number, refDate?: string): Date {
    let date = new Date();
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }

    const range = {
      min: 1000,
      max: (years || 1) * 365 * 24 * 3600 * 1000,
    };

    let future = date.getTime();
    future += this.faker.datatype.number(range); // some time from now to N years later, in milliseconds
    date.setTime(future);

    return date;
  }

  /**
   * between
   *
   * @method faker.date.between
   * @param from
   * @param to
   */
  between(from: string, to: string): Date {
    const fromMilli = Date.parse(from);
    const dateOffset = this.faker.datatype.number(Date.parse(to) - fromMilli);

    const newDate = new Date(fromMilli + dateOffset);

    return newDate;
  }

  /**
   * betweens
   *
   * @method faker.date.between
   * @param from
   * @param to
   * @param num
   */
  betweens(from: string, to: string, num?: number): Date[] {
    if (typeof num == 'undefined') {
      num = 3;
    }
    const newDates: Date[] = [];
    let fromMilli = Date.parse(from);
    const dateOffset = (Date.parse(to) - fromMilli) / (num + 1);
    let lastDate: string | Date = from;
    for (let i = 0; i < num; i++) {
      // TODO @Shinigami92 2022-01-11: It may be a bug that `lastDate` is passed to parse if it's a `Date` not a `string`
      // @ts-expect-error
      fromMilli = Date.parse(lastDate);
      lastDate = new Date(fromMilli + dateOffset);
      newDates.push(lastDate);
    }
    return newDates;
  }

  /**
   * recent
   *
   * @method faker.date.recent
   * @param days
   * @param refDate
   */
  recent(days?: number, refDate?: string): Date {
    let date = new Date();
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }

    const range = {
      min: 1000,
      max: (days || 1) * 24 * 3600 * 1000,
    };

    let future = date.getTime();
    future -= this.faker.datatype.number(range); // some time from now to N days ago, in milliseconds
    date.setTime(future);

    return date;
  }

  /**
   * soon
   *
   * @method faker.date.soon
   * @param days
   * @param refDate
   */
  soon(days?: number, refDate?: string): Date {
    let date = new Date();
    if (typeof refDate !== 'undefined') {
      date = new Date(Date.parse(refDate));
    }

    const range = {
      min: 1000,
      max: (days || 1) * 24 * 3600 * 1000,
    };

    let future = date.getTime();
    future += this.faker.datatype.number(range); // some time from now to N days later, in milliseconds
    date.setTime(future);

    return date;
  }

  /**
   * month
   *
   * @method faker.date.month
   * @param options
   */
  month(options?: { abbr?: boolean; context?: boolean }): string {
    options = options || {};

    let type = 'wide';
    if (options.abbr) {
      type = 'abbr';
    }
    if (
      options.context &&
      typeof this.faker.definitions.date.month[type + '_context'] !==
        'undefined'
    ) {
      type += '_context';
    }

    const source = this.faker.definitions.date.month[type];

    return this.faker.random.arrayElement(source);
  }

  /**
   * weekday
   *
   * @method faker.date.weekday
   * @param options
   */
  weekday(options?: { abbr?: boolean; context?: boolean }): string {
    options = options || {};

    let type = 'wide';
    if (options.abbr) {
      type = 'abbr';
    }
    if (
      options.context &&
      typeof this.faker.definitions.date.weekday[type + '_context'] !==
        'undefined'
    ) {
      type += '_context';
    }

    const source = this.faker.definitions.date.weekday[type];

    return this.faker.random.arrayElement(source);
  }
}