public static getUrlMatch()

in src/app/utils/url.ts [23:111]


    public static getUrlMatch(given, template) {

        var fGiven = given;
        var fTemplate = template;

        // Removen query string and fragment for matching
        given = UrlUtil.trimUrlOfQueryAndHash(given);
        template = UrlUtil.trimUrlOfQueryAndHash(template);

        // Make sure both urls start with a '/'
        if (given[0] != '/') {
            given = "/" + given;
        }
        if (template[0] != '/') {
            template = "/" + template;
        }

        // Make sure no urls end with '/'
        if (given[given.length - 1] === '/') {
            given = given.substring(0, given.length - 1);
        }
        if (template[template.length - 1] === '/') {
            template = template.substring(0, template.length - 1);
        }

        var seg, templateSeg;
        var matches = {}

        var maxRuns = 500;
        var runCount = 0;

        while (given.length > 0 && template.length > 0 && runCount < 500) {
            seg = UrlUtil.getSegment(given);
            given = given.substring(seg.length);

            templateSeg = UrlUtil.getSegment(template);
            template = template.substring(templateSeg.length);

            if (given.length === 0 && template.length !== 0 || given.length !== 0 && template.length === 0) {

                // No match
                return null;
            }

            seg = UrlUtil.removeLeadingSlash(seg);
            templateSeg = UrlUtil.removeLeadingSlash(templateSeg);

            // Check if templateSegment is something like "{id}"
            var templateKey = UrlUtil.isTemplate(templateSeg);

            if (templateKey) {
                matches[templateKey] = seg;
            }
            else {
                if (seg !== templateSeg) {
                    return null;
                }
            }
            runCount++;
        }

        if (runCount >= maxRuns) {
            return null;
        }

        var fTemplateQueryIndex = fTemplate.indexOf('?');
        if (fTemplateQueryIndex != -1) {

            var templateQuery = fTemplate.substring(fTemplateQueryIndex);
            var givenQuery = fGiven.substring(fGiven.indexOf('?'));

            var tQuery = UrlUtil.getQueryParams(templateQuery);
            var gQuery = UrlUtil.getQueryParams(givenQuery);

            for (var key in tQuery) {
                if (!gQuery.hasOwnProperty(key)) {
                    continue;
                }

                var val = UrlUtil.isTemplate(tQuery[key]);

                if (val) {
                    matches[val] = gQuery[key];
                }
            }
        }

        return matches;
    }