all files / adal-ts2/src/ query.string.deserializer.ts

96% Statements 24/25
42.86% Branches 3/7
83.33% Functions 5/6
96% Lines 24/25
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49  1× 1× 1×   2×   1× 2×     2× 2× 2× 2× 4× 4×   2×   1× 8×   1× 2× 1×   1× 1×   2×   1×   1×           1×                
import { Constants } from './constants';
 
export class QueryStringDeserializer {
 
    private plRegex = /\+/g;
 
    public deserialize(queryString: string): any {
 
        queryString = this.trimHashSign(queryString);
 
        let match: RegExpExecArray;
        // Regex for replacing addition symbol with a space
        let searchRegex = /([^&=]+)=([^&]*)/g;
 
        let obj: any = {};
        match = searchRegex.exec(queryString);
        while (match) {
            obj[this.decode(match[1])] = this.decode(match[2]);
            match = searchRegex.exec(queryString);
        }
 
        return obj;
    }
 
    private decode(s: string): string {
        return decodeURIComponent(s.replace(this.plRegex, ' '));
    }
E
    private trimHashSign(hash: string) {
        if (hash.indexOf('#/') > -1) {
            hash = hash.substring(hash.indexOf('#/') + 2);
        } else if (hash.indexOf('#') > -1) {
            hash = hash.substring(1);
        }
 
        return hash;
    }
 
}
 
export function hasAadProps(deserializedHash: any): boolean {
 
    return (
        deserializedHash.hasOwnProperty(Constants.ERROR_DESCRIPTION) ||
        deserializedHash.hasOwnProperty(Constants.ACCESS_TOKEN) ||
        deserializedHash.hasOwnProperty(Constants.ID_TOKEN)
    );
}