import startOfWeek from '../startOfWeek/index' ; import toDate from '../toDate/index' ; import toInteger from '../_lib/toInteger/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; import type { WeekStartOptions , LocaleOptions , FirstWeekContainsDateOptions , } from '../types' ; name category summary description param { Date | Number } date param options param { Locale } options . locale - the locale object. See [Locale] https param { 0 | 1 | 2 | 3 | 4 | 5 | 6 } options . weekStartsOn param { 1 | 2 | 3 | 4 | 5 | 6 | 7 } options . firstWeekContainsDate returns { Number } throws throws throws example example example export default function getWeekYear ( dirtyDate , options ? ) { requiredArgs ( 1 , arguments ) ; const date = toDate ( dirtyDate ) ; const year = date . getFullYear ( ) ; const localeFirstWeekContainsDate = options ?. locale ?. options ?. firstWeekContainsDate ; const defaultFirstWeekContainsDate = localeFirstWeekContainsDate == null ? 1 : toInteger ( localeFirstWeekContainsDate ) ; const firstWeekContainsDate = options ?. firstWeekContainsDate == null ? defaultFirstWeekContainsDate : toInteger ( options . firstWeekContainsDate ) ; if ( ! ( firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7 ) ) { throw new RangeError ( 'firstWeekContainsDate must be between 1 and 7 inclusively' ) ; } const firstWeekOfNextYear = new Date ( 0 ) ; firstWeekOfNextYear . setFullYear ( year + 1 , 0 , firstWeekContainsDate ) ; firstWeekOfNextYear . setHours ( 0 , 0 , 0 , 0 ) ; const startOfNextYear = startOfWeek ( firstWeekOfNextYear , options ) ; const firstWeekOfThisYear = new Date ( 0 ) ; firstWeekOfThisYear . setFullYear ( year , 0 , firstWeekContainsDate ) ; firstWeekOfThisYear . setHours ( 0 , 0 , 0 , 0 ) ; const startOfThisYear = startOfWeek ( firstWeekOfThisYear , options ) ; if ( date . getTime ( ) >= startOfNextYear . getTime ( ) ) { return year + 1 ; } else if ( date . getTime ( ) >= startOfThisYear . getTime ( ) ) { return year ; } else { return year - 1 ; } }