2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.avg <f> Returns the average of an array's values, or an object's properties
|
|
|
|
(collection) -> <n> Average value
|
|
|
|
collection <[n]|o> Array or object with numerical values
|
|
|
|
> Ox.avg([-1, 0, 1])
|
|
|
|
0
|
|
|
|
> Ox.avg({a: 1, b: 2, c: 3})
|
|
|
|
2
|
|
|
|
> Ox.avg('avg is 0.1')
|
|
|
|
0.1
|
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.avg = function(collection) {
|
|
|
|
return Ox.sum(collection) / Ox.len(collection);
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-06-02 14:08:01 +00:00
|
|
|
Ox.clone <f> Returns a (shallow or deep) copy of an array or object
|
2012-05-19 08:06:49 +00:00
|
|
|
> (function() { var a = ['v'], b = Ox.clone(a); a[0] = null; return b[0]; }())
|
2011-10-07 01:04:47 +00:00
|
|
|
'v'
|
2012-05-19 08:06:49 +00:00
|
|
|
> (function() { var a = {k: 'v'}, b = Ox.clone(a); a.k = null; return b.k; }())
|
2011-10-07 01:04:47 +00:00
|
|
|
'v'
|
|
|
|
> Ox.clone(0)
|
|
|
|
0
|
2012-06-02 14:08:01 +00:00
|
|
|
> (function() { var a = [[0, 1]], b = Ox.clone(a); a[0][0] = null; return b[0]; }())
|
|
|
|
[null, 1]
|
|
|
|
> (function() { var a = [[0, 1]], b = Ox.clone(a, true); a[0][0] = null; return b[0]; }())
|
|
|
|
[0, 1]
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-04 09:26:01 +00:00
|
|
|
Ox.clone = function(collection, deep) {
|
2012-05-25 07:39:33 +00:00
|
|
|
var ret, type = Ox.typeOf(collection);
|
2012-06-24 13:32:35 +00:00
|
|
|
if (type != 'array' && type != 'object') {
|
|
|
|
ret = collection;
|
|
|
|
} else if (deep) {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = type == 'array' ? [] : {};
|
|
|
|
Ox.forEach(collection, function(value, key) {
|
|
|
|
type = Ox.typeOf(value);
|
|
|
|
ret[key] = type == 'array' || type == 'object'
|
|
|
|
? Ox.clone(value, true) : value;
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
} else {
|
2013-11-29 20:15:02 +00:00
|
|
|
ret = type == 'array' ? collection.slice() : Ox.extend({}, collection);
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2012-05-19 08:06:49 +00:00
|
|
|
/*@
|
|
|
|
Ox.contains <f> Tests if a collection contains a value
|
2012-06-02 14:08:01 +00:00
|
|
|
(collection, value) -> <b> If true, the collection contains the value
|
|
|
|
collection <a|o|s> Collection
|
|
|
|
value <*> Any value
|
2012-05-19 08:06:49 +00:00
|
|
|
> Ox.contains(['foo', 'bar'], 'foo')
|
|
|
|
true
|
|
|
|
> Ox.contains({foo: 'bar'}, 'bar')
|
|
|
|
true
|
|
|
|
> Ox.contains({foo: 'bar'}, 'foo')
|
|
|
|
false
|
2012-06-02 14:08:01 +00:00
|
|
|
> Ox.contains('foobar', 'bar')
|
2012-05-19 08:06:49 +00:00
|
|
|
true
|
|
|
|
@*/
|
2012-05-24 17:11:48 +00:00
|
|
|
// FIXME: a shorter name would be nice (but IE8 doesn't like 'in')
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.contains = function(collection, value) {
|
|
|
|
return (
|
|
|
|
Ox.isObject(collection) ? Ox.values(collection) : collection
|
|
|
|
).indexOf(value) > -1;
|
2012-05-19 08:06:49 +00:00
|
|
|
};
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.count <f> Counts the occurences of values in a collection
|
2012-06-02 14:08:01 +00:00
|
|
|
(collection) -> <o> Number of occurrences per value
|
|
|
|
(collection, value) -> <n> Number of occurrences of the given value
|
|
|
|
collection <a|o|s> Collection
|
|
|
|
value <*> Any value
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.count(['f', 'o', 'o'])
|
|
|
|
{f: 1, o: 2}
|
|
|
|
> Ox.count({a: 'f', b: 'o', c: 'o'})
|
|
|
|
{f: 1, o: 2}
|
|
|
|
> Ox.count('foo')
|
|
|
|
{f: 1, o: 2}
|
2012-05-30 22:48:55 +00:00
|
|
|
> Ox.count('foo', 'f')
|
|
|
|
1
|
|
|
|
> Ox.count('foo', 'x')
|
|
|
|
0
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-30 22:48:55 +00:00
|
|
|
Ox.count = function(collection, value) {
|
|
|
|
var count = {};
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.forEach(collection, function(value) {
|
2012-05-30 22:48:55 +00:00
|
|
|
count[value] = (count[value] || 0) + 1;
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2012-05-30 22:48:55 +00:00
|
|
|
return value ? count[value] || 0 : count;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.every <f> Tests if every element of a collection satisfies a given condition
|
2012-06-02 14:08:01 +00:00
|
|
|
Unlike `Array.prototype.every`, `Ox.every` works for arrays, objects and
|
|
|
|
strings.
|
2013-01-03 14:56:55 +00:00
|
|
|
(collection[, iterator]) -> <b> True if every element passes the test
|
2012-06-02 14:08:01 +00:00
|
|
|
collection <a|o|s> Collection
|
|
|
|
iterator <f> Iterator
|
|
|
|
value <*> Value
|
|
|
|
key <n|s> Index or key
|
|
|
|
collection <a|o|s> The collection
|
2012-05-25 14:25:35 +00:00
|
|
|
> Ox.every([0, 1, 2], function(v, i) { return v == i; })
|
2011-10-07 01:04:47 +00:00
|
|
|
true
|
|
|
|
> Ox.every({a: 1, b: 2, c: 3}, function(v) { return v == 1; })
|
|
|
|
false
|
|
|
|
> Ox.every("foo", function(v) { return v == 'f'; })
|
|
|
|
false
|
|
|
|
> Ox.every([true, true, true])
|
|
|
|
true
|
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.every = function(collection, iterator) {
|
|
|
|
return Ox.filter(
|
|
|
|
Ox.values(collection), iterator || Ox.identity
|
|
|
|
).length == Ox.len(collection);
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.filter <f> Filters a collection by a given condition
|
2012-06-02 14:08:01 +00:00
|
|
|
Unlike `Array.prototype.filter`, `Ox.filter` works for arrays, objects and
|
|
|
|
strings.
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.filter([2, 1, 0], function(v, i) { return v == i; })
|
|
|
|
[1]
|
2012-01-04 17:27:32 +00:00
|
|
|
> Ox.filter({a: 'c', b: 'b', c: 'a'}, function(v, k) { return v == k; })
|
|
|
|
{b: 'b'}
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.filter(' foo bar ', function(v) { return v != ' '; })
|
|
|
|
'foobar'
|
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.filter = function(collection, iterator, that) {
|
|
|
|
var ret, type = Ox.typeOf(collection);
|
|
|
|
iterator = iterator || Ox.identity;
|
2012-06-17 14:41:39 +00:00
|
|
|
if (type == 'object' || type == 'storage') {
|
2012-05-21 20:17:33 +00:00
|
|
|
ret = {};
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.forEach(collection, function(value, key) {
|
|
|
|
if (iterator.call(that, value, key, collection)) {
|
2012-06-10 09:00:12 +00:00
|
|
|
ret[key] = value;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2012-05-21 20:17:33 +00:00
|
|
|
});
|
|
|
|
} else {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = Ox.toArray(collection).filter(iterator, that);
|
2012-05-21 20:17:33 +00:00
|
|
|
if (type == 'string') {
|
|
|
|
ret = ret.join('');
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2012-05-21 20:17:33 +00:00
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2012-05-25 22:42:32 +00:00
|
|
|
/*@
|
2011-10-07 01:04:47 +00:00
|
|
|
Ox.forEach <f> forEach loop
|
2012-07-05 08:58:08 +00:00
|
|
|
`Ox.forEach` loops over arrays, objects and strings. Returning `false` from
|
|
|
|
the iterator acts like a `break` statement. Unlike `for`, which leaks its
|
|
|
|
counter variable to the outer scope, `Ox.forEach` returns it.
|
2012-06-02 14:08:01 +00:00
|
|
|
(collection, iterator[, that]) -> <n> Next index
|
|
|
|
collection <a|o|s> Collection
|
|
|
|
iterator <f> Iterator
|
2011-10-07 01:04:47 +00:00
|
|
|
value <*> Value
|
2012-06-02 14:08:01 +00:00
|
|
|
key <n|s> Index or key
|
|
|
|
collection <a|o|s> The collection
|
|
|
|
that <o> The iterator's `this` binding
|
2011-10-07 01:04:47 +00:00
|
|
|
<script>
|
|
|
|
Ox.test.string = "";
|
2012-06-02 14:08:01 +00:00
|
|
|
Ox.forEach(['f', 'o', 'o'], function(v, i) { Ox.test.string += i; });
|
|
|
|
Ox.forEach({a: 'f', b: 'o', c: 'o'}, function(v, k) { Ox.test.string += k; });
|
2011-10-07 01:04:47 +00:00
|
|
|
Ox.forEach("foo", function(v) { Ox.test.string += v; });
|
|
|
|
</script>
|
|
|
|
> Ox.test.string
|
|
|
|
"012abcfoo"
|
2012-06-02 14:08:01 +00:00
|
|
|
> Ox.forEach({a: 'f', b: 'o', c: 'o'}, function(v, k) { return v != 'o' });
|
|
|
|
1
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.forEach = function(collection, iterator, that) {
|
2012-06-02 14:08:01 +00:00
|
|
|
var i = 0, key, type = Ox.typeOf(collection);
|
2012-07-05 08:58:08 +00:00
|
|
|
if (type == 'object' || type == 'storage') {
|
|
|
|
for (key in collection) {
|
2013-01-03 14:56:55 +00:00
|
|
|
if (
|
|
|
|
Ox.hasOwn(collection, key)
|
|
|
|
&& iterator.call(that, collection[key], key, collection) === false
|
|
|
|
) {
|
|
|
|
break;
|
2012-05-21 20:17:33 +00:00
|
|
|
}
|
2012-07-05 08:58:08 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
collection = Ox.toArray(collection);
|
|
|
|
for (i = 0; i < collection.length; i++) {
|
2013-01-03 14:56:55 +00:00
|
|
|
if (
|
|
|
|
i in collection
|
|
|
|
&& iterator.call(that, collection[i], i, collection) === false
|
|
|
|
) {
|
|
|
|
break;
|
2012-05-21 20:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2012-06-02 14:08:01 +00:00
|
|
|
return i;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.indexOf <f> Returns the first index of a collection element that passes a test
|
2012-05-21 20:17:33 +00:00
|
|
|
> Ox.indexOf([1, 2, 3], function(val) { return val % 2 == 0; })
|
|
|
|
1
|
2012-05-25 07:39:33 +00:00
|
|
|
> Ox.indexOf({a: 1, b: 2, c: 3}, function(val) { return val % 2 == 0; })
|
|
|
|
'b'
|
|
|
|
> Ox.indexOf('FooBar', function(val) { return val == val.toUpperCase(); })
|
|
|
|
0
|
|
|
|
> Ox.indexOf([1, 2, 3], function(val) { return val == 0; })
|
|
|
|
-1
|
2012-05-21 20:17:33 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.indexOf = function(collection, test) {
|
|
|
|
var index = Ox.forEach(collection, function(value) {
|
2012-07-05 08:58:08 +00:00
|
|
|
return !test(value); // break if test succeeds
|
2012-05-21 20:17:33 +00:00
|
|
|
});
|
2012-06-02 14:08:01 +00:00
|
|
|
return Ox.isObject(collection) ? Object.keys(collection)[index] || null
|
|
|
|
: index == collection.length ? -1 : index;
|
2012-05-21 20:17:33 +00:00
|
|
|
};
|
|
|
|
|
2012-05-22 23:17:17 +00:00
|
|
|
/*@
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.indicesOf <f> Returns all indices of collection elements that pass a test
|
|
|
|
> Ox.indicesOf([1, 2, 3], function(val) { return val % 2 == 1; })
|
|
|
|
[0, 2]
|
|
|
|
> Ox.indicesOf({a: 1, b: 2, c: 3}, function(val) { return val % 2 == 1; })
|
|
|
|
['a', 'c']
|
|
|
|
> Ox.indicesOf('FooBar', function(val) { return val == val.toUpperCase(); })
|
|
|
|
[0, 3]
|
|
|
|
> Ox.indicesOf([1, 2, 3], function(val) { return val == 0; })
|
|
|
|
[]
|
2012-05-22 23:17:17 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.indicesOf = function(collection, test) {
|
|
|
|
var ret = [];
|
|
|
|
Ox.forEach(collection, function(value, index) {
|
|
|
|
test(value) && ret.push(index);
|
2012-05-22 14:29:37 +00:00
|
|
|
});
|
2012-05-25 07:39:33 +00:00
|
|
|
return ret;
|
2012-05-22 14:29:37 +00:00
|
|
|
};
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
2012-06-17 14:41:39 +00:00
|
|
|
Ox.len <f> Returns the length of an array, nodelist, object, storage or string
|
2012-05-30 11:34:57 +00:00
|
|
|
Not to be confused with `Ox.length`, which is the `length` property of the
|
|
|
|
`Ox` function (`1`).
|
2012-05-19 08:06:49 +00:00
|
|
|
> Ox.len((function() { return arguments; }(1, 2, 3)))
|
|
|
|
3
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.len([1, 2, 3])
|
|
|
|
3
|
|
|
|
> Ox.len([,])
|
|
|
|
1
|
2012-05-19 08:06:49 +00:00
|
|
|
> Ox.typeOf(Ox.len(document.getElementsByTagName('a')))
|
|
|
|
'number'
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.len({a: 1, b: 2, c: 3})
|
|
|
|
3
|
2012-06-17 14:41:39 +00:00
|
|
|
> Ox.typeOf(Ox.len(localStorage))
|
|
|
|
'number'
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.len('abc')
|
|
|
|
3
|
2012-05-19 08:06:49 +00:00
|
|
|
> Ox.len(function(a, b, c) {})
|
|
|
|
undefined
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2013-11-29 20:15:02 +00:00
|
|
|
// FIXME: rename to Ox.length
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.len = function(collection) {
|
|
|
|
var ret, type = Ox.typeOf(collection);
|
2012-05-19 08:06:49 +00:00
|
|
|
if (
|
|
|
|
type == 'arguments' || type == 'array'
|
|
|
|
|| type == 'nodelist' || type == 'string'
|
|
|
|
) {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = collection.length;
|
2012-06-17 14:41:39 +00:00
|
|
|
} else if (type == 'object' || type == 'storage') {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = Object.keys(collection).length;
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.map <f> Transforms the values of an array, object or string
|
2012-06-02 14:08:01 +00:00
|
|
|
Unlike `Array.prototype.map`, `Ox.map` works for arrays, objects and
|
|
|
|
strings.
|
2012-05-25 14:25:35 +00:00
|
|
|
> Ox.map([2, 1, 0], function(v, i) { return v == i; })
|
|
|
|
[false, true, false]
|
2012-05-27 20:08:08 +00:00
|
|
|
> Ox.map({a: 'b', b: 'b', c: 'b'}, function(v, k) { return v == k; })
|
|
|
|
{a: false, b: true, c: false}
|
2012-05-23 15:43:32 +00:00
|
|
|
> Ox.map('foo', function(v) { return v.toUpperCase(); })
|
|
|
|
'FOO'
|
2012-05-22 23:17:17 +00:00
|
|
|
> Ox.map([,], function(v, i) { return i; })
|
|
|
|
[,]
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.map = function(collection, iterator, that) {
|
|
|
|
var ret, type = Ox.typeOf(collection);
|
2012-06-17 14:41:39 +00:00
|
|
|
if (type == 'object' || type == 'storage') {
|
2012-05-22 14:29:37 +00:00
|
|
|
ret = {};
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.forEach(collection, function(value, key) {
|
|
|
|
ret[key] = iterator.call(that, value, key, collection);
|
2012-05-22 14:29:37 +00:00
|
|
|
});
|
|
|
|
} else {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = Ox.toArray(collection).map(iterator);
|
2012-05-22 14:29:37 +00:00
|
|
|
if (type == 'string') {
|
|
|
|
ret = ret.join('');
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2012-05-22 14:29:37 +00:00
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.max <f> Returns the maximum value of a collection
|
|
|
|
> Ox.max([1, 2, 3])
|
|
|
|
3
|
|
|
|
> Ox.max({a: 1, b: 2, c: 3})
|
|
|
|
3
|
|
|
|
> Ox.max('123')
|
|
|
|
3
|
2013-01-03 14:56:55 +00:00
|
|
|
> Ox.max([])
|
|
|
|
-Infinity
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.max = function(collection) {
|
|
|
|
var ret, values = Ox.values(collection);
|
2012-05-21 20:17:33 +00:00
|
|
|
if (values.length < Ox.STACK_LENGTH) {
|
|
|
|
ret = Math.max.apply(null, values)
|
|
|
|
} else {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = values.reduce(function(previousValue, currentValue) {
|
|
|
|
return Math.max(previousValue, currentValue);
|
2012-05-21 20:17:33 +00:00
|
|
|
}, -Infinity);
|
|
|
|
}
|
|
|
|
return ret;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.min <f> Returns the minimum value of a collection
|
|
|
|
> Ox.min([1, 2, 3])
|
|
|
|
1
|
|
|
|
> Ox.min({a: 1, b: 2, c: 3})
|
|
|
|
1
|
|
|
|
> Ox.min('123')
|
|
|
|
1
|
2013-01-03 14:56:55 +00:00
|
|
|
> Ox.min([])
|
|
|
|
Infinity
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.min = function(collection) {
|
|
|
|
var ret, values = Ox.values(collection);
|
2012-05-21 20:17:33 +00:00
|
|
|
if (values.length < Ox.STACK_LENGTH) {
|
|
|
|
ret = Math.min.apply(null, values)
|
|
|
|
} else {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = values.reduce(function(previousValue, currentValue) {
|
|
|
|
return Math.min(previousValue, currentValue);
|
2012-05-21 20:17:33 +00:00
|
|
|
}, Infinity);
|
|
|
|
}
|
|
|
|
return ret;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
2013-05-26 09:50:37 +00:00
|
|
|
/*@
|
|
|
|
Ox.numberOf <f> Returns the number of elements in a collection that pass a test
|
|
|
|
(collection, test) -> <n> Number of elements
|
|
|
|
collection <a|o|s> Collection
|
|
|
|
test <f> Test function
|
|
|
|
value <*> Value
|
|
|
|
key <n|s> Key
|
|
|
|
collection <a|o|s> Collection
|
|
|
|
> Ox.numberOf([0, 1, 0, 1], function(v) { return v; })
|
|
|
|
2
|
|
|
|
> Ox.numberOf({a: 'a', b: 'c'}, function(v, k) { return v == k; })
|
|
|
|
1
|
|
|
|
> Ox.numberOf('foo', function(v, k, c) { return v == c[k - 1]; })
|
|
|
|
1
|
|
|
|
@*/
|
|
|
|
Ox.numberOf = function(collection, test) {
|
|
|
|
return Ox.len(Ox.filter(collection, test));
|
|
|
|
};
|
|
|
|
|
2013-05-25 12:24:39 +00:00
|
|
|
/*@
|
|
|
|
Ox.remove <f> Removes an element from an array or object and returns it
|
|
|
|
(collection, element) -> <*> Element, or undefined if not found
|
|
|
|
<script>
|
|
|
|
Ox.test.collection = [
|
|
|
|
['a', 'b', 'c'],
|
|
|
|
{a: 0, b: 1, c: 2}
|
|
|
|
];
|
|
|
|
</script>
|
|
|
|
> Ox.remove(Ox.test.collection[0], 'b')
|
|
|
|
'b'
|
|
|
|
> Ox.remove(Ox.test.collection[1], 1)
|
|
|
|
1
|
|
|
|
> Ox.remove(Ox.test.collection[1], 3)
|
|
|
|
void 0
|
|
|
|
> Ox.test.collection
|
|
|
|
[['a', 'c'], {a: 0, c: 2}]
|
|
|
|
@*/
|
|
|
|
Ox.remove = function(collection, element) {
|
|
|
|
var ret, key;
|
|
|
|
if (Ox.isArray(collection)) {
|
|
|
|
key = collection.indexOf(element);
|
|
|
|
if (key > -1) {
|
|
|
|
ret = collection.splice(key, 1)[0];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key = Ox.keyOf(collection, element);
|
|
|
|
if (key) {
|
|
|
|
ret = collection[key];
|
|
|
|
delete collection[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2012-04-02 21:11:56 +00:00
|
|
|
/*@
|
|
|
|
Ox.reverse <f> Reverses an array or string
|
|
|
|
> Ox.reverse([1, 2, 3])
|
|
|
|
[3, 2, 1]
|
|
|
|
> Ox.reverse('foobar')
|
|
|
|
'raboof'
|
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.reverse = function(collection) {
|
|
|
|
return Ox.isArray(collection)
|
|
|
|
? Ox.clone(collection).reverse()
|
|
|
|
: collection.toString().split('').reverse().join('');
|
2012-04-02 21:11:56 +00:00
|
|
|
};
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.shuffle <f> Randomizes the order of values within a collection
|
|
|
|
> Ox.shuffle([1, 2, 3]).length
|
|
|
|
3
|
|
|
|
> Ox.len(Ox.shuffle({a: 1, b: 2, c: 3}))
|
|
|
|
3
|
2012-04-07 10:44:53 +00:00
|
|
|
> Ox.shuffle('123').split('').sort().join('')
|
|
|
|
'123'
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.shuffle = function(collection) {
|
|
|
|
var keys, ret, type = Ox.typeOf(collection), values;
|
2012-06-17 14:41:39 +00:00
|
|
|
if (type == 'object' || type == 'storage') {
|
2012-05-25 07:39:33 +00:00
|
|
|
keys = Object.keys(collection);
|
|
|
|
values = Ox.shuffle(Ox.values(collection));
|
2011-10-07 01:04:47 +00:00
|
|
|
ret = {};
|
2012-05-25 07:39:33 +00:00
|
|
|
keys.forEach(function(key, index) {
|
|
|
|
ret[key] = values[index];
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2012-05-22 23:17:17 +00:00
|
|
|
} else {
|
|
|
|
ret = [];
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.toArray(collection).forEach(function(value, index) {
|
|
|
|
var random = Math.floor(Math.random() * (index + 1));
|
|
|
|
ret[index] = ret[random];
|
|
|
|
ret[random] = value;
|
2012-05-22 23:17:17 +00:00
|
|
|
});
|
|
|
|
if (type == 'string') {
|
|
|
|
ret = ret.join('');
|
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-05-30 11:34:57 +00:00
|
|
|
Ox.slice <f> Alias for `Array.prototype.slice.call`
|
2013-11-29 20:15:02 +00:00
|
|
|
(collection[, start[, stop]]) -> <a> Array
|
|
|
|
collection <a|o|s> Array-like
|
|
|
|
start <n> Start position
|
|
|
|
stop <n> Stop position
|
|
|
|
> (function() { return Ox.slice(arguments); }(1, 2, 3))
|
|
|
|
[1, 2, 3]
|
|
|
|
> Ox.slice('foo', 0, 1);
|
|
|
|
['f']
|
|
|
|
> Ox.slice({0: 'f', 1: 'o', 2: 'o', length: 3}, -2)
|
|
|
|
['o', 'o']
|
2012-05-21 20:17:33 +00:00
|
|
|
@*/
|
2013-11-29 20:15:02 +00:00
|
|
|
// FIXME: remove toArray alias
|
|
|
|
Ox.slice = Ox.toArray = function(collection, start, stop) {
|
|
|
|
return Array.prototype.slice.call(collection, start, stop);
|
2012-05-21 20:17:33 +00:00
|
|
|
};
|
2012-05-25 17:09:25 +00:00
|
|
|
// IE8 returns an empty array if undefined is passed as stop
|
|
|
|
// and an array of null values if a string is passed as value.
|
2013-11-29 20:15:02 +00:00
|
|
|
// Firefox 3.6 returns an array of undefined values if a string
|
|
|
|
// is passed as value. IE NodeLists may not work with slice.
|
2012-05-25 17:09:25 +00:00
|
|
|
if (
|
|
|
|
Ox.slice([0]).length == 0
|
|
|
|
|| Ox.slice('0')[0] === null
|
|
|
|
|| Ox.slice('0')[0] === void 0
|
2013-11-29 20:15:02 +00:00
|
|
|
|| !(function() {
|
|
|
|
try {
|
|
|
|
return Ox.slice(document.getElementsByTagName('a'));
|
2013-11-29 20:21:38 +00:00
|
|
|
} catch (error) {}
|
2013-11-29 20:15:02 +00:00
|
|
|
}())
|
2012-05-25 17:09:25 +00:00
|
|
|
) {
|
2013-11-29 20:15:02 +00:00
|
|
|
// FIXME: remove toArray alias
|
|
|
|
Ox.slice = Ox.toArray = function(collection, start, stop) {
|
|
|
|
var args = stop === void 0 ? [start] : [start, stop],
|
|
|
|
array = [], index, length, ret;
|
|
|
|
if (Ox.typeOf(collection) == 'string') {
|
|
|
|
collection = collection.split('');
|
2012-05-25 17:09:25 +00:00
|
|
|
}
|
2013-11-29 20:15:02 +00:00
|
|
|
try {
|
|
|
|
ret = Array.prototype.slice.apply(collection, args);
|
2013-11-29 20:21:38 +00:00
|
|
|
} catch (error) {
|
2013-11-29 20:15:02 +00:00
|
|
|
length = collection.length;
|
|
|
|
for (index = 0; index < length; index++) {
|
|
|
|
array[index] = collection[index];
|
|
|
|
}
|
|
|
|
ret = Array.prototype.slice.apply(array, args);
|
|
|
|
}
|
|
|
|
return ret;
|
2012-05-25 16:28:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:17:33 +00:00
|
|
|
/*@
|
|
|
|
Ox.some <f> Tests if one or more elements of a collection meet a given condition
|
2012-06-02 14:08:01 +00:00
|
|
|
Unlike `Array.prototype.some`, `Ox.some` works for arrays, objects and
|
|
|
|
strings.
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.some([2, 1, 0], function(i, v) { return i == v; })
|
|
|
|
true
|
|
|
|
> Ox.some({a: 1, b: 2, c: 3}, function(v) { return v == 1; })
|
|
|
|
true
|
|
|
|
> Ox.some("foo", function(v) { return v == 'f'; })
|
|
|
|
true
|
2012-05-21 20:17:33 +00:00
|
|
|
> Ox.some([false, null, 0, '', void 0])
|
2012-04-08 18:22:27 +00:00
|
|
|
false
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.some = function(collection, iterator) {
|
|
|
|
return Ox.filter(Ox.values(collection), iterator || Ox.identity).length > 0;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.sum <f> Returns the sum of the values of a collection
|
|
|
|
> Ox.sum(1, 2, 3)
|
|
|
|
6
|
|
|
|
> Ox.sum([1, 2, 3])
|
|
|
|
6
|
|
|
|
> Ox.sum({a: 1, b: 2, c: 3})
|
|
|
|
6
|
|
|
|
> Ox.sum('123')
|
|
|
|
6
|
|
|
|
> Ox.sum('123foo')
|
|
|
|
6
|
|
|
|
> Ox.sum('08', -2, 'foo')
|
|
|
|
6
|
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.sum = function(collection) {
|
|
|
|
var ret = 0;
|
|
|
|
collection = arguments.length > 1 ? Ox.toArray(arguments) : collection;
|
|
|
|
Ox.forEach(collection, function(value) {
|
|
|
|
value = +value;
|
|
|
|
ret += isFinite(value) ? value : 0;
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2012-05-25 07:39:33 +00:00
|
|
|
return ret;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
2012-06-02 09:20:55 +00:00
|
|
|
/* FIXME: do we need this kind of zip functionality?
|
|
|
|
|
|
|
|
Ox.arrayToObject = function(array, key) {
|
|
|
|
var ret = {};
|
|
|
|
array.forEach(function(v) {
|
|
|
|
ret[v[key]] = v;
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
Ox.objectToArray = function(object, key) {
|
|
|
|
var ret = [];
|
|
|
|
Ox.forEach(object, function(v, k) {
|
|
|
|
ret.push(Ox.extend(v, key, k));
|
|
|
|
});
|
2013-01-03 14:56:55 +00:00
|
|
|
return ret;
|
2012-06-02 09:20:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.values <f> Returns the values of a collection
|
2012-06-02 14:08:01 +00:00
|
|
|
(collection) -> <a> Array of values
|
|
|
|
collection <a|o|s> Collection
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.values([1, 2, 3])
|
|
|
|
[1, 2, 3]
|
|
|
|
> Ox.values({a: 1, b: 2, c: 3})
|
|
|
|
[1, 2, 3]
|
2012-06-17 14:41:39 +00:00
|
|
|
> Ox.typeOf(Ox.values(localStorage))
|
|
|
|
'array'
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.values('abc')
|
|
|
|
['a', 'b', 'c']
|
|
|
|
> Ox.values([1,,3])
|
2012-05-25 07:39:33 +00:00
|
|
|
[1,,3]
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.values = function(collection) {
|
|
|
|
var ret, type = Ox.typeOf(collection);
|
2012-05-21 20:17:33 +00:00
|
|
|
if (type == 'array') {
|
2012-05-27 20:08:08 +00:00
|
|
|
ret = Ox.clone(collection);
|
2012-06-17 14:41:39 +00:00
|
|
|
} else if (type == 'object' || type == 'storage') {
|
2012-05-21 20:17:33 +00:00
|
|
|
ret = [];
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.forEach(collection, function(value) {
|
|
|
|
ret.push(value);
|
2012-05-21 20:17:33 +00:00
|
|
|
});
|
|
|
|
} else if (type == 'string') {
|
2012-05-25 07:39:33 +00:00
|
|
|
ret = collection.split('');
|
2012-05-21 20:17:33 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-05-25 22:44:21 +00:00
|
|
|
Ox.walk <f> Iterates over a nested data structure
|
2012-06-02 14:08:01 +00:00
|
|
|
(collection, iterator[, that]) -> <u> undefined
|
|
|
|
collection <a|o|s> Collection
|
|
|
|
iterator <f> Iterator
|
|
|
|
value <*> Value
|
|
|
|
keys <a> Array of keys
|
|
|
|
collection <a|o|s> The collection
|
|
|
|
that <o> The iterator's `this` binding
|
2011-10-07 01:04:47 +00:00
|
|
|
<script>
|
|
|
|
Ox.test.number = 0;
|
2012-05-25 22:44:21 +00:00
|
|
|
Ox.walk({a: 1, b: {c: 2, d: 3}}, function(value) {
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.test.number += Ox.isNumber(value) ? value : 0;
|
|
|
|
});
|
|
|
|
Ox.test.array = [];
|
2012-05-25 22:44:21 +00:00
|
|
|
Ox.walk({a: 1, b: {c: 2, d: 3}}, function(value, keys) {
|
2012-05-25 07:39:33 +00:00
|
|
|
Ox.isNumber(value) && Ox.test.array.push(keys)
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
> Ox.test.number
|
|
|
|
6
|
2012-05-25 07:39:33 +00:00
|
|
|
> Ox.test.array
|
|
|
|
[['a'], ['b', 'c'], ['b', 'd']]
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
2012-06-02 14:08:01 +00:00
|
|
|
Ox.walk = function(collection, iterator, that, keys) {
|
2012-05-25 07:39:33 +00:00
|
|
|
keys = keys || [];
|
|
|
|
Ox.forEach(collection, function(value, key) {
|
|
|
|
var keys_ = keys.concat(key);
|
2012-06-02 14:08:01 +00:00
|
|
|
iterator.call(that, value, keys_, collection);
|
|
|
|
Ox.walk(collection[key], iterator, that, keys_);
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2012-05-22 08:35:35 +00:00
|
|
|
};
|