summaryrefslogtreecommitdiff
path: root/date-fns/src/formatDistanceToNowStrict
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/formatDistanceToNowStrict')
-rw-r--r--date-fns/src/formatDistanceToNowStrict/benchmark.js25
-rw-r--r--date-fns/src/formatDistanceToNowStrict/index.d.ts4
-rw-r--r--date-fns/src/formatDistanceToNowStrict/index.js82
-rw-r--r--date-fns/src/formatDistanceToNowStrict/index.js.flow60
-rw-r--r--date-fns/src/formatDistanceToNowStrict/test.js422
5 files changed, 593 insertions, 0 deletions
diff --git a/date-fns/src/formatDistanceToNowStrict/benchmark.js b/date-fns/src/formatDistanceToNowStrict/benchmark.js
new file mode 100644
index 0000000..47d9e08
--- /dev/null
+++ b/date-fns/src/formatDistanceToNowStrict/benchmark.js
@@ -0,0 +1,25 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import formatDistanceToNowStrict from '.'
+import moment from 'moment'
+
+suite(
+ 'formatDistanceToNowStrict',
+ function() {
+ benchmark('date-fns', function() {
+ return formatDistanceToNowStrict(this.date)
+ })
+
+ benchmark('Moment.js', function() {
+ return this.moment.toNow()
+ })
+ },
+ {
+ setup: function() {
+ this.date = new Date()
+ this.moment = moment()
+ }
+ }
+)
diff --git a/date-fns/src/formatDistanceToNowStrict/index.d.ts b/date-fns/src/formatDistanceToNowStrict/index.d.ts
new file mode 100644
index 0000000..4f00e65
--- /dev/null
+++ b/date-fns/src/formatDistanceToNowStrict/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { formatDistanceToNowStrict } from 'date-fns'
+export default formatDistanceToNowStrict
diff --git a/date-fns/src/formatDistanceToNowStrict/index.js b/date-fns/src/formatDistanceToNowStrict/index.js
new file mode 100644
index 0000000..2f61d88
--- /dev/null
+++ b/date-fns/src/formatDistanceToNowStrict/index.js
@@ -0,0 +1,82 @@
+import formatDistanceStrict from '../formatDistanceStrict/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name formatDistanceToNowStrict
+ * @category Common Helpers
+ * @summary Return the distance between the given date and now in words.
+ * @pure false
+ *
+ * @description
+ * Return the distance between the given dates in words, using strict units.
+ * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
+ * 'less than' and the like.
+ *
+ * | Distance between dates | Result |
+ * |------------------------|---------------------|
+ * | 0 ... 59 secs | [0..59] seconds |
+ * | 1 ... 59 mins | [1..59] minutes |
+ * | 1 ... 23 hrs | [1..23] hours |
+ * | 1 ... 29 days | [1..29] days |
+ * | 1 ... 11 months | [1..11] months |
+ * | 1 ... N years | [1..N] years |
+ *
+ * @param {Date|Number} date - the given date
+ * @param {Object} [options] - an object with options.
+ * @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
+ * @param {'second'|'minute'|'hour'|'day'|'month'|'year'} [options.unit] - if specified, will force a unit
+ * @param {'floor'|'ceil'|'round'} [options.roundingMethod='round'] - which way to round partial units
+ * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
+ * @returns {String} the distance in words
+ * @throws {TypeError} 1 argument required
+ * @throws {RangeError} `date` must not be Invalid Date
+ * @throws {RangeError} `options.locale` must contain `formatDistance` property
+ *
+ * @example
+ * // If today is 1 January 2015, what is the distance to 2 July 2014?
+ * var result = formatDistanceToNowStrict(
+ * new Date(2014, 6, 2)
+ * )
+ * //=> '6 months'
+ *
+ * @example
+ * // If now is 1 January 2015 00:00:00,
+ * // what is the distance to 1 January 2015 00:00:15, including seconds?
+ * var result = formatDistanceToNowStrict(
+ * new Date(2015, 0, 1, 0, 0, 15)
+ * )
+ * //=> '20 seconds'
+ *
+ * @example
+ * // If today is 1 January 2015,
+ * // what is the distance to 1 January 2016, with a suffix?
+ * var result = formatDistanceToNowStrict(
+ * new Date(2016, 0, 1),
+ * {addSuffix: true}
+ * )
+ * //=> 'in 1 year'
+ *
+ * @example
+ * // If today is 28 January 2015,
+ * // what is the distance to 1 January 2015, in months, rounded up??
+ * var result = formatDistanceToNowStrict(new Date(2015, 0, 1), {
+ * unit: 'month',
+ * roundingMethod: 'ceil'
+ * })
+ * //=> '1 month'
+ *
+ * @example
+ * // If today is 1 January 2015,
+ * // what is the distance to 1 August 2016 in Esperanto?
+ * var eoLocale = require('date-fns/locale/eo')
+ * var result = formatDistanceToNowStrict(
+ * new Date(2016, 7, 1),
+ * {locale: eoLocale}
+ * )
+ * //=> '1 jaro'
+ */
+export default function formatDistanceToNowStrict(dirtyDate, dirtyOptions) {
+ requiredArgs(1, arguments)
+
+ return formatDistanceStrict(dirtyDate, Date.now(), dirtyOptions)
+}
diff --git a/date-fns/src/formatDistanceToNowStrict/index.js.flow b/date-fns/src/formatDistanceToNowStrict/index.js.flow
new file mode 100644
index 0000000..49fbade
--- /dev/null
+++ b/date-fns/src/formatDistanceToNowStrict/index.js.flow
@@ -0,0 +1,60 @@
+// @flow
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+export type Interval = {
+ start: Date | number,
+ end: Date | number,
+}
+
+export type Locale = {
+ code?: string,
+ formatDistance?: (...args: Array<any>) => any,
+ formatRelative?: (...args: Array<any>) => any,
+ localize?: {
+ ordinalNumber: (...args: Array<any>) => any,
+ era: (...args: Array<any>) => any,
+ quarter: (...args: Array<any>) => any,
+ month: (...args: Array<any>) => any,
+ day: (...args: Array<any>) => any,
+ dayPeriod: (...args: Array<any>) => any,
+ },
+ formatLong?: {
+ date: (...args: Array<any>) => any,
+ time: (...args: Array<any>) => any,
+ dateTime: (...args: Array<any>) => any,
+ },
+ match?: {
+ ordinalNumber: (...args: Array<any>) => any,
+ era: (...args: Array<any>) => any,
+ quarter: (...args: Array<any>) => any,
+ month: (...args: Array<any>) => any,
+ day: (...args: Array<any>) => any,
+ dayPeriod: (...args: Array<any>) => any,
+ },
+ options?: {
+ weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
+ firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
+ },
+}
+
+export type Duration = {
+ years?: number,
+ months?: number,
+ weeks?: number,
+ days?: number,
+ hours?: number,
+ minutes?: number,
+ seconds?: number,
+}
+
+export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6
+
+declare module.exports: (
+ date: Date | number,
+ options?: {
+ addSuffix?: boolean,
+ unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year',
+ roundingMethod?: 'floor' | 'ceil' | 'round',
+ locale?: Locale,
+ }
+) => string
diff --git a/date-fns/src/formatDistanceToNowStrict/test.js b/date-fns/src/formatDistanceToNowStrict/test.js
new file mode 100644
index 0000000..016476f
--- /dev/null
+++ b/date-fns/src/formatDistanceToNowStrict/test.js
@@ -0,0 +1,422 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import sinon from 'sinon'
+import formatDistanceToNowStrict from '.'
+
+describe('formatDistanceToNowStrict', function () {
+ let clock
+ beforeEach(() => {
+ clock = sinon.useFakeTimers(new Date(1986, 3, 4, 10, 32, 0).getTime())
+ })
+
+ afterEach(() => {
+ clock.restore()
+ })
+
+ describe('seconds', function () {
+ describe('when no unit is set', function () {
+ it('0 seconds', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 32, 0))
+ assert(result === '0 seconds')
+ })
+
+ it('5 seconds', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 32, 5))
+ assert(result === '5 seconds')
+ })
+ })
+ })
+
+ describe('minutes', function () {
+ it('1 minute', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 33, 0))
+ assert(result === '1 minute')
+ })
+
+ it('n minutes', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 35, 0))
+ assert(result === '3 minutes')
+ })
+ })
+
+ describe('hours', function () {
+ it('1 hour', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 11, 32, 0))
+ assert(result === '1 hour')
+ })
+
+ it('n hours', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 13, 32, 0))
+ assert(result === '3 hours')
+ })
+ })
+
+ describe('days', function () {
+ it('1 day', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 5, 10, 32, 0))
+ assert(result === '1 day')
+ })
+
+ it('n days', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 7, 10, 32, 0))
+ assert(result === '3 days')
+ })
+ })
+
+ describe('months', function () {
+ it('1 month', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 4, 4, 10, 32, 0))
+ assert(result === '1 month')
+ })
+
+ it('n months', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 6, 4, 10, 32, 0))
+ assert(result === '3 months')
+ })
+ })
+
+ describe('years', function () {
+ it('1 year', function () {
+ var result = formatDistanceToNowStrict(new Date(1987, 3, 4, 10, 32, 0))
+ assert(result === '1 year')
+ })
+
+ it('n years', function () {
+ var result = formatDistanceToNowStrict(new Date(1991, 3, 4, 10, 32, 0))
+ assert(result === '5 years')
+ })
+ })
+
+ describe('when the unit option is supplied', function () {
+ describe('second', function () {
+ it('0 seconds', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 0),
+ { unit: 'second' }
+ )
+ assert(result === '0 seconds')
+ })
+
+ it('5 seconds', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 5),
+ { unit: 'second' }
+ )
+ assert(result === '5 seconds')
+ })
+
+ it('120 seconds', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 34, 0),
+ { unit: 'second' }
+ )
+ assert(result === '120 seconds')
+ })
+ })
+
+ describe('minute', function () {
+ it('0 minutes', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 0),
+ { unit: 'minute' }
+ )
+ assert(result === '0 minutes')
+ })
+
+ it('5 minutes', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 37, 0),
+ { unit: 'minute' }
+ )
+ assert(result === '5 minutes')
+ })
+
+ it('120 minutes', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 12, 32, 0),
+ { unit: 'minute' }
+ )
+ assert(result === '120 minutes')
+ })
+ })
+
+ describe('hour', function () {
+ it('0 hours', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 0),
+ { unit: 'hour' }
+ )
+ assert(result === '0 hours')
+ })
+
+ it('5 hours', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 15, 32, 0),
+ { unit: 'hour' }
+ )
+ assert(result === '5 hours')
+ })
+
+ it('48 hours', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 6, 10, 32, 0),
+ { unit: 'hour' }
+ )
+ assert(result === '48 hours')
+ })
+ })
+
+ describe('day', function () {
+ it('0 days', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 0),
+ { unit: 'day' }
+ )
+ assert(result === '0 days')
+ })
+
+ it('5 days', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 9, 10, 32, 0),
+ { unit: 'day' }
+ )
+ assert(result === '5 days')
+ })
+
+ it('60 days', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 5, 3, 10, 32, 0),
+ { unit: 'day' }
+ )
+ assert(result === '60 days')
+ })
+ })
+ describe('month', function () {
+ it('0 months', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 0),
+ { unit: 'month' }
+ )
+ assert(result === '0 months')
+ })
+
+ it('5 months', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 7, 4, 10, 32, 0),
+ { unit: 'month' }
+ )
+ assert(result === '4 months')
+ })
+
+ it('24 months', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1988, 3, 4, 10, 32, 0),
+ { unit: 'month' }
+ )
+ assert(result === '24 months')
+ })
+ })
+
+ describe('year', function () {
+ it('returns `1 year` - see issue 2388', () => {
+ const result = formatDistanceToNowStrict(
+ new Date(1985, 3, 4, 10, 32, 0)
+ )
+ assert(result === '1 year')
+ })
+
+ it('returns `2 years` - see issue 2388', () => {
+ const result = formatDistanceToNowStrict(
+ new Date(1984, 3, 4, 10, 32, 0)
+ )
+ assert(result === '2 years')
+ })
+
+ it('0 years', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 0),
+ { unit: 'year' }
+ )
+ assert(result === '0 years')
+ })
+
+ it('5 years', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1991, 3, 4, 15, 32, 0),
+ { unit: 'year' }
+ )
+ assert(result === '5 years')
+ })
+ })
+ })
+
+ it('accepts timestamps', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 11, 32, 0).getTime()
+ )
+ assert(result === '1 hour')
+ })
+
+ describe('when the addSuffix option is true', function () {
+ it('adds a past suffix', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 31, 35), {
+ addSuffix: true,
+ })
+ assert(result === '25 seconds ago')
+ })
+
+ it('adds a future suffix', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 11, 32, 0), {
+ addSuffix: true,
+ })
+ assert(result === 'in 1 hour')
+ })
+ })
+
+ describe('when the roundingMethod option is supplied', function () {
+ it('default is "round"', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 33, 59))
+ assert(result === '2 minutes')
+ })
+
+ it('"floor"', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 33, 59), {
+ roundingMethod: 'floor',
+ })
+ assert(result === '1 minute')
+ })
+
+ it('"ceil"', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 33, 1), {
+ roundingMethod: 'ceil',
+ })
+ assert(result === '2 minutes')
+ })
+
+ it('"round" (down)', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 33, 29), {
+ roundingMethod: 'round',
+ })
+ assert(result === '1 minute')
+ })
+
+ it('"round" (up)', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 3, 4, 10, 33, 30), {
+ roundingMethod: 'round',
+ })
+ assert(result === '2 minutes')
+ })
+ })
+
+ describe('implicit conversion of options', function () {
+ it('`options.unit`', function () {
+ // eslint-disable-next-line no-new-wrappers
+ var unit = new String('year')
+
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 32, 0),
+ // $ExpectedMistake
+ { unit: unit }
+ )
+ assert(result === '0 years')
+ })
+
+ it('`options.addSuffix`', function () {
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 31, 35),
+ // $ExpectedMistake
+ { addSuffix: 1 }
+ )
+ assert(result === '25 seconds ago')
+ })
+
+ it('`options.ceil`', function () {
+ // eslint-disable-next-line no-new-wrappers
+ var roundingMethod = new String('ceil')
+
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 33, 1),
+ // $ExpectedMistake
+ { roundingMethod: roundingMethod }
+ )
+ assert(result === '2 minutes')
+ })
+ })
+
+ describe('custom locale', function () {
+ it('can be passed to the function', function () {
+ function localizeDistance(token, count, options) {
+ assert(token === 'xSeconds')
+ assert(count === 15)
+ assert(options.addSuffix === true)
+ assert(options.comparison < 0)
+ return 'It works!'
+ }
+
+ var customLocale = {
+ formatDistance: localizeDistance,
+ }
+
+ var result = formatDistanceToNowStrict(
+ new Date(1986, 3, 4, 10, 31, 45),
+ // $ExpectedMistake
+ { addSuffix: true, locale: customLocale }
+ )
+
+ assert(result === 'It works!')
+ })
+
+ describe('does not contain `formatDistance` property', function () {
+ it('throws `RangeError`', function () {
+ var customLocale = {}
+ var block = formatDistanceToNowStrict.bind(
+ null,
+ // $ExpectedMistake
+ new Date(1986, 3, 4, 10, 37, 0),
+ { unit: 'minute', locale: customLocale }
+ )
+ assert.throws(block, RangeError)
+ })
+ })
+ })
+
+ describe('edge cases', function () {
+ it('detects unit correctly for short months', function () {
+ var result = formatDistanceToNowStrict(new Date(1986, 2 /* Mar */, 7))
+ assert(result === '28 days')
+ })
+ })
+
+ it('throws `RangeError` if the date is `Invalid Date`', function () {
+ assert.throws(
+ formatDistanceToNowStrict.bind(null, new Date(NaN)),
+ RangeError
+ )
+ })
+
+ it("throws `RangeError` if `options.roundingMethod` is not 'floor', 'ceil', 'round' or undefined", function () {
+ var block = formatDistanceToNowStrict.bind(
+ null,
+ new Date(1986, 3, 4, 10, 33, 29),
+ // $ExpectedMistake
+ { roundingMethod: 'foobar' }
+ )
+ assert.throws(block, RangeError)
+ })
+
+ it("throws `RangeError` if `options.unit` is not 's', 'm', 'h', 'd', 'M', 'Y' or undefined", function () {
+ var block = formatDistanceToNowStrict.bind(
+ null,
+ new Date(1986, 3, 4, 10, 33, 29),
+ // $ExpectedMistake
+ { unit: 'foobar' }
+ )
+ assert.throws(block, RangeError)
+ })
+
+ it('throws TypeError exception if passed less than 1 arguments', function () {
+ assert.throws(formatDistanceToNowStrict.bind(null), TypeError)
+ })
+})