diff options
Diffstat (limited to 'date-fns/src/subBusinessDays')
-rw-r--r-- | date-fns/src/subBusinessDays/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/subBusinessDays/index.js | 28 | ||||
-rw-r--r-- | date-fns/src/subBusinessDays/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/subBusinessDays/test.js | 63 |
4 files changed, 147 insertions, 0 deletions
diff --git a/date-fns/src/subBusinessDays/index.d.ts b/date-fns/src/subBusinessDays/index.d.ts new file mode 100644 index 0000000..7a0549a --- /dev/null +++ b/date-fns/src/subBusinessDays/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { subBusinessDays } from 'date-fns' +export default subBusinessDays diff --git a/date-fns/src/subBusinessDays/index.js b/date-fns/src/subBusinessDays/index.js new file mode 100644 index 0000000..dbfa505 --- /dev/null +++ b/date-fns/src/subBusinessDays/index.js @@ -0,0 +1,28 @@ +import toInteger from '../_lib/toInteger/index' +import addBusinessDays from '../addBusinessDays/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name subBusinessDays + * @category Day Helpers + * @summary Substract the specified number of business days (mon - fri) to the given date. + * + * @description + * Substract the specified number of business days (mon - fri) to the given date, ignoring weekends. + * + * @param {Date|Number} date - the date to be changed + * @param {Number} amount - the amount of business days to be subtracted. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. + * @returns {Date} the new date with the business days subtracted + * @throws {TypeError} 2 arguments required + * + * @example + * // Substract 10 business days from 1 September 2014: + * var result = subBusinessDays(new Date(2014, 8, 1), 10) + * //=> Mon Aug 18 2014 00:00:00 (skipped weekend days) + */ +export default function subBusinessDays(dirtyDate, dirtyAmount) { + requiredArgs(2, arguments) + + var amount = toInteger(dirtyAmount) + return addBusinessDays(dirtyDate, -amount) +} diff --git a/date-fns/src/subBusinessDays/index.js.flow b/date-fns/src/subBusinessDays/index.js.flow new file mode 100644 index 0000000..f568f0d --- /dev/null +++ b/date-fns/src/subBusinessDays/index.js.flow @@ -0,0 +1,52 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +export type Interval = { + start: Date | number, + end: Date | number, +} + +export type Locale = { + code?: string, + formatDistance?: (...args: Array<any>) => any, + formatRelative?: (...args: Array<any>) => any, + localize?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + formatLong?: { + date: (...args: Array<any>) => any, + time: (...args: Array<any>) => any, + dateTime: (...args: Array<any>) => any, + }, + match?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + }, +} + +export type Duration = { + years?: number, + months?: number, + weeks?: number, + days?: number, + hours?: number, + minutes?: number, + seconds?: number, +} + +export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6 + +declare module.exports: (date: Date | number, amount: number) => Date diff --git a/date-fns/src/subBusinessDays/test.js b/date-fns/src/subBusinessDays/test.js new file mode 100644 index 0000000..96564a1 --- /dev/null +++ b/date-fns/src/subBusinessDays/test.js @@ -0,0 +1,63 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import subBusinessDays from '.' + +describe('subBusinessDays', function() { + it('substract the given number of business days', function() { + const result = subBusinessDays(new Date(2014, 8 /* Sep */, 1), 10) + assert.deepEqual(result, new Date(2014, 7 /* Aug */, 18)) + }) + + it('handles negative amount', function() { + var result = subBusinessDays(new Date(2014, 7 /* Sep */, 18), -10) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1)) + }) + + it('can handle a large number of business days', function() { + if (typeof this.timeout === 'function') { + this.timeout(500 /* 500 ms test timeout */) + } + + var result = subBusinessDays(new Date(15000, 0 /* Jan */, 1), 3387885) + assert.deepEqual(result, new Date(2014, 0 /* Jan */, 1)) + }) + + it('accepts a timestamp', function() { + var result = subBusinessDays(new Date(2014, 8 /* Sep */, 1).getTime(), 10) + assert.deepEqual(result, new Date(2014, 7 /* Aug */, 18)) + }) + + it('converts a fractional number to an integer', function() { + var result = subBusinessDays(new Date(2014, 8 /* Sep */, 1), 10.5) + assert.deepEqual(result, new Date(2014, 7 /* Aug */, 18)) + }) + + it('implicitly converts number arguments', function() { + // $ExpectedMistake + var result = subBusinessDays(new Date(2014, 8 /* Sep */, 1), '10') + assert.deepEqual(result, new Date(2014, 7 /* Aug */, 18)) + }) + + it('does not mutate the original date', function() { + var date = new Date(2014, 8 /* Sep */, 1) + subBusinessDays(date, 11) + assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1)) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + var result = subBusinessDays(new Date(NaN), 10) + assert(result instanceof Date && isNaN(result)) + }) + + it('returns `Invalid Date` if the given amount is NaN', function() { + var result = subBusinessDays(new Date(2014, 8 /* Sep */, 1), NaN) + assert(result instanceof Date && isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + assert.throws(subBusinessDays.bind(null), TypeError) + assert.throws(subBusinessDays.bind(null, 1), TypeError) + }) +}) |