aboutsummaryrefslogblamecommitdiff
path: root/src/duration.ts
blob: cb2510784309198e27fda3d61affe9d593f7075e (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 TrackPeriodFlat = {
    name: string,
    start: DurationFlat,
    end: DurationFlat
};

export class TrackPeriod {
    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: TrackPeriodFlat) => (
        new TrackPeriod(obj.name, Duration.inflate(obj.start), Duration.inflate(obj.end))
    );
}