-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatDuration_index.js.html
151 lines (127 loc) · 15.8 KB
/
formatDuration_index.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: formatDuration/index.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: formatDuration/index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import defaultLocale from '../locale/en-US/index'
const 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, options = {}) {
if (arguments.length < 1) {
throw new TypeError(
`1 argument required, but only ${arguments.length} present`
)
}
const format = options.format || defaultFormat
const locale = options.locale || defaultLocale
const zero = options.zero || false
const delimiter = options.delimiter || ' '
const result = format
.reduce((acc, unit) => {
const token = `x${unit.replace(/(^.)/, m => m.toUpperCase())}`
const addChunk =
typeof duration[unit] === 'number' && (zero || duration[unit])
return addChunk
? acc.concat(locale.formatDistance(token, duration[unit]))
: acc
}, [])
.join(delimiter)
return result
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#add">add</a></li><li><a href="global.html#addBusinessDays">addBusinessDays</a></li><li><a href="global.html#addDays">addDays</a></li><li><a href="global.html#addHours">addHours</a></li><li><a href="global.html#addISOWeekYears">addISOWeekYears</a></li><li><a href="global.html#addMilliseconds">addMilliseconds</a></li><li><a href="global.html#addMinutes">addMinutes</a></li><li><a href="global.html#addMonths">addMonths</a></li><li><a href="global.html#addQuarters">addQuarters</a></li><li><a href="global.html#addSeconds">addSeconds</a></li><li><a href="global.html#addWeeks">addWeeks</a></li><li><a href="global.html#addYears">addYears</a></li><li><a href="global.html#areIntervalsOverlapping">areIntervalsOverlapping</a></li><li><a href="global.html#closestIndexTo">closestIndexTo</a></li><li><a href="global.html#closestTo">closestTo</a></li><li><a href="global.html#compareAsc">compareAsc</a></li><li><a href="global.html#compareDesc">compareDesc</a></li><li><a href="global.html#differenceInBusinessDays">differenceInBusinessDays</a></li><li><a href="global.html#differenceInCalendarDays">differenceInCalendarDays</a></li><li><a href="global.html#differenceInCalendarISOWeeks">differenceInCalendarISOWeeks</a></li><li><a href="global.html#differenceInCalendarISOWeekYears">differenceInCalendarISOWeekYears</a></li><li><a href="global.html#differenceInCalendarMonths">differenceInCalendarMonths</a></li><li><a href="global.html#differenceInCalendarQuarters">differenceInCalendarQuarters</a></li><li><a href="global.html#differenceInCalendarWeeks">differenceInCalendarWeeks</a></li><li><a href="global.html#differenceInCalendarYears">differenceInCalendarYears</a></li><li><a href="global.html#differenceInDays">differenceInDays</a></li><li><a href="global.html#differenceInHours">differenceInHours</a></li><li><a href="global.html#differenceInISOWeekYears">differenceInISOWeekYears</a></li><li><a href="global.html#differenceInMilliseconds">differenceInMilliseconds</a></li><li><a href="global.html#differenceInMinutes">differenceInMinutes</a></li><li><a href="global.html#differenceInMonths">differenceInMonths</a></li><li><a href="global.html#differenceInQuarters">differenceInQuarters</a></li><li><a href="global.html#differenceInSeconds">differenceInSeconds</a></li><li><a href="global.html#differenceInWeeks">differenceInWeeks</a></li><li><a href="global.html#differenceInYears">differenceInYears</a></li><li><a href="global.html#eachDayOfInterval">eachDayOfInterval</a></li><li><a href="global.html#eachHourOfInterval">eachHourOfInterval</a></li><li><a href="global.html#eachMonthOfInterval">eachMonthOfInterval</a></li><li><a href="global.html#eachQuarterOfInterval">eachQuarterOfInterval</a></li><li><a href="global.html#eachWeekendOfInterval">eachWeekendOfInterval</a></li><li><a href="global.html#eachWeekendOfMonth">eachWeekendOfMonth</a></li><li><a href="global.html#eachWeekendOfYear">eachWeekendOfYear</a></li><li><a href="global.html#eachWeekOfInterval">eachWeekOfInterval</a></li><li><a href="global.html#eachYearOfInterval">eachYearOfInterval</a></li><li><a href="global.html#endOfDay">endOfDay</a></li><li><a href="global.html#endOfDecade">endOfDecade</a></li><li><a href="global.html#endOfHour">endOfHour</a></li><li><a href="global.html#endOfISOWeek">endOfISOWeek</a></li><li><a href="global.html#endOfISOWeekYear">endOfISOWeekYear</a></li><li><a href="global.html#endOfMinute">endOfMinute</a></li><li><a href="global.html#endOfMonth">endOfMonth</a></li><li><a href="global.html#endOfQuarter">endOfQuarter</a></li><li><a href="global.html#endOfSecond">endOfSecond</a></li><li><a href="global.html#endOfToday">endOfToday</a></li><li><a href="global.html#endOfTomorrow">endOfTomorrow</a></li><li><a href="global.html#endOfWeek">endOfWeek</a></li><li><a href="global.html#endOfYear">endOfYear</a></li><li><a href="global.html#endOfYesterday">endOfYesterday</a></li><li><a href="global.html#eraValues">eraValues</a></li><li><a href="global.html#format">format</a></li><li><a href="global.html#formatDistance">formatDistance</a></li><li><a href="global.html#formatDistanceLocale">formatDistanceLocale</a></li><li><a href="global.html#formatDistanceStrict">formatDistanceStrict</a></li><li><a href="global.html#formatDistanceToNow">formatDistanceToNow</a></li><li><a href="global.html#formatDistanceToNowStrict">formatDistanceToNowStrict</a></li><li><a href="global.html#formatDuration">formatDuration</a></li><li><a href="global.html#formatISO">formatISO</a></li><li><a href="global.html#formatISO9075">formatISO9075</a></li><li><a href="global.html#formatISODuration">formatISODuration</a></li><li><a href="global.html#formatRelative">formatRelative</a></li><li><a href="global.html#formatRFC3339">formatRFC3339</a></li><li><a href="global.html#formatRFC7231">formatRFC7231</a></li><li><a href="global.html#fromUnixTime">fromUnixTime</a></li><li><a href="global.html#getDate">getDate</a></li><li><a href="global.html#getDay">getDay</a></li><li><a href="global.html#getDayOfYear">getDayOfYear</a></li><li><a href="global.html#getDaysInMonth">getDaysInMonth</a></li><li><a href="global.html#getDaysInYear">getDaysInYear</a></li><li><a href="global.html#getDecade">getDecade</a></li><li><a href="global.html#getHours">getHours</a></li><li><a href="global.html#getISODay">getISODay</a></li><li><a href="global.html#getISOWeek">getISOWeek</a></li><li><a href="global.html#getISOWeeksInYear">getISOWeeksInYear</a></li><li><a href="global.html#getISOWeekYear">getISOWeekYear</a></li><li><a href="global.html#getMilliseconds">getMilliseconds</a></li><li><a href="global.html#getMinutes">getMinutes</a></li><li><a href="global.html#getMonth">getMonth</a></li><li><a href="global.html#getOverlappingDaysInIntervals">getOverlappingDaysInIntervals</a></li><li><a href="global.html#getQuarter">getQuarter</a></li><li><a href="global.html#getSeconds">getSeconds</a></li><li><a href="global.html#getTime">getTime</a></li><li><a href="global.html#getTimezoneOffsetInMilliseconds">getTimezoneOffsetInMilliseconds</a></li><li><a href="global.html#getUnixTime">getUnixTime</a></li><li><a href="global.html#getWeek">getWeek</a></li><li><a href="global.html#getWeekOfMonth">getWeekOfMonth</a></li><li><a href="global.html#getWeeksInMonth">getWeeksInMonth</a></li><li><a href="global.html#getWeekYear">getWeekYear</a></li><li><a href="global.html#getYear">getYear</a></li><li><a href="global.html#intervalToDuration">intervalToDuration</a></li><li><a href="global.html#isAfter">isAfter</a></li><li><a href="global.html#isBefore">isBefore</a></li><li><a href="global.html#isDate">isDate</a></li><li><a href="global.html#isEqual">isEqual</a></li><li><a href="global.html#isExists">isExists</a></li><li><a href="global.html#isFirstDayOfMonth">isFirstDayOfMonth</a></li><li><a href="global.html#isFriday">isFriday</a></li><li><a href="global.html#isFuture">isFuture</a></li><li><a href="global.html#isLastDayOfMonth">isLastDayOfMonth</a></li><li><a href="global.html#isLeapYear">isLeapYear</a></li><li><a href="global.html#isMatch">isMatch</a></li><li><a href="global.html#isMonday">isMonday</a></li><li><a href="global.html#isPast">isPast</a></li><li><a href="global.html#isSameDay">isSameDay</a></li><li><a href="global.html#isSameHour">isSameHour</a></li><li><a href="global.html#isSameISOWeek">isSameISOWeek</a></li><li><a href="global.html#isSameISOWeekYear">isSameISOWeekYear</a></li><li><a href="global.html#isSameMinute">isSameMinute</a></li><li><a href="global.html#isSameMonth">isSameMonth</a></li><li><a href="global.html#isSameQuarter">isSameQuarter</a></li><li><a href="global.html#isSameSecond">isSameSecond</a></li><li><a href="global.html#isSameWeek">isSameWeek</a></li><li><a href="global.html#isSameYear">isSameYear</a></li><li><a href="global.html#isSaturday">isSaturday</a></li><li><a href="global.html#isSunday">isSunday</a></li><li><a href="global.html#isThisHour">isThisHour</a></li><li><a href="global.html#isThisISOWeek">isThisISOWeek</a></li><li><a href="global.html#isThisMinute">isThisMinute</a></li><li><a href="global.html#isThisMonth">isThisMonth</a></li><li><a href="global.html#isThisQuarter">isThisQuarter</a></li><li><a href="global.html#isThisSecond">isThisSecond</a></li><li><a href="global.html#isThisWeek">isThisWeek</a></li><li><a href="global.html#isThisYear">isThisYear</a></li><li><a href="global.html#isThursday">isThursday</a></li><li><a href="global.html#isToday">isToday</a></li><li><a href="global.html#isTomorrow">isTomorrow</a></li><li><a href="global.html#isTuesday">isTuesday</a></li><li><a href="global.html#isValid">isValid</a></li><li><a href="global.html#isWednesday">isWednesday</a></li><li><a href="global.html#isWeekend">isWeekend</a></li><li><a href="global.html#isWithinInterval">isWithinInterval</a></li><li><a href="global.html#isYesterday">isYesterday</a></li><li><a href="global.html#lastDayOfDecade">lastDayOfDecade</a></li><li><a href="global.html#lastDayOfISOWeek">lastDayOfISOWeek</a></li><li><a href="global.html#lastDayOfISOWeekYear">lastDayOfISOWeekYear</a></li><li><a href="global.html#lastDayOfMonth">lastDayOfMonth</a></li><li><a href="global.html#lastDayOfQuarter">lastDayOfQuarter</a></li><li><a href="global.html#lastDayOfWeek">lastDayOfWeek</a></li><li><a href="global.html#lastDayOfYear">lastDayOfYear</a></li><li><a href="global.html#lightFormat">lightFormat</a></li><li><a href="global.html#locale">locale</a></li><li><a href="global.html#max">max</a></li><li><a href="global.html#maxTime">maxTime</a></li><li><a href="global.html#milliseconds">milliseconds</a></li><li><a href="global.html#min">min</a></li><li><a href="global.html#parse">parse</a></li><li><a href="global.html#parseISO">parseISO</a></li><li><a href="global.html#parseJSON">parseJSON</a></li><li><a href="global.html#roundToNearestMinutes">roundToNearestMinutes</a></li><li><a href="global.html#set">set</a></li><li><a href="global.html#setDate">setDate</a></li><li><a href="global.html#setDay">setDay</a></li><li><a href="global.html#setDayOfYear">setDayOfYear</a></li><li><a href="global.html#setHours">setHours</a></li><li><a href="global.html#setISODay">setISODay</a></li><li><a href="global.html#setISOWeek">setISOWeek</a></li><li><a href="global.html#setISOWeekYear">setISOWeekYear</a></li><li><a href="global.html#setMilliseconds">setMilliseconds</a></li><li><a href="global.html#setMinutes">setMinutes</a></li><li><a href="global.html#setMonth">setMonth</a></li><li><a href="global.html#setQuarter">setQuarter</a></li><li><a href="global.html#setSeconds">setSeconds</a></li><li><a href="global.html#setWeek">setWeek</a></li><li><a href="global.html#setWeekYear">setWeekYear</a></li><li><a href="global.html#setYear">setYear</a></li><li><a href="global.html#startOfDay">startOfDay</a></li><li><a href="global.html#startOfDecade">startOfDecade</a></li><li><a href="global.html#startOfHour">startOfHour</a></li><li><a href="global.html#startOfISOWeek">startOfISOWeek</a></li><li><a href="global.html#startOfISOWeekYear">startOfISOWeekYear</a></li><li><a href="global.html#startOfMinute">startOfMinute</a></li><li><a href="global.html#startOfMonth">startOfMonth</a></li><li><a href="global.html#startOfQuarter">startOfQuarter</a></li><li><a href="global.html#startOfSecond">startOfSecond</a></li><li><a href="global.html#startOfToday">startOfToday</a></li><li><a href="global.html#startOfTomorrow">startOfTomorrow</a></li><li><a href="global.html#startOfWeek">startOfWeek</a></li><li><a href="global.html#startOfWeekYear">startOfWeekYear</a></li><li><a href="global.html#startOfYear">startOfYear</a></li><li><a href="global.html#startOfYesterday">startOfYesterday</a></li><li><a href="global.html#sub">sub</a></li><li><a href="global.html#subBusinessDays">subBusinessDays</a></li><li><a href="global.html#subDays">subDays</a></li><li><a href="global.html#subHours">subHours</a></li><li><a href="global.html#subISOWeekYears">subISOWeekYears</a></li><li><a href="global.html#subMilliseconds">subMilliseconds</a></li><li><a href="global.html#subMinutes">subMinutes</a></li><li><a href="global.html#subMonths">subMonths</a></li><li><a href="global.html#subQuarters">subQuarters</a></li><li><a href="global.html#subSeconds">subSeconds</a></li><li><a href="global.html#subWeeks">subWeeks</a></li><li><a href="global.html#subYears">subYears</a></li><li><a href="global.html#toDate">toDate</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Apr 16 2021 13:00:04 GMT+0800 (GMT+08:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>