este-library-oldschool
Version:
Library for github.com/steida/este.git
148 lines (139 loc) • 3.31 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
/**
@fileoverview este.labs.Route.
TODO: Add tests for path Array.<string>.
*/
goog.provide('este.labs.Route');
/**
@param {string|Array.<string>} path
@constructor
*/
este.labs.Route = function(path1) {
this.path = path1;
this.keys = [];
this.pathToRegExp();
}
/**
@type {Array.<Object>}
@protected
*/
este.labs.Route.prototype.keys = null;
/**
@type {RegExp}
@protected
*/
este.labs.Route.prototype.regexp = null;
/**
@param {string} url
@return {boolean}
*/
este.labs.Route.prototype.match = function(url) {
return !!this.getMatches(url);
};
/**
TODO: Refactor.
@param {string} url
@return {Object}
*/
este.labs.Route.prototype.params = function(url) {
var i, j, key, len, match, matches, params, value;
matches = this.getMatches(url);
if (!matches) {
return null;
}
params = null;
for (i = j = 0, len = matches.length; j < len; i = ++j) {
match = matches[i];
if (!i) {
continue;
}
key = this.keys[i - 1];
value = typeof match === 'string' ? this.decodeMatch(match) : match;
if (key) {
if (params == null) {
params = {};
}
params[key.name] = value;
} else {
if (params == null) {
params = [];
}
params.push(value);
}
}
return params;
};
/**
@param {(Object|Array)} params
@return {string}
*/
este.labs.Route.prototype.url = function(params) {
var index, key, regex, url, value;
if (Array.isArray(params)) {
index = 0;
url = this.path.replace(/\*/g, function() {
return params[index++];
});
} else {
url = this.path;
for (key in params) {
value = params[key];
if (value === void 0) {
value = '';
}
regex = new RegExp("\\:" + key + "\\??");
url = url.replace(regex, value);
}
url = url.replace(/\:[^\/]*/g, '');
}
if (url.length > 1) {
url = url.replace(/[.\/]+$/g, '');
}
return url;
};
/**
@param {string} url
@return {Array.<string>}
@protected
*/
este.labs.Route.prototype.getMatches = function(url) {
var index, pathname;
index = url.indexOf('?');
pathname = index > -1 ? url.slice(0, index) : url;
return this.regexp.exec(pathname);
};
/**
@param {string} str
@return {string}
@protected
*/
este.labs.Route.prototype.decodeMatch = function(str) {
var e, error;
try {
return decodeURIComponent(str);
} catch (error) {
e = error;
}
return str;
};
/**
@protected
*/
este.labs.Route.prototype.pathToRegExp = function() {
var path;
path = this.path;
if (Array.isArray(path)) {
path = '(' + path.join('|') + ')';
}
path = path.concat('/?').replace(/\/\(/g, '(?:/').replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, (function(_this) {
return function(_, slash, format, key, capture, optional, star) {
_this.keys.push({
name: key,
optional: !!optional
});
slash = slash || '';
return (optional ? '' : slash) + '(?:' + (optional ? slash : '') + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')' + (optional || '') + (star ? '(/*)?' : '');
};
})(this)).replace(/([\/.])/g, '\\$1').replace(/\*/g, '(.*)');
return this.regexp = new RegExp("^" + path + "$", 'i');
};