forked from 0x2620/oxjs
add Function.js, some updates in OxJS
This commit is contained in:
parent
80f9a1a33d
commit
1db649bd61
6 changed files with 123 additions and 90 deletions
|
|
@ -11,7 +11,6 @@ Ox.avg <f> Returns the average of an array's values, or an object's properties
|
|||
> Ox.avg('avg is 0.1')
|
||||
0.1
|
||||
@*/
|
||||
|
||||
Ox.avg = function(obj) {
|
||||
return Ox.sum(obj) / Ox.len(obj);
|
||||
};
|
||||
|
|
@ -44,9 +43,8 @@ Ox.copy <f> Returns a (shallow or deep) copy of an object or array
|
|||
> Ox.clone(0)
|
||||
0
|
||||
@*/
|
||||
|
||||
Ox.copy = Ox.clone = function(col, deep) {
|
||||
// fixme: remove references to Ox.clone
|
||||
// fixme: copy or clone?
|
||||
// fixme: is there any use case for shallow copy?
|
||||
var ret = Ox.isArray(col) ? [] : {};
|
||||
if (deep) {
|
||||
|
|
@ -109,7 +107,6 @@ Ox.filter <f> Filters a collection by a given condition
|
|||
> Ox.filter(' foo bar ', function(v) { return v != ' '; })
|
||||
'foobar'
|
||||
@*/
|
||||
|
||||
Ox.filter = function(col, fn) {
|
||||
var type = Ox.typeOf(col),
|
||||
ret = type == 'array' ? [] : type == 'object' ? {} : '';
|
||||
|
|
@ -285,7 +282,6 @@ Ox.getset <f> Generic getter and setter function
|
|||
> Ox.test.object.options({foo: "foo", bar: "bar"}).options()
|
||||
{"key": "val", "foo": "foo", "bar": "bar"}
|
||||
@*/
|
||||
|
||||
Ox.getset = function(obj, args, callback, context) {
|
||||
var obj_ = Ox.clone(obj), ret;
|
||||
if (args.length == 0) {
|
||||
|
|
@ -322,6 +318,8 @@ Ox.isEmpty <f> Returns true if a collection is empty
|
|||
false
|
||||
> Ox.isEmpty(null)
|
||||
false
|
||||
> Ox.isEmpty(0)
|
||||
false
|
||||
> Ox.isEmpty()
|
||||
false
|
||||
@*/
|
||||
|
|
@ -345,7 +343,6 @@ Ox.keys <f> Returns the keys of a collection
|
|||
> Ox.keys('abc')
|
||||
[0, 1, 2]
|
||||
@*/
|
||||
|
||||
// fixme: is this really needed? arrays... ok... but strings??
|
||||
Ox.keys = function(obj) {
|
||||
var keys = [];
|
||||
|
|
@ -402,47 +399,6 @@ Ox.len = function(col) {
|
|||
: type == 'object' ? Ox.values(col).length : void 0;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.loop <f> For-loop, functional-style
|
||||
Returning <code>false</code> from the iterator function acts like a
|
||||
<code>break</code> statement. Unlike a <code>for</code> loop,
|
||||
<code>Ox.loop</code> doesn't leak its counter variable to the outer scope,
|
||||
but returns it.
|
||||
(stop, callback) -> <n> Next value
|
||||
equivalent to <code>for (var i = 0; i < stop; i++)</code>
|
||||
(start, stop, callback) -> <n> Next value
|
||||
equivalent to <code>for (var i = start; i < stop; i++)</code> or,
|
||||
if <code>start</code> is larger than <code>stop</code>,
|
||||
<code>for (var i = start; i > stop; i--)</code>
|
||||
(start, stop, step, callback) -> <n> Next value
|
||||
equivalent to <code>for (var i = start; i < stop; i += step)</code> or,
|
||||
if <code>step</code> is negative,
|
||||
<code>for (var i = start; i > stop; i += step)</code>
|
||||
start <n> Start value
|
||||
stop <n> Stop value (exclusive)
|
||||
step <n> Step value
|
||||
callback <f> Iterator function
|
||||
i <n> Counter value
|
||||
> Ox.loop(10, function(i) { return i != 4; })
|
||||
4
|
||||
> Ox.loop(0, 3, 2, function() {})
|
||||
4
|
||||
@*/
|
||||
Ox.loop = function() {
|
||||
var len = arguments.length,
|
||||
start = len > 2 ? arguments[0] : 0,
|
||||
stop = arguments[len > 2 ? 1 : 0],
|
||||
step = len == 4 ? arguments[2] : (start <= stop ? 1 : -1),
|
||||
callback = arguments[len - 1],
|
||||
i;
|
||||
for (i = start; step > 0 ? i < stop : i > stop; i += step) {
|
||||
if (callback(i) === false) {
|
||||
break;
|
||||
};
|
||||
}
|
||||
return i;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.makeArray <f> Takes an array-like object and returns a true array
|
||||
Alias for <code>Array.prototype.slice.call</code>
|
||||
|
|
@ -558,41 +514,6 @@ Ox.min = function(col) {
|
|||
return Math.min.apply(Math, Ox.values(col));
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.range <f> Python-style range
|
||||
(stop) -> <[n]> range
|
||||
Returns an array of integers from <code>0</code> (inclusive) to
|
||||
<code>stop</code> (exclusive).
|
||||
(start, stop) -> <[n]> range
|
||||
Returns an array of integers from <code>start</code> (inclusive) to
|
||||
<code>stop</code> (exclusive).
|
||||
(start, stop, step) -> <[n]> range
|
||||
Returns an array of numbers from <code>start</code> (inclusive) to
|
||||
<code>stop</code> (exclusive), incrementing by <code>step</step>.
|
||||
start <n> Start value
|
||||
stop <n> Stop value
|
||||
step <n> Step value
|
||||
> Ox.range(3)
|
||||
[0, 1, 2]
|
||||
> Ox.range(1, 4)
|
||||
[1, 2, 3]
|
||||
> Ox.range(3, 0)
|
||||
[3, 2, 1]
|
||||
> Ox.range(1, 2, 0.5)
|
||||
[1, 1.5]
|
||||
> Ox.range(-1, -2, -0.5)
|
||||
[-1, -1.5]
|
||||
@*/
|
||||
Ox.range = function() {
|
||||
var args = Ox.makeArray(arguments),
|
||||
arr = [];
|
||||
args.push(function(i) {
|
||||
arr.push(i);
|
||||
});
|
||||
Ox.loop.apply(null, args);
|
||||
return arr;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.setPropertyOnce <f> Sets a property once
|
||||
Given a array of objects, each of which has a property with a boolean
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue