diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/toDate | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-master.tar.gz node-vendor-master.tar.bz2 node-vendor-master.zip |
Diffstat (limited to 'date-fns/src/toDate')
-rw-r--r-- | date-fns/src/toDate/benchmark.js | 16 | ||||
-rw-r--r-- | date-fns/src/toDate/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/toDate/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/toDate/index.ts | 61 | ||||
-rw-r--r-- | date-fns/src/toDate/test.ts | 136 |
5 files changed, 269 insertions, 0 deletions
diff --git a/date-fns/src/toDate/benchmark.js b/date-fns/src/toDate/benchmark.js new file mode 100644 index 0000000..290d5d3 --- /dev/null +++ b/date-fns/src/toDate/benchmark.js @@ -0,0 +1,16 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import toDate from '.' +import moment from 'moment' + +suite('toDate', function() { + benchmark('date-fns', function() { + return toDate(539980200000) + }) + + benchmark('Moment.js', function() { + return moment(539980200000) + }) +}) diff --git a/date-fns/src/toDate/index.d.ts b/date-fns/src/toDate/index.d.ts new file mode 100644 index 0000000..0231ae7 --- /dev/null +++ b/date-fns/src/toDate/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { toDate } from 'date-fns' +export default toDate diff --git a/date-fns/src/toDate/index.js.flow b/date-fns/src/toDate/index.js.flow new file mode 100644 index 0000000..19292c7 --- /dev/null +++ b/date-fns/src/toDate/index.js.flow @@ -0,0 +1,52 @@ +// @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: (argument: Date | number) => Date diff --git a/date-fns/src/toDate/index.ts b/date-fns/src/toDate/index.ts new file mode 100644 index 0000000..a7282ed --- /dev/null +++ b/date-fns/src/toDate/index.ts @@ -0,0 +1,61 @@ +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name toDate + * @category Common Helpers + * @summary Convert the given argument to an instance of Date. + * + * @description + * Convert the given argument to an instance of Date. + * + * If the argument is an instance of Date, the function returns its clone. + * + * If the argument is a number, it is treated as a timestamp. + * + * If the argument is none of the above, the function returns Invalid Date. + * + * **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`. + * + * @param {Date|Number} argument - the value to convert + * @returns {Date} the parsed date in the local time zone + * @throws {TypeError} 1 argument required + * + * @example + * // Clone the date: + * const result = toDate(new Date(2014, 1, 11, 11, 30, 30)) + * //=> Tue Feb 11 2014 11:30:30 + * + * @example + * // Convert the timestamp to date: + * const result = toDate(1392098430000) + * //=> Tue Feb 11 2014 11:30:30 + */ +export default function toDate(argument: Date | number): Date { + requiredArgs(1, arguments) + + const argStr = Object.prototype.toString.call(argument) + + // Clone the date + if ( + argument instanceof Date || + (typeof argument === 'object' && argStr === '[object Date]') + ) { + // Prevent the date to lose the milliseconds when passed to new Date() in IE10 + return new Date(argument.getTime()) + } else if (typeof argument === 'number' || argStr === '[object Number]') { + return new Date(argument) + } else { + if ( + (typeof argument === 'string' || argStr === '[object String]') && + typeof console !== 'undefined' + ) { + // eslint-disable-next-line no-console + console.warn( + "Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule" + ) + // eslint-disable-next-line no-console + console.warn(new Error().stack) + } + return new Date(NaN) + } +} diff --git a/date-fns/src/toDate/test.ts b/date-fns/src/toDate/test.ts new file mode 100644 index 0000000..c376727 --- /dev/null +++ b/date-fns/src/toDate/test.ts @@ -0,0 +1,136 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import sinon from 'sinon' +import toDate from '.' + +describe('toDate', () => { + describe('date argument', () => { + it('returns a clone of the given date', () => { + const date = new Date(2016, 0, 1) + const dateClone = toDate(date) + dateClone.setFullYear(2015) + assert.deepEqual(date, new Date(2016, 0, 1)) + }) + }) + + describe('timestamp argument', () => { + it('creates a date from the timestamp', () => { + const timestamp = new Date(2016, 0, 1, 23, 30, 45, 123).getTime() + const result = toDate(timestamp) + assert.deepEqual(result, new Date(2016, 0, 1, 23, 30, 45, 123)) + }) + }) + + describe('invalid argument', () => { + mockConsoleWarn() + + it('returns Invalid Date if argument is a string', () => { + // $ExpectedMistake + // @ts-expect-error + const result = toDate('1987-02-11') + assert(result instanceof Date) + // @ts-expect-error + assert(isNaN(result)) + }) + + it('prints deprecation warning if the argument is a string', () => { + // $ExpectedMistake + console.warn = sinon.spy() // eslint-disable-line no-console + // $ExpectedMistake + // @ts-expect-error + toDate('1987-02-11') + assert( + // eslint-disable-next-line no-console + // @ts-expect-error + console.warn.calledWith( + "Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule" + ) + ) + }) + + it('returns Invalid Date if argument is NaN', () => { + const result = toDate(NaN) + assert(result instanceof Date) + // @ts-expect-error + assert(isNaN(result)) + }) + + it('returns Invalid Date if argument is Invalid Date', () => { + const result = toDate(new Date(NaN)) + assert(result instanceof Date) + // @ts-expect-error + assert(isNaN(result)) + }) + + it('returns Invalid Date if argument is null', () => { + // $ExpectedMistake + // @ts-expect-error + const result = toDate(null) + assert(result instanceof Date) + // @ts-expect-error + assert(isNaN(result)) + }) + + it('returns Invalid Date if argument is undefined', () => { + // $ExpectedMistake + // @ts-expect-error + const result = toDate(undefined) + assert(result instanceof Date) + // @ts-expect-error + assert(isNaN(result)) + }) + + it('returns Invalid Date if argument is false', () => { + // $ExpectedMistake + // @ts-expect-error + const result = toDate(false) + assert(result instanceof Date) + // @ts-expect-error + assert(isNaN(result)) + }) + + it('returns Invalid Date if argument is true', () => { + // $ExpectedMistake + // @ts-expect-error + const result = toDate(true) + assert(result instanceof Date) + // @ts-expect-error + assert(isNaN(result)) + }) + }) + + describe('argument conversion', () => { + it('implicitly converts instance of Number into a number', () => { + // eslint-disable-next-line no-new-wrappers + const timestamp = new Number( + new Date(2016, 0, 1, 23, 30, 45, 123).getTime() + ) + // $ExpectedMistake + // @ts-expect-error + const result = toDate(timestamp) + assert.deepEqual(result, new Date(2016, 0, 1, 23, 30, 45, 123)) + }) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(toDate.bind(null), TypeError) + }) +}) + +function mockConsoleWarn() { + let originalWarn + + beforeEach(() => { + originalWarn = console.warn // eslint-disable-line no-console + // $ExpectedMistake + console.warn = () => {} // eslint-disable-line no-console + }) + + afterEach(() => { + // $ExpectedMistake + // @ts-expect-error + console.warn = originalWarn // eslint-disable-line no-console + }) +} |