Skip to content

Commit

Permalink
feat(sort): Add sort option
Browse files Browse the repository at this point in the history
  • Loading branch information
thaiat committed Nov 23, 2015
1 parent f39ad77 commit ba03c53
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});
};
Expand All @@ -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
Expand All @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('</h1>')[1];
var urls = body.split(/<a href="([^"]*)"/).filter(function(s, i){ return i%2; });
console.log(urls)
assert.deepEqual(urls, [
'/%E3%81%95%E3%81%8F%E3%82%89.txt',
'/users',
'/todo.txt',
'/nums',
'/g%23%20%253%20o%20%26%20%252525%20%2537%20dir',
'/foo%20%26%20bar',
'/file%20%231.txt',
'/collect',
'/%23directory'
]);
done();
});
});
});

describe('with "template" option', function () {
describe('when setting a custom template file', function () {
var server;
Expand Down

0 comments on commit ba03c53

Please sign in to comment.