Skip to content

Commit

Permalink
Add more path utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed May 17, 2015
1 parent 6291787 commit 6eba291
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
29 changes: 23 additions & 6 deletions modules/PathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

function escapeSource(string) {
return escapeRegExp(string).replace(/\/+/g, '/+');
}

function _compilePattern(pattern) {
var escapedSource = '';
var paramNames = [];
Expand All @@ -18,7 +22,7 @@ function _compilePattern(pattern) {
while (match = matcher.exec(pattern)) {
if (match.index !== lastIndex) {
tokens.push(pattern.slice(lastIndex, match.index));
escapedSource += escapeRegExp(pattern.slice(lastIndex, match.index));
escapedSource += escapeSource(pattern.slice(lastIndex, match.index));
}

if (match[1]) {
Expand All @@ -40,7 +44,7 @@ function _compilePattern(pattern) {

if (lastIndex !== pattern.length) {
tokens.push(pattern.slice(lastIndex, pattern.length));
escapedSource += escapeRegExp(pattern.slice(lastIndex, pattern.length));
escapedSource += escapeSource(pattern.slice(lastIndex, pattern.length));
}

return {
Expand All @@ -60,15 +64,28 @@ function compilePattern(pattern) {
return _compiledPatterns[pattern];
}

function stripLeadingSlashes(path) {
return path ? path.replace(/^\/+/, '') : '';
}

function stripTrailingSlashes(path) {
return path.replace(/\/+$/, '');
}

function isAbsolutePath(path) {
return typeof path === 'string' && path.charAt(0) === '/';
}

var PathUtils = {

compilePattern,
stripLeadingSlashes,
stripTrailingSlashes,
isAbsolutePath,

/**
* Returns true if the given path is absolute.
*/
isAbsolute: function (path) {
return path.charAt(0) === '/';
},

/**
* Joins two URL paths together.
*/
Expand Down
12 changes: 2 additions & 10 deletions modules/findMatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var { withoutQuery, extractQuery, extractParams, compilePattern } = require('./PathUtils');
var { withoutQuery, extractQuery, extractParams, compilePattern, stripLeadingSlashes, stripTrailingSlashes } = require('./PathUtils');
var { loopAsync } = require('./AsyncUtils');

function getChildRoutes(route, callback) {
Expand All @@ -7,18 +7,10 @@ function getChildRoutes(route, callback) {
} else if (route.getChildRoutes) {
route.getChildRoutes(callback);
} else {
callback(null, null);
callback();
}
}

function stripLeadingSlashes(path) {
return path ? path.replace(/^\/+/, '') : '';
}

function stripTrailingSlashes(path) {
return path.replace(/\/+$/, '');
}

function assignParams(params, paramNames, paramValues) {
return paramNames.reduceRight(function (params, paramName, index) {
var paramValue = paramValues[index];
Expand Down

0 comments on commit 6eba291

Please sign in to comment.