Skip to content

Commit

Permalink
refactoring prevousDay and nextDay functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Silva committed Jun 21, 2021
1 parent 506e78f commit e10c1a8
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 92 deletions.
Empty file modified src/index.js 100644 → 100755
Empty file.
21 changes: 7 additions & 14 deletions src/nextDay/index.ts
Expand Up @@ -4,7 +4,6 @@ import addDays from '../addDays'
import toDate from '../toDate'
import { Day } from '../types'

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

/**
* @name nextDay
Expand All @@ -14,7 +13,7 @@ const baseMap = [7, 6, 5, 4, 3, 2, 1]
* @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 {Date | number} dirtyDate - 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
Expand All @@ -29,18 +28,12 @@ const baseMap = [7, 6, 5, 4, 3, 2, 1]
* 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 {
export default function nextDay(dirtyDate: Date | number, day: Day): Date {
requiredArgs(2, arguments)
const map = genMap(day)
return addDays(toDate(date), map[getDay(toDate(date))])
}
const date = toDate(dirtyDate)

let delta = day - getDay(date)
if(delta<=0) delta += 7

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)
}
return addDays(date, delta)
}
21 changes: 7 additions & 14 deletions src/previousDay/index.ts 100644 → 100755
Expand Up @@ -4,7 +4,6 @@ import subDays from '../subDays'
import toDate from '../toDate'
import { Day } from '../types'

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

/**
* @name previousDay
Expand All @@ -14,7 +13,7 @@ const baseMap = [1,2,3,4,5,6,7]
* @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 {Date | number} dirtyDate - 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
Expand All @@ -29,18 +28,12 @@ const baseMap = [1,2,3,4,5,6,7]
* 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 {
export default function previousDay(dirtyDate: Date | number, day: Day): Date {
requiredArgs(2, arguments)
const map = genMap(day+1)
return subDays(toDate(date), map[getDay(toDate(date))])
}
const date = toDate(dirtyDate)

let delta = getDay(date) - day
if (delta <= 0) delta += 7

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)
}
return subDays(date, delta)
}
2 changes: 1 addition & 1 deletion src/previousDay/test.ts 100644 → 100755
Expand Up @@ -73,7 +73,7 @@ describe('previousDay', function () {
)
})

it('returns previours Sunday given the day is Sunday', function () {
it('returns previous Sunday given the day is Sunday', function () {
assert.deepStrictEqual(
previousDay(new Date(2021, 5 /* Jun */, 27), 0),
new Date(2021, 5 /* Jun */, 20)
Expand Down
Empty file modified src/previousFriday/index.ts 100644 → 100755
Empty file.
18 changes: 9 additions & 9 deletions src/previousFriday/test.ts 100644 → 100755
Expand Up @@ -2,43 +2,43 @@
/* eslint-env mocha */

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

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

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

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

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

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

assert.deepStrictEqual(
previoursFriday(new Date(2021, 5 /* Jun */, 24)),
previousFriday(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)
assert(previousFriday(new Date(NaN)) instanceof Date)
})
})
Empty file modified src/previousMonday/index.ts 100644 → 100755
Empty file.
18 changes: 9 additions & 9 deletions src/previousMonday/test.ts 100644 → 100755
Expand Up @@ -2,43 +2,43 @@
/* eslint-env mocha */

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

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

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

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

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

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

assert.deepStrictEqual(
previoursMonday(new Date(2021, 5 /* Jun */, 16)),
previousMonday(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)
assert(previousMonday(new Date(NaN)) instanceof Date)
})
})
Empty file modified src/previousSaturday/index.ts 100644 → 100755
Empty file.
18 changes: 9 additions & 9 deletions src/previousSaturday/test.ts 100644 → 100755
Expand Up @@ -2,43 +2,43 @@
/* eslint-env mocha */

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

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

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

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

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

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

