Ox.api: cache sort; Ox.sortBy: improve performance
This commit is contained in:
parent
178e2bc487
commit
1f4b0f7faa
1 changed files with 25 additions and 13 deletions
|
@ -161,6 +161,7 @@ Ox.api = function(items, options) {
|
||||||
},
|
},
|
||||||
fn = function(options, callback) {
|
fn = function(options, callback) {
|
||||||
var data,
|
var data,
|
||||||
|
keys,
|
||||||
result = {data: {}, status: {code: 200, text: 'ok'}},
|
result = {data: {}, status: {code: 200, text: 'ok'}},
|
||||||
sort = {};
|
sort = {};
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
@ -175,7 +176,14 @@ Ox.api = function(items, options) {
|
||||||
}
|
}
|
||||||
if (options.sort && result.data.items.length > 1) {
|
if (options.sort && result.data.items.length > 1) {
|
||||||
// sort
|
// sort
|
||||||
options.sort = parseSort(options.sort.concat(api.sort));
|
keys = [];
|
||||||
|
options.sort = parseSort(
|
||||||
|
options.sort.concat(api.sort)
|
||||||
|
).filter(function(v) {
|
||||||
|
var ret = keys.indexOf(v.key) == -1;
|
||||||
|
keys.push(v.key);
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
options.sort.forEach(function(v) {
|
options.sort.forEach(function(v) {
|
||||||
var key = v.key;
|
var key = v.key;
|
||||||
if (api.enums[key]) {
|
if (api.enums[key]) {
|
||||||
|
@ -188,7 +196,9 @@ Ox.api = function(items, options) {
|
||||||
};
|
};
|
||||||
}*/
|
}*/
|
||||||
});
|
});
|
||||||
result.data.items = Ox.sortBy(result.data.items, options.sort, sort);
|
if (options.keys || options.positions) {
|
||||||
|
result.data.items = sortBy(result.data.items, options.sort, sort, options.query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (options.positions) {
|
if (options.positions) {
|
||||||
// return positions
|
// return positions
|
||||||
|
@ -268,7 +278,14 @@ Ox.api = function(items, options) {
|
||||||
}
|
}
|
||||||
callback && callback(result);
|
callback && callback(result);
|
||||||
return result;
|
return result;
|
||||||
};
|
},
|
||||||
|
sortBy = Ox.cache(function sortBy(array, by, map, query) {
|
||||||
|
return Ox.sortBy(array, by, map);
|
||||||
|
}, {
|
||||||
|
key: function(args) {
|
||||||
|
return JSON.stringify([args[1], args[3]])
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function parseEnums(enums) {
|
function parseEnums(enums) {
|
||||||
// make enumerable strings lowercase
|
// make enumerable strings lowercase
|
||||||
|
@ -552,22 +569,17 @@ Ox.range = function() {
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
function getSortValues(array, map) {
|
function getSortValues(array, map) {
|
||||||
var mappedArray = map ? array.map(map) : array,
|
var mappedArray = map ? array.map(map) : array, length = 0, sort = {};
|
||||||
length, matches = [], sort = {};
|
// find numbers, and length of longest number
|
||||||
// find numbers
|
|
||||||
array.forEach(function(value, i) {
|
array.forEach(function(value, i) {
|
||||||
var match, mappedValue = mappedArray[i];
|
var match, mappedValue = mappedArray[i];
|
||||||
if (Ox.isString(mappedValue)) {
|
if (Ox.isString(mappedValue)) {
|
||||||
match = mappedValue.match(/\d+/g);
|
match = mappedValue.match(/\d+/g);
|
||||||
if (match) {
|
if (match && match.length > length) {
|
||||||
matches = matches.concat(match);
|
length = match.length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// get length of longest number
|
|
||||||
length = Ox.max(Ox.map(matches, function(value) {
|
|
||||||
return value.length;
|
|
||||||
}));
|
|
||||||
// make lowercase, remove leading non-word characters,
|
// make lowercase, remove leading non-word characters,
|
||||||
// pad numbers and move leading articles to the end
|
// pad numbers and move leading articles to the end
|
||||||
array.forEach(function(value, i) {
|
array.forEach(function(value, i) {
|
||||||
|
|
Loading…
Reference in a new issue