Skip to content

Commit

Permalink
Followup fixes for the weekend helpers (#959)
Browse files Browse the repository at this point in the history
- Format code and examples using Prettier
- Adjust the change log entry
- Remove redundant getWeekendsBetween
- Rebuild the package
- Fix Flow typings
  • Loading branch information
kossnocorp committed Oct 29, 2018
1 parent 9bfa303 commit 721a722
Show file tree
Hide file tree
Showing 43 changed files with 934 additions and 352 deletions.
17 changes: 8 additions & 9 deletions CHANGELOG.md
Expand Up @@ -15,14 +15,6 @@ for the list of changes made since `v2.0.0-alpha.1`.

### Added

- New interval, month, and year helpers to fetch a list of all Saturdays and Sundays (weekends) for a given date interval. `eachWeekendOfInterval` is the handler function while the other two are wrapper functions.

- `eachWeekendOfInterval`

- `eachWeekendOfMonth`

- `eachWeekendOfYear`

- FP functions like those in [lodash](https://github.com/lodash/lodash/wiki/FP-Guide),
that support [currying](https://en.wikipedia.org/wiki/Currying), and, as a consequence,
functional-style [function composing](https://medium.com/making-internets/why-using-chain-is-a-mistake-9bc1f80d51ba).
Expand Down Expand Up @@ -141,7 +133,6 @@ for the list of changes made since `v2.0.0-alpha.1`.

- [fi locale is updated for v2 format](https://github.com/date-fns/date-fns/pull/775). Kudos to [@sjuvonen](https://github.com/sjuvonen).


- New locale-dependent week-numbering year helpers:

- `getWeek`
Expand Down Expand Up @@ -172,6 +163,14 @@ for the list of changes made since `v2.0.0-alpha.1`.

- Added new function `fromUnixTime`. Thansk to [@xkizer](https://github.com/xkizer).

- New interval, month, and year helpers to fetch a list of all Saturdays and Sundays (weekends) for a given date interval. `eachWeekendOfInterval` is the handler function while the other two are wrapper functions. Kudos to [@laekettavong](https://github.com/laekettavong)!

- `eachWeekendOfInterval`

- `eachWeekendOfMonth`

- `eachWeekendOfYear`

### Changed

- **BREAKING**: new format string API for `format` function
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -48,7 +48,7 @@
"babel-preset-power-assert": "^1.0.0",
"cloc": "^2.2.0",
"firebase": "^3.7.1",
"flow-bin": "0.72",
"flow-bin": "0.84.0",
"fs-promise": "^1.0.0",
"glob-promise": "^2.0.0",
"gzip-size-cli": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/eachWeekendOfInterval/index.d.ts
@@ -1,4 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import {eachWeekendOfInterval} from 'date-fns'
import { eachWeekendOfInterval } from 'date-fns'
export = eachWeekendOfInterval
54 changes: 25 additions & 29 deletions src/eachWeekendOfInterval/index.js
Expand Up @@ -17,52 +17,48 @@ import isWeekend from '../isWeekend/index.js'
* @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
* @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @returns {Array} an array containing all the Saturdays and Sundays
* @returns {Date[]} an array containing all the Saturdays and Sundays
* @throws {TypeError} 1 argument required
* @throws {RangeError} The start of an interval cannot be after its end
* @throws {RangeError} Date in interval cannot be `Invalid Date`
*
* @example
* // Lists all Saturdays and Sundays in the given date interval
* var result = eachWeekendOfInterval(
* {start: new Date(2022, 8, 17),
* var result = eachWeekendOfInterval({
* start: new Date(2022, 8, 17),
* end: new Date(2022, 8, 30)
* },
* {weekStartsOn: 2}
* )
* //=> ['Sat Sep 17 2022', 'Sun Sep 18 2022', 'Sat Sep 24 2022', 'Sun Sep 25 2022']
* })
* //=> [
* 2022-09-17T22:00:00.000Z,
* 2022-09-23T22:00:00.000Z,
* 2022-09-24T22:00:00.000Z
* ]
*
* @example
* // Lists all Saturdays and Sundays in the given date interval
* var result = eachWeekendOfInterval(
* {start: new Date(2016, 2, 5),
* end: new Date(2016, 2, 19)
* },
* {weekStartsOn: 2}
* )
* //=> ['Sat Mar 05 2016', 'Sun Mar 06 2016, 'Sat Mar 12 2016', 'Sun Mar 13 2016', 'Sat Mar 19 2016']
*
* @example
* // Lists all Saturdays and Sundays in the given date interval
* var result = eachWeekendOfInterval(
* {start: new Date(2016, 2, 25),
* var result = eachWeekendOfInterval({
* start: new Date(2016, 2, 25),
* end: new Date(2016, 2, 5)
* },
* {weekStartsOn: 2}
* )
* //=> RangeError
* })
* //=> RangeError: Invalid interval
*/

export default function eachWeekendOfInterval (dirtyInterval, dirtyOptions) {
export default function eachWeekendOfInterval(dirtyInterval, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError('1 argument required, but only ' + arguments.length + ' present')
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}

var options = dirtyOptions || {}
var locale = options.locale
var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn
var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : toInteger(localeWeekStartsOn)
var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : toInteger(options.weekStartsOn)
var localeWeekStartsOn =
locale && locale.options && locale.options.weekStartsOn
var defaultWeekStartsOn =
localeWeekStartsOn == null ? 0 : toInteger(localeWeekStartsOn)
var weekStartsOn =
options.weekStartsOn == null
? defaultWeekStartsOn
: toInteger(options.weekStartsOn)

// Test if weekStartsOn is between 0 and 6 _and_ is not NaN
if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
Expand Down
8 changes: 3 additions & 5 deletions src/eachWeekendOfInterval/index.js.flow
Expand Up @@ -14,7 +14,8 @@ type Options = {
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year',
roundingMethod?: 'floor' | 'ceil' | 'round'
roundingMethod?: 'floor' | 'ceil' | 'round',
awareOfUnicodeTokens?: boolean
}

type Locale = {
Expand Down Expand Up @@ -46,7 +47,4 @@ type Locale = {
}
}

declare module.exports: (
interval: Interval,
options?: Options
) => Array
declare module.exports: (interval: Interval, options?: Options) => Date[]
72 changes: 41 additions & 31 deletions src/eachWeekendOfInterval/test.js
Expand Up @@ -4,143 +4,153 @@
import assert from 'power-assert'
import eachWeekendOfInterval from '.'

describe('eachWeekendOfInterval', function () {
it('returns array length of 4', function () {
describe('eachWeekendOfInterval', function() {
it('returns array length of 4', function() {
var result = eachWeekendOfInterval({
start: new Date(2018, 8 /* Sept */, 17),
end: new Date(2018, 8 /* Sept */, 30)
})
assert(result.length === 4)
})

it('returns array length of 4 with date.toISOString()', function () {
it('returns array length of 4 with date.toISOString()', function() {
var result = eachWeekendOfInterval({
start: new Date(2018, 8 /* Sept */, 17).toISOString(),
end: new Date(2018, 8 /* Sept */, 30).toISOString()
})
assert(result.length === 4)
})

it('returns array length of 4 when date strings are used', function () {
it('returns array length of 4 when date strings are used', function() {
var result = eachWeekendOfInterval({
start: new Date('September 17, 2019'),
end: new Date('September 30, 2019')
})
assert(result.length === 4)
})

it('the first Saturday returned is Sat Sep 22 2018', function () {
it('the first Saturday returned is Sat Sep 22 2018', function() {
var result = eachWeekendOfInterval({
start: new Date(2018, 8 /* Sept */, 17),
end: new Date(2018, 8 /* Sept */, 30)
})
assert(result[0].toDateString() === 'Sat Sep 22 2018')
})

it('the first Sunday returned is Sun Sep 23 2018', function () {
it('the first Sunday returned is Sun Sep 23 2018', function() {
var result = eachWeekendOfInterval({
start: new Date(2018, 8, 17),
end: new Date(2018, 8, 30)
})
assert(result[1].toDateString() === 'Sun Sep 23 2018')
})

it('the second Saturday returned is Sat Sep 29 2018', function () {
it('the second Saturday returned is Sat Sep 29 2018', function() {
var result = eachWeekendOfInterval({
start: new Date(2018, 8 /* Sept */, 17),
end: new Date(2018, 8 /* Sept */, 30)
})
assert(result[2].toDateString() === 'Sat Sep 29 2018')
})

it('the second Sunday returned is Sun Sep 30 2018', function () {
it('the second Sunday returned is Sun Sep 30 2018', function() {
var result = eachWeekendOfInterval({
start: new Date(2018, 8 /* Sept */, 17),
end: new Date(2018, 8 /* Sept */, 30)
})
assert(result[3].toDateString() === 'Sun Sep 30 2018')
})

it('returns array length of 104', function () {
it('returns array length of 104', function() {
var result = eachWeekendOfInterval({
start: new Date(2019, 0 /* Jan */, 1),
end: new Date(2019, 11 /* Dec */, 31)
})
assert(result.length === 104)
})

it('returns Sun Mar 17 2019', function () {
it('returns Sun Mar 17 2019', function() {
var result = eachWeekendOfInterval({
start: new Date(2019, 0 /* Jan */, 1),
end: new Date(2019, 11 /* Dec */, 31)
})
assert(result[21].toDateString() === 'Sun Mar 17 2019')
})

it('returns Sat Oct 26 2019', function () {
it('returns Sat Oct 26 2019', function() {
var result = eachWeekendOfInterval({
start: new Date(2019, 0 /* Jan */, 1),
end: new Date(2019, 11 /* Dec */, 31)
})
assert(result[84].toDateString() === 'Sat Oct 26 2019')
})

it('throws `RangeError` invalid interval start date is used', function () {
it('throws `RangeError` invalid interval start date is used', function() {
// $ExpectedMistake
var block = eachWeekendOfInterval.bind(null,
var block = eachWeekendOfInterval.bind(
null,
{
start: new Date(NaN),
end: new Date(2019, 11 /* Dec */, 31)
},
{ weekStartsOn: 6 })
{ weekStartsOn: 6 }
)
assert.throws(block, RangeError)
})

it('throws `RangeError` invalid interval end date is used', function () {
it('throws `RangeError` invalid interval end date is used', function() {
// $ExpectedMistake
var block = eachWeekendOfInterval.bind(null,
var block = eachWeekendOfInterval.bind(
null,
{
start: new Date(2019, 0 /* Jan */, 1),
end: new Date(NaN)
},
{ weekStartsOn: 6 })
{ weekStartsOn: 6 }
)
assert.throws(block, RangeError)
})

it('throws TypeError exception if passed less than 1 argument', function () {
assert.throws(eachWeekendOfInterval.bind(), TypeError)
it('throws TypeError exception if passed less than 1 argument', function() {
assert.throws(eachWeekendOfInterval, TypeError)
})

it('throws `RangeError` if `options.weekStartsOn` is not convertable to 0, 1, ..., 6 or undefined', function () {
// $ExpectedMistake
var block = eachWeekendOfInterval.bind(null,
it('throws `RangeError` if `options.weekStartsOn` is not convertable to 0, 1, ..., 6 or undefined', function() {
var block = eachWeekendOfInterval.bind(
null,
// $ExpectedMistake
{
start: new Date(2018, 8 /* Sept */, 17),
end: new Date(2018, 8 /* Sept */, 30)
},
{ weekStartsOn: 9 })
{ weekStartsOn: 9 }
)
assert.throws(block, RangeError)
})

it('throws `RangeError` if `options.weekStartsOn` when passing in invalid value', function () {
// $ExpectedMistake
var block = eachWeekendOfInterval.bind(null,
it('throws `RangeError` if `options.weekStartsOn` when passing in invalid value', function() {
var block = eachWeekendOfInterval.bind(
null,
// $ExpectedMistake
{
start: new Date(2018, 8 /* Sept */, 17),
end: new Date(2018, 8 /* Sept */, 30)
},
{ weekStartsOn: NaN })
{ weekStartsOn: NaN }
)
assert.throws(block, RangeError)
})

it('throws `RangeError` if start of an interval is after its end', function () {
// $ExpectedMistake
var block = eachWeekendOfInterval.bind(null,
it('throws `RangeError` if start of an interval is after its end', function() {
var block = eachWeekendOfInterval.bind(
null,
// $ExpectedMistake
{
start: new Date(2018, 8 /* Sept */, 25),
end: new Date(2018, 8 /* Sept */, 6)
},
{ weekStartsOn: NaN })
{ weekStartsOn: NaN }
)
assert.throws(block, RangeError)
})
})
2 changes: 1 addition & 1 deletion src/eachWeekendOfMonth/index.d.ts
@@ -1,4 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import {eachWeekendOfMonth} from 'date-fns'
import { eachWeekendOfMonth } from 'date-fns'
export = eachWeekendOfMonth
13 changes: 9 additions & 4 deletions src/eachWeekendOfMonth/index.js
Expand Up @@ -13,16 +13,21 @@ import endOfMonth from '../endOfMonth/index.js'
* @param {Date|String|Number} date - the given month
* @param {Options} [options] - the object with options. See [Options]{@link https://date-fns.org/docs/Options}
* @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
* @returns {Array} an array containing all the Saturdays and Sundays
* @returns {Date[]} an array containing all the Saturdays and Sundays
* @throws {TypeError} 1 argument required
*
* @example
* // Lists all Saturdays and Sundays in the given month
* var result = eachWeekendOfMonth(new Date(2020, 1, 1))
* //=> ['Sat Feb 01 2020', 'Sun Feb 02 2020', 'Sat Feb 08 2020', ... , 'Sat Feb 22 2020', 'Sun Feb 23 2020', 'Sat Feb 29 2020']
* //=> [
* 2020-02-01T23:00:00.000Z,
* 2020-02-07T23:00:00.000Z,
* 2020-02-08T23:00:00.000Z,
* ...
* 2020-02-28T23:00:00.000Z
* ]
*/

export default function eachWeekendOfMonth (dirtyDate, dirtyOptions) {
export default function eachWeekendOfMonth(dirtyDate, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError(
'1 arguments required, but only ' + arguments.length + ' present'
Expand Down
5 changes: 3 additions & 2 deletions src/eachWeekendOfMonth/index.js.flow
Expand Up @@ -14,7 +14,8 @@ type Options = {
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year',
roundingMethod?: 'floor' | 'ceil' | 'round'
roundingMethod?: 'floor' | 'ceil' | 'round',
awareOfUnicodeTokens?: boolean
}

type Locale = {
Expand Down Expand Up @@ -49,4 +50,4 @@ type Locale = {
declare module.exports: (
date: Date | string | number,
options?: Options
) => Array
) => Date[]

0 comments on commit 721a722

Please sign in to comment.