<commit_before>/**
 * @class  	Router
 * @author  <NAME> <<EMAIL>>
 */


var Util = require('T/util');

var routes = []; // storage for all routes

/**
 * @properties currentUrl
 * Latest URL dispatched
 * @type {String}
 */
exports.currentUrl = null;

/**
 * @method on
 * Register a route with defined callbacks
 *
 * @param  {String|RegExp|Function}   	key 			The route name.
 * It can be:
 *
 * * `String`: (exact route match)
 * * `RegExp`: is evaluated with the argument and the matches are passed to the callback
 * * `Function`: must return a `non-undefined` value to be executed. That value is passed to the callback
 *
 * @param  {Function}	callback  		The callback
 */
exports.on = function(key, callback) {
	routes.push({
		key: key,
		callback: callback
	});
};

/**
 * @method dispatch
 * Dispatch the router
 *
 * This function call the defined function with `Router.on`
 *
 * The param `this` represents an XCallback-URL Object for selected route.
 *
 * See https://github.com/FokkeZB/UTiL/blob/master/XCallbackURL/XCallbackURL.js for more details.
 *
 * The arguments passed are the matches for your regex definition (if present)
 *
 * @param  {String} 	url 		The route
 */
exports.dispatch = function(url) {
	var run = false;
	var matches = null;

	var X = Util.parseAsXCallbackURL(url);
	X.path = X.path.replace(/\/$/g, '');
	Ti.API.debug('Router: dispatching <' + url + '> with path <' + X.path + '>');

	exports.currentUrl = url;

	for (var i in routes) {
		var routeDefinition = routes[i];

		if (_.isString(routeDefinition.key)) {
			// Regular string equals
			run = (routeDefinition.key === X.path);
		} else if (_.isRegExp(routeDefinition.key)) {
			// Regular expression complex match
			matches = X.path.match(routeDefinition.key);
			run = !!(matches);
			if (matches) matches.shift();
		} else if (_.isFunction(routeDefinition.key)) {
			// Function match
			matches = routeDefinition.key(X.path);
			run = (matches !== undefined);
		}

		if (run === true) {
			Ti.API.debug('Router: Matched with key <' + routeDefinition.key + '> and matches <' + JSON.stringify(matches) + '>');

			routeDefinition.callback.apply(X, matches);
			return; // break the f***g cycle
		}
	}

	Ti.API.warn('Router: no matches for the selected route', url);
};

/**
 * @method go
 * @inheritDoc #dispatch
 * Alias for {@link #dispatch}
 */
exports.go = exports.dispatch;
<commit_msg>Router.alias<commit_after>/**
 * @class  	Router
 * @author  <NAME> <<EMAIL>>
 */


var Util = require('T/util');

var routes = []; // storage for all routes

/**
 * @properties currentUrl
 * Latest URL dispatched
 * @type {String}
 */
exports.currentUrl = null;

/**
 * @properties currentRoute
 * Latest Route (not URL) dispatched
 * @type {Object}
 */
exports.currentRoute = null;

/**
 * @method on
 * Register a route with defined callbacks
 *
 * @param  {String|RegExp|Function}   	key 			The route name.
 * It can be:
 *
 * * `String`: (exact route match)
 * * `RegExp`: is evaluated with the argument and the matches are passed to the callback
 * * `Function`: must return a `non-undefined` value to be executed. That value is passed to the callback
 *
 * @param  {Function}	callback  		The callback
 */
exports.on = function(key, callback) {
	routes.push({
		key: key,
		callback: callback
	});
};

/**
 * @method dispatch
 * Dispatch the router
 *
 * This function call the defined function with `Router.on`
 *
 * The param `this` represents an XCallback-URL Object for selected route.
 *
 * See https://github.com/FokkeZB/UTiL/blob/master/XCallbackURL/XCallbackURL.js for more details.
 *
 * The arguments passed are the matches for your regex definition (if present)
 *
 * @param  {String} 	url 		The route
 */
exports.dispatch = function(url) {
	var run = false;
	var matches = null;

	var X = Util.parseAsXCallbackURL(url);
	X.path = X.path.replace(/\/$/g, '');
	Ti.API.debug('Router: dispatching <' + url + '> with path <' + X.path + '>');

	exports.currentUrl = url;

	for (var i in routes) {
		var routeDefinition = routes[i];

		if (_.isString(routeDefinition.key)) {
			// Regular string equals
			run = (routeDefinition.key === X.path);
		} else if (_.isRegExp(routeDefinition.key)) {
			// Regular expression complex match
			matches = X.path.match(routeDefinition.key);
			run = !!(matches);
			if (matches) matches.shift();
		} else if (_.isFunction(routeDefinition.key)) {
			// Function match
			matches = routeDefinition.key(X.path);
			run = (matches !== undefined);
		}

		if (run === true) {
			Ti.API.debug('Router: Matched with key <' + routeDefinition.key + '> and matches <' + JSON.stringify(matches) + '>');

			exports.currentRoute = routeDefinition;
			exports.currentRoute.callback.apply(X, matches);

			return; // break the cycle
		}
	}

	Ti.API.warn('Router: no matches for the selected route', url);
};

/**
 * @method go
 * @inheritDoc #dispatch
 * Alias for {@link #dispatch}
 */
exports.go = exports.dispatch;

/**
 * Make an alias route
 * @param  {String} url
 * @param  {String} newUrl
 */
exports.alias = function(url, newUrl) {
	exports.on(url, function() {
		exports.go(newUrl);
	});
};


{'fraction_non_alphanumeric': 0.1489569116786898, 'fraction_numerical': 0.0, 'mean_word_length': 3.414802065404475, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '2386336', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1637, 'n_words': 488}