From 4c9db507fb9630c0223ba384be73d5b17c3cb1f7 Mon Sep 17 00:00:00 2001 From: Sasha Koss Date: Thu, 15 Apr 2021 15:11:03 +0800 Subject: [PATCH] Fix default argument value usage Fixed a breaking change introduced by using modern default argument value syntax (see https://github.com/Hacker0x01/react-datepicker/issues/2870). --- src/formatDuration/index.js | 14 +++++++------- src/getWeekYear/index.ts | 7 +++---- src/getWeeksInMonth/index.ts | 5 ++++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/formatDuration/index.js b/src/formatDuration/index.js index 8b39f59c59..c4130a6721 100644 --- a/src/formatDuration/index.js +++ b/src/formatDuration/index.js @@ -7,7 +7,7 @@ const defaultFormat = [ 'days', 'hours', 'minutes', - 'seconds' + 'seconds', ] /** @@ -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 diff --git a/src/getWeekYear/index.ts b/src/getWeekYear/index.ts index ce31d5d27e..649b8d1102 100644 --- a/src/getWeekYear/index.ts +++ b/src/getWeekYear/index.ts @@ -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) diff --git a/src/getWeeksInMonth/index.ts b/src/getWeeksInMonth/index.ts index b8da563e3b..288ca59348 100644 --- a/src/getWeeksInMonth/index.ts +++ b/src/getWeeksInMonth/index.ts @@ -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 (