Skip to content

Commit 333d46b

Browse files
author
deiu
committed
Added extra checks for basename()
1 parent fd8e951 commit 333d46b

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎lib/utils.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ function uriBase(req) {
4343
}
4444

4545
function basename(fullpath) {
46-
var bname = (fullpath.lastIndexOf('/') === fullpath.length - 1)?'':path.basename(fullpath);
46+
var bname = '';
47+
if (fullpath) {
48+
bname = (fullpath.lastIndexOf('/') === fullpath.length - 1)?'':path.basename(fullpath);
49+
}
4750
return bname;
4851
}
4952

‎test/utils.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ var utils = require('../lib/utils');
55
describe('Utility functions', function() {
66

77
describe('basename', function() {
8-
it('should return bar as relative path', function() {
8+
it('should return bar as relative path for /foo/bar', function() {
99
assert.equal(utils.basename('/foo/bar'), 'bar');
1010
});
11-
it('should return empty as relative path', function() {
11+
it('should return empty as relative path for /foo/', function() {
1212
assert.equal(utils.basename('/foo/'), '');
1313
});
14-
it('should return empty as relative path', function() {
14+
it('should return empty as relative path for /', function() {
1515
assert.equal(utils.basename('/'), '');
1616
});
17+
it('should return empty as relative path for empty path', function() {
18+
assert.equal(utils.basename(''), '');
19+
});
20+
it('should return empty as relative path for undefined path', function() {
21+
assert.equal(utils.basename(undefined), '');
22+
});
1723
});
1824
});

0 commit comments

Comments
 (0)