Moved DataAccessObject.sortByIds to utils.js

This commit is contained in:
Fabien Franzen 2014-08-15 19:39:18 +02:00
parent c599de42cd
commit 9f1c5d9c37
4 changed files with 64 additions and 51 deletions

View File

@ -341,41 +341,10 @@ DataAccessObject.findByIds = function(ids, cond, cb) {
filter.where[pk] = { inq: ids };
mergeQuery(filter, cond || {});
this.find(filter, function(err, results) {
cb(err, err ? results : this.sortByIds(ids, results));
cb(err, err ? results : utils.sortObjectsByIds(pk, ids, results));
}.bind(this));
};
DataAccessObject.sortByIds = function(ids, results) {
var pk = this.dataSource.idName(this.modelName) || 'id';
ids = ids.map(function(id) {
return (typeof id === 'object') ? id.toString() : id;
});
var indexOf = function(x) {
var id = (typeof x[pk] === 'object') ? x[pk].toString() : x[pk];
return ids.indexOf(id);
};
var heading = [];
var tailing = [];
results.forEach(function(x) {
var idx = indexOf(x);
idx === -1 ? tailing.push(x) : heading.push(x);
});
heading.sort(function(x, y) {
var a = indexOf(x);
var b = indexOf(y);
if (a === -1 || b === -1) return 1; // last
if (a === b) return 0;
if (a > b) return 1;
if (a < b) return -1;
});
return heading.concat(tailing);
};
function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return cb();

View File

@ -6,6 +6,7 @@ exports.parseSettings = parseSettings;
exports.mergeSettings = mergeSettings;
exports.isPlainObject = isPlainObject;
exports.defineCachedRelations = defineCachedRelations;
exports.sortObjectsByIds = sortObjectsByIds;
var traverse = require('traverse');
@ -203,4 +204,39 @@ function defineCachedRelations(obj) {
function isPlainObject(obj) {
return (typeof obj === 'object') && (obj !== null)
&& (obj.constructor === Object);
}
}
function sortObjectsByIds(idName, ids, objects) {
ids = ids.map(function(id) {
return (typeof id === 'object') ? id.toString() : id;
});
var indexOf = function(x) {
var isObj = (typeof x[idName] === 'object'); // ObjectID
var id = isObj ? x[idName].toString() : x[idName];
return ids.indexOf(id);
};
var heading = [];
var tailing = [];
objects.forEach(function(x) {
if (typeof x === 'object') {
var idx = indexOf(x);
idx === -1 ? tailing.push(x) : heading.push(x);
}
});
heading.sort(function(x, y) {
var a = indexOf(x);
var b = indexOf(y);
if (a === -1 || b === -1) return 1; // last
if (a === b) return 0;
if (a > b) return 1;
if (a < b) return -1;
});
return heading.concat(tailing);
};

View File

@ -87,24 +87,6 @@ describe('basic-querying', function () {
done();
});
});
it('should sortByIds', function(done) {
User.find(function(err, users) {
sorted = User.sortByIds([6, 5, 4, 3, 2, 1], users);
var names = sorted.map(function(u) { return u.name; });
names.should.eql(['f', 'e', 'd', 'c', 'b', 'a']);
done();
});
});
it('should sortByIds - partial ids', function(done) {
User.find(function(err, users) {
sorted = User.sortByIds([5, 3, 2], users);
var names = sorted.map(function(u) { return u.name; });
names.should.eql(['e', 'c', 'b', 'a', 'd', 'f']);
done();
});
});
});

View File

@ -3,6 +3,7 @@ var utils = require('../lib/utils');
var fieldsToArray = utils.fieldsToArray;
var removeUndefined = utils.removeUndefined;
var mergeSettings = utils.mergeSettings;
var sortObjectsByIds = utils.sortObjectsByIds;
describe('util.fieldsToArray', function () {
it('Turn objects and strings into an array of fields to include when finding models', function () {
@ -185,4 +186,29 @@ describe('mergeSettings', function () {
should.deepEqual(dst.acls, expected.acls, 'Merged settings should match the expectation');
});
});
describe('sortObjectsByIds', function () {
var items = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'd' },
{ id: 5, name: 'e' },
{ id: 6, name: 'f' }
];
it('should sort', function() {
var sorted = sortObjectsByIds('id', [6, 5, 4, 3, 2, 1], items);
var names = sorted.map(function(u) { return u.name; });
should.deepEqual(names, ['f', 'e', 'd', 'c', 'b', 'a']);
});
it('should sort - partial ids', function() {
var sorted =sortObjectsByIds('id', [5, 3, 2], items);
var names = sorted.map(function(u) { return u.name; });
should.deepEqual(names, ['e', 'c', 'b', 'a', 'd', 'f']);
});
});