Skip to content

Commit

Permalink
adding previoursDay and its variations
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Silva committed Jun 18, 2021
1 parent ea3f050 commit 506e78f
Show file tree
Hide file tree
Showing 17 changed files with 619 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.js
Expand Up @@ -174,6 +174,14 @@ export { default as nextWednesday } from './nextWednesday/index'
export { default as parse } from './parse/index'
export { default as parseISO } from './parseISO/index'
export { default as parseJSON } from './parseJSON/index'
export { default as previousDay } from './previousDay/index'
export { default as previousMonday } from './previousMonday/index'
export { default as previousTuesday } from './previousTuesday/index'
export { default as previousWednesday } from './previousWednesday/index'
export { default as previousThursday } from './previousThursday/index'
export { default as previousFriday } from './previousFriday/index'
export { default as previousSaturday } from './previousSaturday/index'
export { default as previousSunday } from './previousSunday/index'
export { default as quartersToMonths } from './quartersToMonths/index'
export { default as quartersToYears } from './quartersToYears/index'
export { default as roundToNearestMinutes } from './roundToNearestMinutes/index'
Expand Down
46 changes: 46 additions & 0 deletions src/previousDay/index.ts
@@ -0,0 +1,46 @@
import requiredArgs from '../_lib/requiredArgs/index'
import getDay from '../getDay'
import subDays from '../subDays'
import toDate from '../toDate'
import { Day } from '../types'

const baseMap = [1,2,3,4,5,6,7]

/**
* @name previousDay
* @category Weekday Helpers
* @summary When is the previous day of the week?
*
* @description
* When is the previous 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 previous day of week
* @throws {TypeError} - 2 arguments required
*
* @example
* // When is the previous Monday before Mar, 20, 2020?
* const result = previousDay(new Date(2020, 2, 20), 1)
* //=> Mon Mar 16 2020 00:00:00
*
* @example
* // When is the previous Tuesday before Mar, 21, 2020?
* const result = previousDay(new Date(2020, 2, 21), 2)
* //=> Tue Mar 17 2020 00:00:00
*/
export default function previousDay(date: Date | number, day: Day): Date {
requiredArgs(2, arguments)
const map = genMap(day+1)
return subDays(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)
}
}
82 changes: 82 additions & 0 deletions src/previousDay/test.ts
@@ -0,0 +1,82 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import previousDay from '.'

describe('previousDay', function () {
it('returns the previous Monday given various dates after the same', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 18), 1),
new Date(2021, 5 /* Jun */, 14)
)

assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 17), 1),
new Date(2021, 5 /* Jun */, 14)
)

assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 14), 1),
new Date(2021, 5 /* Jun */, 7)
)

assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 9), 1),
new Date(2021, 5 /* Jun */, 7)
)

assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 8), 1),
new Date(2021, 5 /* Jun */, 7)
)

assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 7), 1),
new Date(2021, 4 /* May */, 31)
)

})

it('returns the previous Tuesday given the Saturday after it', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 26), 2),
new Date(2021, 5 /* Jun */, 22)
)
})

it('returns the previous Wednesday given the Saturday after it', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 26), 3),
new Date(2021, 5 /* Jun */, 23)
)
})

it('returns the previous Thursday given the Saturday after it', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 26), 4),
new Date(2021, 5 /* Jun */, 24)
)
})

it('returns the previous Friday given the Saturday after it', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 26), 5),
new Date(2021, 5 /* Jun */, 25)
)
})

it('returns the previous Saturday given the Saturday after it', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 26), 6),
new Date(2021, 5 /* Jun */, 19)
)
})

it('returns previours Sunday given the day is Sunday', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 27), 0),
new Date(2021, 5 /* Jun */, 20)
)
})
})
25 changes: 25 additions & 0 deletions src/previousFriday/index.ts
@@ -0,0 +1,25 @@
import requiredArgs from '../_lib/requiredArgs/index'
import previousDay from '../previousDay/index'
import toDate from '../toDate/index'

/**
* @name previousFriday
* @category Weekday Helpers
* @summary When is the previous Friday?
*
* @description
* When is the previous Friday?
*
* @param {Date | number} date - the date to start counting from
* @returns {Date} the previous Friday
* @throws {TypeError} 1 argument required
*
* @example
* // When is the previous Friday before Jun, 19, 2021?
* const result = previousFriday(new Date(2021, 5, 19))
* //=> Fri June 18 2021 00:00:00
*/
export default function previousFriday(date: Date | number): Date {
requiredArgs(1, arguments)
return previousDay(toDate(date), 5)
}
44 changes: 44 additions & 0 deletions src/previousFriday/test.ts
@@ -0,0 +1,44 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import previoursFriday from '.'

describe('previoursFriday', function () {
it('returns the following Friday given various dates after the same', function () {
assert.deepStrictEqual(
previoursFriday(new Date(2021, 5 /* Jun */, 5)),
new Date(2021, 5 /* Jun */, 4)
)

assert.deepStrictEqual(
previoursFriday(new Date(2021, 5 /* Jun */, 6)),
new Date(2021, 5 /* Jun */, 4)
)

assert.deepStrictEqual(
previoursFriday(new Date(2021, 5 /* Jun */, 11)),
new Date(2021, 5 /* Jun */, 4)
)

assert.deepStrictEqual(
previoursFriday(new Date(2021, 5 /* Jun */, 14)),
new Date(2021, 5 /* Jun */, 11)
)

assert.deepStrictEqual(
previoursFriday(new Date(2021, 5 /* Jun */, 15)),
new Date(2021, 5 /* Jun */, 11)
)

assert.deepStrictEqual(
previoursFriday(new Date(2021, 5 /* Jun */, 24)),
new Date(2021, 5 /* Jun */, 18)
)

})

it('returns `Invalid Date` if the given date is invalid', function () {
assert(previoursFriday(new Date(NaN)) instanceof Date)
})
})
25 changes: 25 additions & 0 deletions src/previousMonday/index.ts
@@ -0,0 +1,25 @@
import requiredArgs from '../_lib/requiredArgs/index'
import previousDay from '../previousDay/index'
import toDate from '../toDate/index'

