Skip to content

Commit adeb25d

Browse files
author
Forbes Lindesay
committedAug 26, 2012
Initial, fully working commit
1 parent 410e818 commit adeb25d

11 files changed

+8853
-0
lines changed
 

‎app.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
$(function () {
2+
var t;
3+
$('#inputRoute, #inputPath').keyup(function () {
4+
clearTimeout(t);
5+
t = setTimeout(update, 500);
6+
});
7+
function update() {
8+
console.log('updating');
9+
var keys = [];
10+
var regexp = pathRegexp($('#inputRoute').val(), keys);
11+
$('#regexp-display').html(regexp.toString());
12+
if (keys.length) {
13+
$('#keys-display').html('<ol>' + keys.map(function wrap(key) {
14+
return '<li>' + key.name + (key.optional?' (optional)':'') + '</li>';
15+
}).join('') + '</ol>');
16+
} else {
17+
$('#keys-display').html('There are no keys captured by this route');
18+
}
19+
20+
var path = $('#inputPath').val();
21+
var isMatch = regexp.test(path);
22+
if (isMatch) {
23+
$('.is-not-match').hide();
24+
$('.is-match').show();
25+
var result = regexp.exec(path);
26+
$('#keys-results-display').html('<dl class="dl-horizontal">' + keys.map(function (key, i) {
27+
return '<dt>' + key.name + '</dt><dd>' +
28+
result[i + 1] + '</dd>';
29+
}).join('') + '</dl>');
30+
} else {
31+
$('.is-not-match').show();
32+
$('.is-match').hide();
33+
}
34+
}
35+
update();
36+
37+
/**
38+
* Normalize the given path string,
39+
* returning a regular expression.
40+
*
41+
* An empty array should be passed,
42+
* which will contain the placeholder
43+
* key names. For example "/user/:id" will
44+
* then contain ["id"].
45+
*
46+
* @param {String} path
47+
* @param {Array} keys
48+
* @param {Boolean} sensitive
49+
* @param {Boolean} strict
50+
* @return {String}
51+
* @api private
52+
*/
53+
function pathRegexp(path, keys, sensitive, strict) {
54+
path = path
55+
.concat(strict ? '' : '/?')
56+
.replace(/\/\(/g, '(?:/')
57+
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){
58+
keys.push({ name: key, optional: !! optional });
59+
slash = slash || '';
60+
return ''
61+
+ (optional ? '' : slash)
62+
+ '(?:'
63+
+ (optional ? slash : '')
64+
+ (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
65+
+ (optional || '')
66+
+ (star ? '(/*)?' : '');
67+
})
68+
.replace(/([\/.])/g, '\\$1')
69+
.replace(/\*/g, '(.*)');
70+
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
71+
}
72+
});

‎css/bootstrap-responsive.css

+1,040
Large diffs are not rendered by default.

‎css/bootstrap-responsive.min.css

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎css/bootstrap.css

+5,624
Large diffs are not rendered by default.

‎css/bootstrap.min.css

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎img/glyphicons-halflings-white.png

8.57 KB
Loading

‎img/glyphicons-halflings.png

12.5 KB
Loading

‎index.html

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Express Route Tester</title>
5+
<!-- Bootstrap -->
6+
<link href="css/bootstrap.min.css" rel="stylesheet">
7+
<meta name="viewport"
8+
content="width=device-width, initial-scale=1.0">
9+
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">
10+
</head>
11+
<body>
12+
<div class="container-fluid">
13+
<div class="row-fluid">
14+
<div class="span12">
15+
<h1>Express Route Tester</h1>
16+
<div class="well">
17+
<form class="form-horizontal">
18+
<legend>Enter a route to test</legend>
19+
<div class="control-group">
20+
<label class="control-label" for="inputRoute">Route</label>
21+
<div class="controls">
22+
<input type="text" id="inputRoute"
23+
placeholder="/path/:key/">
24+
</div>
25+
</div>
26+
<div class="control-group">
27+
<label class="control-label" for="inputPath">Path</label>
28+
<div class="controls">
29+
<input type="text" id="inputPath"
30+
placeholder="/path/value/">
31+
</div>
32+
</div>
33+
</form>
34+
</div><!--/well-->
35+
</div><!--/span-->
36+
</div><!--/row-->
37+
<div class="row-fluid">
38+
<div class="span4">
39+
<h2>RegExp</h2>
40+
<pre><code id="regexp-display">/^foo/g</code></pre>
41+
</div><!--/span-->
42+
<div class="span4">
43+
<h2>Keys</h2>
44+
<div id="keys-display">
45+
</div>
46+
</div><!--/span-->
47+
<div class="span4">
48+
<h2>Results</h2>
49+
<div class="alert alert-error is-not-match">
50+
The path does not match the route
51+
</div>
52+
<div class="alert alert-success is-match">
53+
The path matches the route
54+
</div>
55+
<div id="keys-results-display" class="is-match">
56+
</div>
57+
</div><!--/span-->
58+
</div><!--/row-->
59+
</div><!--/container-->
60+
<script src="js/jquery.min.js"></script>
61+
<script src="js/bootstrap.min.js"></script>
62+
<script src="app.js"></script>
63+
</body>
64+
</html>

‎js/bootstrap.js

+2,027
Large diffs are not rendered by default.

‎js/bootstrap.min.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎js/jquery.min.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.