All files / src cookie-version-matcher.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 4/4
100% Lines 7/7
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 27 28 29 30 31 32                9x       4x       2x       9x 4x     5x           10x  
import {ICookieMap} from "./types";
 
type MATCHER_FN = (cookies: ICookieMap) => string;
 
class CookieVersionMatcher {
    private readonly matcher: MATCHER_FN;
 
    constructor(matcher: MATCHER_FN | string) {
        this.matcher = this.parseMatcher(matcher);
    }
 
    match(cookies: ICookieMap){
        return this.matcher(cookies) || null;
    }
 
    toJSON() {
        return this.matcher.toString();
    }
 
    private parseMatcher(matcher: MATCHER_FN | string): MATCHER_FN {
        if (typeof matcher === 'string') {
            return eval(matcher);
        }
 
        return matcher;
    }
}
 
export {
    MATCHER_FN,
    CookieVersionMatcher
};