-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathutil.js
More file actions
111 lines (96 loc) · 3.45 KB
/
util.js
File metadata and controls
111 lines (96 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { exec, pathRankSort, segmentize, rank, strip } from 'src/util';
describe('util', () => {
describe('strip', () => {
it('should strip preceeding slashes', () => {
expect(strip('')).to.equal('');
expect(strip('/')).to.equal('');
expect(strip('/a')).to.equal('a');
expect(strip('//a')).to.equal('a');
expect(strip('//a/')).to.equal('a');
});
it('should strip trailing slashes', () => {
expect(strip('')).to.equal('');
expect(strip('/')).to.equal('');
expect(strip('a/')).to.equal('a');
expect(strip('/a//')).to.equal('a');
});
});
describe('rank', () => {
it('should return rank of path segments', () => {
expect(rank('')).to.eql('5');
expect(rank('/')).to.eql('5');
expect(rank('//')).to.eql('5');
expect(rank('a/b/c')).to.eql('555');
expect(rank('/a/b/c/')).to.eql('555');
expect(rank('/:a/b?/:c?/:d*/:e+')).to.eql('45312');
});
});
describe('segmentize', () => {
it('should split path on slashes', () => {
expect(segmentize('')).to.eql(['']);
expect(segmentize('/')).to.eql(['']);
expect(segmentize('//')).to.eql(['']);
expect(segmentize('a/b/c')).to.eql(['a','b','c']);
expect(segmentize('/a/b/c/')).to.eql(['a','b','c']);
});
});
describe('pathRankSort', () => {
it('should sort by highest rank first', () => {
let paths = arr => arr.map( path => ({attributes:{path}}) );
expect(
paths(['/:a*','/a','/:a+','/:a?','/a/:b*']).sort(pathRankSort)
).to.eql(
paths(['/a/:b*','/a','/:a?','/:a+','/:a*'])
);
});
it('should return default routes last', () => {
let paths = arr => arr.map( path => ({attributes:{path}}) );
let defaultPath = {attributes:{default:true}};
let p = paths(['/a/b/','/a/b','/','b']);
p.splice(2,0,defaultPath);
expect(
p.sort(pathRankSort)
).to.eql(
paths(['/a/b/','/a/b','/','b']).concat(defaultPath)
);
});
});
describe('exec', () => {
it('should match explicit equality', () => {
expect(exec('/','/')).to.eql({});
expect(exec('/a','/a')).to.eql({});
expect(exec('/a','/b')).to.eql(false);
expect(exec('/a/b','/a/b')).to.eql({});
expect(exec('/a/b','/a/a')).to.eql(false);
expect(exec('/a/b','/b/b')).to.eql(false);
});
it('should match param segments', () => {
expect(exec('/', '/:foo')).to.eql(false);
expect(exec('/bar', '/:foo')).to.eql({ foo:'bar' });
});
it('should match optional param segments', () => {
expect(exec('/', '/:foo?')).to.eql({ foo:'' });
expect(exec('/bar', '/:foo?')).to.eql({ foo:'bar' });
expect(exec('/', '/:foo?/:bar?')).to.eql({ foo:'', bar:'' });
expect(exec('/bar', '/:foo?/:bar?')).to.eql({ foo:'bar', bar:'' });
expect(exec('/bar', '/:foo?/bar')).to.eql(false);
expect(exec('/foo/bar', '/:foo?/bar')).to.eql({ foo:'foo' });
});
it('should match splat param segments', () => {
expect(exec('/', '/:foo*')).to.eql({ foo:'' });
expect(exec('/a', '/:foo*')).to.eql({ foo:'a' });
expect(exec('/a/b', '/:foo*')).to.eql({ foo:'a/b' });
expect(exec('/a/b/c', '/:foo*')).to.eql({ foo:'a/b/c' });
});
it('should match required splat param segments', () => {
expect(exec('/', '/:foo+')).to.eql(false);
expect(exec('/a', '/:foo+')).to.eql({ foo:'a' });
expect(exec('/a/b', '/:foo+')).to.eql({ foo:'a/b' });
expect(exec('/a/b/c', '/:foo+')).to.eql({ foo:'a/b/c' });
});
it('should handle query-string', () => {
expect(exec('/?foo=bar', '/')).to.eql({ foo: 'bar' });
expect(exec('/a?foo=bar', '/:foo')).to.eql({ foo: 'a' });
});
});
});