diff options
Diffstat (limited to 'date-fns/src/formatISODuration')
-rw-r--r-- | date-fns/src/formatISODuration/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/formatISODuration/index.js | 46 | ||||
-rw-r--r-- | date-fns/src/formatISODuration/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/formatISODuration/test.js | 78 |
4 files changed, 180 insertions, 0 deletions
diff --git a/date-fns/src/formatISODuration/index.d.ts b/date-fns/src/formatISODuration/index.d.ts new file mode 100644 index 0000000..dfbd333 --- /dev/null +++ b/date-fns/src/formatISODuration/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { formatISODuration } from 'date-fns' +export default formatISODuration diff --git a/date-fns/src/formatISODuration/index.js b/date-fns/src/formatISODuration/index.js new file mode 100644 index 0000000..0702457 --- /dev/null +++ b/date-fns/src/formatISODuration/index.js @@ -0,0 +1,46 @@ +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name formatISODuration + * @category Common Helpers + * @summary Format a duration object according as ISO 8601 duration string + * + * @description + * Format a duration object according to the ISO 8601 duration standard (https://www.digi.com/resources/documentation/digidocs/90001437-13/reference/r_iso_8601_duration_format.htm) + * + * @param {Duration} duration - the duration to format + * + * @returns {String} The ISO 8601 duration string + * @throws {TypeError} Requires 1 argument + * @throws {Error} Argument must be an object + * + * @example + * // Format the given duration as ISO 8601 string + * formatISODuration({ + * years: 39, + * months: 2, + * days: 20, + * hours: 7, + * minutes: 5, + * seconds: 0 + * }) + * //=> 'P39Y2M20DT0H0M0S' + */ + +export default function formatISODuration(duration) { + requiredArgs(1, arguments) + + if (typeof duration !== 'object') + throw new Error('Duration must be an object') + + const { + years = 0, + months = 0, + days = 0, + hours = 0, + minutes = 0, + seconds = 0 + } = duration + + return `P${years}Y${months}M${days}DT${hours}H${minutes}M${seconds}S` +} diff --git a/date-fns/src/formatISODuration/index.js.flow b/date-fns/src/formatISODuration/index.js.flow new file mode 100644 index 0000000..3f8bb2c --- /dev/null +++ b/date-fns/src/formatISODuration/index.js.flow @@ -0,0 +1,52 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +export type Interval = { + start: Date | number, + end: Date | number, +} + +export type Locale = { + code?: string, + formatDistance?: (...args: Array<any>) => any, + formatRelative?: (...args: Array<any>) => any, + localize?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + formatLong?: { + date: (...args: Array<any>) => any, + time: (...args: Array<any>) => any, + dateTime: (...args: Array<any>) => any, + }, + match?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + }, +} + +export type Duration = { + years?: number, + months?: number, + weeks?: number, + days?: number, + hours?: number, + minutes?: number, + seconds?: number, +} + +export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6 + +declare module.exports: (duration: Duration) => string diff --git a/date-fns/src/formatISODuration/test.js b/date-fns/src/formatISODuration/test.js new file mode 100644 index 0000000..ecea358 --- /dev/null +++ b/date-fns/src/formatISODuration/test.js @@ -0,0 +1,78 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import formatISODuration from '.' +import intervalToDuration from '../intervalToDuration' + +describe('formatISODuration', function() { + it('Everything returns correct duration for arbitrary dates', function() { + const start = new Date(1929, 0, 15, 12, 0, 0) + const end = new Date(1968, 3, 4, 19, 5, 0) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P39Y2M20DT7H5M0S') + }) + it('Everything returns P1Y1M1DT1H1M1S (1 of everything)', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2021, 3, 2, 13, 1, 1) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P1Y1M1DT1H1M1S') + }) + it('Returns P0Y0M0DT0H0M0S when the dates are the same', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2020, 2, 1, 12, 0, 0) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P0Y0M0DT0H0M0S') + }) + it('Seconds returns P0Y0M0DT0H0M1S (1 second)', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2020, 2, 1, 12, 0, 1) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P0Y0M0DT0H0M1S') + }) + it('Minutes returns P0Y0M0DT0H1M0S (1 minute)', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2020, 2, 1, 12, 1, 0) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P0Y0M0DT0H1M0S') + }) + it('Hours returns P0Y0M0DT1H0M0S (1 hour)', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2020, 2, 1, 13, 0, 0) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P0Y0M0DT1H0M0S') + }) + it('Days returns P0Y0M1DT0H0M0S (1 day)', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2020, 2, 2, 12, 0, 0) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P0Y0M1DT0H0M0S') + }) + it('Months returns P0Y1M0DT0H0M0S (1 month)', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2020, 3, 1, 12, 0, 0) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P0Y1M0DT0H0M0S') + }) + it('Years returns P1Y0M0DT0H0M1S (1 year)', function() { + const start = new Date(2020, 2, 1, 12, 0, 0) + const end = new Date(2021, 2, 1, 12, 0, 0) + const result = formatISODuration(intervalToDuration({ start, end })) + + assert(result === 'P1Y0M0DT0H0M0S') + }) + + it('returns P0Y0M0DT0H0M0S when given an empty object', function() { + const result = formatISODuration({}) + + assert(result === 'P0Y0M0DT0H0M0S') + }) +}) |