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/nextDay | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-master.tar.gz node-vendor-master.tar.bz2 node-vendor-master.zip |
Diffstat (limited to 'date-fns/src/nextDay')
-rw-r--r-- | date-fns/src/nextDay/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/nextDay/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/nextDay/index.ts | 46 | ||||
-rw-r--r-- | date-fns/src/nextDay/test.ts | 86 |
4 files changed, 188 insertions, 0 deletions
diff --git a/date-fns/src/nextDay/index.d.ts b/date-fns/src/nextDay/index.d.ts new file mode 100644 index 0000000..ed09673 --- /dev/null +++ b/date-fns/src/nextDay/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { nextDay } from 'date-fns' +export default nextDay diff --git a/date-fns/src/nextDay/index.js.flow b/date-fns/src/nextDay/index.js.flow new file mode 100644 index 0000000..4b45d7f --- /dev/null +++ b/date-fns/src/nextDay/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: (date: Date | number, day: Day) => Date diff --git a/date-fns/src/nextDay/index.ts b/date-fns/src/nextDay/index.ts new file mode 100644 index 0000000..25c879a --- /dev/null +++ b/date-fns/src/nextDay/index.ts @@ -0,0 +1,46 @@ +import requiredArgs from '../_lib/requiredArgs/index' +import getDay from '../getDay' +import addDays from '../addDays' +import toDate from '../toDate' +import { Day } from '../types' + +const baseMap = [7, 6, 5, 4, 3, 2, 1] + +/** + * @name nextDay + * @category Weekday Helpers + * @summary When is the next day of the week? + * + * @description + * When is the next day of the week? 0-6 the day of the week, 0 represents Sunday. + * + * @param {Date | number} date - the date to check + * @param {Day} day - day of the week + * @returns {Date} - the date is the next day of week + * @throws {TypeError} - 2 arguments required + * + * @example + * // When is the next Monday after Mar, 20, 2020? + * const result = nextDay(new Date(2020, 2, 20), 1) + * //=> Mon Mar 23 2020 00:00:00 + * + * @example + * // When is the next Tuesday after Mar, 21, 2020? + * const result = nextDay(new Date(2020, 2, 21), 2) + * //=> Tue Mar 24 2020 00:00:00 + */ +export default function nextDay(date: Date | number, day: Day): Date { + requiredArgs(2, arguments) + const map = genMap(day) + return addDays(toDate(date), map[getDay(toDate(date))]) +} + +function genMap(daysToMove: number): number[] { + if (daysToMove === 0) { + return baseMap + } else { + const mapStart = baseMap.slice(-daysToMove) + const mapEnd = baseMap.slice(0, baseMap.length - daysToMove) + return mapStart.concat(mapEnd) + } +} diff --git a/date-fns/src/nextDay/test.ts b/date-fns/src/nextDay/test.ts new file mode 100644 index 0000000..495bd2e --- /dev/null +++ b/date-fns/src/nextDay/test.ts @@ -0,0 +1,86 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import nextDay from '.' + +describe('nextDay', function () { + it('returns the following Monday given various dates before the same', function () { + assert.deepStrictEqual( + nextDay(new Date(2020, 2 /* Mar */, 20), 1), + new Date(2020, 2 /* Mar */, 23) + ) + + assert.deepStrictEqual( + nextDay(new Date(2020, 2 /* Mar */, 19), 1), + new Date(2020, 2 /* Mar */, 23) + ) + + assert.deepStrictEqual( + nextDay(new Date(2020, 2 /* Mar */, 18), 1), + new Date(2020, 2 /* Mar */, 23) + ) + + assert.deepStrictEqual( + nextDay(new Date(2020, 2 /* Mar */, 17), 1), + new Date(2020, 2 /* Mar */, 23) + ) + + assert.deepStrictEqual( + nextDay(new Date(2020, 2 /* Mar */, 16), 1), + new Date(2020, 2 /* Mar */, 23) + ) + + assert.deepStrictEqual( + nextDay(new Date(2020, 2 /* Mar */, 22), 1), + new Date(2020, 2 /* Mar */, 23) + ) + + assert.deepStrictEqual( + nextDay(new Date(2020, 4 /* May */, 2), 1), + new Date(2020, 4 /* May */, 4) + ) + }) + + it('returns the following Tuesday given the Saturday before it', function () { + assert.deepStrictEqual( + nextDay(new Date(2020, 4 /* May */, 2), 2), + new Date(2020, 4 /* May */, 5) + ) + }) + + it('returns the following Wednesday given the Saturday before it', function () { + assert.deepStrictEqual( + nextDay(new Date(2020, 4 /* May */, 2), 3), + new Date(2020, 4 /* May */, 6) + ) + }) + + it('returns the following Thursday given the Saturday before it', function () { + assert.deepStrictEqual( + nextDay(new Date(2020, 4 /* May */, 2), 4), + new Date(2020, 4 /* May */, 7) + ) + }) + + it('returns the following Friday given the Saturday before it', function () { + assert.deepStrictEqual( + nextDay(new Date(2020, 4 /* May */, 2), 5), + new Date(2020, 4 /* May */, 8) + ) + }) + + it('returns the following Saturday given the Saturday before it', function () { + assert.deepStrictEqual( + nextDay(new Date(2020, 4 /* May */, 2), 6), + new Date(2020, 4 /* May */, 9) + ) + }) + + it('returns next Sunday given the day is Sunday', function () { + assert.deepStrictEqual( + nextDay(new Date(2020, 2 /* Mar */, 22), 0), + new Date(2020, 2 /* Mar */, 29) + ) + }) +}) |