Skip to content

Commit

Permalink
Fix default argument value usage (#2423)
Browse files Browse the repository at this point in the history
Fixed a breaking change introduced by using modern default argument value syntax (see Hacker0x01/react-datepicker#2870).
  • Loading branch information
kossnocorp committed Apr 15, 2021
1 parent 535c979 commit 0f8d306
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/formatDuration/index.js
Expand Up @@ -7,7 +7,7 @@ const defaultFormat = [
'days',
'hours',
'minutes',
'seconds'
'seconds',
]

/**
Expand Down Expand Up @@ -73,21 +73,21 @@ const defaultFormat = [
* formatDuration({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' })
* //=> '2 years, 9 months, 3 weeks'
*/
export default function formatDuration(duration, options = {}) {
export default function formatDuration(duration, options) {
if (arguments.length < 1) {
throw new TypeError(
`1 argument required, but only ${arguments.length} present`
)
}

const format = options.format || defaultFormat
const locale = options.locale || defaultLocale
const zero = options.zero || false
const delimiter = options.delimiter || ' '
const format = options?.format || defaultFormat
const locale = options?.locale || defaultLocale
const zero = options?.zero || false
const delimiter = options?.delimiter || ' '

const result = format
.reduce((acc, unit) => {
const token = `x${unit.replace(/(^.)/, m => m.toUpperCase())}`
const token = `x${unit.replace(/(^.)/, (m) => m.toUpperCase())}`
const addChunk =
typeof duration[unit] === 'number' && (zero || duration[unit])
return addChunk
Expand Down
7 changes: 3 additions & 4 deletions src/getWeekYear/index.ts
Expand Up @@ -53,22 +53,21 @@ import {
*/
export default function getWeekYear(
dirtyDate: Date | number,
options: LocaleOptions & WeekStartOptions & FirstWeekContainsDateOptions = {}
options?: LocaleOptions & WeekStartOptions & FirstWeekContainsDateOptions
): number {
requiredArgs(1, arguments)

const date = toDate(dirtyDate)
const year = date.getFullYear()

const locale = options.locale
const localeFirstWeekContainsDate =
locale && locale.options && locale.options.firstWeekContainsDate
options?.locale?.options?.firstWeekContainsDate
const defaultFirstWeekContainsDate =
localeFirstWeekContainsDate == null
? 1
: toInteger(localeFirstWeekContainsDate)
const firstWeekContainsDate =
options.firstWeekContainsDate == null
options?.firstWeekContainsDate == null
? defaultFirstWeekContainsDate
: toInteger(options.firstWeekContainsDate)

Expand Down
5 changes: 4 additions & 1 deletion src/getWeeksInMonth/index.ts
Expand Up @@ -35,7 +35,10 @@ import { LocaleOptions, WeekStartOptions } from '../types'
* const result = getWeeksInMonth(new Date(2017, 6, 5), { weekStartsOn: 1 })
* //=> 6
*/
export default function getWeeksInMonth(date: Date | number, options: LocaleOptions & WeekStartOptions = {}): number {
export default function getWeeksInMonth(
date: Date | number,
options?: LocaleOptions & WeekStartOptions
): number {
requiredArgs(1, arguments)

return (
Expand Down

0 comments on commit 0f8d306

Please sign in to comment.