in Ox.api, add default sort, and allow for values to be arrays
This commit is contained in:
parent
18ba0d5347
commit
4a09d329c8
1 changed files with 28 additions and 17 deletions
|
@ -8,6 +8,7 @@ Ox.api <f> Turns an array into a list API
|
|||
items <[o]> An array of objects (key/value stores)
|
||||
options <o> Options object
|
||||
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
|
||||
unique <s|'id'> The name of the unique key
|
||||
<script>
|
||||
|
@ -113,9 +114,12 @@ Ox.api <f> Turns an array into a list API
|
|||
@*/
|
||||
Ox.api = function(items, options) {
|
||||
|
||||
var enums = options.enums ? parseEnums(options.enums) : {},
|
||||
sums = options.sums || [],
|
||||
unique = options.unique || 'id';
|
||||
var api = {
|
||||
enums: options.enums ? parseEnums(options.enums) : {},
|
||||
sort: options.sort || [],
|
||||
sums: options.sums || [],
|
||||
unique: options.unique || 'id'
|
||||
};
|
||||
|
||||
function parseEnums(enums) {
|
||||
// make enumerable strings lowercase
|
||||
|
@ -142,11 +146,11 @@ Ox.api = function(items, options) {
|
|||
if (Ox.isString(value)) {
|
||||
value = value.toLowerCase();
|
||||
}
|
||||
if (enums[key] && (
|
||||
if (api.enums[key] && (
|
||||
operator.indexOf('<') > -1
|
||||
|| operator.indexOf('>') > -1
|
||||
)) {
|
||||
value = enums[key].indexOf(value);
|
||||
value = api.enums[key].indexOf(value);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
@ -190,11 +194,11 @@ Ox.api = function(items, options) {
|
|||
if (Ox.isString(itemValue)) {
|
||||
itemValue = itemValue.toLowerCase();
|
||||
}
|
||||
if (enums[key] && (
|
||||
if (api.enums[key] && (
|
||||
operator.indexOf('<') > -1
|
||||
|| operator.indexOf('>') > -1
|
||||
)) {
|
||||
itemValue = enums[key].indexOf(itemValue);
|
||||
itemValue = api.enums[key].indexOf(itemValue);
|
||||
}
|
||||
return test[operator](itemValue, value) == !not;
|
||||
}
|
||||
|
@ -233,25 +237,32 @@ Ox.api = function(items, options) {
|
|||
}
|
||||
if (options.sort) {
|
||||
// sort
|
||||
options.sort = parseSort(options.sort);
|
||||
Ox.forEach(enums, function(values, key) {
|
||||
options.sort = parseSort(Ox.merge(options.sort, api.sort));
|
||||
options.sort.forEach(function(v) {
|
||||
var key = v.key;
|
||||
if (api.enums[key]) {
|
||||
sort[key] = function(value) {
|
||||
return values.indexOf(value.toLowerCase());
|
||||
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);
|
||||
}
|
||||
if (options.positions) {
|
||||
// return positions
|
||||
data = {positions: {}};
|
||||
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;
|
||||
} else if (!options.keys) {
|
||||
// return totals
|
||||
data = {};
|
||||
sums.forEach(function(key) {
|
||||
api.sums.forEach(function(key) {
|
||||
data[key] = Ox.sum(result.data.items.map(function(item) {
|
||||
return item[key];
|
||||
}));
|
||||
|
@ -262,8 +273,8 @@ Ox.api = function(items, options) {
|
|||
// return items
|
||||
if (!Ox.isEmpty(options.keys)) {
|
||||
// filter keys
|
||||
if (options.keys.indexOf(unique) == -1) {
|
||||
options.keys.push(unique);
|
||||
if (options.keys.indexOf(api.unique) == -1) {
|
||||
options.keys.push(api.unique);
|
||||
}
|
||||
result.data.items = result.data.items.map(function(item) {
|
||||
var ret = {};
|
||||
|
|
Loading…
Reference in a new issue