in Ox.api, use Ox.indexOf, not Ox.getIndex; faster Ox.merge

This commit is contained in:
rolux 2012-05-21 22:10:51 +02:00
parent bf9a19a71e
commit bfa9394850

View file

@ -1,6 +1,6 @@
'use strict';
/*@
/*
Ox.api <f> Turns an array into a list API
<code>Ox.api</code> takes an array and returns a function that allows you to
run complex queries against it. See the examples below for details.
@ -113,7 +113,7 @@ Ox.api <f> Turns an array into a list API
{positions: {foo: 2, bar: 0}}
> Ox.test.apiResults[8].data
{items: [{i: 2, size: 'L'}, {i: 1, size: 'M'}]}
@*/
*/
Ox.api = function(items, options) {
var api = {
@ -159,7 +159,9 @@ Ox.api = function(items, options) {
// return positions
data = {positions: {}};
options.positions.forEach(function(id) {
data.positions[id] = Ox.getIndex(result.data.items, api.unique, id)
data.positions[id] = Ox.indexOf(result.data.items, function(item) {
return item[api.unique] == id;
});
});
result.data = data;
} else if (!options.keys) {
@ -356,9 +358,11 @@ Ox.flatten = function(arr) {
return ret;
};
/*
Ox.indexOf = function(arr) {
// indexOf for primitives, test for function, deep equal for others
};
*/
/*@
Ox.merge <f> Merges an array with one or more other arrays
@ -368,13 +372,17 @@ Ox.merge <f> Merges an array with one or more other arrays
> Ox.merge(1, [2, 3, 2], 1)
[1, 2, 3, 2, 1]
@*/
// FIXME: a1.push.apply(a1, a2) should be much faster
Ox.merge = function(arr) {
arr = Ox.makeArray(arr);
Ox.forEach(Array.prototype.slice.call(arguments, 1), function(arg) {
Ox.forEach(Ox.makeArray(arg), function(val) {
Ox.slice(arguments, 1).forEach(function(arg) {
arg = Ox.makeArray(arg);
if (arg.length < Ox.STACK_SIZE) {
arr.push.apply(arr, arg);
} else {
arg.forEach(function(val) {
arr.push(val);
});
}
});
return arr;
};