Skip to content

Commit e269a8c

Browse files
committed
Release 1.6.0. Several improvements:
- parth.cache -> parth.store, we are not caching here at the moment - simplified version of path lookup, now we can infer path depth directly using the `masterRE` - parth.set and parth.get returns a regex with useful properties - less properties attached to the optional object given - number parameters are parsed
1 parent ee8b94b commit e269a8c

14 files changed

Lines changed: 235 additions & 283 deletions

example.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
'use strict';
22

3-
var input, path, regex, extra;
3+
var input, regex, extra;
44
var parth = require('./.')();
55

66
// #set
77
input = [
8-
'obj.path.here', 'obj.path.:here(\\w+)',
9-
'obj.:path(\\d+).:here(\\w+)', ':obj.:path(\\w+).:here(\\d+)',
10-
'get /page/view', 'get /:page(\\w+(?:end))/view', ':method(get|post) /:page/:view',
8+
'obj.path.here',
9+
'obj.path.:here(\\w+)',
10+
'obj.:path(\\d+).:here(\\w+)',
11+
':obj.:path(\\w+).:here(\\d+)',
12+
'get /page/view',
13+
'get /:page(\\w+(?:end))/baby user.:data(\\d+).:drink :when',
14+
':method(get|post) /:page/:view',
1115
];
1216

1317
console.log('\n -- parth.set -- \n');
@@ -21,8 +25,13 @@ input.forEach(function(stem, index){
2125

2226
// #get
2327
input = [
24-
'obj.path.10', 'obj.10.prop', 'obj.10.10', 'array.method.prop',
25-
'get /weekend/view?query', 'get /user/view/#hash', 'post /user/page/?query=name&path=tree#hash'
28+
'obj.path.10',
29+
'obj.10.prop',
30+
'obj.10.10',
31+
'array.method.prop',
32+
'get /weekend/baby?query=string#hash user.10.beers now',
33+
'get /user/view/#hash',
34+
'post /user/page/?query=name&path=tree#hash'
2635
];
2736

2837
console.log('\n -- parth.get -- ');

index.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function Parth(){
2929
// --
3030
//
3131

32-
util.paramRE = /(^|\W)\:([^()?#\.\/ ]+)(\(.+?\))?/g;
32+
util.paramRE = /(^|\W)\:([^()?#\.\/ ]+)(\(+[^ ]*\)+)?/g;
3333

3434
Parth.prototype.set = function(p, o){
3535
o = o || { }; if(!util.boil(p, o)){ return this; }
@@ -38,11 +38,11 @@ Parth.prototype.set = function(p, o){
3838
var store = this.store;
3939

4040
// number of default and custom regex
41-
o.custom = o.default = o.depth;
41+
o.custom = o.default = 0;
4242
o.regex = '^' + o.path.replace(/\S+/g, function(stem){
4343
o.sep = (/\//).test(stem) ? '/#?' : '.';
4444
return stem.replace(util.paramRE, function($0, $1, $2, $3){
45-
if($3){ o.custom--; } else { o.default--; }
45+
if($3){ o.custom++; } else { o.default++; }
4646
return $1 + ($3 || '([^' + o.sep + '^]+)');
4747
});
4848
}).replace(/[\/\.]/g, '\\$&') // scape path tokens
@@ -64,7 +64,7 @@ Parth.prototype.set = function(p, o){
6464
// reorder them
6565
store.regex[o.depth].push(o.regex);
6666
store.regex[o.depth] = store.regex[o.depth].sort(function(a, b){
67-
return ((a.def - 2*a.cust) + (b.def - 2*b.cust)) || -Infinity;
67+
return ((a.def - a.cust) - (b.def - b.cust));
6868
});
6969

7070
// sum up all learned: void groups and make it one
@@ -107,18 +107,24 @@ Parth.prototype.get = function(p, o){
107107
} else { o.depth--; }
108108
}
109109

110-
o.index = o.found.indexOf(o.found.join(''));
110+
var found = o.found.join('');
111+
o.index = o.found.indexOf(found);
111112
o.regex = this.store.regex[o.depth][o.index];
112113

113114
o.index = 0;
114115
o.params = { _ : o.path.match(o.regex).slice(1) };
115-
o.notFound = o.path.replace(
116-
o.regex.path.replace(util.paramRE, function($0, $1, $2){
117-
var p = o.params._[o.index];
118-
o.params[$2] = o.params._[o.index++] = Number(p) || p;
119-
return $1 + p;
120-
}), '')[0] || ' ';
121-
122-
o.notFound = !(/^[ ]/).test(o.notFound);
123-
return o.regex;
116+
o.regex.path.replace(util.paramRE, function($0, $1, $2){
117+
var p = o.params._[o.index];
118+
o.params[$2] = o.params._[o.index++] = Number(p) || p;
119+
return $1 + p;
120+
});
121+
122+
o.notFound = Boolean(o.path.replace(found, '').trim());
123+
return util.merge(new RegExp(o.regex.source), {
124+
notFound: o.notFound,
125+
url: o.url,
126+
path: o.regex.path,
127+
argv: o.regex.argv.slice(),
128+
params: o.params
129+
});
124130
};

lib/boil.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ exports = module.exports = boil;
2020
// --
2121

2222
util.boilRE = /((?:\/)[^\/]+|[^\. ]+\.)/g;
23-
util.stripUrlRE = /(?:\/)?[?#]+[^\W ]+$|\/$/g;
23+
util.stripUrlRE = /(?:\/)?[?#]+[^: ]+$|\/$/g;
2424

2525
function boil(p, o){
2626
p = util.type(p);

lib/fold.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict';
22

3+
var util = require('./util');
4+
35
exports = module.exports = foldPath;
46

5-
var foldRE = /((?:\/|\?|\#)[^\/\?\# ]+)[ ]+/g;
7+
util.foldRE = /((?:[ ]+)(?:\/|\?|\#)[^\/\?\# ]+)/g;
68

79
function foldPath(stem){
8-
var depth, path = stem.replace(/(\.)[ ]+/g, '$1');
9-
if((depth = (path.match(foldRE) || [ ]).length)){
10-
path = path.replace(foldRE, function($0, $1){
11-
if(--depth){ return $1; } return $0;
12-
});
13-
}
14-
return path.replace(/[ ]+/g, ' ').trim();
10+
var notFirst;
11+
return stem.replace(/(\.)[ ]+/g, '$1')
12+
.replace(util.foldRE, function($0, $1){
13+
if(notFirst){ return $1.trim(); }
14+
notFirst = true;
15+
return $0;
16+
});
1517
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "parth",
33
"main": "index.js",
4-
"version": "1.3.12",
4+
"version": "1.6.0",
55
"engines": {
66
"node": ">= 0.9.0"
77
},

0 commit comments

Comments
 (0)