AnP/Public/ecma/Models/RouteModel.ecma.js

59 lines
1.5 KiB
JavaScript

"use strict";
import {Check} from "../Utils/Checks.ecma.js";
import {Common} from "../Utils/Common.ecma.js";
/**
* @class RouteModel
* @constructor
* @param {!(string|RegExp)} route
* @param {!string} view
* @returns {void}
* @access public
* @static
*/
export const RouteModel = (function(){
/**
* @constructs RouteModel
* @param {!(string|RegExp)} route
* @param {!string} view
* @returns {void}
* @access private
* @static
*/
const RouteModel = function(route, view){
/** @type {RouteModel} */
const self = this;
/** @type {RegExp|null} */
this.route = null;
/** @type {Array.<string>} */
this.variables = [];
/** @type {string} */
this.view = view;
/**
* @returns {void}
* @access private
*/
const constructor = () => {
if(Check.is_string(route))
route = new RegExp("^" + Common.to_regular_expression(route.replace(RE.STRING_VARIABLES, (_, key) => {
self.variables.push(key);
return "#######";
})).replace(/#{7}/g, "([^\\/]+)") + "\\/?$", "i");
else if(Check.is_regular_expression(route))
(this.route = route).source.replace(/\([^\(\)]+\)/g, all => {
self.variables.push("" + self.variables.length);
return all;
});
};
constructor();
};
return RouteModel;
})();