Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move argument length requirement checks to a shared function to dedupe code #1563

Merged
merged 1 commit into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/_lib/getUTCDayOfYear/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import toDate from '../../toDate/index.js'
import requiredArgs from '../requiredArgs/index.js'

var MILLISECONDS_IN_DAY = 86400000

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function getUTCDayOfYear(dirtyDate) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(1, arguments)

var date = toDate(dirtyDate)
var timestamp = date.getTime()
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/getUTCISOWeek/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import toDate from '../../toDate/index.js'
import startOfUTCISOWeek from '../startOfUTCISOWeek/index.js'
import startOfUTCISOWeekYear from '../startOfUTCISOWeekYear/index.js'
import requiredArgs from '../requiredArgs/index.js'

var MILLISECONDS_IN_WEEK = 604800000

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function getUTCISOWeek(dirtyDate) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(1, arguments)

var date = toDate(dirtyDate)
var diff =
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/getUTCISOWeekYear/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import toDate from '../../toDate/index.js'
import startOfUTCISOWeek from '../startOfUTCISOWeek/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function getUTCISOWeekYear(dirtyDate) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(1, arguments)

var date = toDate(dirtyDate)
var year = date.getUTCFullYear()
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/getUTCWeek/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import toDate from '../../toDate/index.js'
import startOfUTCWeek from '../startOfUTCWeek/index.js'
import startOfUTCWeekYear from '../startOfUTCWeekYear/index.js'
import requiredArgs from '../requiredArgs/index.js'

var MILLISECONDS_IN_WEEK = 604800000

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function getUTCWeek(dirtyDate, options) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(1, arguments)

