<commit_before>var _ = require('underscore')._,
    Q = require('q'),
	url = require('url'),
    apis = require('./apis');
	
_.str = require('underscore.string');

function KeyValuePair(key, value) {
    this.key = key;
    this.value = value;
}

KeyValuePair.prototype.toString = function() {
    return "(" + this.key.toString() + "=" + this.value.toString() + ")";
}

var verifyUrl = function(input) {
    if (! _.isString(input)) {
        return false;
    }
    var parsed = url.parse(input);
    return (parsed && parsed.href);
};

var Router = function(routingOptions) {
    this.addRoutes(routingOptions || []);
}

Router.prototype = {
    get: function(url, mediaHandler, excludedSites) {
        url || (url = "");
        var success = false;
        var deferred = Q.defer();
        mediaHandler = mediaHandler || function(data) {return data;};
        var isString = typeof url == "string", isUrl = false;
        if (isString) {
            url = _.str.trim(url.toString());
            isUrl = verifyUrl(url);
        }
        if (url) {
            if (isUrl || url.key && url.value) {
                var func = this.testResource(url, excludedSites);
                if (func) {
                    deferred = func({query: url})
                    deferred.then(mediaHandler);
                    success = true;
                }
            }
        }
        if (!success) {
            if (url && !isUrl && verifyUrl('http://' + url)) {
                return this.get('http://' + url);
            }
            deferred.reject({
                success: false,
                error: "The resource submitted could not be identified."
            });
            deferred = deferred.promise;
        }
        return deferred;
    },
    addRoutes: function(routes) {
        var newRoutes =  _.clone(routes) || {};
        this.routingTable = _.extend({}, (this.routingTable || {}, newRoutes));
    },
    testResource: function(input, exclusions) {
        // Finds the first routing function from the routing table, 
        // possibly excluding some routes beforehand
        if (!exclusions) {
            exclusions = [];
        } else if (!_.isArray(exclusions)) {
            exclusions = [exclusions];
        }
        var route, callback = null, possibleRoutes = {}, i, j;
         _.each(this.routingTable, function(route, siteCode) {
            if (!_.contains(exclusions, siteCode)) {
                possibleRoutes[siteCode] = route;
            }
        });
        route = _.find(possibleRoutes, function(route, siteCode, list) {
            var accepted = route.locate(input);
            return accepted;
        });
        route && (callback = route.request);
        return callback;
    },
};

module.exports = {
	getRouter: function() {
		return new Router(apis.streams);
	},
	getURLParams: function(source, useOrderedList) {
		return getURLParams(source, useOrderedList);
	}
};<commit_msg>Code cleanup.<commit_after>var _ = require('underscore')._,
    Q = require('q'),
	url = require('url'),
    apis = require('./apis');
	
_.str = require('underscore.string');

function KeyValuePair(key, value) {
    this.key = key;
    this.value = value;
}

KeyValuePair.prototype.toString = function() {
    return "(" + this.key.toString() + "=" + this.value.toString() + ")";
}

var verifyUrl = function(input) {
    if (! _.isString(input)) {
        return false;
    }
    var parsed = url.parse(input);
    return (parsed && parsed.href);
};

var Router = function(routingOptions) {
    this.addRoutes(routingOptions || []);
}

Router.prototype = {
    get: function(url, mediaHandler, excludedSites) {
        url || (url = "");
        var success = false;
        var deferred = Q.defer();
        mediaHandler = mediaHandler || function(data) {return data;};
        var isString = typeof url == "string", isUrl = false;
        if (isString) {
            url = _.str.trim(url.toString());
            isUrl = verifyUrl(url);
        }
        if (url) {
            if (isUrl || url.key && url.value) {
                var func = this.testResource(url, excludedSites);
                if (func) {
                    deferred = func({query: url})
                    deferred.then(mediaHandler);
                    success = true;
                }
            }
        }
        if (!success) {
            if (url && !isUrl && verifyUrl('http://' + url)) {
                return this.get('http://' + url);
            }
            deferred.reject({
                success: false,
                error: "The resource submitted could not be identified."
            });
            deferred = deferred.promise;
        }
        return deferred;
    },
    addRoutes: function(routes) {
        var newRoutes =  _.clone(routes) || {};
        this.routingTable = _.extend({}, (this.routingTable || {}, newRoutes));
    },
    testResource: function(input, exclusions) {
        // Finds the first routing function from the routing table, 
        // possibly excluding some routes beforehand
        if (!exclusions) {
            exclusions = [];
        } else if (!_.isArray(exclusions)) {
            exclusions = [exclusions];
        }
        var route, callback = null, possibleRoutes = {}, i, j;
         _.each(this.routingTable, function(route, siteCode) {
            if (!_.contains(exclusions, siteCode)) {
                possibleRoutes[siteCode] = route;
            }
        });
        route = _.find(possibleRoutes, function(route, siteCode, list) {
            var accepted = route.locate(input);
            return accepted;
        });
        route && (callback = route.request);
        return callback;
    },
};

module.exports = {
	getRouter: function() {
		return new Router(apis.streams);
	},
	getURLParams: getURLParams
};
{'fraction_non_alphanumeric': 0.14025438294946718, 'fraction_numerical': 0.0, 'mean_word_length': 1.9065934065934067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4252348', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1623, 'n_words': 375}