aboutsummaryrefslogblamecommitdiff
path: root/src/duration.ts
blob: 5c2cceacdeeda7c4ba17a763ae014db73c08b98c (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                             
                            
                  


                
                       
                  
                   
                                                



                          
                                                                                    
                
                                                                 



                                  


                                                                          

                                                                
                                                                                          
 
 
 
                                 
                 

                        
  
 
                            

















                                                               

                                                  

                                        

      
import moment from 'moment';

export type TimeUnit = moment.unitOfTime.DurationConstructor;

export type DurationFlat = {
    value: string,
    unit: string
};

export class Duration {
    value: string;
    unit: TimeUnit;
    constructor(value: string, unit: TimeUnit) {
        this.value = value
        this.unit = unit
    }

    isValid() { return moment.duration(parseInt(this.value), this.unit).isValid(); }
    toMoment() {
        let m = moment.duration(parseInt(this.value), this.unit);
        if (m.isValid()) return m;
        return null;
    }

    static days(n: number) { return new Duration(String(n), 'days'); }
    static weeks(n: number) { return new Duration(String(n), 'weeks'); }
    static months(n: number) { return new Duration(String(n), 'months'); }

    deflate() { return { value: this.value, unit: this.unit }; }
    static inflate = (obj: DurationFlat) => new Duration(obj.value, obj.unit as TimeUnit);
}


export type TrackedPeriodFlat = {
    name: string,
    start: DurationFlat,
    end: DurationFlat
};

export class TrackedPeriod {
    name: string;
    start: Duration;
    end: Duration;

    constructor(name: string, start: Duration, end: Duration) {
        this.name = name;
        this.start = start;
        this.end = end;
    }

    deflate() {
        return {
            name: this.name,
            start: this.start.deflate(),
            end: this.end.deflate()
        };
    }

    static inflate = (obj: TrackedPeriodFlat) => (
        new TrackedPeriod(obj.name,
            Duration.inflate(obj.start),
            Duration.inflate(obj.end))
    );
}