UNPKG

976 BJavaScriptView Raw
1/**
2 * @name formatISODuration
3 * @category Common Helpers
4 * @summary Format a duration object according as ISO 8601 duration string
5 *
6 * @description
7 * Format a duration object according to the ISO 8601 duration standard (https://www.digi.com/resources/documentation/digidocs//90001488-13/reference/r_iso_8601_duration_format.htm)
8 *
9 * @param duration - The duration to format
10 *
11 * @returns The ISO 8601 duration string
12 *
13 * @example
14 * // Format the given duration as ISO 8601 string
15 * const result = formatISODuration({
16 * years: 39,
17 * months: 2,
18 * days: 20,
19 * hours: 7,
20 * minutes: 5,
21 * seconds: 0
22 * })
23 * //=> 'P39Y2M20DT0H0M0S'
24 */
25export function formatISODuration(duration) {
26 const {
27 years = 0,
28 months = 0,
29 days = 0,
30 hours = 0,
31 minutes = 0,
32 seconds = 0,
33 } = duration;
34
35 return `P${years}Y${months}M${days}DT${hours}H${minutes}M${seconds}S`;
36}
37
38// Fallback for modularized imports:
39export default formatISODuration;