Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chronic_duration.js « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1073d736b064841652295110b3deb01cc137822b (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * NOTE:
 * Changes to this file should be kept in sync with
 * https://gitlab.com/gitlab-org/gitlab-chronic-duration/-/blob/master/lib/gitlab_chronic_duration.rb.
 */

/*
 * This code is based on code from
 * https://gitlab.com/gitlab-org/gitlab-chronic-duration and is
 * distributed under the following license:
 *
 * MIT License
 *
 * Copyright (c) Henry Poydar
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

export class DurationParseError extends Error {}

// On average, there's a little over 4 weeks in month.
const FULL_WEEKS_PER_MONTH = 4;

const HOURS_PER_DAY = 24;
const DAYS_PER_MONTH = 30;

const FLOAT_MATCHER = /[0-9]*\.?[0-9]+/g;
const DURATION_UNITS_LIST = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'];

const MAPPINGS = {
  seconds: 'seconds',
  second: 'seconds',
  secs: 'seconds',
  sec: 'seconds',
  s: 'seconds',
  minutes: 'minutes',
  minute: 'minutes',
  mins: 'minutes',
  min: 'minutes',
  m: 'minutes',
  hours: 'hours',
  hour: 'hours',
  hrs: 'hours',
  hr: 'hours',
  h: 'hours',
  days: 'days',
  day: 'days',
  dy: 'days',
  d: 'days',
  weeks: 'weeks',
  week: 'weeks',
  wks: 'weeks',
  wk: 'weeks',
  w: 'weeks',
  months: 'months',
  mo: 'months',
  mos: 'months',
  month: 'months',
  years: 'years',
  year: 'years',
  yrs: 'years',
  yr: 'years',
  y: 'years',
};

const JOIN_WORDS = ['and', 'with', 'plus'];

function convertToNumber(string) {
  const f = parseFloat(string);
  return f % 1 > 0 ? f : parseInt(string, 10);
}

function durationUnitsSecondsMultiplier(unit, opts) {
  if (!DURATION_UNITS_LIST.includes(unit)) {
    return 0;
  }

  const hoursPerDay = opts.hoursPerDay || HOURS_PER_DAY;
  const daysPerMonth = opts.daysPerMonth || DAYS_PER_MONTH;
  const daysPerWeek = Math.trunc(daysPerMonth / FULL_WEEKS_PER_MONTH);

  switch (unit) {
    case 'years':
      return 31557600;
    case 'months':
      return 3600 * hoursPerDay * daysPerMonth;
    case 'weeks':
      return 3600 * hoursPerDay * daysPerWeek;
    case 'days':
      return 3600 * hoursPerDay;
    case 'hours':
      return 3600;
    case 'minutes':
      return 60;
    case 'seconds':
      return 1;
    default:
      return 0;
  }
}

function calculateFromWords(string, opts) {
  let val = 0;
  const words = string.split(' ');
  words.forEach((v, k) => {
    if (v === '') {
      return;
    }
    if (v.search(FLOAT_MATCHER) >= 0) {
      val +=
        convertToNumber(v) *
        durationUnitsSecondsMultiplier(
          words[parseInt(k, 10) + 1] || opts.defaultUnit || 'seconds',
          opts,
        );
    }
  });
  return val;
}

// Parse 3:41:59 and return 3 hours 41 minutes 59 seconds
function filterByType(string) {
  const chronoUnitsList = DURATION_UNITS_LIST.filter((v) => v !== 'weeks');
  if (
    string
      .replace(/ +/g, '')
      .search(RegExp(`${FLOAT_MATCHER.source}(:${FLOAT_MATCHER.source})+`, 'g')) >= 0
  ) {
    const res = [];
    string
      .replace(/ +/g, '')
      .split(':')
      .reverse()
      .forEach((v, k) => {
        if (!chronoUnitsList[k]) {
          return;
        }
        res.push(`${v} ${chronoUnitsList[k]}`);
      });
    return res.reverse().join(' ');
  }
  return string;
}

// Get rid of unknown words and map found
// words to defined time units
function filterThroughWhiteList(string, opts) {
  const res = [];
  string.split(' ').forEach((word) => {
    if (word === '') {
      return;
    }
    if (word.search(FLOAT_MATCHER) >= 0) {
      res.push(word.trim());
      return;
    }
    const strippedWord = word.trim().replace(/^,/g, '').replace(/,$/g, '');
    if (MAPPINGS[strippedWord] !== undefined) {
      res.push(MAPPINGS[strippedWord]);
    } else if (!JOIN_WORDS.includes(strippedWord) && opts.raiseExceptions) {
      throw new DurationParseError(
        `An invalid word ${JSON.stringify(word)} was used in the string to be parsed.`,
      );
    }
  });
  // add '1' at front if string starts with something recognizable but not with a number, like 'day' or 'minute 30sec'
  if (res.length > 0 && MAPPINGS[res[0]]) {
    res.splice(0, 0, 1);
  }
  return res.join(' ');
}

function cleanup(string, opts) {
  let res = string.toLowerCase();
  /*
   * TODO The Ruby implementation of this algorithm uses the Numerizer module,
   * which converts strings like "forty two" to "42", but there is no
   * JavaScript equivalent of Numerizer. Skip it for now until Numerizer is
   * ported to JavaScript.
   */
  res = filterByType(res);
  res = res
    .replace(FLOAT_MATCHER, (n) => ` ${n} `)
    .replace(/ +/g, ' ')
    .trim();
  return filterThroughWhiteList(res, opts);
}

