From cf055b79cdc082e1a1f7e2b241f6cde81ac3d119 Mon Sep 17 00:00:00 2001 From: Avi Haiat Date: Mon, 23 Nov 2015 03:23:41 +0200 Subject: [PATCH 01/10] feat(sort): Add sort option Fixes #35 --- README.md | 18 ++++++++++++++++++ index.js | 8 ++++---- test/test.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e124605..9b204099 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,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 e707bbc5..f304bf48 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 b0c9531d..22ecaedb 100644 --- a/test/test.js +++ b/test/test.js @@ -343,6 +343,35 @@ 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(/ Date: Sun, 12 Aug 2018 20:03:49 -0700 Subject: [PATCH 02/10] eslint --fix test/ --- test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 22ecaedb..51d34d2e 100644 --- a/test/test.js +++ b/test/test.js @@ -343,10 +343,10 @@ describe('serveIndex(root)', function () { }); }); - describe('with "sort" option', 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()); + return String(b.name).toLocaleLowerCase().localeCompare(String(a.name).toLocaleLowerCase()); }}); request(server) From 76ed184829ba036c0f7d8917bc26e62d2990dfb3 Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Sun, 12 Aug 2018 20:07:19 -0700 Subject: [PATCH 03/10] js --> javascript --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9b204099..3a11e91e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ $ npm install serve-index ## API -```js +```javascript var serveIndex = require('serve-index') ``` @@ -62,7 +62,7 @@ Optional path to a CSS stylesheet. Defaults to a built-in stylesheet. 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 +```javascript function(file1, file2) { // sort ".." to the top if (file1.name === '..' || file2.name === '..') { @@ -110,7 +110,7 @@ Display mode. `tiles` and `details` are available. Defaults to `tiles`. ### Serve directory indexes with vanilla node.js http server -```js +```javascript var finalhandler = require('finalhandler') var http = require('http') var serveIndex = require('serve-index') @@ -137,7 +137,7 @@ server.listen(3000) ### Serve directory indexes with express -```js +```javascript var express = require('express') var serveIndex = require('serve-index') From 367251fa1e29c9f62a7e059a13f5289bb90f4f58 Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Sun, 12 Aug 2018 20:12:39 -0700 Subject: [PATCH 04/10] fix linting error in javascript example in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a11e91e..92b2ce72 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ 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: ```javascript -function(file1, file2) { +function sortbyModifiedDate(file1, file2) { // sort ".." to the top if (file1.name === '..' || file2.name === '..') { return file1.name === file2.name ? 0 From 93aca7afbd878550a97de15bc4be75e768ac548d Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Sun, 12 Aug 2018 20:13:50 -0700 Subject: [PATCH 05/10] Revert "js --> javascript" This reverts commit 76ed184829ba036c0f7d8917bc26e62d2990dfb3. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 92b2ce72..8ad2e12e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ $ npm install serve-index ## API -```javascript +```js var serveIndex = require('serve-index') ``` @@ -62,7 +62,7 @@ Optional path to a CSS stylesheet. Defaults to a built-in stylesheet. 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: -```javascript +```js function sortbyModifiedDate(file1, file2) { // sort ".." to the top if (file1.name === '..' || file2.name === '..') { @@ -110,7 +110,7 @@ Display mode. `tiles` and `details` are available. Defaults to `tiles`. ### Serve directory indexes with vanilla node.js http server -```javascript +```js var finalhandler = require('finalhandler') var http = require('http') var serveIndex = require('serve-index') @@ -137,7 +137,7 @@ server.listen(3000) ### Serve directory indexes with express -```javascript +```js var express = require('express') var serveIndex = require('serve-index') From d6677e7314791a18edf33497428c524cd5d39230 Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Wed, 17 Jul 2019 22:59:47 -0700 Subject: [PATCH 06/10] sort the json and plaintext response types as well --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index cee1edb3..270ad333 100644 --- a/index.js +++ b/index.js @@ -217,13 +217,13 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path * Respond with application/json. */ -serveIndex.json = function _json (req, res, files, next, dir, showUp, icons, path) { +serveIndex.json = function _json (req, res, files, next, dir, showUp, icons, path, sort) { // stat all files stat(path, files, function (err, fileList) { if (err) return next(err) // sort file list - fileList.sort(fileSort) + fileList.sort(sort) // serialize var body = JSON.stringify(fileList.map(function (file) { @@ -238,13 +238,13 @@ serveIndex.json = function _json (req, res, files, next, dir, showUp, icons, pat * Respond with text/plain. */ -serveIndex.plain = function _plain (req, res, files, next, dir, showUp, icons, path) { +serveIndex.plain = function _plain (req, res, files, next, dir, showUp, icons, path, sort) { // stat all files stat(path, files, function (err, fileList) { if (err) return next(err) // sort file list - fileList.sort(fileSort) + fileList.sort(sort) // serialize var body = fileList.map(function (file) { From f304cb9fe9653178efe63a1ee0f32e92c2cf0e42 Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Wed, 17 Jul 2019 23:22:39 -0700 Subject: [PATCH 07/10] fix some failing tests, ensure that sort is defined --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 270ad333..56187f11 100644 --- a/index.js +++ b/index.js @@ -164,7 +164,8 @@ 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, sort); + console.log('sort at top', sort) + serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, sort, view, template, stylesheet); }); }); }; @@ -174,7 +175,7 @@ function serveIndex(root, options) { * Respond with text/html. */ -serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet, sort) { +serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, sort, view, template, stylesheet) { var render = typeof template !== 'function' ? createHtmlRender(template) : template @@ -218,10 +219,13 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path */ serveIndex.json = function _json (req, res, files, next, dir, showUp, icons, path, sort) { + console.log('sort at top of json', sort) // stat all files stat(path, files, function (err, fileList) { if (err) return next(err) + console.log('sort from json', sort) + // sort file list fileList.sort(sort) From 3fcf196ea85d36367dbbb99263ed567d82cbae41 Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Wed, 17 Jul 2019 23:24:39 -0700 Subject: [PATCH 08/10] add new ninth parameter 'sort' to serveIndex.html function in tests --- test/test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test.js b/test/test.js index 67363b23..c90f1e20 100644 --- a/test/test.js +++ b/test/test.js @@ -613,7 +613,7 @@ describe('serveIndex(root)', function () { it('should get template path', function (done) { var server = createServer() - serveIndex.html = function (req, res, files, next, dir, showUp, icons, path, view, template) { + serveIndex.html = function (req, res, files, next, dir, showUp, icons, path, sort, view, template) { res.setHeader('Content-Type', 'text/html') res.end(String(fs.existsSync(template))) } @@ -627,7 +627,7 @@ describe('serveIndex(root)', function () { it('should get template with tokens', function (done) { var server = createServer() - serveIndex.html = function (req, res, files, next, dir, showUp, icons, path, view, template) { + serveIndex.html = function (req, res, files, next, dir, showUp, icons, path, sort, view, template) { res.setHeader('Content-Type', 'text/html') res.end(fs.readFileSync(template, 'utf8')) } @@ -645,7 +645,7 @@ describe('serveIndex(root)', function () { it('should get stylesheet path', function (done) { var server = createServer() - serveIndex.html = function (req, res, files, next, dir, showUp, icons, path, view, template, stylesheet) { + serveIndex.html = function (req, res, files, next, dir, showUp, icons, path, sort, view, template, stylesheet) { res.setHeader('Content-Type', 'text/html') res.end(String(fs.existsSync(stylesheet))) } From 9dfb97c154830be0c274b541e91170c2bf2bc3f1 Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Wed, 17 Jul 2019 23:25:17 -0700 Subject: [PATCH 09/10] remove dev logging --- index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.js b/index.js index 56187f11..a71b84b8 100644 --- a/index.js +++ b/index.js @@ -164,7 +164,6 @@ function serveIndex(root, options) { // not acceptable if (!type) return next(createError(406)); - console.log('sort at top', sort) serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, sort, view, template, stylesheet); }); }); @@ -219,13 +218,10 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path */ serveIndex.json = function _json (req, res, files, next, dir, showUp, icons, path, sort) { - console.log('sort at top of json', sort) // stat all files stat(path, files, function (err, fileList) { if (err) return next(err) - console.log('sort from json', sort) - // sort file list fileList.sort(sort) From e9c7d550acfcad5a69d356bb8df9b306a6db1d54 Mon Sep 17 00:00:00 2001 From: Micah Stubbs Date: Thu, 18 Jul 2019 00:55:01 -0700 Subject: [PATCH 10/10] add small README change to trigger new CI run --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ad2e12e..fa5e5acd 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ function sortbyModifiedDate(file1, file2) { return file1.name === file2.name ? 0 : file1.name === '..' ? -1 : 1; } - // sort directories first then files by date of modification + // sort directories first then sort 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); }