Skip to content

Commit

Permalink
Migrate formatDistance to TS (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-korzun committed May 5, 2021
1 parent 67f77d6 commit 8cf4074
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 59 deletions.
50 changes: 26 additions & 24 deletions src/formatDistance/index.js → src/formatDistance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import toDate from '../toDate/index'
import cloneObject from '../_lib/cloneObject/index'
import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'
import { LocaleOptions } from '../types';

var MINUTES_IN_DAY = 1440
var MINUTES_IN_ALMOST_TWO_DAYS = 2520
var MINUTES_IN_MONTH = 43200
var MINUTES_IN_TWO_MONTHS = 86400
const MINUTES_IN_DAY = 1440
const MINUTES_IN_ALMOST_TWO_DAYS = 2520
const MINUTES_IN_MONTH = 43200
const MINUTES_IN_TWO_MONTHS = 86400

/**
* @name formatDistance
Expand Down Expand Up @@ -91,13 +92,13 @@ var MINUTES_IN_TWO_MONTHS = 86400
*
* @example
* // What is the distance between 2 July 2014 and 1 January 2015?
* var result = formatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1))
* const result = formatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1))
* //=> '6 months'
*
* @example
* // What is the distance between 1 January 2015 00:00:15
* // and 1 January 2015 00:00:00, including seconds?
* var result = formatDistance(
* const result = formatDistance(
* new Date(2015, 0, 1, 0, 0, 15),
* new Date(2015, 0, 1, 0, 0, 0),
* { includeSeconds: true }
Expand All @@ -107,41 +108,42 @@ var MINUTES_IN_TWO_MONTHS = 86400
* @example
* // What is the distance from 1 January 2016
* // to 1 January 2015, with a suffix?
* var result = formatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), {
* const result = formatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), {
* addSuffix: true
* })
* //=> 'about 1 year ago'
*
* @example
* // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
* import { eoLocale } from 'date-fns/locale/eo'
* var result = formatDistance(new Date(2016, 7, 1), new Date(2015, 0, 1), {
* const result = formatDistance(new Date(2016, 7, 1), new Date(2015, 0, 1), {
* locale: eoLocale
* })
* //=> 'pli ol 1 jaro'
*/
export default function formatDistance(dirtyDate, dirtyBaseDate, dirtyOptions) {


export default function formatDistance(dirtyDate: Date | number, dirtyBaseDate: Date | number, options: LocaleOptions & { includeSeconds?: boolean, addSuffix?: boolean } = {}): 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 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 @@ -150,13 +152,13 @@ export default function formatDistance(dirtyDate, dirtyBaseDate, dirtyOptions) {
dateRight = toDate(dirtyBaseDate)
}

var seconds = differenceInSeconds(dateRight, dateLeft)
var offsetInSeconds =
const seconds = differenceInSeconds(dateRight, dateLeft)
const offsetInSeconds =
(getTimezoneOffsetInMilliseconds(dateRight) -
getTimezoneOffsetInMilliseconds(dateLeft)) /
1000
var minutes = Math.round((seconds - offsetInSeconds) / 60)
var months
const minutes = Math.round((seconds - offsetInSeconds) / 60)
let months

// 0 up to 2 mins
if (minutes < 2) {
Expand Down Expand Up @@ -192,7 +194,7 @@ export default function formatDistance(dirtyDate, dirtyBaseDate, dirtyOptions) {

// 1.5 hrs up to 24 hrs
} else if (minutes < MINUTES_IN_DAY) {
var hours = Math.round(minutes / 60)
const hours = Math.round(minutes / 60)
return locale.formatDistance('aboutXHours', hours, localizeOptions)

// 1 day up to 1.75 days
Expand All @@ -201,7 +203,7 @@ export default function formatDistance(dirtyDate, dirtyBaseDate, dirtyOptions) {

// 1.75 days up to 30 days
} else if (minutes < MINUTES_IN_MONTH) {
var days = Math.round(minutes / MINUTES_IN_DAY)
const days = Math.round(minutes / MINUTES_IN_DAY)
return locale.formatDistance('xDays', days, localizeOptions)

// 1 month up to 2 months
Expand All @@ -214,13 +216,13 @@ export default function formatDistance(dirtyDate, dirtyBaseDate, dirtyOptions) {

// 2 months up to 12 months
if (months < 12) {
var nearestMonth = Math.round(minutes / MINUTES_IN_MONTH)
const nearestMonth = Math.round(minutes / MINUTES_IN_MONTH)
return locale.formatDistance('xMonths', nearestMonth, localizeOptions)

// 1 year up to max Date
} else {
var monthsSinceStartOfYear = months % 12
var years = Math.floor(months / 12)
const monthsSinceStartOfYear = months % 12
const years = Math.floor(months / 12)

// N years up to 1 years 3 months
if (monthsSinceStartOfYear < 3) {
Expand Down

0 comments on commit 8cf4074

Please sign in to comment.