Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate formatDistanceStrict to TS #2091

Merged
merged 4 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
* )
Expand All @@ -130,23 +131,23 @@ 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'
*
* @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'
*
* @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'
* })
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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)
}

Expand Down