import { Constants } from './Constants' ; import { Day , DayInput , DayProperty } from './Day' ; import { Unit } from './DayFunctions' ; import { Event } from './Event' ; import { FrequencyCheck } from './Frequency' ; import { Functions as fn } from './Functions' ; import { Schedule , ScheduleInput } from './Schedule' ; import { ScheduleModifier } from './ScheduleModifier' ; import { Time } from './Time' ; /**
 * The class which takes user input and parses it to specific structures.
 */ export class Parse { param input param property returns see public static frequency ( input , property ) { let check = ( value ) => { return true ; } ; check . given = false ; if ( fn . isFrequencyValueEvery ( input ) ) { const every = input . every ; const offset = ( input . offset || 0 ) % every ; check = ( value ) => { return value % every === offset ; } ; check . given = true ; } if ( fn . isFrequencyValueOneOf ( input ) ) { const map = { } ; for ( const i of input ) { map [ i ] = true ; } check = ( value ) => { return ! ! map [ value ] ; } ; check . given = true ; } if ( fn . isFrequencyValueEquals ( input ) ) { check = ( value ) => { return value === input ; } ; check . given = true ; } check . input = fn . coalesce ( input , null ) ; check . property = property ; return check ; } param input returns public static day ( input ) { if ( fn . isNumber ( input ) ) { return Day . unix ( input ) ; } else if ( fn . isString ( input ) ) { return Day . fromString ( input ) ; } else if ( input instanceof Day ) { return input ; } else if ( fn . isArray < number > ( input ) ) { return Day . fromArray ( input ) ; } else if ( fn . isObject < object > ( input ) ) { return Day . fromObject ( input ) ; } else if ( input === true ) { return Day . today ( ) ; } return null ; } param input returns see see public static time ( input ) { if ( input instanceof Time ) { return input ; } if ( fn . isNumber ( input ) ) { return Time . fromIdentifier ( input ) ; } if ( fn . isString ( input ) ) { return Time . fromString ( input ) ; } if ( fn . isObject < any > ( input ) && fn . isNumber ( input . hour ) ) { return new Time ( input . hour , input . minute , input . second , input . millisecond ) ; } return null ; } param input returns see public static times ( input ) { const times = [ ] ; if ( fn . isArray ( input ) ) { for ( const timeInput of input ) { const time = this . time ( timeInput ) ; if ( time ) { times . push ( time ) ; } } times . sort ( ( a , b ) => { return a . toMilliseconds ( ) - b . toMilliseconds ( ) ; } ) ; } return times ; } param input param value param parseMeta param out returns see public static modifier < T > ( input , value , parseMeta = ( x => x ) , out = new ScheduleModifier < T > ( ) ) { const map = { } ; if ( fn . isArray ( input ) ) { for ( const identifier of input ) { if ( identifier instanceof Day ) { map [ identifier . dayIdentifier ] = value ; } else if ( fn . isNumber ( identifier ) ) { map [ identifier ] = value ; } else if ( fn . isString ( identifier ) ) { map [ identifier ] = value ; } } } if ( fn . isObject ( input ) ) { for ( const identifier in input ) { map [ identifier ] = parseMeta ( input [ identifier ] ) ; } } out . map = map ; return out ; } /**
     *
     */ public static unit ( input , all = false ) { if ( typeof input !== 'string' ) { return Constants . DURATION_DEFAULT_UNIT ( all ) ; } switch ( input . toLowerCase ( ) ) { case 'ms' : case 'milli' : case 'millis' : case 'millisecond' : case 'milliseconds' : return 'millis' ; case 's' : case 'sec' : case 'secs' : case 'second' : case 'seconds' : return 'second' ; case 'm' : case 'min' : case 'mins' : case 'minute' : case 'minutes' : return 'minute' ; case 'h' : case 'hr' : case 'hour' : case 'hours' : return 'hour' ; case 'd' : case 'day' : case 'days' : return 'day' ; case 'w' : case 'wk' : case 'week' : case 'weeks' : return 'week' ; case 'mon' : case 'month' : case 'months' : return 'month' ; case 'q' : case 'quarter' : case 'quarters' : return 'quarter' ; case 'y' : case 'yr' : case 'yrs' : case 'year' : case 'years' : return 'year' ; } return Constants . DURATION_DEFAULT_UNIT ( all ) ; } param input param parseMeta param out returns public static schedule < M > ( input , parseMeta = ( x => x ) , out = new Schedule < M > ( ) ) { if ( input instanceof Schedule ) { return input ; } const on = this . day ( input . on ) ; const times = this . times ( input . times ) ; const fullDay = times . length === 0 ; if ( on ) { input . start = on . startOf ( 'day' ) ; input . end = on . endOf ( 'day' ) ; input . year = [ on . year ] ; input . month = [ on . month ] ; input . dayOfMonth = [ on . dayOfMonth ] ; } out . times = times ; out . duration = fn . coalesce ( input . duration , Constants . DURATION_DEFAULT ) ; out . durationUnit = this . unit ( input . durationUnit , fullDay ) ; out . start = this . day ( input . start ) ; out . end = this . day ( input . end ) ; out . maxOccurrences = fn . coalesce ( input . maxOccurrences , 0 ) ; out . exclude = this . modifier ( input . exclude , true , undefined , out . exclude ) ; out . include = this . modifier ( input . include , true , undefined , out . include ) ; out . cancel = this . modifier ( input . cancel , true , undefined , out . cancel ) ; out . meta = this . modifier ( input . meta , null , parseMeta , out . meta ) ; out . year = this . frequency ( input . year , 'year' ) ; out . month = this . frequency ( input . month , 'month' ) ; out . day = this . frequency ( input . day , 'day' ) ; out . quarter = this . frequency ( input . quarter , 'quarter' ) ; out . week = this . frequency ( input . week , 'week' ) ; out . weekOfYear = this . frequency ( input . weekOfYear , 'weekOfYear' ) ; out . weekspanOfYear = this . frequency ( input . weekspanOfYear , 'weekspanOfYear' ) ; out . fullWeekOfYear = this . frequency ( input . fullWeekOfYear , 'fullWeekOfYear' ) ; out . lastWeekspanOfYear = this . frequency ( input . lastWeekspanOfYear , 'lastWeekspanOfYear' ) ; out . lastFullWeekOfYear = this . frequency ( input . lastFullWeekOfYear , 'lastFullWeekOfYear' ) ; out . weekOfMonth = this . frequency ( input . weekOfMonth , 'weekOfMonth' ) ; out . weekspanOfMonth = this . frequency ( input . weekspanOfMonth , 'weekspanOfMonth' ) ; out . fullWeekOfMonth = this . frequency ( input . fullWeekOfMonth , 'fullWeekOfMonth' ) ; out . lastWeekspanOfMonth = this . frequency ( input . lastWeekspanOfMonth , 'lastWeekspanOfMonth' ) ; out . lastFullWeekOfMonth = this . frequency ( input . lastFullWeekOfMonth , 'lastFullWeekOfMonth' ) ; out . dayOfWeek = this . frequency ( input . dayOfWeek , 'dayOfWeek' ) ; out . dayOfMonth = this . frequency ( input . dayOfMonth , 'dayOfMonth' ) ; out . lastDayOfMonth = this . frequency ( input . lastDayOfMonth , 'lastDayOfMonth' ) ; out . dayOfYear = this . frequency ( input . dayOfYear , 'dayOfYear' ) ; out . updateDurationInDays ( ) ; out . updateChecks ( ) ; out . updateEnd ( ) ; return out ; } param checks returns public static givenFrequency ( checks ) { const out = [ ] ; for ( const check of checks ) { if ( check . given ) { out . push ( check ) ; } } return out ; } param input param parseData param parseMeta returns public static event < T , M > ( input , parseData = ( x => x ) , parseMeta = ( x => x ) ) { if ( input instanceof Event ) { return input ; } if ( ! input . schedule ) { return null ; } const schedule = this . schedule < M > ( input . schedule , parseMeta ) ; return new Event ( schedule , parseData ( input . data ) , input . id , input . visible ) ; } /**
     * Parses a schedule from a CRON pattern. TODO
     */ public static cron < M > ( pattern , out = new Schedule < M > ( ) ) { return out ; } }