in Ox.api, add default sort, and allow for values to be arrays

This commit is contained in:
rolux 2012-03-31 22:09:55 +02:00
parent 18ba0d5347
commit 4a09d329c8

View file

@ -8,6 +8,7 @@ Ox.api <f> Turns an array into a list API
items <[o]> An array of objects (key/value stores) items <[o]> An array of objects (key/value stores)
options <o> Options object options <o> Options object
enums <o> Enumerables, for example <code>{size: ['S', 'M', 'L', 'XL']}</code> enums <o> Enumerables, for example <code>{size: ['S', 'M', 'L', 'XL']}</code>
sort <[o]|[s]> Default sort, for example <code> ['+name', '-age']
sums <[s]> List of keys to be included in totals sums <[s]> List of keys to be included in totals
unique <s|'id'> The name of the unique key unique <s|'id'> The name of the unique key
<script> <script>
@ -113,9 +114,12 @@ Ox.api <f> Turns an array into a list API
@*/ @*/
Ox.api = function(items, options) { Ox.api = function(items, options) {
var enums = options.enums ? parseEnums(options.enums) : {}, var api = {
sums = options.sums || [], enums: options.enums ? parseEnums(options.enums) : {},
unique = options.unique || 'id'; sort: options.sort || [],
sums: options.sums || [],
unique: options.unique || 'id'
};
function parseEnums(enums) { function parseEnums(enums) {
// make enumerable strings lowercase // make enumerable strings lowercase
@ -142,11 +146,11 @@ Ox.api = function(items, options) {
if (Ox.isString(value)) { if (Ox.isString(value)) {
value = value.toLowerCase(); value = value.toLowerCase();
} }
if (enums[key] && ( if (api.enums[key] && (
operator.indexOf('<') > -1 operator.indexOf('<') > -1
|| operator.indexOf('>') > -1 || operator.indexOf('>') > -1
)) { )) {
value = enums[key].indexOf(value); value = api.enums[key].indexOf(value);
} }
return value; return value;
}); });
@ -190,11 +194,11 @@ Ox.api = function(items, options) {
if (Ox.isString(itemValue)) { if (Ox.isString(itemValue)) {
itemValue = itemValue.toLowerCase(); itemValue = itemValue.toLowerCase();
} }
if (enums[key] && ( if (api.enums[key] && (
operator.indexOf('<') > -1 operator.indexOf('<') > -1
|| operator.indexOf('>') > -1 || operator.indexOf('>') > -1
)) { )) {
itemValue = enums[key].indexOf(itemValue); itemValue = api.enums[key].indexOf(itemValue);
} }
return test[operator](itemValue, value) == !not; return test[operator](itemValue, value) == !not;
} }
@ -233,25 +237,32 @@ Ox.api = function(items, options) {
} }
if (options.sort) { if (options.sort) {
// sort // sort
options.sort = parseSort(options.sort); options.sort = parseSort(Ox.merge(options.sort, api.sort));
Ox.forEach(enums, function(values, key) { options.sort.forEach(function(v) {
sort[key] = function(value) { var key = v.key;
return values.indexOf(value.toLowerCase()); if (api.enums[key]) {
}; sort[key] = function(value) {
}) return api.enums[key].indexOf(value.toLowerCase());
};
} else if (Ox.isArray(items[0][key])) {
sort[key] = function(value) {
return value.join(', ')
};
}
});
result.data.items = Ox.sortBy(result.data.items, options.sort, sort); result.data.items = Ox.sortBy(result.data.items, options.sort, sort);
} }
if (options.positions) { if (options.positions) {
// return positions // return positions
data = {positions: {}}; data = {positions: {}};
options.positions.forEach(function(id) { options.positions.forEach(function(id) {
data.positions[id] = Ox.getIndex(result.data.items, unique, id) data.positions[id] = Ox.getIndex(result.data.items, api.unique, id)
}); });
result.data = data; result.data = data;
} else if (!options.keys) { } else if (!options.keys) {
// return totals // return totals
data = {}; data = {};
sums.forEach(function(key) { api.sums.forEach(function(key) {
data[key] = Ox.sum(result.data.items.map(function(item) { data[key] = Ox.sum(result.data.items.map(function(item) {
return item[key]; return item[key];
})); }));
@ -262,8 +273,8 @@ Ox.api = function(items, options) {
// return items // return items
if (!Ox.isEmpty(options.keys)) { if (!Ox.isEmpty(options.keys)) {
// filter keys // filter keys
if (options.keys.indexOf(unique) == -1) { if (options.keys.indexOf(api.unique) == -1) {
options.keys.push(unique); options.keys.push(api.unique);
} }
result.data.items = result.data.items.map(function(item) { result.data.items = result.data.items.map(function(item) {
var ret = {}; var ret = {};