import type { LocaleOptions , WeekStartOptions } from '../types' ; import toDate from '../toDate/index' ; import toInteger from '../_lib/toInteger/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; 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 returns { Date } throws throws example example export default function startOfWeek ( dirtyDate , dirtyOptions ? ) { requiredArgs ( 1 , arguments ) ; const options = dirtyOptions || { } ; const locale = options . locale ; const localeWeekStartsOn = locale && locale . options && locale . options . weekStartsOn ; const defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : toInteger ( localeWeekStartsOn ) ; const weekStartsOn = options . weekStartsOn == null ? defaultWeekStartsOn : toInteger ( options . weekStartsOn ) ; if ( ! ( weekStartsOn >= 0 && weekStartsOn <= 6 ) ) { throw new RangeError ( 'weekStartsOn must be between 0 and 6 inclusively' ) ; } const date = toDate ( dirtyDate ) ; const day = date . getDay ( ) ; const diff = ( day < weekStartsOn ? 7 : 0 ) + day - weekStartsOn ; date . setDate ( date . getDate ( ) - diff ) ; date . setHours ( 0 , 0 , 0 , 0 ) ; return date ; }