Skip to content

Commit

Permalink
v2: arguments conversion (#452)
Browse files Browse the repository at this point in the history
* Add `dirty` prefix to uncleaned arguments

* Convert `parse` arguments

* Add missing properties to Options shared doc

* Argument conversion in private functions

* Add fallbacks to `parse`

* Remove `toDate` fallback in `parse`

* Make linter happy
  • Loading branch information
leshakoss committed Mar 8, 2017
1 parent 2e4e777 commit b763deb
Show file tree
Hide file tree
Showing 524 changed files with 1,241 additions and 298 deletions.
2 changes: 1 addition & 1 deletion dist/date_fns_docs.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/Options.js
Expand Up @@ -16,6 +16,10 @@
* @property {Locale} [locale=enLocale] - the locale object.
* Used by `distanceInWords`, `distanceInWordsStrict`, `format` and `parse`.
* See [Locale]{@link docs/Locale}
* @property {Boolean} [includeSeconds=false] - used by `distanceInWords`.
* If true, distances less than a minute are more detailed
* @property {Boolean} [addSuffix=false] - used by `distanceInWords` and `distanceInWordsStrinct`.
* If true, the result will indicate if the second date is earlier or later than the first
* @property {'s'|'m'|'h'|'d'|'M'|'Y'} [unit] - used by `distanceInWordsStrict`.
* If specified, will force a unit
* @property {'floor'|'ceil'|'round'} [partialMethod='floor'] - used by `distanceInWordsStrict`.
Expand Down
2 changes: 2 additions & 0 deletions src/addDays/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addHours/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addISOYears/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addMilliseconds/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addMinutes/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addMonths/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addQuarters/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addSeconds/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addWeeks/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/addYears/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
10 changes: 5 additions & 5 deletions src/areIntervalsOverlapping/index.js
Expand Up @@ -30,11 +30,11 @@ import toDate from '../toDate/index.js'
* )
* //=> false
*/
export default function areIntervalsOverlapping (dirtyIntervalLeft, dirtyIntervalRight, options) {
var leftStartTime = toDate(dirtyIntervalLeft.start, options).getTime()
var leftEndTime = toDate(dirtyIntervalLeft.end, options).getTime()
var rightStartTime = toDate(dirtyIntervalRight.start, options).getTime()
var rightEndTime = toDate(dirtyIntervalRight.end, options).getTime()
export default function areIntervalsOverlapping (dirtyIntervalLeft, dirtyIntervalRight, dirtyOptions) {
var leftStartTime = toDate(dirtyIntervalLeft.start, dirtyOptions).getTime()
var leftEndTime = toDate(dirtyIntervalLeft.end, dirtyOptions).getTime()
var rightStartTime = toDate(dirtyIntervalRight.start, dirtyOptions).getTime()
var rightEndTime = toDate(dirtyIntervalRight.end, dirtyOptions).getTime()

if (leftStartTime > leftEndTime || rightStartTime > rightEndTime) {
throw new Error('The start of an interval cannot be after its end')
Expand Down
2 changes: 2 additions & 0 deletions src/areIntervalsOverlapping/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/closestIndexTo/index.js
Expand Up @@ -24,15 +24,15 @@ import toDate from '../toDate/index.js'
* var result = closestIndexTo(dateToCompare, datesArray)
* //=> 1
*/
export default function closestIndexTo (dirtyDateToCompare, dirtyDatesArray, options) {
var dateToCompare = toDate(dirtyDateToCompare, options)
export default function closestIndexTo (dirtyDateToCompare, dirtyDatesArray, dirtyOptions) {
var dateToCompare = toDate(dirtyDateToCompare, dirtyOptions)
var timeToCompare = dateToCompare.getTime()

var result
var minDistance

dirtyDatesArray.forEach(function (dirtyDate, index) {
var currentDate = toDate(dirtyDate, options)
var currentDate = toDate(dirtyDate, dirtyOptions)
var distance = Math.abs(timeToCompare - currentDate.getTime())
if (result === undefined || distance < minDistance) {
result = index
Expand Down
2 changes: 2 additions & 0 deletions src/closestIndexTo/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/closestTo/index.js
Expand Up @@ -22,15 +22,15 @@ import toDate from '../toDate/index.js'
* ])
* //=> Tue Jan 01 2030 00:00:00
*/
export default function closestTo (dirtyDateToCompare, dirtyDatesArray, options) {
var dateToCompare = toDate(dirtyDateToCompare, options)
export default function closestTo (dirtyDateToCompare, dirtyDatesArray, dirtyOptions) {
var dateToCompare = toDate(dirtyDateToCompare, dirtyOptions)
var timeToCompare = dateToCompare.getTime()

var result
var minDistance

dirtyDatesArray.forEach(function (dirtyDate) {
var currentDate = toDate(dirtyDate, options)
var currentDate = toDate(dirtyDate, dirtyOptions)
var distance = Math.abs(timeToCompare - currentDate.getTime())
if (result === undefined || distance < minDistance) {
result = currentDate
Expand Down
2 changes: 2 additions & 0 deletions src/closestTo/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/compareAsc/index.js
Expand Up @@ -35,10 +35,10 @@ import toDate from '../toDate/index.js'
* // Sun Jul 02 1995 00:00:00
* // ]
*/
export default function compareAsc (dirtyDateLeft, dirtyDateRight, options) {
var dateLeft = toDate(dirtyDateLeft, options)
export default function compareAsc (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var timeLeft = dateLeft.getTime()
var dateRight = toDate(dirtyDateRight, options)
var dateRight = toDate(dirtyDateRight, dirtyOptions)
var timeRight = dateRight.getTime()

if (timeLeft < timeRight) {
Expand Down
2 changes: 2 additions & 0 deletions src/compareAsc/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/compareDesc/index.js
Expand Up @@ -35,10 +35,10 @@ import toDate from '../toDate/index.js'
* // Wed Feb 11 1987 00:00:00
* // ]
*/
export default function compareDesc (dirtyDateLeft, dirtyDateRight, options) {
var dateLeft = toDate(dirtyDateLeft, options)
export default function compareDesc (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var timeLeft = dateLeft.getTime()
var dateRight = toDate(dirtyDateRight, options)
var dateRight = toDate(dirtyDateRight, dirtyOptions)
var timeRight = dateRight.getTime()

if (timeLeft > timeRight) {
Expand Down
2 changes: 2 additions & 0 deletions src/compareDesc/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/differenceInCalendarDays/index.js
Expand Up @@ -25,9 +25,9 @@ var MILLISECONDS_IN_DAY = 86400000
* )
* //=> 366
*/
export default function differenceInCalendarDays (dirtyDateLeft, dirtyDateRight, options) {
var startOfDayLeft = startOfDay(dirtyDateLeft, options)
var startOfDayRight = startOfDay(dirtyDateRight, options)
export default function differenceInCalendarDays (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var startOfDayLeft = startOfDay(dirtyDateLeft, dirtyOptions)
var startOfDayRight = startOfDay(dirtyDateRight, dirtyOptions)

var timestampLeft = startOfDayLeft.getTime() -
startOfDayLeft.getTimezoneOffset() * MILLISECONDS_IN_MINUTE
Expand Down
2 changes: 2 additions & 0 deletions src/differenceInCalendarDays/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/differenceInCalendarISOWeeks/index.js
Expand Up @@ -26,9 +26,9 @@ var MILLISECONDS_IN_WEEK = 604800000
* )
* //=> 3
*/
export default function differenceInCalendarISOWeeks (dirtyDateLeft, dirtyDateRight, options) {
var startOfISOWeekLeft = startOfISOWeek(dirtyDateLeft, options)
var startOfISOWeekRight = startOfISOWeek(dirtyDateRight, options)
export default function differenceInCalendarISOWeeks (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var startOfISOWeekLeft = startOfISOWeek(dirtyDateLeft, dirtyOptions)
var startOfISOWeekRight = startOfISOWeek(dirtyDateRight, dirtyOptions)

var timestampLeft = startOfISOWeekLeft.getTime() -
startOfISOWeekLeft.getTimezoneOffset() * MILLISECONDS_IN_MINUTE
Expand Down
2 changes: 2 additions & 0 deletions src/differenceInCalendarISOWeeks/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
4 changes: 2 additions & 2 deletions src/differenceInCalendarISOYears/index.js
Expand Up @@ -23,6 +23,6 @@ import getISOYear from '../getISOYear/index.js'
* )
* //=> 2
*/
export default function differenceInCalendarISOYears (dirtyDateLeft, dirtyDateRight, options) {
return getISOYear(dirtyDateRight, options) - getISOYear(dirtyDateLeft, options)
export default function differenceInCalendarISOYears (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
return getISOYear(dirtyDateRight, dirtyOptions) - getISOYear(dirtyDateLeft, dirtyOptions)
}
2 changes: 2 additions & 0 deletions src/differenceInCalendarISOYears/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/differenceInCalendarMonths/index.js
Expand Up @@ -21,9 +21,9 @@ import toDate from '../toDate/index.js'
* )
* //=> 8
*/
export default function differenceInCalendarMonths (dirtyDateLeft, dirtyDateRight, options) {
var dateLeft = toDate(dirtyDateLeft, options)
var dateRight = toDate(dirtyDateRight, options)
export default function differenceInCalendarMonths (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var dateRight = toDate(dirtyDateRight, dirtyOptions)

var yearDiff = dateRight.getFullYear() - dateLeft.getFullYear()
var monthDiff = dateRight.getMonth() - dateLeft.getMonth()
Expand Down
2 changes: 2 additions & 0 deletions src/differenceInCalendarMonths/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
8 changes: 4 additions & 4 deletions src/differenceInCalendarQuarters/index.js
Expand Up @@ -22,12 +22,12 @@ import toDate from '../toDate/index.js'
* )
* //=> 3
*/
export default function differenceInCalendarQuarters (dirtyDateLeft, dirtyDateRight, options) {
var dateLeft = toDate(dirtyDateLeft, options)
var dateRight = toDate(dirtyDateRight, options)
export default function differenceInCalendarQuarters (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var dateRight = toDate(dirtyDateRight, dirtyOptions)

var yearDiff = dateRight.getFullYear() - dateLeft.getFullYear()
var quarterDiff = getQuarter(dateRight, options) - getQuarter(dateLeft, options)
var quarterDiff = getQuarter(dateRight, dirtyOptions) - getQuarter(dateLeft, dirtyOptions)

return yearDiff * 4 + quarterDiff
}
2 changes: 2 additions & 0 deletions src/differenceInCalendarQuarters/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
2 changes: 2 additions & 0 deletions src/differenceInCalendarWeeks/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
6 changes: 3 additions & 3 deletions src/differenceInCalendarYears/index.js
Expand Up @@ -21,9 +21,9 @@ import toDate from '../toDate/index.js'
* )
* //=> 2
*/
export default function differenceInCalendarYears (dirtyDateLeft, dirtyDateRight, options) {
var dateLeft = toDate(dirtyDateLeft, options)
var dateRight = toDate(dirtyDateRight, options)
export default function differenceInCalendarYears (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var dateRight = toDate(dirtyDateRight, dirtyOptions)

return dateRight.getFullYear() - dateLeft.getFullYear()
}
2 changes: 2 additions & 0 deletions src/differenceInCalendarYears/index.js.flow
Expand Up @@ -10,6 +10,8 @@ type Options = {
weekStartsOn?: number,
additionalDigits?: 0 | 1 | 2,
locale?: Locale,
includeSeconds?: boolean,
addSuffix?: boolean,
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y',
partialMethod?: 'floor' | 'ceil' | 'round'
}
Expand Down
12 changes: 6 additions & 6 deletions src/differenceInDays/index.js
Expand Up @@ -24,16 +24,16 @@ import compareDesc from '../compareDesc/index.js'
* )
* //=> 365
*/
export default function differenceInDays (dirtyDateLeft, dirtyDateRight, options) {
var dateLeft = toDate(dirtyDateLeft, options)
var dateRight = toDate(dirtyDateRight, options)
export default function differenceInDays (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var dateRight = toDate(dirtyDateRight, dirtyOptions)

var sign = compareDesc(dateLeft, dateRight, options)
var difference = Math.abs(differenceInCalendarDays(dateLeft, dateRight, options))
var sign = compareDesc(dateLeft, dateRight, dirtyOptions)
var difference = Math.abs(differenceInCalendarDays(dateLeft, dateRight, dirtyOptions))

// Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full
// If so, result must be decreased by 1 in absolute value
dateRight.setDate(dateRight.getDate() - sign * difference)
var isLastDayNotFull = compareDesc(dateLeft, dateRight, options) === -sign
var isLastDayNotFull = compareDesc(dateLeft, dateRight, dirtyOptions) === -sign
return sign * (difference - isLastDayNotFull)
}

0 comments on commit b763deb

Please sign in to comment.