Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new feature differenceInBusinessDays
- Loading branch information
Showing
15 changed files
with
525 additions
and
200 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,4 @@ | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
import { differenceInBusinessDays } from 'date-fns' | ||
export default differenceInBusinessDays |
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,50 @@ | ||
import toDate from '../toDate/index.js' | ||
import eachDayOfInterval from '../eachDayOfInterval/index.js' | ||
import isWeekend from '../isWeekend/index.js' | ||
|
||
/** | ||
* @name differenceInBusinessDays | ||
* @category Day Helpers | ||
* @summary Get the number of business days between the given dates. | ||
* | ||
* @description | ||
* Get the number of business day periods between the given dates. | ||
* Business days being days that arent in the weekend. | ||
* | ||
* @param {Date|Number} dateLeft - the later date | ||
* @param {Date|Number} dateRight - the earlier date | ||
* @returns {Number} the number of business days | ||
* @throws {TypeError} 2 arguments required | ||
* | ||
* @example | ||
* // How many business days are between | ||
* // 10 Januari 2014 and 20 July 2014? | ||
* var result = differenceInBusinessDays( | ||
* new Date(2014, 0, 10) | ||
* new Date(2014, 6, 20), | ||
* ) | ||
* //=> 136 | ||
*/ | ||
export default function differenceInBusinessDays( | ||
dirtyDateLeft, | ||
dirtyDateRight | ||
) { | ||
if (arguments.length < 2) { | ||
throw new TypeError( | ||
'2 arguments required, but only ' + arguments.length + ' present' | ||
) | ||
} | ||
|
||
var dateLeft = toDate(dirtyDateLeft) | ||
var dateRight = toDate(dirtyDateRight) | ||
var interval = { start: dateLeft, end: dateRight } | ||
|
||
var daysOfInterval = eachDayOfInterval(interval) | ||
var difference = daysOfInterval.filter(function(day) { | ||
return !isWeekend(toDate(day)) | ||
}) | ||
var result = difference.length | ||
|
||
// Prevent negative zero | ||
return difference.length === 0 ? 0 : difference.length | ||
} |
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,41 @@ | ||
// @flow | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
type Interval = { | ||
start: Date | number, | ||
end: Date | number | ||
} | ||
|
||
type Locale = { | ||
formatDistance: Function, | ||
formatRelative: Function, | ||
localize: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
formatLong: Object, | ||
date: Function, | ||
time: Function, | ||
dateTime: Function, | ||
match: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
options?: { | ||
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, | ||
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | ||
} | ||
} | ||
|
||
declare module.exports: ( | ||
dateLeft: Date | number, | ||
dateRight: Date | number | ||
) => number |
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,49 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'power-assert' | ||
import differenceInBusinessDays from '.' | ||
|
||
describe('differenceInBusinessDays', function() { | ||
it('returns the number of business days between the given dates, excluding weekends', function() { | ||
var result = differenceInBusinessDays( | ||
new Date(2014, 0 /* Jan */, 10), | ||
new Date(2014, 6 /* Jul */, 20) | ||
) | ||
assert(result === 136) | ||
}) | ||
|
||
it('accepts timestamps', function() { | ||
var result = differenceInBusinessDays( | ||
new Date(2014, 0, 10).getTime(), | ||
new Date(2014, 6, 20).getTime() | ||
) | ||
assert(result === 136) | ||
}) | ||
|
||
describe('edge cases', function() { | ||
it('counts endDate, even when date is the same', function() { | ||
var result = differenceInBusinessDays( | ||
new Date(2014, 8 /* Sep */, 5, 0, 0), | ||
new Date(2014, 8 /* Sep */, 5, 0, 0) | ||
) | ||
assert(result === 1) | ||
}) | ||
|
||
it('throws RangeError when start date is after end date', function() { | ||
assert.throws( | ||
differenceInBusinessDays.bind( | ||
null, | ||
new Date(2014, 6, 20).getTime(), | ||
new Date(2014, 0, 10).getTime() | ||
), | ||
RangeError | ||
) | ||
}) | ||
}) | ||
|
||
it('throws TypeError exception if passed less than 2 arguments', function() { | ||
assert.throws(differenceInBusinessDays.bind(null), TypeError) | ||
assert.throws(differenceInBusinessDays.bind(null, 1), TypeError) | ||
}) | ||
}) |
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
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,4 @@ | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
import { differenceInBusinessDays } from 'date-fns/fp' | ||
export default differenceInBusinessDays |
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,8 @@ | ||
// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it. | ||
|
||
import fn from '../../differenceInBusinessDays/index.js' | ||
import convertToFP from '../_lib/convertToFP/index.js' | ||
|
||
var differenceInBusinessDays = convertToFP(fn, 2) | ||
|
||
export default differenceInBusinessDays |
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 | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
type Interval = { | ||
start: Date | number, | ||
end: Date | number | ||
} | ||
|
||
type Locale = { | ||
formatDistance: Function, | ||
formatRelative: Function, | ||
localize: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
formatLong: Object, | ||
date: Function, | ||
time: Function, | ||
dateTime: Function, | ||
match: { | ||
ordinalNumber: Function, | ||
era: Function, | ||
quarter: Function, | ||
month: Function, | ||
day: Function, | ||
dayPeriod: Function | ||
}, | ||
options?: { | ||
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, | ||
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | ||
} | ||
} | ||
|
||
type CurriedFn1<A, R> = <A>(a: A) => R | ||
|
||
type CurriedFn2<A, B, R> = <A>( | ||
a: A | ||
) => CurriedFn1<B, R> | (<A, B>(a: A, b: B) => R) | ||
|
||
declare module.exports: CurriedFn2<Date | number, Date | number, number> |
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
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
Oops, something went wrong.