function humanizeTimeUnit(number, unit, pluralize, keepZero) {
  if (number === '0' && !keepZero) {
    return null;
  }
  let res = number + unit;
  // A poor man's pluralizer
  if (number !== '1' && pluralize) {
    res += 's';
  }
  return res;
}

// Given a string representation of elapsed time,
// return an integer (or float, if fractions of a
// second are input)
export function parseChronicDuration(string, opts = {}) {
  const result = calculateFromWords(cleanup(string, opts), opts);
  return !opts.keepZero && result === 0 ? null : result;
}

// Given an integer and an optional format,
// returns a formatted string representing elapsed time
export function outputChronicDuration(seconds, opts = {}) {
  const units = {
    years: 0,
    months: 0,
    weeks: 0,
    days: 0,
    hours: 0,
    minutes: 0,
    seconds,
  };

  const hoursPerDay = opts.hoursPerDay || HOURS_PER_DAY;
  const daysPerMonth = opts.daysPerMonth || DAYS_PER_MONTH;
  const daysPerWeek = Math.trunc(daysPerMonth / FULL_WEEKS_PER_MONTH);

  const decimalPlaces =
    seconds % 1 !== 0 ? seconds.toString().split('.').reverse()[0].length : null;

  const minute = 60;
  const hour = 60 * minute;
  const day = hoursPerDay * hour;
  const month = daysPerMonth * day;
  const year = 31557600;

  if (units.seconds >= 31557600 && units.seconds % year < units.seconds % month) {
    units.years = Math.trunc(units.seconds / year);
    units.months = Math.trunc((units.seconds % year) / month);
    units.days = Math.trunc(((units.seconds % year) % month) / day);
    units.hours = Math.trunc((((units.seconds % year) % month) % day) / hour);
    units.minutes = Math.trunc(((((units.seconds % year) % month) % day) % hour) / minute);
    units.seconds = Math.trunc(((((units.seconds % year) % month) % day) % hour) % minute);
  } else if (seconds >= 60) {
    units.minutes = Math.trunc(seconds / 60);
    units.seconds %= 60;
    if (units.minutes >= 60) {
      units.hours = Math.trunc(units.minutes / 60);
      units.minutes = Math.trunc(units.minutes % 60);
      if (!opts.limitToHours) {
        if (units.hours >= hoursPerDay) {
          units.days = Math.trunc(units.hours / hoursPerDay);
          units.hours = Math.trunc(units.hours % hoursPerDay);
          if (opts.weeks) {
            if (units.days >= daysPerWeek) {
              units.weeks = Math.trunc(units.days / daysPerWeek);
              units.days = Math.trunc(units.days % daysPerWeek);
              if (units.weeks >= FULL_WEEKS_PER_MONTH) {
                units.months = Math.trunc(units.weeks / FULL_WEEKS_PER_MONTH);
                units.weeks = Math.trunc(units.weeks % FULL_WEEKS_PER_MONTH);
              }
            }
          } else if (units.days >= daysPerMonth) {
            units.months = Math.trunc(units.days / daysPerMonth);
            units.days = Math.trunc(units.days % daysPerMonth);
          }
        }
      }
    }
  }

  let joiner = opts.joiner || ' ';
  let process = null;

  let dividers;
  switch (opts.format) {
    case 'micro':
      dividers = {
        years: 'y',
        months: 'mo',
        weeks: 'w',
        days: 'd',
        hours: 'h',
        minutes: 'm',
        seconds: 's',
      };
      joiner = '';
      break;
    case 'short':
      dividers = {
        years: 'y',
        months: 'mo',
        weeks: 'w',
        days: 'd',
        hours: 'h',
        minutes: 'm',
        seconds: 's',
      };
      break;
    case 'long':
      dividers = {
        /* eslint-disable @gitlab/require-i18n-strings */
        years: ' year',
        months: ' month',
        weeks: ' week',
        days: ' day',
        hours: ' hour',
        minutes: ' minute',
        seconds: ' second',
        /* eslint-enable @gitlab/require-i18n-strings */
        pluralize: true,
      };
      break;
    case 'chrono':
      dividers = {
        years: ':',
        months: ':',
        weeks: ':',
        days: ':',
        hours: ':',
        minutes: ':',
        seconds: ':',
        keepZero: true,
      };
      process = (str) => {
        // Pad zeros
        // Get rid of lead off times if they are zero
        // Get rid of lead off zero
        // Get rid of trailing:
        const divider = ':';
        const processed = [];
        str.split(divider).forEach((n) => {
          if (n === '') {
            return;
          }
          // add zeros only if n is an integer
          if (n.search('\\.') >= 0) {
            processed.push(
              parseFloat(n)
                .toFixed(decimalPlaces)
                .padStart(3 + decimalPlaces, '0'),
            );
          } else {
            processed.push(n.padStart(2, '0'));
          }
        });
        return processed
          .join(divider)
          .replace(/^(00:)+/g, '')
          .replace(/^0/g, '')
          .replace(/:$/g, '');
      };
      joiner = '';
      break;
    default:
      dividers = {
        /* eslint-disable @gitlab/require-i18n-strings */
        years: ' yr',
        months: ' mo',
        weeks: ' wk',
        days: ' day',
        hours: ' hr',
        minutes: ' min',
        seconds: ' sec',
        /* eslint-enable @gitlab/require-i18n-strings */
        pluralize: true,
      };
      break;
  }

  let result = [];
  ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'].forEach((t) => {
    if (t === 'weeks' && !opts.weeks) {
      return;
    }
    let num = units[t];
    if (t === 'seconds' && num % 0 !== 0) {
      num = num.toFixed(decimalPlaces);
    } else {
      num = num.toString();
    }
    const keepZero = !dividers.keepZero && t === 'seconds' ? opts.keepZero : dividers.keepZero;
    const humanized = humanizeTimeUnit(num, dividers[t], dividers.pluralize, keepZero);
    if (humanized !== null) {
      result.push(humanized);
    }
  });

  if (opts.units) {
    result = result.slice(0, opts.units);
  }

  result = result.join(joiner);

  if (process) {
    result = process(result);
  }

  return result.length === 0 ? null : result;
}