assert.deepStrictEqual(
previoursSaturday(new Date(2021, 5 /* Jun */, 24)),
previousSaturday(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)
assert(previousSaturday(new Date(NaN)) instanceof Date)
})
})
Empty file modified src/previousSunday/index.ts 100644 → 100755
Empty file.
18 changes: 9 additions & 9 deletions src/previousSunday/test.ts 100644 → 100755
Expand Up @@ -2,43 +2,43 @@
/* eslint-env mocha */

import assert from 'power-assert'
import previoursSunday from '.'
import previousSunday from '.'

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

assert.deepStrictEqual(
previoursSunday(new Date(2021, 5 /* Jun */, 8)),
previousSunday(new Date(2021, 5 /* Jun */, 8)),
new Date(2021, 5 /* Jun */, 6)
)

assert.deepStrictEqual(
previoursSunday(new Date(2021, 5 /* Jun */, 13)),
previousSunday(new Date(2021, 5 /* Jun */, 13)),
new Date(2021, 5 /* Jun */, 6)
)

assert.deepStrictEqual(
previoursSunday(new Date(2021, 5 /* Jun */, 16)),
previousSunday(new Date(2021, 5 /* Jun */, 16)),
new Date(2021, 5 /* Jun */, 13)
)

assert.deepStrictEqual(
previoursSunday(new Date(2021, 5 /* Jun */, 17)),
previousSunday(new Date(2021, 5 /* Jun */, 17)),
new Date(2021, 5 /* Jun */, 13)
)

assert.deepStrictEqual(
previoursSunday(new Date(2021, 5 /* Jun */, 24)),
previousSunday(new Date(2021, 5 /* Jun */, 24)),
new Date(2021, 5 /* Jun */, 20)
)

})

it('returns `Invalid Date` if the given date is invalid', function () {
assert(previoursSunday(new Date(NaN)) instanceof Date)
assert(previousSunday(new Date(NaN)) instanceof Date)
})
})
Empty file modified src/previousThursday/index.ts 100644 → 100755
Empty file.
18 changes: 9 additions & 9 deletions src/previousThursday/test.ts 100644 → 100755
Expand Up @@ -2,43 +2,43 @@
/* eslint-env mocha */

import assert from 'power-assert'
import previoursThursday from '.'
import previousThursday from '.'

describe('previoursThursday', function () {
describe('previousThursday', function () {
it('returns the following Thursday given various dates after the same', function () {
assert.deepStrictEqual(
previoursThursday(new Date(2021, 5 /* Jun */, 5)),
previousThursday(new Date(2021, 5 /* Jun */, 5)),
new Date(2021, 5 /* Jun */, 3)
)

assert.deepStrictEqual(
previoursThursday(new Date(2021, 5 /* Jun */, 6)),
previousThursday(new Date(2021, 5 /* Jun */, 6)),
new Date(2021, 5 /* Jun */, 3)
)

assert.deepStrictEqual(
previoursThursday(new Date(2021, 5 /* Jun */, 10)),
previousThursday(new Date(2021, 5 /* Jun */, 10)),
new Date(2021, 5 /* Jun */, 3)
)

assert.deepStrictEqual(
previoursThursday(new Date(2021, 5 /* Jun */, 14)),
previousThursday(new Date(2021, 5 /* Jun */, 14)),
new Date(2021, 5 /* Jun */, 10)
)

assert.deepStrictEqual(
previoursThursday(new Date(2021, 5 /* Jun */, 15)),
previousThursday(new Date(2021, 5 /* Jun */, 15)),
new Date(2021, 5 /* Jun */, 10)
)

assert.deepStrictEqual(
previoursThursday(new Date(2021, 5 /* Jun */, 24)),
previousThursday(new Date(2021, 5 /* Jun */, 24)),
new Date(2021, 5 /* Jun */, 17)
)

})

it('returns `Invalid Date` if the given date is invalid', function () {
assert(previoursThursday(new Date(NaN)) instanceof Date)
assert(previousThursday(new Date(NaN)) instanceof Date)
})
})
Empty file modified src/previousTuesday/index.ts 100644 → 100755
Empty file.

0 comments on commit e10c1a8

Please sign in to comment.