aboutsummaryrefslogtreecommitdiff
path: root/src/duration.ts
blob: 18849a04faca8a1471215914fbcdbc981296d766 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import moment from 'moment';

export type TimeUnit = moment.unitOfTime.DurationConstructor;

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

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

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

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