From 171400fea8bb3b78689cb8c283f78da0172faac4 Mon Sep 17 00:00:00 2001 From: Daley Date: Wed, 18 May 2016 16:22:56 +0800 Subject: [PATCH 1/2] add sort option --- index.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7b9d856c..76043aaa 100644 --- a/index.js +++ b/index.js @@ -97,6 +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; return function (req, res, next) { if (req.method !== 'GET' && req.method !== 'HEAD') { @@ -160,7 +161,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 +171,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 @@ -187,9 +188,11 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path var fileList = files.map(function (file, i) { return { name: file, stat: stats[i] }; }); - + + + sort=sort||serveIndex.fileSortWithName; // sort file list - fileList.sort(fileSort); + fileList.sort(sort); // read stylesheet fs.readFile(stylesheet, 'utf8', function (err, style) { @@ -341,6 +344,22 @@ function fileSort(a, b) { String(a.name).toLocaleLowerCase().localeCompare(String(b.name).toLocaleLowerCase()); } +/** + * Sort function for with directories first.then mtime. + */ +function fileSortWithMtime(a,b){ + if (a.name === '..' || b.name === '..') { + return a.name === b.name ? 0 + : a.name === '..' ? -1 : 1; + } + var tb=b.stat?b.stat.mtime:0; + var ta=a.stat?a.stat.mtime:0; + + return Number(b.stat && b.stat.isDirectory()) - Number(a.stat && a.stat.isDirectory()) ||(tb-ta); +}; +serveIndex.fileSortWithName=fileSort; +serveIndex.fileSortWithMTime=fileSortWithMtime; + /** * Map html `dir`, returning a linked path. */ From adb1d88d62fc630acbe32c6367d3e56fb9f38751 Mon Sep 17 00:00:00 2001 From: Daley Date: Wed, 18 May 2016 16:29:17 +0800 Subject: [PATCH 2/2] add sort option --- README.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dde8372..7d9267e9 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,10 @@ provided locals: Display mode. `tiles` and `details` are available. Defaults to `tiles`. +##### sort + +A function to sort directorys and files, default is serveIndex.fileSortWithName,you can use serveIndex.fileSortWithMTime instead,or write one. + ## Examples ### Serve directory indexes with vanilla node.js http server diff --git a/package.json b/package.json index a6b8da51..7838c50c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "serve-index", "description": "Serve directory listings", - "version": "1.7.3", + "version": "1.7.4", "author": "Douglas Christopher Wilson ", "license": "MIT", "repository": "expressjs/serve-index",