import dateutil from './dateutil' ; import IterResult , { IterArgs } from './iterresult' ; import CallbackIterResult from './callbackiterresult' ; import { Language } from './nlp/i18n' ; import { fromText , parseText , toText , isFullyConvertible } from './nlp/index' ; import { DateFormatter , GetText } from './nlp/totext' ; import { ParsedOptions , Options , Frequency , QueryMethods , QueryMethodTypes , IterResultType } from './types' ; import { parseOptions , initializeOptions } from './parseoptions' ; import { parseString } from './parsestring' ; import { optionsToString } from './optionstostring' ; import { Cache , CacheKeys } from './cache' ; import { Weekday } from './weekday' ; import { iter } from './iter/index' ; export const Days = { MO : new Weekday ( 0 ) , TU : new Weekday ( 1 ) , WE : new Weekday ( 2 ) , TH : new Weekday ( 3 ) , FR : new Weekday ( 4 ) , SA : new Weekday ( 5 ) , SU : new Weekday ( 6 ) } ; export const DEFAULT_OPTIONS = { freq : Frequency . YEARLY , dtstart : null , interval : 1 , wkst : Days . MO , count : null , until : null , tzid : null , bysetpos : null , bymonth : null , bymonthday : null , bynmonthday : null , byyearday : null , byweekno : null , byweekday : null , bynweekday : null , byhour : null , byminute : null , bysecond : null , byeaster : null } ; export const defaultKeys = Object . keys ( DEFAULT_OPTIONS ) as ( keyof Options ) [ ] ; param { Options ? } options constructor export default class RRule implements QueryMethods { public _cache : Cache | null ; public origOptions : Partial < Options > ; public options : ParsedOptions ; static readonly FREQUENCIES : ( keyof typeof Frequency ) [ ] = [ 'YEARLY' , 'MONTHLY' , 'WEEKLY' , 'DAILY' , 'HOURLY' , 'MINUTELY' , 'SECONDLY' ] ; static readonly YEARLY = Frequency . YEARLY ; static readonly MONTHLY = Frequency . MONTHLY ; static readonly WEEKLY = Frequency . WEEKLY ; static readonly DAILY = Frequency . DAILY ; static readonly HOURLY = Frequency . HOURLY ; static readonly MINUTELY = Frequency . MINUTELY ; static readonly SECONDLY = Frequency . SECONDLY ; static readonly MO = Days . MO ; static readonly TU = Days . TU ; static readonly WE = Days . WE ; static readonly TH = Days . TH ; static readonly FR = Days . FR ; static readonly SA = Days . SA ; static readonly SU = Days . SU ; constructor ( options = { } , noCache = false ) { this . _cache = noCache ? null : new Cache ( ) ; this . origOptions = initializeOptions ( options ) ; const { parsedOptions } = parseOptions ( options ) ; this . options = parsedOptions ; } static parseText ( text , language ? ) { return parseText ( text , language ) ; } static fromText ( text , language ? ) { return fromText ( text , language ) ; } static parseString = parseString ; static fromString ( str ) { return new RRule ( RRule . parseString ( str ) || undefined ) ; } static optionsToString = optionsToString ; protected _iter < M extends QueryMethodTypes > ( iterResult ) { return iter ( iterResult , this . options ) ; } private _cacheGet ( what , args ? ) { if ( ! this . _cache ) return false ; return this . _cache . _cacheGet ( what , args ) ; } public _cacheAdd ( what , value , args ? ) { if ( ! this . _cache ) return ; return this . _cache . _cacheAdd ( what , value , args ) ; } param { Function } iterator return all ( iterator ? ) { if ( iterator ) { return this . _iter ( new CallbackIterResult ( 'all' , { } , iterator ) ) ; } let result = this . _cacheGet ( 'all' ) as Date [ ] | false ; if ( result === false ) { result = this . _iter ( new IterResult ( 'all' , { } ) ) ; this . _cacheAdd ( 'all' , result ) ; } return result ; } return between ( after , before , inc = false , iterator ? ) { if ( ! dateutil . isValidDate ( after ) || ! dateutil . isValidDate ( before ) ) throw new Error ( 'Invalid date passed in to RRule.between' ) ; const args = { before , after , inc } ; if ( iterator ) { return this . _iter ( new CallbackIterResult ( 'between' , args , iterator ) ) ; } let result = this . _cacheGet ( 'between' , args ) ; if ( result === false ) { result = this . _iter ( new IterResult ( 'between' , args ) ) ; this . _cacheAdd ( 'between' , result , args ) ; } return result as Date [ ] ; } return before ( dt , inc = false ) { if ( ! dateutil . isValidDate ( dt ) ) throw new Error ( 'Invalid date passed in to RRule.before' ) ; const args = { dt : dt , inc : inc } ; let result = this . _cacheGet ( 'before' , args ) ; if ( result === false ) { result = this . _iter ( new IterResult ( 'before' , args ) ) ; this . _cacheAdd ( 'before' , result , args ) ; } return result as Date ; } return after ( dt , inc = false ) { if ( ! dateutil . isValidDate ( dt ) ) throw new Error ( 'Invalid date passed in to RRule.after' ) ; const args = { dt : dt , inc : inc } ; let result = this . _cacheGet ( 'after' , args ) ; if ( result === false ) { result = this . _iter ( new IterResult ( 'after' , args ) ) ; this . _cacheAdd ( 'after' , result , args ) ; } return result as Date ; } /**
     * Returns the number of recurrences in this set. It will have go trough
     * the whole recurrence, if this hasn't been done before.
     */ count ( ) { return this . all ( ) . length ; } see return toString ( ) { return optionsToString ( this . origOptions ) ; } /**
     * Will convert all rules described in nlp:ToText
     * to text.
     */ toText ( gettext ? , language ? , dateFormatter ? ) { return toText ( this , gettext , language , dateFormatter ) ; } isFullyConvertibleToText ( ) { return isFullyConvertible ( this ) ; } return clone ( ) { return new RRule ( this . origOptions ) ; } }