Merge pull request #120 from Mikxail/patch-1
Update lib/adapters/memory.js
This commit is contained in:
commit
8389fa2de6
|
@ -70,29 +70,20 @@ Memory.prototype.all = function all(model, filter, callback) {
|
||||||
// do we need some sorting?
|
// do we need some sorting?
|
||||||
if (filter.order) {
|
if (filter.order) {
|
||||||
var props = this._models[model].properties;
|
var props = this._models[model].properties;
|
||||||
var allNumeric = true;
|
|
||||||
var orders = filter.order;
|
var orders = filter.order;
|
||||||
var reverse = false;
|
|
||||||
if (typeof filter.order === "string") {
|
if (typeof filter.order === "string") {
|
||||||
orders = [filter.order];
|
orders = [filter.order];
|
||||||
}
|
}
|
||||||
orders.forEach(function (key, i) {
|
orders.forEach(function (key, i) {
|
||||||
|
var reverse = 1;
|
||||||
var m = key.match(/\s+(A|DE)SC$/i);
|
var m = key.match(/\s+(A|DE)SC$/i);
|
||||||
if (m) {
|
if (m) {
|
||||||
key = key.replace(/\s+(A|DE)SC/i, '');
|
key = key.replace(/\s+(A|DE)SC/i, '');
|
||||||
if (m[1] === 'DE') reverse = true;
|
if (m[1] === 'DE') reverse = -1;
|
||||||
}
|
|
||||||
orders[i] = key;
|
|
||||||
if (props[key].type.name !== 'Number' && props[key].type.name !== 'Date') {
|
|
||||||
allNumeric = false;
|
|
||||||
}
|
}
|
||||||
|
orders[i] = {"key": key, "reverse": reverse};
|
||||||
});
|
});
|
||||||
if (allNumeric) {
|
nodes = nodes.sort(sorting.bind(orders));
|
||||||
nodes = nodes.sort(numerically.bind(orders));
|
|
||||||
} else {
|
|
||||||
nodes = nodes.sort(literally.bind(orders));
|
|
||||||
}
|
|
||||||
if (reverse) nodes = nodes.reverse();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,13 +91,16 @@ Memory.prototype.all = function all(model, filter, callback) {
|
||||||
callback(null, nodes);
|
callback(null, nodes);
|
||||||
});
|
});
|
||||||
|
|
||||||
function numerically(a, b) {
|
function sorting(a, b) {
|
||||||
return a[this[0]] - b[this[0]];
|
for (var i=0, l=this.length; i<l; i++) {
|
||||||
}
|
if (a[this[i].key] > b[this[i].key]) {
|
||||||
|
return 1*this[i].reverse;
|
||||||
function literally(a, b) {
|
} else if (a[this[i].key] < b[this[i].key]) {
|
||||||
return a[this[0]] > b[this[0]];
|
return -1*this[i].reverse;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function applyFilter(filter) {
|
function applyFilter(filter) {
|
||||||
|
|
Loading…
Reference in New Issue