Add dir argument to filter function

fixes #11
closes #18
This commit is contained in:
Evgenus
2014-10-03 08:56:01 +03:00
committed by Douglas Christopher Wilson
parent 5e050a7d23
commit cececc8c2e
4 changed files with 29 additions and 2 deletions
+1
View File
@@ -1,6 +1,7 @@
unreleased
==========
* Add `dir` argument to `filter` function
* Support using tokens multiple times
1.3.1 / 2014-10-01
+5 -1
View File
@@ -35,7 +35,11 @@ Serve index accepts these properties in the options object.
##### filter
Apply this filter function to files. Defaults to `false`.
Apply this filter function to files. Defaults to `false`. The `filter` function
is called for each file, with the signature `filter(filename, index, files, dir)`
where `filename` is the name of the file, `index` is the array index, `files` is
the array of files and `dir` is the absolute path the file is located (and thus,
the directory the listing is for).
##### hidden
+3 -1
View File
@@ -142,7 +142,9 @@ exports = module.exports = function serveIndex(root, options){
fs.readdir(path, function(err, files){
if (err) return next(err);
if (!hidden) files = removeHidden(files);
if (filter) files = files.filter(filter);
if (filter) files = files.filter(function(filename, index, list) {
return filter(filename, index, list, path);
});
files.sort();
// content-negotiation
+20
View File
@@ -264,6 +264,26 @@ describe('serveIndex(root)', function () {
done()
});
});
it('should filter directory paths', function (done) {
var seen = false
var server = createServer(fixtures, {'filter': filter})
function filter(name, index, list, dir) {
if (path.normalize(dir) === path.normalize(path.join(fixtures, '/users'))) {
seen = true
}
return true
}
request(server)
.get('/users')
.expect(200, function (err, res) {
if (err) return done(err)
seen.should.be.true
done()
});
});
});
describe('with "icons" option', function () {