var date = toDate(dirtyDate)
var diff =
Expand Down
7 changes: 3 additions & 4 deletions src/_lib/getUTCWeekYear/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import toInteger from '../toInteger/index.js'
import toDate from '../../toDate/index.js'
import startOfUTCWeek from '../startOfUTCWeek/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function getUTCWeekYear (dirtyDate, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError('1 argument required, but only ' + arguments.length + ' present')
}
export default function getUTCWeekYear(dirtyDate, dirtyOptions) {
requiredArgs(1, arguments)

var date = toDate(dirtyDate, dirtyOptions)
var year = date.getUTCFullYear()
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/isSameUTCWeek/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import startOfUTCWeek from '../startOfUTCWeek/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function isSameUTCWeek(dirtyDateLeft, dirtyDateRight, options) {
if (arguments.length < 2) {
throw new TypeError(
'2 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var dateLeftStartOfWeek = startOfUTCWeek(dirtyDateLeft, options)
var dateRightStartOfWeek = startOfUTCWeek(dirtyDateRight, options)
Expand Down
9 changes: 9 additions & 0 deletions src/_lib/requiredArgs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function requiredArgs(required, args) {
if (args.length < required) {
throw new TypeError(
required + ' argument' + required > 1
? 's'
: '' + ' required, but only ' + args.length + ' present'
)
}
}
7 changes: 2 additions & 5 deletions src/_lib/setUTCDay/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import toInteger from '../toInteger/index.js'
import toDate from '../../toDate/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function setUTCDay(dirtyDate, dirtyDay, dirtyOptions) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var options = dirtyOptions || {}
var locale = options.locale
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/setUTCISODay/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import toInteger from '../toInteger/index.js'
import toDate from '../../toDate/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function setUTCISODay(dirtyDate, dirtyDay) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var day = toInteger(dirtyDay)

Expand Down
7 changes: 2 additions & 5 deletions src/_lib/setUTCISOWeek/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import toInteger from '../toInteger/index.js'
import toDate from '../../toDate/index.js'
import getUTCISOWeek from '../getUTCISOWeek/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function setUTCISOWeek(dirtyDate, dirtyISOWeek) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var date = toDate(dirtyDate)
var isoWeek = toInteger(dirtyISOWeek)
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/setUTCWeek/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import toInteger from '../toInteger/index.js'
import toDate from '../../toDate/index.js'
import getUTCWeek from '../getUTCWeek/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function setUTCWeek(dirtyDate, dirtyWeek, options) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var date = toDate(dirtyDate)
var week = toInteger(dirtyWeek)
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/startOfUTCISOWeek/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import toDate from '../../toDate/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function startOfUTCISOWeek(dirtyDate) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(1, arguments)

var weekStartsOn = 1

Expand Down
7 changes: 2 additions & 5 deletions src/_lib/startOfUTCISOWeekYear/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import getUTCISOWeekYear from '../getUTCISOWeekYear/index.js'
import startOfUTCISOWeek from '../startOfUTCISOWeek/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function startOfUTCISOWeekYear(dirtyDate) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(1, arguments)

var year = getUTCISOWeekYear(dirtyDate)
var fourthOfJanuary = new Date(0)
Expand Down
7 changes: 2 additions & 5 deletions src/_lib/startOfUTCWeek/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import toInteger from '../toInteger/index.js'
import toDate from '../../toDate/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function startOfUTCWeek(dirtyDate, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError(
'1 argument required, but only ' + arguments.length + ' present'
)
}
requiredArgs(1, arguments)

var options = dirtyOptions || {}
var locale = options.locale
Expand Down
7 changes: 3 additions & 4 deletions src/_lib/startOfUTCWeekYear/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import toInteger from '../toInteger/index.js'
import getUTCWeekYear from '../getUTCWeekYear/index.js'
import startOfUTCWeek from '../startOfUTCWeek/index.js'
import requiredArgs from '../requiredArgs/index.js'

// This function will be a part of public API when UTC function will be implemented.
// See issue: https://github.com/date-fns/date-fns/issues/376
export default function startOfUTCWeekYear (dirtyDate, dirtyOptions) {
if (arguments.length < 1) {
throw new TypeError('1 argument required, but only ' + arguments.length + ' present')
}
export default function startOfUTCWeekYear(dirtyDate, dirtyOptions) {
requiredArgs(1, arguments)

var options = dirtyOptions || {}
var locale = options.locale
Expand Down
7 changes: 2 additions & 5 deletions src/addBusinessDays/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import isWeekend from '../isWeekend/index.js'
import toDate from '../toDate/index.js'
import toInteger from '../_lib/toInteger/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

/**
* @name addBusinessDays
Expand All @@ -21,11 +22,7 @@ import toInteger from '../_lib/toInteger/index.js'
* //=> Mon Sep 15 2014 00:00:00 (skipped weekend days)
*/
export default function addBusinessDays(dirtyDate, dirtyAmount) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var date = toDate(dirtyDate)
var amount = toInteger(dirtyAmount)
Expand Down
7 changes: 2 additions & 5 deletions src/addDays/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import toInteger from '../_lib/toInteger/index.js'
import toDate from '../toDate/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

/**
* @name addDays
Expand All @@ -24,11 +25,7 @@ import toDate from '../toDate/index.js'
* //=> Thu Sep 11 2014 00:00:00
*/
export default function addDays(dirtyDate, dirtyAmount) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var date = toDate(dirtyDate)
var amount = toInteger(dirtyAmount)
Expand Down
7 changes: 2 additions & 5 deletions src/addHours/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import toInteger from '../_lib/toInteger/index.js'
import addMilliseconds from '../addMilliseconds/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

var MILLISECONDS_IN_HOUR = 3600000

Expand All @@ -26,11 +27,7 @@ var MILLISECONDS_IN_HOUR = 3600000
* //=> Fri Jul 11 2014 01:00:00
*/
export default function addHours(dirtyDate, dirtyAmount) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var amount = toInteger(dirtyAmount)
return addMilliseconds(dirtyDate, amount * MILLISECONDS_IN_HOUR)
Expand Down
7 changes: 2 additions & 5 deletions src/addISOWeekYears/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import toInteger from '../_lib/toInteger/index.js'
import getISOWeekYear from '../getISOWeekYear/index.js'
import setISOWeekYear from '../setISOWeekYear/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

/**
* @name addISOWeekYears
Expand Down Expand Up @@ -32,11 +33,7 @@ import setISOWeekYear from '../setISOWeekYear/index.js'
* //=> Fri Jun 26 2015 00:00:00
*/
export default function addISOWeekYears(dirtyDate, dirtyAmount) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var amount = toInteger(dirtyAmount)
return setISOWeekYear(dirtyDate, getISOWeekYear(dirtyDate) + amount)
Expand Down
7 changes: 2 additions & 5 deletions src/addMilliseconds/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import toInteger from '../_lib/toInteger/index.js'
import toDate from '../toDate/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

/**
* @name addMilliseconds
Expand All @@ -24,11 +25,7 @@ import toDate from '../toDate/index.js'
* //=> Thu Jul 10 2014 12:45:30.750
*/
export default function addMilliseconds(dirtyDate, dirtyAmount) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var timestamp = toDate(dirtyDate).getTime()
var amount = toInteger(dirtyAmount)
Expand Down
7 changes: 2 additions & 5 deletions src/addMinutes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import toInteger from '../_lib/toInteger/index.js'
import addMilliseconds from '../addMilliseconds/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

var MILLISECONDS_IN_MINUTE = 60000

Expand All @@ -26,11 +27,7 @@ var MILLISECONDS_IN_MINUTE = 60000
* //=> Thu Jul 10 2014 12:30:00
*/
export default function addMinutes(dirtyDate, dirtyAmount) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var amount = toInteger(dirtyAmount)
return addMilliseconds(dirtyDate, amount * MILLISECONDS_IN_MINUTE)
Expand Down
7 changes: 2 additions & 5 deletions src/addMonths/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import toInteger from '../_lib/toInteger/index.js'
import toDate from '../toDate/index.js'
import getDaysInMonth from '../getDaysInMonth/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

/**
* @name addMonths
Expand All @@ -25,11 +26,7 @@ import getDaysInMonth from '../getDaysInMonth/index.js'
* //=> Sun Feb 01 2015 00:00:00
*/
export default function addMonths(dirtyDate, dirtyAmount) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
requiredArgs(2, arguments)

var date = toDate(dirtyDate)
var amount = toInteger(dirtyAmount)
Expand Down