Skip to content

Commit

Permalink
relativeDateFormat: Accept ISO string for toDate and fromDate aswell
Browse files Browse the repository at this point in the history
  • Loading branch information
runely committed Apr 5, 2022
1 parent 12c337a commit 4c6f1c0
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 30 deletions.
8 changes: 4 additions & 4 deletions cjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ declare const _exports: {
uniqueValues(array: any[], key: string, keys: any[]): any[];
relativeDateFormat(options: {
/**
* Date object to format relative date from
* Date object or date ISO string to format relative date from
*/
toDate: Date;
toDate: string | Date;
/**
* Date object to be starting point (Default = new Date())
* Date object or date ISO string to be starting point (Default = new Date())
*/
fromDate?: Date;
fromDate?: string | Date;
/**
* Locale to format date into (Default = 'en')
*/
Expand Down
8 changes: 4 additions & 4 deletions es.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ declare const _exports: {
uniqueValues(array: any[], key: string, keys: any[]): any[];
relativeDateFormat(options: {
/**
* Date object to format relative date from
* Date object or date ISO string to format relative date from
*/
toDate: Date;
toDate: string | Date;
/**
* Date object to be starting point (Default = new Date())
* Date object or date ISO string to be starting point (Default = new Date())
*/
fromDate?: Date;
fromDate?: string | Date;
/**
* Locale to format date into (Default = 'en')
*/
Expand Down
12 changes: 6 additions & 6 deletions lib/utilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ declare class Utilities {
/**
* RelativeDateOptions
* @typedef {Object} RelativeDateOptions
* @property {Date} toDate Date object to format relative date from
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
* @property {Date|String} toDate Date object or date ISO string to format relative date from
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
* @property {String} [locale] Locale to format date into (Default = 'en')
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
*/
Expand All @@ -57,13 +57,13 @@ declare class Utilities {
*/
static relativeDateFormat(options: {
/**
* Date object to format relative date from
* Date object or date ISO string to format relative date from
*/
toDate: Date;
toDate: Date | string;
/**
* Date object to be starting point (Default = new Date())
* Date object or date ISO string to be starting point (Default = new Date())
*/
fromDate?: Date;
fromDate?: Date | string;
/**
* Locale to format date into (Default = 'en')
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ module.exports = class Utilities {
/**
* RelativeDateOptions
* @typedef {Object} RelativeDateOptions
* @property {Date} toDate Date object to format relative date from
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
* @property {Date|String} toDate Date object or date ISO string to format relative date from
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
* @property {String} [locale] Locale to format date into (Default = 'en')
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
*/
Expand Down
12 changes: 6 additions & 6 deletions lib/utilities_es.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ declare class Utilities {
/**
* RelativeDateOptions
* @typedef {Object} RelativeDateOptions
* @property {Date} toDate Date object to format relative date from
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
* @property {Date|String} toDate Date object or date ISO string to format relative date from
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
* @property {String} [locale] Locale to format date into (Default = 'en')
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
*/
Expand All @@ -50,13 +50,13 @@ declare class Utilities {
*/
static relativeDateFormat(options: {
/**
* Date object to format relative date from
* Date object or date ISO string to format relative date from
*/
toDate: Date;
toDate: Date | string;
/**
* Date object to be starting point (Default = new Date())
* Date object or date ISO string to be starting point (Default = new Date())
*/
fromDate?: Date;
fromDate?: Date | string;
/**
* Locale to format date into (Default = 'en')
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/utilities_es.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ module.exports = class Utilities {
/**
* RelativeDateOptions
* @typedef {Object} RelativeDateOptions
* @property {Date} toDate Date object to format relative date from
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
* @property {Date|String} toDate Date object or date ISO string to format relative date from
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
* @property {String} [locale] Locale to format date into (Default = 'en')
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/util/relative-date-format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ describe('formatRelativeDate', () => {
})
})
})

test('"toDate" and "fromDate" passed as ISO strings', () => {
const now = new Date()
const futureDate = new Date(new Date().setDate(now.getDate() + 7))
const formatted = formatRelativeDate({ toDate: now.toISOString(), fromDate: futureDate.toISOString() })
expect(['in 7 days', 'in 1 week', '1 week ago'].includes(formatted)).toBe(true)
})
})
8 changes: 4 additions & 4 deletions util/relative-date-format.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export function formatRelativeDate({ toDate, fromDate, locale, options }: Relati
*/
export type RelativeDateOptions = {
/**
* Date object to format relative date from
* Date object or date ISO string to format relative date from
*/
toDate: Date;
toDate: Date | string;
/**
* Date object to be starting point (Default = new Date())
* Date object or date ISO string to be starting point (Default = new Date())
*/
fromDate?: Date;
fromDate?: Date | string;
/**
* Locale to format date into (Default = 'en')
*/
Expand Down
10 changes: 8 additions & 2 deletions util/relative-date-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const DIVISIONS = [
/**
* RelativeDateOptions
* @typedef {Object} RelativeDateOptions
* @property {Date} toDate Date object to format relative date from
* @property {Date} [fromDate] Date object to be starting point (Default = new Date())
* @property {Date|String} toDate Date object or date ISO string to format relative date from
* @property {Date|String} [fromDate] Date object or date ISO string to be starting point (Default = new Date())
* @property {String} [locale] Locale to format date into (Default = 'en')
* @property {RelativeTimeFormatOptions} [options] RelativeTimeFormat options
*/
Expand All @@ -35,6 +35,12 @@ const getRelativeTimeFormatter = (locale, options) => {
*/
module.exports.formatRelativeDate = ({ toDate, fromDate = new Date(), locale = 'en', options = undefined }) => {
if (toDate === null || toDate === undefined) throw new Error('Pass at least "toDate"')
if (typeof toDate === 'string' && Date.parse(toDate) > 0) {
toDate = new Date(toDate)
}
if (typeof fromDate === 'string' && Date.parse(fromDate) > 0) {
fromDate = new Date(fromDate)
}

const FORMATTER = getRelativeTimeFormatter(locale, options)
let duration = (toDate - fromDate) / 1000
Expand Down

0 comments on commit 4c6f1c0

Please sign in to comment.