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