aboutsummaryrefslogblamecommitdiff
path: root/src/pattern.js
blob: 858f2a348ef36f12e35d68cfd864dd2b236e1eb9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                                     







                                  

                                                                    
                                                                                   


                           
                                                             



                                  
                           

     




                                    

                                        


          
                                                                                                                                      
                                             
                          

                                                             
 
export class Pattern {
    constructor(id, isRegex, value, label) {
        this.id = id;
        this.isRegex = isRegex;
        this.value = value;
        this.label = label;
    }

    get regex() { return new RegExp(this.isRegex ? this.value : `^${this.value}$`); }
    get isEmpty() { return this.label === null; }
    deflate() {
        return {
            id: this.id,
            isRegex: this.isRegex,
            value: this.value,
            label: this.label
        };
    }
    static emptyPattern = () => new Pattern(0, true, '', null);
    static anyPattern = () => new Pattern('any', true, '.*', 'Any');
    static inflate = obj => new Pattern(obj.id, obj.isRegex, obj.value, obj.label);
}

export class PatternEntry {
    constructor(name, idx, calPattern, eventPattern, color) {
        this.name = name;
        this.idx = idx;
        this.cal = calPattern;
        this.event = eventPattern;
        this.color = color;
    }

    deflate() {
        return {
            name: this.name,
            idx: this.idx,
            cal: this.cal.deflate(),
            event: this.event.deflate(),
            color: this.color
        };
    }

    static defaultPatternEntry = (idx) => new PatternEntry('', idx, Pattern.emptyPattern(), Pattern.anyPattern(), {background: null});
    static inflate = obj => new PatternEntry(
        obj.name, obj.idx,
        Pattern.inflate(obj.cal), Pattern.inflate(obj.event),
        obj.color);
}