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 formatRelative function #2316

Merged
merged 2 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
25 changes: 14 additions & 11 deletions src/formatRelative/index.js → src/formatRelative/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import subMilliseconds from '../subMilliseconds/index'
import toDate from '../toDate/index'
import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'
import { LocaleOptions, WeekStartOptions } from '../types'

/**
* @name formatRelative
Expand Down Expand Up @@ -41,14 +42,16 @@ import requiredArgs from '../_lib/requiredArgs/index'
* @throws {RangeError} `options.locale` must contain `formatLong` property
* @throws {RangeError} `options.locale` must contain `formatRelative` property
*/
export default function formatRelative(dirtyDate, dirtyBaseDate, dirtyOptions) {
export default function formatRelative(dirtyDate: Date | number, dirtyBaseDate: Date | number, dirtyOptions?: LocaleOptions & WeekStartOptions): string {
requiredArgs(2, arguments)

var date = toDate(dirtyDate)
var baseDate = toDate(dirtyBaseDate)
const date = toDate(dirtyDate)
const baseDate = toDate(dirtyBaseDate)

var options = dirtyOptions || {}
var locale = options.locale || defaultLocale
const {
locale = defaultLocale,
weekStartsOn = 0,
} = dirtyOptions || {}

if (!locale.localize) {
throw new RangeError('locale must contain localize property')
Expand All @@ -62,13 +65,13 @@ export default function formatRelative(dirtyDate, dirtyBaseDate, dirtyOptions) {
throw new RangeError('locale must contain formatRelative property')
}

var diff = differenceInCalendarDays(date, baseDate)
const diff = differenceInCalendarDays(date, baseDate)

if (isNaN(diff)) {
throw new RangeError('Invalid time value')
}

var token
let token
if (diff < -6) {
token = 'other'
} else if (diff < -1) {
Expand All @@ -85,11 +88,11 @@ export default function formatRelative(dirtyDate, dirtyBaseDate, dirtyOptions) {
token = 'other'
}

var utcDate = subMilliseconds(date, getTimezoneOffsetInMilliseconds(date))
var utcBaseDate = subMilliseconds(
const utcDate = subMilliseconds(date, getTimezoneOffsetInMilliseconds(date))
const utcBaseDate = subMilliseconds(
baseDate,
getTimezoneOffsetInMilliseconds(baseDate)
)
var formatStr = locale.formatRelative(token, utcDate, utcBaseDate, options)
return format(date, formatStr, options)
const formatStr = locale.formatRelative(token, utcDate, utcBaseDate, { locale, weekStartsOn })
return format(date, formatStr, { locale, weekStartsOn })
}
101 changes: 50 additions & 51 deletions src/formatRelative/test.js → src/formatRelative/test.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import assert from 'assert'
import formatRelative from '.'

describe('formatRelative', function() {
var baseDate = new Date(1986, 3 /* Apr */, 4, 10, 32, 0, 900)
describe('formatRelative', function () {
const baseDate = new Date(1986, 3 /* Apr */, 4, 10, 32, 0, 900)

it('accepts a timestamp', function() {
var date = new Date(2014, 3 /* Apr */, 4)
it('accepts a timestamp', function () {
const date = new Date(2014, 3 /* Apr */, 4)
assert(formatRelative(date.getTime(), baseDate.getTime()) === '04/04/2014')
})

it('before the last week', function() {
var result = formatRelative(
it('before the last week', function () {
const result = formatRelative(
new Date(1986, 2 /* Mar */, 28, 16, 50),
baseDate
)
assert(result === '03/28/1986')
})

it('last week', function() {
var result = formatRelative(new Date(1986, 3 /* Apr */, 1), baseDate)
it('last week', function () {
const result = formatRelative(new Date(1986, 3 /* Apr */, 1), baseDate)
assert(result === 'last Tuesday at 12:00 AM')
})

it('yesterday', function() {
var result = formatRelative(
it('yesterday', function () {
const result = formatRelative(
new Date(1986, 3 /* Apr */, 3, 22, 22),
baseDate
)
assert(result === 'yesterday at 10:22 PM')
})

it('today', function() {
var result = formatRelative(
it('today', function () {
const result = formatRelative(
new Date(1986, 3 /* Apr */, 4, 16, 50),
baseDate
)
assert(result === 'today at 4:50 PM')
})

it('tomorrow', function() {
var result = formatRelative(new Date(1986, 3 /* Apr */, 5, 7, 30), baseDate)
it('tomorrow', function () {
const result = formatRelative(new Date(1986, 3 /* Apr */, 5, 7, 30), baseDate)
assert(result === 'tomorrow at 7:30 AM')
})

it('next week', function() {
var result = formatRelative(new Date(1986, 3 /* Apr */, 6, 12, 0), baseDate)
it('next week', function () {
const result = formatRelative(new Date(1986, 3 /* Apr */, 6, 12, 0), baseDate)
assert(result === 'Sunday at 12:00 PM')
})

it('after the next week', function() {
var result = formatRelative(
it('after the next week', function () {
const result = formatRelative(
new Date(1986, 3 /* Apr */, 11, 16, 50),
baseDate
)
assert(result === '04/11/1986')
})

describe('edge cases', function() {
it("throws RangeError if the date isn't valid", function() {
describe('edge cases', function () {
it("throws RangeError if the date isn't valid", function () {
assert.throws(
formatRelative.bind(null, new Date(NaN), baseDate),
RangeError
)
})

it("throws RangeError if the base date isn't valid", function() {
it("throws RangeError if the base date isn't valid", function () {
assert.throws(
formatRelative.bind(
null,
Expand All @@ -78,92 +77,92 @@ describe('formatRelative', function() {
)
})

it("throws RangeError if both dates aren't valid", function() {
it("throws RangeError if both dates aren't valid", function () {
assert.throws(
formatRelative.bind(null, new Date(NaN), new Date(NaN)),
RangeError
)
})

it('handles dates before 100 AD', function() {
var date = new Date(0)
it('handles dates before 100 AD', function () {
const date = new Date(0)
date.setFullYear(7, 11 /* Dec */, 31)
date.setHours(0, 0, 0, 0)
assert(formatRelative(date, baseDate) === '12/31/0007')
})
})

describe('custom locale', function() {
it('allows to pass a custom locale', function() {
var customLocale = {
describe('custom locale', function () {
it('allows to pass a custom locale', function () {
const customLocale = {
localize: {
month: function() {
month: function () {
return 'works'
}
},
formatLong: {
date: function() {
date: function () {
return "'It' MMMM"
}
},
formatRelative: function() {
formatRelative: function () {
return "P 'perfectly!'"
}
}
var result = formatRelative(
const result = formatRelative(
new Date(1986, 2 /* Mar */, 28, 16, 50),
baseDate,
// $ExpectedMistake
// @ts-expect-error
{ locale: customLocale }
)
assert(result === 'It works perfectly!')
})

it("throws `RangeError` if `options.locale` doesn't have `localize` property", function() {
var customLocale = {
it("throws `RangeError` if `options.locale` doesn't have `localize` property", function () {
const customLocale = {
formatLong: {},
formatRelative: function() {
formatRelative: function () {
return ''
}
}
// $ExpectedMistake
var block = formatRelative.bind(null, new Date(2017, 0, 1), baseDate, {
// @ts-expect-error
const block = formatRelative.bind(null, new Date(2017, 0, 1), baseDate, {
locale: customLocale
})
assert.throws(block, RangeError)
})

it("throws `RangeError` if `options.locale` doesn't have `formatLong` property", function() {
var customLocale = {
// $ExpectedMistake
it("throws `RangeError` if `options.locale` doesn't have `formatLong` property", function () {
const customLocale = {
localize: {},
formatRelative: function() {
formatRelative: function () {
return ''
}
}
// $ExpectedMistake
var block = formatRelative.bind(null, new Date(2017, 0, 1), baseDate, {
// @ts-expect-error
const block = formatRelative.bind(null, new Date(2017, 0, 1), baseDate, {
locale: customLocale
})
assert.throws(block, RangeError)
})

it("throws `RangeError` if `options.locale` doesn't have `formatRelative` property", function() {
var customLocale = {
// $ExpectedMistake
it("throws `RangeError` if `options.locale` doesn't have `formatRelative` property", function () {
const customLocale = {
localize: {},
formatLong: {}
}
// $ExpectedMistake
var block = formatRelative.bind(null, new Date(2017, 0, 1), baseDate, {
// @ts-expect-error
const block = formatRelative.bind(null, new Date(2017, 0, 1), baseDate, {
locale: customLocale
})
assert.throws(block, RangeError)
})
})

it('throws TypeError exception if passed less than 2 arguments', function() {
it('throws TypeError exception if passed less than 2 arguments', function () {
// @ts-expect-error
assert.throws(formatRelative.bind(null), TypeError)
// @ts-expect-error
assert.throws(formatRelative.bind(null, 1), TypeError)
})
})