diff --git a/README.md b/README.md index 5dde8372..9db87c36 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,24 @@ Display icons. Defaults to `false`. Optional path to a CSS stylesheet. Defaults to a built-in stylesheet. +##### sort + +Apply this sort function to files. Optional. The `sort` function is +called for each file tuple with the signature `sort(file1, file2)`. +An example for sorting files by date of modification (descending) is: +```js +function(file1, file2) { + // sort ".." to the top + if (file1.name === '..' || file2.name === '..') { + return file1.name === file2.name ? 0 + : file1.name === '..' ? -1 : 1; + } + // sort directories first then files by date of modification + return Number(file2.stat && file2.stat.isDirectory()) - Number(file1.stat && file1.stat.isDirectory()) || + new Date(file2.stat.mtime) - new Date(file1.stat.mtime); +} +``` + ##### template Optional path to an HTML template or a function that will render a HTML diff --git a/index.js b/index.js index 7b9d856c..4e3c1400 100644 --- a/index.js +++ b/index.js @@ -97,7 +97,7 @@ function serveIndex(root, options) { var stylesheet = opts.stylesheet || defaultStylesheet; var template = opts.template || defaultTemplate; var view = opts.view || 'tiles'; - + var sort = opts.sort || fileSort; return function (req, res, next) { if (req.method !== 'GET' && req.method !== 'HEAD') { res.statusCode = 'OPTIONS' === req.method ? 200 : 405; @@ -160,7 +160,7 @@ function serveIndex(root, options) { // not acceptable if (!type) return next(createError(406)); - serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet); + serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet, sort); }); }); }; @@ -170,7 +170,7 @@ function serveIndex(root, options) { * Respond with text/html. */ -serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet) { +serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet, sort) { var render = typeof template !== 'function' ? createHtmlRender(template) : template @@ -189,7 +189,7 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path }); // sort file list - fileList.sort(fileSort); + fileList.sort(sort); // read stylesheet fs.readFile(stylesheet, 'utf8', function (err, style) { diff --git a/test/test.js b/test/test.js index 408ba92d..d6d818aa 100644 --- a/test/test.js +++ b/test/test.js @@ -304,6 +304,36 @@ describe('serveIndex(root)', function () { }); }); + describe('with "sort" option', function () { + it('should include icons for html', function (done) { + var server = createServer(fixtures, {'sort': function (a, b) { + return String(b.name).toLocaleLowerCase().localeCompare(String(a.name).toLocaleLowerCase()); + }}); + + request(server) + .get('/') + .expect(200) + .end(function (err, res) { + if (err) done(err); + var body = res.text.split('')[1]; + var urls = body.split(/