Skip to content

Commit

Permalink
Move argument length requirement checks to a shared function to dedup…
Browse files Browse the repository at this point in the history
…e code.

I noticed in a compiled bundle that there were a lot of duplicated strings which were used for argument length validation, so I was interested to see what effect deduplicating these would have on bundle size.

Bundle | Bytes
-------|--------
Before | `25474`
Ater   | `24528`

That's a reduction of 946 bytes (3.71%) from a complete minified bundle.
  • Loading branch information
levibuzolic committed Jan 4, 2020
1 parent b69dfac commit b647987
Show file tree
Hide file tree
Showing 190 changed files with 398 additions and 950 deletions.
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
16 changes: 8 additions & 8 deletions src/_lib/getUTCWeekYear/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
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()

var options = dirtyOptions || {}
var locale = options.locale
var localeFirstWeekContainsDate = locale &&
locale.options &&
locale.options.firstWeekContainsDate
var localeFirstWeekContainsDate =
locale && locale.options && locale.options.firstWeekContainsDate
var defaultFirstWeekContainsDate =
localeFirstWeekContainsDate == null
? 1
Expand All @@ -28,7 +26,9 @@ export default function getUTCWeekYear (dirtyDate, dirtyOptions) {

// Test if weekStartsOn is between 1 and 7 _and_ is not NaN
if (!(firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7)) {
throw new RangeError('firstWeekContainsDate must be between 1 and 7 inclusively')
throw new RangeError(
'firstWeekContainsDate must be between 1 and 7 inclusively'
)
}

var firstWeekOfNextYear = new Date(0)
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
12 changes: 5 additions & 7 deletions src/_lib/startOfUTCWeekYear/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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
var localeFirstWeekContainsDate = locale &&
locale.options &&
locale.options.firstWeekContainsDate
var localeFirstWeekContainsDate =
locale && locale.options && locale.options.firstWeekContainsDate
var defaultFirstWeekContainsDate =
localeFirstWeekContainsDate == null
? 1
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

0 comments on commit b647987

Please sign in to comment.