/**
* @name previousMonday
* @category Weekday Helpers
* @summary When is the previous Monday?
*
* @description
* When is the previous Monday?
*
* @param {Date | number} date - the date to start counting from
* @returns {Date} the previous Monday
* @throws {TypeError} 1 argument required
*
* @example
* // When is the previous Monday before Jun, 18, 2021?
* const result = previousMonday(new Date(2021, 5, 18))
* //=> Mon June 14 2021 00:00:00
*/
export default function previousMonday(date: Date | number): Date {
requiredArgs(1, arguments)
return previousDay(toDate(date), 1)
}
44 changes: 44 additions & 0 deletions src/previousMonday/test.ts
@@ -0,0 +1,44 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import previoursMonday from '.'

describe('previoursMonday', function () {
it('returns the following Monday given various dates after the same', function () {
assert.deepStrictEqual(
previoursMonday(new Date(2021, 5 /* Jun */, 5)),
new Date(2021, 4 /* May */, 31)
)

assert.deepStrictEqual(
previoursMonday(new Date(2021, 5 /* Jun */, 6)),
new Date(2021, 4 /* May */, 31)
)

assert.deepStrictEqual(
previoursMonday(new Date(2021, 5 /* Jun */, 7)),
new Date(2021, 4 /* May */, 31)
)

assert.deepStrictEqual(
previoursMonday(new Date(2021, 5 /* Jun */, 14)),
new Date(2021, 5 /* Jun */, 7)
)

assert.deepStrictEqual(
previoursMonday(new Date(2021, 5 /* Jun */, 15)),
new Date(2021, 5 /* Jun */, 14)
)

assert.deepStrictEqual(
previoursMonday(new Date(2021, 5 /* Jun */, 16)),
new Date(2021, 5 /* Jun */, 14)
)

})

it('returns `Invalid Date` if the given date is invalid', function () {
assert(previoursMonday(new Date(NaN)) instanceof Date)
})
})
25 changes: 25 additions & 0 deletions src/previousSaturday/index.ts
@@ -0,0 +1,25 @@
import requiredArgs from '../_lib/requiredArgs/index'
import previousDay from '../previousDay/index'
import toDate from '../toDate/index'

/**
* @name previousSaturday
* @category Weekday Helpers
* @summary When is the previous Saturday?
*
* @description
* When is the previous Saturday?
*
* @param {Date | number} date - the date to start counting from
* @returns {Date} the previous Saturday
* @throws {TypeError} 1 argument required
*
* @example
* // When is the previous Saturday before Jun, 20, 2021?
* const result = previousSaturday(new Date(2021, 5, 20))
* //=> Fri June 19 2021 00:00:00
*/
export default function previousSaturday(date: Date | number): Date {
requiredArgs(1, arguments)
return previousDay(toDate(date), 6)
}
44 changes: 44 additions & 0 deletions src/previousSaturday/test.ts
@@ -0,0 +1,44 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import previoursSaturday from '.'

describe('previoursSaturday', function () {
it('returns the following Saturday given various dates after the same', function () {
assert.deepStrictEqual(
previoursSaturday(new Date(2021, 5 /* Jun */, 7)),
new Date(2021, 5 /* Jun */, 5)
)

assert.deepStrictEqual(
previoursSaturday(new Date(2021, 5 /* Jun */, 8)),
new Date(2021, 5 /* Jun */, 5)
)

assert.deepStrictEqual(
previoursSaturday(new Date(2021, 5 /* Jun */, 12)),
new Date(2021, 5 /* Jun */, 5)
)

assert.deepStrictEqual(
previoursSaturday(new Date(2021, 5 /* Jun */, 16)),
new Date(2021, 5 /* Jun */, 12)
)

assert.deepStrictEqual(
previoursSaturday(new Date(2021, 5 /* Jun */, 17)),
new Date(2021, 5 /* Jun */, 12)
)

assert.deepStrictEqual(
previoursSaturday(new Date(2021, 5 /* Jun */, 24)),
new Date(2021, 5 /* Jun */, 19)
)

})

it('returns `Invalid Date` if the given date is invalid', function () {
assert(previoursSaturday(new Date(NaN)) instanceof Date)
})
})
25 changes: 25 additions & 0 deletions src/previousSunday/index.ts
@@ -0,0 +1,25 @@
import requiredArgs from '../_lib/requiredArgs/index'
import previousDay from '../previousDay/index'
import toDate from '../toDate/index'

/**
* @name previousSunday
* @category Weekday Helpers
* @summary When is the previous Sunday?
*
* @description
* When is the previous Sunday?
*
* @param {Date | number} date - the date to start counting from
* @returns {Date} the previous Sunday
* @throws {TypeError} 1 argument required
*
* @example
* // When is the previous Sunday before Jun, 21, 2021?
* const result = previousSunday(new Date(2021, 5, 21))
* //=> Fri June 20 2021 00:00:00
*/
export default function previousSunday(date: Date | number): Date {
requiredArgs(1, arguments)
return previousDay(toDate(date), 0)
}

0 comments on commit 506e78f

Please sign in to comment.