add Ox.api, to apify and array
This commit is contained in:
parent
b807fc81af
commit
7b4002b340
1 changed files with 171 additions and 1 deletions
|
@ -1,5 +1,175 @@
|
|||
'use strict';
|
||||
|
||||
/*@
|
||||
Ox.api <f> Turns an array into a list API
|
||||
(items) -> <f> List API
|
||||
(items, keys) -> <f> List API
|
||||
items <[o]> An array of objects
|
||||
keys <[s]> Array of keys to be included in totals
|
||||
<script>
|
||||
Ox.test.api = Ox.api([
|
||||
{id: 'foo', n: 2},
|
||||
{id: 'bar', n: 2},
|
||||
{id: 'baz', n: 1}
|
||||
], ['n']);
|
||||
Ox.test.apiResults = {
|
||||
0: Ox.test.api(),
|
||||
1: Ox.test.api({
|
||||
keys: []
|
||||
}),
|
||||
2: Ox.test.api({
|
||||
keys: ['id'],
|
||||
sort: ['-n', '+id']
|
||||
}),
|
||||
3: Ox.test.api({
|
||||
keys: [],
|
||||
query: {
|
||||
conditions: [
|
||||
{key: 'id', value: 'f', operator: '!^'},
|
||||
{key: 'n', value: 1, operator: '>'}
|
||||
],
|
||||
operator: '&'
|
||||
}
|
||||
}),
|
||||
4: Ox.test.api({
|
||||
keys: [],
|
||||
query: {
|
||||
conditions: [
|
||||
{key: 'id', value: 'O', operator: '='},
|
||||
{key: 'n', value: [1, 2], operator: '='}
|
||||
],
|
||||
operator: '|'
|
||||
},
|
||||
sort: ['+id']
|
||||
}),
|
||||
5: Ox.test.api({
|
||||
keys: [],
|
||||
query: {
|
||||
conditions: [
|
||||
{key: 'id', value: 'f', operator: '='},
|
||||
{
|
||||
conditions: [
|
||||
{key: 'id', value: 'a', operator: '='},
|
||||
{key: 'id', value: 'z', operator: '='}
|
||||
],
|
||||
operator: '&'
|
||||
}
|
||||
],
|
||||
operator: '|'
|
||||
},
|
||||
sort: ['+id']
|
||||
}),
|
||||
6: Ox.test.api({
|
||||
keys: [],
|
||||
range: [1, 2],
|
||||
sort: ['+id']
|
||||
})
|
||||
};
|
||||
</script>
|
||||
> Ox.test.apiResults[0].data
|
||||
{items: 3, n: 5}
|
||||
> Ox.test.apiResults[1].data
|
||||
{items: [{id: 'foo', n: 2}, {id: 'bar', n: 2}, {id: 'baz', n: 1}]}
|
||||
> Ox.test.apiResults[2].data
|
||||
{items: [{id: 'bar'}, {id: 'foo'}, {id: 'baz'}]}
|
||||
> Ox.test.apiResults[3].data
|
||||
{items: [{id: 'bar', n: 2}]}
|
||||
> Ox.test.apiResults[4].data
|
||||
{items: [{id: 'baz', n: 1}, {id: 'foo', n: 2}]}
|
||||
> Ox.test.apiResults[5].data
|
||||
{items: [{id: 'baz', n: 1}, {id: 'foo', n: 2}]}
|
||||
> Ox.test.apiResults[6].data
|
||||
{items: [{id: 'baz', n: 1}]}
|
||||
@*/
|
||||
Ox.api = function(items, keys) {
|
||||
keys = keys || [];
|
||||
function testCondition(item, condition) {
|
||||
var key = condition.key,
|
||||
operator = condition.operator.replace('!', ''),
|
||||
value = condition.value,
|
||||
not = condition.operator[0] == '!',
|
||||
itemValue = item[key],
|
||||
test = {
|
||||
'=': function(a, b) {
|
||||
return Ox.isArray(b) ? a >= b[0] && a < b[1]
|
||||
: Ox.isString(a) ? a.indexOf(b) > -1
|
||||
: a === b;
|
||||
},
|
||||
'==': function(a, b) { return a === b; },
|
||||
'<': function(a, b) { return a < b; },
|
||||
'>': function(a, b) { return a > b; },
|
||||
'<=': function(a, b) { return a <= b; },
|
||||
'>=': function(a, b) { return a >= b; },
|
||||
'^': function(a, b) { return Ox.starts(a, b); },
|
||||
'$': function(a, b) { return Ox.ends(a, b); },
|
||||
};
|
||||
if (Ox.isString(value)) {
|
||||
value = value.toLowerCase();
|
||||
}
|
||||
if (Ox.isString(itemValue)) {
|
||||
itemValue = itemValue.toLowerCase();
|
||||
}
|
||||
return test[operator](itemValue, value) == !not;
|
||||
}
|
||||
function testQuery(item, query) {
|
||||
var match = true;
|
||||
Ox.forEach(query.conditions, function(condition) {
|
||||
if (condition.conditions) {
|
||||
match = testQuery(item, condition);
|
||||
} else {
|
||||
match = testCondition(item, condition)
|
||||
}
|
||||
if (
|
||||
(query.operator == '&' && !match)
|
||||
|| (query.operator == '|' && match)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return match;
|
||||
}
|
||||
return function(options) {
|
||||
var data,
|
||||
result = {data: {}, status: {code: 200, text: 'ok'}};
|
||||
options = options || {};
|
||||
if (options.query) {
|
||||
result.data.items = items.filter(function(item) {
|
||||
return testQuery(item, options.query);
|
||||
});
|
||||
} else {
|
||||
result.data.items = Ox.clone(items);
|
||||
}
|
||||
if (options.keys) {
|
||||
if (options.sort) {
|
||||
result.data.items = Ox.sortBy(result.data.items, options.sort);
|
||||
}
|
||||
if (!Ox.isEmpty(options.keys)) {
|
||||
result.data.items = result.data.items.map(function(item) {
|
||||
var ret = {};
|
||||
options.keys.forEach(function(key) {
|
||||
ret[key] = item[key];
|
||||
});
|
||||
return ret;
|
||||
});
|
||||
}
|
||||
if (options.range) {
|
||||
result.data.items = Ox.sub(
|
||||
result.data.items, options.range[0], options.range[1]
|
||||
);
|
||||
}
|
||||
} else {
|
||||
data = {items: result.data.items.length};
|
||||
keys.forEach(function(key) {
|
||||
data[key] = Ox.sum(result.data.items.map(function(item) {
|
||||
return item[key];
|
||||
}));
|
||||
})
|
||||
result.data = data;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.compact <f> Removes <code>null</code> or <code>undefined</code> values
|
||||
> Ox.compact([null,,1,,2,,3])
|
||||
|
@ -151,7 +321,7 @@ Ox.range = function() {
|
|||
var length = by.length, values = {};
|
||||
by = by.map(function(v) {
|
||||
return {
|
||||
key: v.replace(/^[\+\-]/g, ''),
|
||||
key: v.replace(/^[\+\-]/, ''),
|
||||
operator: v[0] == '-' ? '-' : '+'
|
||||
};
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue