<commit_before>/**
 * UrlGenerator.js
 *
 * @author: <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015-2016, QuorraJS.
 * @license See LICENSE.txt
 */

var _ = require('lodash');
var str = require('../support/str');
var url = require('url');

function UrlGenerator(routes, app) {
    /**
     * The route collection.
     */
    this.__routes = routes;

    /**
     * The application instance.
     */
    this.__app = app;

    /**
     * A cached copy of the URL schema for the current request.
     *
     * @var string|null
     */
    this.__cachedSchema;

    /**
     * The forced schema for URLs.
     *
     * @var string
     */
    this.__forceSchema;

    /**
     * A cached copy of the URL root.
     *
     * @var string|null
     */
    this.__cachedRoot = null;

    /**
     * The forced URL root.
     *
     * @var string
     */
    this.__forcedRoot;

    /**
     * Characters that should not be URL encoded.
     *
     * @var {Array}
     */
    this.__dontEncode = {
        '%2F': '/',
        '%40': '@',
        '%3A': ':',
        '%3B': ';',
        '%2C': ',',
        '%3D': '=',
        '%2B': '+',
        '%21': '!',
        '%2A': '*',
        '%7C': '|',
        '%3F': '?',
        '%26': '&',
        '%23': '#',
        '%25': '%'
    };

    /**
     * The root namespace being applied to controller actions.
     *
     * @var {String}
     */
    this.__rootNamespace = 'controllers';
}

/**
 * Generate a absolute URL to the given path.
 *
 * @param  {String}  path
 * @param  {*}  extra
 * @param  {boolean|null}  secure
 * @return {String}
 */
UrlGenerator.prototype.to = function (path, extra, secure) {
    extra = this.__formatParameters(extra);

    // First we will check if the URL is already a valid URL. If it is we will not
    // try to generate a new one but will simply return the URL as is, which is
    // convenient since developers do not always have to check if it's valid.
    if (this.isValidUrl(path)) {
        return path;
    }

    var scheme = this.__getScheme(secure);

    // Once we have the scheme we will compile the "tail" by collapsing the values
    // into a single string delimited by slashes. This just makes it convenient
    // for passing the array of parameters to this URL as a list of segments.
    var tail = _.map(extra, encodeURIComponent).join('/');

    var root = this.__getRootUrl(scheme);

    return this.__formUrl(root, path, tail);
};

/**
 * Format URL parameters.
 *
 * @param  {Array|Object} params
 * @return {Array|Object}
 */
UrlGenerator.prototype.__formatParameters = function(params) {
    if(!_.isObject(params)) {
        return params !== undefined? parseArray(params) : [];
    } else {
        return params
    }
};

/**
 * Generate a secure, absolute URL to the given path.
 *
 * @param  {String} path
 * @param  {Array}  parameters
 * @return {String}
 */
UrlGenerator.prototype.secure = function (path, parameters) {
    return this.to(path, parameters, true);
};

/**
 * Generate a URL to an application asset.
 *
 * @param  {String}  path
 * @param  {Boolean|null} secure
 * @return {string}
 */
UrlGenerator.prototype.asset = function (path, secure) {
};

/**
 * Determine if the given path is a valid URL.<commit_msg>Update asset method<commit_after>/**
 * UrlGenerator.js
 *
 * @author: <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015-2016, QuorraJS.
 * @license See LICENSE.txt
 */

var _ = require('lodash');
var str = require('../support/str');
var url = require('url');

function UrlGenerator(routes, app) {
    /**
     * The route collection.
     */
    this.__routes = routes;

    /**
     * The application instance.
     */
    this.__app = app;

    /**
     * A cached copy of the URL schema for the current request.
     *
     * @var string|null
     */
    this.__cachedSchema;

    /**
     * The forced schema for URLs.
     *
     * @var string
     */
    this.__forceSchema;

    /**
     * A cached copy of the URL root.
     *
     * @var string|null
     */
    this.__cachedRoot = null;

    /**
     * The forced URL root.
     *
     * @var string
     */
    this.__forcedRoot;

    /**
     * Characters that should not be URL encoded.
     *
     * @var {Array}
     */
    this.__dontEncode = {
        '%2F': '/',
        '%40': '@',
        '%3A': ':',
        '%3B': ';',
        '%2C': ',',
        '%3D': '=',
        '%2B': '+',
        '%21': '!',
        '%2A': '*',
        '%7C': '|',
        '%3F': '?',
        '%26': '&',
        '%23': '#',
        '%25': '%'
    };

    /**
     * The root namespace being applied to controller actions.
     *
     * @var {String}
     */
    this.__rootNamespace = 'controllers';
}

/**
 * Generate a absolute URL to the given path.
 *
 * @param  {String}  path
 * @param  {*}  extra
 * @param  {boolean|null}  secure
 * @return {String}
 */
UrlGenerator.prototype.to = function (path, extra, secure) {
    extra = this.__formatParameters(extra);

    // First we will check if the URL is already a valid URL. If it is we will not
    // try to generate a new one but will simply return the URL as is, which is
    // convenient since developers do not always have to check if it's valid.
    if (this.isValidUrl(path)) {
        return path;
    }

    var scheme = this.__getScheme(secure);

    // Once we have the scheme we will compile the "tail" by collapsing the values
    // into a single string delimited by slashes. This just makes it convenient
    // for passing the array of parameters to this URL as a list of segments.
    var tail = _.map(extra, encodeURIComponent).join('/');

    var root = this.__getRootUrl(scheme);

    return this.__formUrl(root, path, tail);
};

/**
 * Format URL parameters.
 *
 * @param  {Array|Object} params
 * @return {Array|Object}
 */
UrlGenerator.prototype.__formatParameters = function(params) {
    if(!_.isObject(params)) {
        return params !== undefined? parseArray(params) : [];
    } else {
        return params
    }
};

/**
 * Generate a secure, absolute URL to the given path.
 *
 * @param  {String} path
 * @param  {Array}  parameters
 * @return {String}
 */
UrlGenerator.prototype.secure = function (path, parameters) {
    return this.to(path, parameters, true);
};

/**
 * Generate a URL to an application asset.
 *
 * @param  {String}  path
 * @param  {Boolean|null} secure
 * @return {string}
 */
UrlGenerator.prototype.asset = function (path, secure) {
    if (this.isValidUrl(path)) {
        return path;
    }

    var root = this.__getRootUrl(this.__getScheme(secure));

    return root + '/' + str.trim(path, '/');
};

/**
 * Determine if the given path is a valid URL.
{'fraction_non_alphanumeric': 0.15075905606493312, 'fraction_numerical': 0.00811663911017586, 'mean_word_length': 2.2238372093023258, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4303563', 'n_tokens_mistral': 2102, 'n_tokens_neox': 2070, 'n_words': 631}