Browse Source

added response data sorting middleware

pull/850/head
Stephen Hess 8 years ago
parent
commit
8429e0f22a
  1. 19
      middleware/sortResponseData.js
  2. 88
      test/unit/middleware/sortResponseData.js
  3. 1
      test/unit/run.js

19
middleware/sortResponseData.js

@ -0,0 +1,19 @@
const _ = require('lodash');
function setup(comparator) {
function middleware(req, res, next) {
// do nothing if there's nothing to do
if (_.isEmpty(_.get(res, 'data', []))) {
return next();
}
res.data = res.data.sort(comparator);
next();
}
return middleware;
}
module.exports = setup;

88
test/unit/middleware/sortResponseData.js

@ -0,0 +1,88 @@
const sortResponseData = require('../../../middleware/sortResponseData');
module.exports.tests = {};
module.exports.tests.doIt = (test, common) => {
test('undefined res should return without interacting with comparator', (t) => {
const comparator = () => {
throw Error('should not have been called');
};
const sort = sortResponseData(comparator);
sort(undefined, undefined, () => {
t.end();
});
});
test('undefined res.data should return without interacting with comparator', (t) => {
const comparator = () => {
throw Error('should not have been called');
};
const sort = sortResponseData(comparator);
const res = {};
sort(undefined, res, () => {
t.deepEquals(res, {});
t.end();
});
});
test('empty res.data should not cause problems', (t) => {
const comparator = () => {
throw Error('should not have been called');
};
const sort = sortResponseData(comparator);
const res = {
data: []
};
sort(undefined, res, () => {
t.deepEquals(res.data, [], 'res.data should still be empty');
t.end();
});
});
test('comparator should be consulted for sorting res.data when defined', (t) => {
const comparator = (a, b) => {
return a.key > b.key;
};
const sort = sortResponseData(comparator);
const res = {
data: [
{ key: 3 },
{ key: 2 },
{ key: 1 },
]
};
sort(undefined, res, () => {
t.deepEquals(res.data.shift(), { key: 1 });
t.deepEquals(res.data.shift(), { key: 2 });
t.deepEquals(res.data.shift(), { key: 3 });
t.end();
});
});
};
module.exports.all = (tape, common) => {
function test(name, testFunction) {
return tape(`[middleware] sortResponseData: ${name}`, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

1
test/unit/run.js

@ -40,6 +40,7 @@ var tests = [
require('./middleware/parseBBox'), require('./middleware/parseBBox'),
require('./middleware/sendJSON'), require('./middleware/sendJSON'),
require('./middleware/normalizeParentIds'), require('./middleware/normalizeParentIds'),
require('./middleware/sortResponseData'),
require('./middleware/trimByGranularity'), require('./middleware/trimByGranularity'),
require('./middleware/trimByGranularityStructured'), require('./middleware/trimByGranularityStructured'),
require('./middleware/requestLanguage'), require('./middleware/requestLanguage'),

Loading…
Cancel
Save