diff --git a/src/formatDistanceStrict/index.js b/src/formatDistanceStrict/index.ts similarity index 81% rename from src/formatDistanceStrict/index.js rename to src/formatDistanceStrict/index.ts index 0c0d1d08b5..d792e7337b 100644 --- a/src/formatDistanceStrict/index.js +++ b/src/formatDistanceStrict/index.ts @@ -4,11 +4,12 @@ import toDate from '../toDate/index' import cloneObject from '../_lib/cloneObject/index' import defaultLocale from '../locale/en-US/index' import requiredArgs from '../_lib/requiredArgs/index' +import { LocaleOptions, Unit } from '../types'; -var MILLISECONDS_IN_MINUTE = 1000 * 60 -var MINUTES_IN_DAY = 60 * 24 -var MINUTES_IN_MONTH = MINUTES_IN_DAY * 30 -var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365 +const MILLISECONDS_IN_MINUTE = 1000 * 60 +const MINUTES_IN_DAY = 60 * 24 +const MINUTES_IN_MONTH = MINUTES_IN_DAY * 30 +const MINUTES_IN_YEAR = MINUTES_IN_DAY * 365 /** * @name formatDistanceStrict @@ -115,13 +116,13 @@ var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365 * * @example * // What is the distance between 2 July 2014 and 1 January 2015? - * var result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2)) + * const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2)) * //=> '6 months' * * @example * // What is the distance between 1 January 2015 00:00:15 * // and 1 January 2015 00:00:00? - * var result = formatDistanceStrict( + * const result = formatDistanceStrict( * new Date(2015, 0, 1, 0, 0, 15), * new Date(2015, 0, 1, 0, 0, 0) * ) @@ -130,7 +131,7 @@ var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365 * @example * // What is the distance from 1 January 2016 * // to 1 January 2015, with a suffix? - * var result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), { + * const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), { * addSuffix: true * }) * //=> '1 year ago' @@ -138,7 +139,7 @@ var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365 * @example * // What is the distance from 1 January 2016 * // to 1 January 2015, in minutes? - * var result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), { + * const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), { * unit: 'minute' * }) * //=> '525600 minutes' @@ -146,7 +147,7 @@ var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365 * @example * // What is the distance from 1 January 2015 * // to 28 January 2015, in months, rounded up? - * var result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), { + * const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), { * unit: 'month', * roundingMethod: 'ceil' * }) @@ -155,37 +156,41 @@ var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365 * @example * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto? * import { eoLocale } from 'date-fns/locale/eo' - * var result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), { + * const result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), { * locale: eoLocale * }) * //=> '1 jaro' */ + export default function formatDistanceStrict( - dirtyDate, - dirtyBaseDate, - dirtyOptions -) { + dirtyDate: Date | number, + dirtyBaseDate: Date | number, + options: LocaleOptions & { + addSuffix?: boolean, + unit?: Unit, + roundingMethod?: 'floor' | 'ceil' | 'round', + } = {} +): string { requiredArgs(2, arguments) - var options = dirtyOptions || {} - var locale = options.locale || defaultLocale + const locale = options.locale || defaultLocale if (!locale.formatDistance) { throw new RangeError('locale must contain localize.formatDistance property') } - var comparison = compareAsc(dirtyDate, dirtyBaseDate) + const comparison = compareAsc(dirtyDate, dirtyBaseDate) if (isNaN(comparison)) { throw new RangeError('Invalid time value') } - var localizeOptions = cloneObject(options) + const localizeOptions = cloneObject(options) localizeOptions.addSuffix = Boolean(options.addSuffix) localizeOptions.comparison = comparison - var dateLeft - var dateRight + let dateLeft + let dateRight if (comparison > 0) { dateLeft = toDate(dirtyBaseDate) dateRight = toDate(dirtyDate) @@ -194,9 +199,9 @@ export default function formatDistanceStrict( dateRight = toDate(dirtyBaseDate) } - var roundingMethod = + const roundingMethod = options.roundingMethod == null ? 'round' : String(options.roundingMethod) - var roundingMethodFn + let roundingMethodFn if (roundingMethod === 'floor') { roundingMethodFn = Math.floor @@ -208,19 +213,19 @@ export default function formatDistanceStrict( throw new RangeError("roundingMethod must be 'floor', 'ceil' or 'round'") } - var milliseconds = dateRight.getTime() - dateLeft.getTime() - var minutes = milliseconds / MILLISECONDS_IN_MINUTE + const milliseconds = dateRight.getTime() - dateLeft.getTime() + const minutes = milliseconds / MILLISECONDS_IN_MINUTE - var timezoneOffset = + const timezoneOffset = getTimezoneOffsetInMilliseconds(dateRight) - getTimezoneOffsetInMilliseconds(dateLeft) // Use DST-normalized difference in minutes for years, months and days; // use regular difference in minutes for hours, minutes and seconds. - var dstNormalizedMinutes = + const dstNormalizedMinutes = (milliseconds - timezoneOffset) / MILLISECONDS_IN_MINUTE - var unit + let unit if (options.unit == null) { if (minutes < 1) { unit = 'second' @@ -241,34 +246,34 @@ export default function formatDistanceStrict( // 0 up to 60 seconds if (unit === 'second') { - var seconds = roundingMethodFn(milliseconds / 1000) + const seconds = roundingMethodFn(milliseconds / 1000) return locale.formatDistance('xSeconds', seconds, localizeOptions) // 1 up to 60 mins } else if (unit === 'minute') { - var roundedMinutes = roundingMethodFn(minutes) + const roundedMinutes = roundingMethodFn(minutes) return locale.formatDistance('xMinutes', roundedMinutes, localizeOptions) // 1 up to 24 hours } else if (unit === 'hour') { - var hours = roundingMethodFn(minutes / 60) + const hours = roundingMethodFn(minutes / 60) return locale.formatDistance('xHours', hours, localizeOptions) // 1 up to 30 days } else if (unit === 'day') { - var days = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_DAY) + const days = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_DAY) return locale.formatDistance('xDays', days, localizeOptions) // 1 up to 12 months } else if (unit === 'month') { - var months = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_MONTH) + const months = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_MONTH) return months === 12 && options.unit !== 'month' ? locale.formatDistance('xYears', 1, localizeOptions) : locale.formatDistance('xMonths', months, localizeOptions) // 1 year up to max Date } else if (unit === 'year') { - var years = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_YEAR) + const years = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_YEAR) return locale.formatDistance('xYears', years, localizeOptions) } diff --git a/src/formatDistanceStrict/test.js b/src/formatDistanceStrict/test.ts similarity index 82% rename from src/formatDistanceStrict/test.js rename to src/formatDistanceStrict/test.ts index 52d94c8475..52bb2bda5b 100644 --- a/src/formatDistanceStrict/test.js +++ b/src/formatDistanceStrict/test.ts @@ -1,14 +1,13 @@ -// @flow /* eslint-env mocha */ -import assert from 'power-assert' +import assert from 'assert' import formatDistanceStrict from '.' describe('formatDistanceStrict', function () { describe('seconds', function () { describe('when no unit is set', function () { it('0 seconds', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 5), new Date(1986, 3, 4, 10, 32, 5) ) @@ -16,7 +15,7 @@ describe('formatDistanceStrict', function () { }) it('5 seconds', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 5) ) @@ -27,7 +26,7 @@ describe('formatDistanceStrict', function () { describe('minutes', function () { it('1 minute', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 0) ) @@ -35,7 +34,7 @@ describe('formatDistanceStrict', function () { }) it('n minutes', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 35, 0) ) @@ -45,7 +44,7 @@ describe('formatDistanceStrict', function () { describe('hours', function () { it('1 hour', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 11, 32, 0) ) @@ -53,7 +52,7 @@ describe('formatDistanceStrict', function () { }) it('n hours', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 13, 32, 0) ) @@ -63,7 +62,7 @@ describe('formatDistanceStrict', function () { describe('days', function () { it('1 day', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 5, 10, 32, 0) ) @@ -71,7 +70,7 @@ describe('formatDistanceStrict', function () { }) it('n days', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 7, 10, 32, 0) ) @@ -81,7 +80,7 @@ describe('formatDistanceStrict', function () { describe('months', function () { it('1 month', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 4, 4, 10, 32, 0) ) @@ -89,7 +88,7 @@ describe('formatDistanceStrict', function () { }) it('n months', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 6, 4, 10, 32, 0) ) @@ -115,7 +114,7 @@ describe('formatDistanceStrict', function () { }) it('1 year', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1987, 3, 4, 10, 32, 0) ) @@ -123,7 +122,7 @@ describe('formatDistanceStrict', function () { }) it('n years', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1991, 3, 4, 10, 32, 0) ) @@ -134,7 +133,7 @@ describe('formatDistanceStrict', function () { describe('when the unit option is supplied', function () { describe('second', function () { it('0 seconds', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 0), { unit: 'second' } @@ -143,7 +142,7 @@ describe('formatDistanceStrict', function () { }) it('5 seconds', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 5), { unit: 'second' } @@ -152,7 +151,7 @@ describe('formatDistanceStrict', function () { }) it('120 seconds', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 34, 0), { unit: 'second' } @@ -163,7 +162,7 @@ describe('formatDistanceStrict', function () { describe('minute', function () { it('0 minutes', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 0), { unit: 'minute' } @@ -172,7 +171,7 @@ describe('formatDistanceStrict', function () { }) it('5 minutes', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 37, 0), { unit: 'minute' } @@ -181,7 +180,7 @@ describe('formatDistanceStrict', function () { }) it('120 minutes', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 12, 32, 0), { unit: 'minute' } @@ -192,7 +191,7 @@ describe('formatDistanceStrict', function () { describe('hour', function () { it('0 hours', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 0), { unit: 'hour' } @@ -201,7 +200,7 @@ describe('formatDistanceStrict', function () { }) it('5 hours', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 15, 32, 0), { unit: 'hour' } @@ -210,7 +209,7 @@ describe('formatDistanceStrict', function () { }) it('48 hours', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 6, 10, 32, 0), { unit: 'hour' } @@ -221,7 +220,7 @@ describe('formatDistanceStrict', function () { describe('day', function () { it('0 days', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 0), { unit: 'day' } @@ -230,7 +229,7 @@ describe('formatDistanceStrict', function () { }) it('5 days', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 9, 10, 32, 0), { unit: 'day' } @@ -239,7 +238,7 @@ describe('formatDistanceStrict', function () { }) it('60 days', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 5, 3, 10, 32, 0), { unit: 'day' } @@ -249,7 +248,7 @@ describe('formatDistanceStrict', function () { }) describe('month', function () { it('0 months', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 0), { unit: 'month' } @@ -258,7 +257,7 @@ describe('formatDistanceStrict', function () { }) it('5 months', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 7, 4, 10, 32, 0), { unit: 'month' } @@ -267,7 +266,7 @@ describe('formatDistanceStrict', function () { }) it('12 months - see issue 2388', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 7, 4, 10, 32, 0), new Date(1985, 7, 4, 10, 32, 0), { unit: 'month' } @@ -276,7 +275,7 @@ describe('formatDistanceStrict', function () { }) it('24 months', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1988, 3, 4, 10, 32, 0), { unit: 'month' } @@ -287,7 +286,7 @@ describe('formatDistanceStrict', function () { describe('year', function () { it('0 years', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 0), { unit: 'year' } @@ -296,7 +295,7 @@ describe('formatDistanceStrict', function () { }) it('5 years', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1991, 3, 4, 15, 32, 0), { unit: 'year' } @@ -307,7 +306,7 @@ describe('formatDistanceStrict', function () { }) it('accepts timestamps', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0).getTime(), new Date(1986, 3, 4, 11, 32, 0).getTime() ) @@ -316,7 +315,7 @@ describe('formatDistanceStrict', function () { describe('when the addSuffix option is true', function () { it('adds a past suffix', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 25), { addSuffix: true } @@ -325,7 +324,7 @@ describe('formatDistanceStrict', function () { }) it('adds a future suffix', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 11, 32, 0), new Date(1986, 3, 4, 10, 32, 0), { addSuffix: true } @@ -336,7 +335,7 @@ describe('formatDistanceStrict', function () { describe('when the roundingMethod option is supplied', function () { it('default is "round"', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 59) ) @@ -344,7 +343,7 @@ describe('formatDistanceStrict', function () { }) it('"floor"', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 59), { roundingMethod: 'floor' } @@ -353,7 +352,7 @@ describe('formatDistanceStrict', function () { }) it('"ceil"', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 1), { roundingMethod: 'ceil' } @@ -362,7 +361,7 @@ describe('formatDistanceStrict', function () { }) it('"round" (down)', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 29), { roundingMethod: 'round' } @@ -371,7 +370,7 @@ describe('formatDistanceStrict', function () { }) it('"round" (up)', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 30), { roundingMethod: 'round' } @@ -383,22 +382,22 @@ describe('formatDistanceStrict', function () { describe('implicit conversion of options', function () { it('`options.unit`', function () { // eslint-disable-next-line no-new-wrappers - var unit = new String('year') + const unit = new String('year') - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 0), - // $ExpectedMistake + // @ts-expect-error { unit: unit } ) assert(result === '0 years') }) it('`options.addSuffix`', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 25), - // $ExpectedMistake + // @ts-expect-error { addSuffix: 1 } ) assert(result === '25 seconds ago') @@ -406,12 +405,12 @@ describe('formatDistanceStrict', function () { it('`options.ceil`', function () { // eslint-disable-next-line no-new-wrappers - var roundingMethod = new String('ceil') + const roundingMethod = new String('ceil') - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 1), - // $ExpectedMistake + // @ts-expect-error { roundingMethod: roundingMethod } ) assert(result === '2 minutes') @@ -420,7 +419,7 @@ describe('formatDistanceStrict', function () { describe('custom locale', function () { it('can be passed to the function', function () { - function localizeDistance(token, count, options) { + function localizeDistance(token: string, count: number, options: { addSuffix: boolean; comparison: number }) { assert(token === 'xSeconds') assert(count === 25) assert(options.addSuffix === true) @@ -428,14 +427,14 @@ describe('formatDistanceStrict', function () { return 'It works!' } - var customLocale = { + const customLocale = { formatDistance: localizeDistance, } - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 32, 25), - // $ExpectedMistake + // @ts-expect-error { addSuffix: true, locale: customLocale } ) @@ -444,11 +443,11 @@ describe('formatDistanceStrict', function () { describe('does not contain `formatDistance` property', function () { it('throws `RangeError`', function () { - var customLocale = {} - var block = formatDistanceStrict.bind( + const customLocale = {} + // @ts-expect-error + const block = formatDistanceStrict.bind( null, new Date(1986, 3, 4, 10, 32, 0), - // $ExpectedMistake new Date(1986, 3, 4, 10, 37, 0), { unit: 'minute', locale: customLocale } ) @@ -459,7 +458,7 @@ describe('formatDistanceStrict', function () { describe('edge cases', function () { it('detects unit correctly for short months', function () { - var result = formatDistanceStrict( + const result = formatDistanceStrict( new Date(2018, 1 /* Feb */, 1), new Date(2018, 2 /* Mar */, 1) ) @@ -497,29 +496,29 @@ describe('formatDistanceStrict', function () { }) it("throws `RangeError` if `options.roundingMethod` is not 'floor', 'ceil', 'round' or undefined", function () { - var block = formatDistanceStrict.bind( - null, + const block = () => formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 29), - // $ExpectedMistake + // @ts-expect-error { roundingMethod: 'foobar' } ) assert.throws(block, RangeError) }) it("throws `RangeError` if `options.unit` is not 's', 'm', 'h', 'd', 'M', 'Y' or undefined", function () { - var block = formatDistanceStrict.bind( - null, + const block = () => formatDistanceStrict( new Date(1986, 3, 4, 10, 32, 0), new Date(1986, 3, 4, 10, 33, 29), - // $ExpectedMistake + // @ts-expect-error { unit: 'foobar' } ) assert.throws(block, RangeError) }) it('throws TypeError exception if passed less than 2 arguments', function () { + // @ts-expect-error assert.throws(formatDistanceStrict.bind(null), TypeError) + // @ts-expect-error assert.throws(formatDistanceStrict.bind(null, 1), TypeError) }) }) diff --git a/src/types.ts b/src/types.ts index 1ccc4658a2..39b9c2e952 100644 --- a/src/types.ts +++ b/src/types.ts @@ -44,3 +44,10 @@ export interface DateValues { seconds?: number milliseconds?: number } + +export type Unit = 'second' + | 'minute' + | 'hour' + | 'day' + | 'month' + | 'year';