index.js
2.69 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
import defaultLocale from '../locale/en-US/index.js';
var defaultFormat = ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
/**
* @name formatDuration
* @category Common Helpers
* @summary Formats a duration in human-readable format
*
* @description
* Return human-readable duration string i.e. "9 months 2 days"
*
* @param {Duration} duration - the duration to format
* @param {Object} [options] - an object with options.
* @param {string[]} [options.format=['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds']] - the array of units to format
* @param {boolean} [options.zero=false] - should be zeros be included in the output?
* @param {string} [options.delimiter=' '] - delimiter string
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @returns {string} the formatted date string
* @throws {TypeError} 1 argument required
*
* @example
* // Format full duration
* formatDuration({
* years: 2,
* months: 9,
* weeks: 1,
* days: 7,
* hours: 5,
* minutes: 9,
* seconds: 30
* })
* //=> '2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds
*
* @example
* // Format partial duration
* formatDuration({ months: 9, days: 2 })
* //=> '9 months 2 days'
*
* @example
* // Customize the format
* formatDuration(
* {
* years: 2,
* months: 9,
* weeks: 1,
* days: 7,
* hours: 5,
* minutes: 9,
* seconds: 30
* },
* { format: ['months', 'weeks'] }
* ) === '9 months 1 week'
*
* @example
* // Customize the zeros presence
* formatDuration({ years: 0, months: 9 })
* //=> '9 months'
* formatDuration({ years: 0, months: 9 }, null, { zero: true })
* //=> '0 years 9 months'
*
* @example
* // Customize the delimiter
* formatDuration({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' })
* //=> '2 years, 9 months, 3 weeks'
*/
export default function formatDuration(duration) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (arguments.length < 1) {
throw new TypeError("1 argument required, but only ".concat(arguments.length, " present"));
}
var format = options.format || defaultFormat;
var locale = options.locale || defaultLocale;
var zero = options.zero || false;
var delimiter = options.delimiter || ' ';
var result = format.reduce(function (acc, unit) {
var token = "x".concat(unit.replace(/(^.)/, function (m) {
return m.toUpperCase();
}));
var addChunk = typeof duration[unit] === 'number' && (zero || duration[unit]);
return addChunk ? acc.concat(locale.formatDistance(token, duration[unit])) : acc;
}, []).join(delimiter);
return result;
}