Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding previoursDay and its variations
- Loading branch information
Lucas Silva
committed
Jun 18, 2021
1 parent
ea3f050
commit 7ef7d9b
Showing
17 changed files
with
619 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(2020, 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) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Oops, something went wrong.