updating ox.js

This commit is contained in:
rlx 2011-04-19 21:21:08 +00:00
parent 0e9432076d
commit cc87bc3c3f

View file

@ -120,7 +120,7 @@ Ox.getset = function(obj, args, callback, context) {
ret = obj;
} else if (args.length == 1 && !Ox.isObject(args[0])) {
// getset([key])
ret = obj[args[0]]
ret = obj[args[0]];
} else {
// getset([key, val]) or getset([{key: val, ...}])
args = Ox.makeObject(Ox.isObject(args[0]) ? args[0] : args);
@ -145,7 +145,7 @@ Ox.print = function() {
(Ox.pad(+date % 1000, 3)));
window.console.log.apply(window.console, args);
}
}
};
Ox.uid = (function() {
/***
@ -183,7 +183,7 @@ Ox.wrap = function(val, chained) {
args.unshift(val);
ret = Ox[name].apply(Ox, args);
return wrapper.chained ? Ox.wrap(ret, true) : ret;
}
};
}
});
return wrapper;
@ -226,11 +226,10 @@ Ox.compact = function(arr) {
return Ox.map(arr, function(val) {
return Ox.isUndefined(val) ? null : val;
});
}
};
Ox.count = function(arr) {
/***
returns the number of occu
>>> Ox.count(['foo', 'bar', 'foo']).foo
2
***/
@ -295,20 +294,29 @@ Ox.extend = function() {
return obj;
};
Ox.filter = function(arr, fn) {
/*
Ox.filter works for arrays and strings, like $.grep(), unlike [].filter()
Ox.filter = function(obj, fn) {
/***
Ox.filter works for arrays, objects and strings, unlike [].filter()
>>> Ox.filter([2, 1, 0], function(v, i) { return v == i; })
[1]
>>> Ox.filter('210', function(v, i) { return v == i; })
['1']
*/
var i, len = arr.length, ret = [];
for (i = 0; i < len; i++) {
if (fn(arr[i], i)) {
ret.push(arr[i]);
>>> Ox.keys(Ox.filter({a: 'c', b: 'b', c: 'a'}, function(v, k) { return v == k; }))
['b']
>>> Ox.filter(' hello world ', function(v) { return v != ' '; })
'helloworld'
***/
var type = Ox.typeOf(obj),
ret = type == 'array' ? [] : type == 'object' ? {} : '';
Ox.forEach(obj, function(v, k) {
if (fn(v, k)) {
if (type == 'array') {
ret.push(v);
} else if (type == 'object') {
ret[k] = v;
} else {
ret += v;
}
}
});
return ret;
};
@ -348,7 +356,7 @@ Ox.find = function(arr, str) {
index > -1 && ret[index == 0 ? 0 : 1].push(arr[i]);
});
return ret;
}
};
Ox.forEach = function(obj, fn) {
/*
@ -446,7 +454,7 @@ Ox.isEqual = function(obj0, obj1) {
}
}
return ret;
}
};
Ox.keys = function(obj) {
/*
@ -475,12 +483,21 @@ Ox.len = function(obj) {
3
>>> Ox.len('abc')
3
>>> Ox.len(function(x, y) { return x + y; })
2
>>> Ox.len([,])
1
*/
return Ox.isObject(obj) ? Ox.values(obj).length : obj.length;
};
Ox.loop = function(num, fn) {
var i;
for (i = 0; i < num; i++) {
fn(i);
}
};
Ox.makeArray = function(arg) {
/*
like $.makeArray()
@ -505,18 +522,18 @@ Ox.makeObject = function() {
>>> (function() { return Ox.makeObject(arguments); }("foo", "bar")).foo
"bar"
*/
var arg = arguments, obj = {};
if (arg.length == 1) {
if (Ox.isObject(arg[0])) {
var obj = {};
if (arguments.length == 1) {
if (Ox.isObject(arguments[0])) {
// ({foo: 'bar'})
obj = arg[0];
obj = arguments[0];
} else {
// (['foo', 'bar'])
obj[arg[0][0]] = arg[0][1];
obj[arguments[0][0]] = arguments[0][1];
}
} else {
// ('foo', 'bar')
obj[arg[0]] = arg[1];
obj[arguments[0]] = arguments[1];
}
return obj;
};
@ -535,7 +552,7 @@ Ox.map = function(obj, fn) {
[0]
*/
var isObject = Ox.isObject(obj),
ret = isObject ? {} : []
ret = isObject ? {} : [];
Ox.forEach(obj, function(val, key) {
if ((v = fn(val, key)) !== null) {
ret[isObject ? key : ret.length] = v;
@ -630,7 +647,7 @@ Ox.setPropertyOnce = function(arr, str) {
pos = 0;
}
return pos;
}
};
Ox.shuffle = function(arr) {
/*