File

lib/core/src/items/schedules/schedule.class.ts

Extends

Item

Index

Properties
Methods
Accessors

Constructor

constructor(config?: ScheduleConfig)
Parameters :
Name Type Optional
config ScheduleConfig Yes

Properties

Static acceptsAsConfig
Default value : isScheduleConfig
Public daysOfWeek
Type : number[]
Static timeOfDay
Default value : new TimeOfDay([ //sunday: { 'morning': 10, 'afternoon': 13, 'evening': 18, 'night': 23 }, //weekdays: ...Array(5).fill({ 'morning': 8, 'afternoon': 12, 'evening': 17, 'night': 22 }), //saturday: { 'morning': 11, 'afternoon': 14, 'evening': 19, 'night': 23 }, ] as DayConfig[])
Public timesOfDay
Type : string[]
Private Optional _config
Type : C
Inherited from Item
Defined in Item:54
Public Optional id
Type : string
Inherited from Item
Defined in Item:50
Public Optional update$
Type : Observable<string>
Inherited from Item
Defined in Item:52
Protected Optional updateSubject
Type : Subject<string>
Inherited from Item
Defined in Item:51

Methods

Static assertData
assertData(data)
Inherited from Item
Defined in Item:51
Parameters :
Name Optional
data No
Returns : ScheduleConfig
Static getIsoStringWithTimezone
getIsoStringWithTimezone(date: Date)
Parameters :
Name Type Optional Default value
date Date No new Date()
Returns : string
Static hoursToTimestring
hoursToTimestring(h: number)
Parameters :
Name Type Optional
h number No
Returns : string
matches
matches(x)
Inherited from Item
Defined in Item:159
Parameters :
Name Optional
x No
Returns : boolean
matchesDate
matchesDate(date: Date)
Parameters :
Name Type Optional Default value
date Date No new Date()
Returns : boolean
matchesSchedule
matchesSchedule(schedule: Schedule | ScheduleConfig)
Parameters :
Name Type Optional
schedule Schedule | ScheduleConfig No
Returns : boolean
Static parse
parse(str: string)
Parameters :
Name Type Optional
str string No
Static acceptsAsConfig
acceptsAsConfig(x)
Inherited from Item
Defined in Item:27
Parameters :
Name Optional
x No
Static findConfigs
findConfigs(data)
Inherited from Item
Defined in Item:32
Type parameters :
  • U
Parameters :
Name Optional
data No
Returns : U[]
Public toJSON
toJSON()
Inherited from Item
Defined in Item:75
Returns : string

Accessors

config
getconfig()
setconfig(undefined: ScheduleConfig)
Parameters :
Name Type Optional
ScheduleConfig No
Returns : void
everyDay
geteveryDay()
allDay
getallDay()
import	{
			assert
		}									from '../../utils'

import	{	
			ScheduleConfig,	
			isScheduleConfig,	
			ParsedIsoString
		} 									from './interfaces'

import	{
			DayConfig,
			TimeOfDay
		}									from './time-of-day.class'

import	{	Item						}	from '../item.class'




export class Schedule extends Item<ScheduleConfig>{


	// STATIC:

	public 	static acceptsAsConfig 	= 	isScheduleConfig
	public 	static timeOfDay 		= 	new TimeOfDay([
											//sunday:
											{
												'morning': 		10,
												'afternoon':	13,
												'evening':		18,
												'night':		23
											},
											//weekdays:
											...Array(5).fill({
												'morning': 		8,
												'afternoon':	12,
												'evening':		17,
												'night':		22
											}),
											//saturday:
											{
												'morning': 		11,
												'afternoon':	14,
												'evening':		19,
												'night':		23
											},
										] as DayConfig[])

	static assertData(data:unknown): asserts data is ScheduleConfig {
		assert(this.acceptsAsConfig(data), "Schedule#assertData(): bad data")
	}

	static parse(str: string) : ParsedIsoString | null {

		if(typeof str != "string") return null

		const match = str.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}).*([\+\-Z])(\d{2})?:?(\d{2})?$/i)

		if(!match) return null

		const s		=	match.slice(1) 
						.map( x =>	['+','-','Z'].includes(x) 
								?	({'+': 1, '-': -1, 'Z': 0 } as any)[x] 
								:	parseInt( x || '0' ) 
						)

		const local_date = new Date(s[0], s[1]-1, s[2], s[3], s[4], 0, 0)	

		if(isNaN(local_date.getTime())) return null

		return 	{
					timezoneOffset:		s[5]*(s[6]+s[7]/60),
					dayOfWeek:			local_date.getDay(),
					timeOfDay:			this.timeOfDay.at(local_date).key,
					asLocalDate: 		local_date
				}

		
	}

	static hoursToTimestring(h:number) : string {
		const offsetSign 	= Math.sign(h)
		const offsetAbs		= Math.abs(h)
		const offsetHours 	= Math.floor(offsetAbs)
		const offsetMinutes = Math.floor( (offsetAbs-offsetHours) *60 )

		return 	(offsetSign < 0 ? '-' : '+')
				+	String(offsetHours).padStart(2, '0')
				+	':'
				+	String(offsetMinutes).padStart(2, '0')
	}


	static getIsoStringWithTimezone(date: Date = new Date()): string {

		return 		String(date.getFullYear()) 
				+ 	'-' 
				+	String(date.getMonth() + 1).padStart(2,'0') 
				+	'-' 
				+	String(date.getDate()).padStart(2,'0') 
				+	'T'
				+	String(date.getHours()).padStart(2,'0')
				+ 	':' 
				+	String(date.getMinutes()).padStart(2,'0')
				+	this.hoursToTimestring(-1* date.getTimezoneOffset() / 60)

	}








	// INSTANCE

	// TODO change into Sets

	public daysOfWeek	: number[]	
	public timesOfDay	: string[]	


	constructor(config?: ScheduleConfig){

		super(config || [[],[]])

	}

	set config([daysOfWeek, timesOfDay]: ScheduleConfig){

		if(!Schedule.acceptsAsConfig([daysOfWeek, timesOfDay])) throw new Error ("Schedule.set config(): Invalid Schedule config.")
		this.daysOfWeek = [...daysOfWeek]
		this.timesOfDay = [...timesOfDay]

	}

	get config() : ScheduleConfig {

		return 	[
					[...this.daysOfWeek],
					[...this.timesOfDay]
				]

	}

	get everyDay(): boolean {
		return	[0,1,2,3,4,5,6].every( day => this.daysOfWeek.includes(day) )
				||
				[0,1,2,3,4,5,6].every( day => !this.daysOfWeek.includes(day) )
	}

	get allDay() : boolean {
		return	this.timesOfDay.length == 0
	}

	matches(x: unknown) : boolean {
		if(x instanceof Date) 		return this.matchesDate(x)
		if(typeof x == 'string')	return this.matchesDate(Schedule.parse(x).asLocalDate)
		if(isScheduleConfig(x))		return this.matchesSchedule(x)	
		if(x instanceof Schedule)	return this.matchesSchedule(x)

		return false	
	}	
	
	matchesDate(date: Date = new Date() ): boolean {
		const time_slot 	= Schedule.timeOfDay.at(date)
		const day			= time_slot.startDate.getDay()
		const time_of_day	= time_slot.key

		return 	(this.daysOfWeek.length == 0 || this.daysOfWeek.includes(day))
				&&
				(this.timesOfDay.length == 0 || this.timesOfDay.includes(time_of_day))
	}

	matchesSchedule(schedule: Schedule | ScheduleConfig) : boolean {

		function matchArrays(array_1 : unknown[], array_2: unknown[]): boolean {

			if(array_1.length != array_2.length) return false

			const set = new Set([...array_1, ...array_2])	

			return set.size == array_1.length
		}

		const daysOfWeek = isScheduleConfig(schedule) ? schedule[0] : schedule.daysOfWeek
		const timesOfDay = isScheduleConfig(schedule) ? schedule[1] : schedule.timesOfDay

		return 	matchArrays(this.daysOfWeek, daysOfWeek)
				&&
				matchArrays(this.timesOfDay, timesOfDay)

	}

}

results matching ""

    No results matching ""