{"size": 1925, "ext": "js", "max_stars_repo_path": "src/UrlUtility.js", "max_stars_repo_name": "dbadeveloping/react-native-oidc-client", "max_stars_repo_stars_event_min_datetime": "2020-12-24T20:16:00.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-29T19:53:48.000Z", "max_issues_repo_path": "src/UrlUtility.js", "max_issues_repo_name": "dbadeveloping/react-native-oidc-client", "max_issues_repo_issues_event_min_datetime": "2020-03-31T06:51:31.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-09T17:09:33.000Z", "max_forks_repo_path": "src/UrlUtility.js", "max_forks_repo_name": "dbadeveloping/react-native-oidc-client", "max_forks_repo_forks_event_min_datetime": "2020-08-13T12:50:37.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-13T12:50:37.000Z"}
{"max_stars_count": 2, "max_issues_count": 7, "max_forks_count": 1, "avg_line_length": 25.6666666667, "max_line_length": 77, "alphanum_fraction": 0.4446753247}
import Log from './Log';
export default class UrlUtility {
    static addQueryParam(url, name, value) {
        if (url.indexOf('?') < 0) {
            url += '?';
        }

        if (url[url.length - 1] !== '?') {
            url += '&';
        }

        url += encodeURIComponent(name);
        url += '=';
        url += encodeURIComponent(value);

        return url;
    }

    static parseUrlFragment(value, delimiter = '#') {
        Log.debug('UrlUtility.parseUrlFragment');

        var idx = value.lastIndexOf(delimiter);
        if (idx >= 0) {
            value = value.substr(idx + 1);
        }

        var params = {},
            regex = /([^&=]+)=([^&]*)/g,
            m;

        var counter = 0;
        while ((m = regex.exec(value))) {
            params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
            if (counter++ > 50) {
                Log.error(
                    'response exceeded expected number of parameters',
                    value
                );
                return {
                    error: 'Response exceeded expected number of parameters',
                };
            }
        }

        for (var prop in params) {
            return params;
        }

        return {};
    }

    static buildRequestForm(request) {
        return Object.keys(request)
            .map(key => {
                return (
                    encodeURIComponent(key) +
                    '=' +
                    encodeURIComponent(request[key])
                );
            })
            .join('&');
    }

    static buildRequestUrl(url, request) {
        if (!url) {
            Log.error('No url passed to AuthorizationGrant');
            throw new Error('no url');
        }
        Object.getOwnPropertyNames(request).forEach(param => {
            url = UrlUtility.addQueryParam(url, param, request[param]);
        });
        return url;
    }
}
