Skip to content

Commit

Permalink
migrate formatDistance to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
a-korzun committed Mar 26, 2021
1 parent 196bc87 commit fb0f912
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
55 changes: 31 additions & 24 deletions src/formatDistance/index.js → src/formatDistance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import cloneObject from '../_lib/cloneObject/index'
import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'

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 +91,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 +107,48 @@ 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 interface Options {
includeSeconds?: boolean
addSuffix?: boolean
locale?: Locale
}

export default function formatDistance(dirtyDate: Date | number, dirtyBaseDate: Date | number, dirtyOptions?: Options) {
requiredArgs(2, arguments)

var options = dirtyOptions || {}
var locale = options.locale || defaultLocale
const options = dirtyOptions || {}
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 +157,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 +199,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 +208,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 +221,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
9 changes: 3 additions & 6 deletions src/formatDistance/test.js → src/formatDistance/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import assert from 'assert'
import formatDistance from '.'

describe('formatDistance', function () {
Expand Down Expand Up @@ -218,7 +217,7 @@ describe('formatDistance', function () {
var result = formatDistance(
new Date(1986, 3, 4, 10, 32, 0),
new Date(1986, 3, 4, 10, 32, 7),
// $ExpectedMistake
// @ts-expect-error
{ includeSeconds: 1 }
)
assert(result === 'less than 10 seconds')
Expand All @@ -228,7 +227,7 @@ describe('formatDistance', function () {
var result = formatDistance(
new Date(1986, 3, 4, 11, 32, 0),
new Date(1986, 3, 4, 10, 32, 0),
// $ExpectedMistake
// @ts-expect-error
{ addSuffix: 1 }
)
assert(result === 'in about 1 hour')
Expand All @@ -252,7 +251,6 @@ describe('formatDistance', function () {
var result = formatDistance(
new Date(1986, 3, 4, 10, 32, 3),
new Date(1986, 3, 4, 10, 32, 0),
// $ExpectedMistake
{ includeSeconds: true, addSuffix: true, locale: customLocale }
)

Expand All @@ -265,7 +263,6 @@ describe('formatDistance', function () {
var block = formatDistance.bind(
null,
new Date(1986, 3, 4, 10, 32, 0),
// $ExpectedMistake
new Date(1986, 3, 4, 10, 32, 3),
{ includeSeconds: true, locale: customLocale }
)
Expand Down

0 comments on commit fb0f912

Please sign in to comment.