2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.extend <function> Extends an object with one or more other objects
|
|
|
|
> Ox.extend({a: 1, b: 1, c: 1}, {b: 2, c: 2}, {c: 3})
|
|
|
|
{a: 1, b: 2, c: 3}
|
|
|
|
@*/
|
|
|
|
Ox.extend = function() {
|
|
|
|
var obj = arguments[0];
|
|
|
|
Ox.forEach(Array.prototype.slice.call(arguments, 1), function(arg, i) {
|
|
|
|
Ox.forEach(arg, function(val, key) {
|
|
|
|
obj[key] = val;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2011-10-27 18:50:23 +00:00
|
|
|
/*@
|
2012-01-04 07:42:48 +00:00
|
|
|
Ox.keyOf <f> Equivalent of [].indexOf for objects
|
|
|
|
> Ox.keyOf({a: 1, b: 2, c: 3}, 1)
|
|
|
|
'a'
|
2011-10-27 18:50:23 +00:00
|
|
|
@*/
|
|
|
|
Ox.keyOf = function(obj, val) {
|
|
|
|
var key;
|
|
|
|
Ox.forEach(obj, function(v, k) {
|
|
|
|
if (v == val) {
|
|
|
|
key = k;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return key;
|
|
|
|
};
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.serialize <f> Parses an object into query parameters
|
|
|
|
> Ox.serialize({a: 1, b: 2, c: 3})
|
|
|
|
'a=1&b=2&c=3'
|
|
|
|
> Ox.serialize({a: -1, b: 2.3, c: [4, 5]})
|
|
|
|
'a=-1&b=2.3&c=4,5'
|
|
|
|
> Ox.serialize({string: 'foo', empty: {}, null: null, undefined: void 0})
|
|
|
|
'string=foo'
|
|
|
|
@*/
|
|
|
|
Ox.serialize = function(obj) {
|
|
|
|
var arr = [];
|
|
|
|
Ox.forEach(obj, function(val, key) {
|
|
|
|
if (!Ox.isEmpty(val) && !Ox.isNull(val) && !Ox.isUndefined(val)) {
|
|
|
|
arr.push(key + '=' + val);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return arr.join('&');
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.unserialize <f> Parses query parameters into an object
|
|
|
|
> Ox.unserialize('a=1&b=2&c=3')
|
|
|
|
{a: '1', b: '2', c: '3'}
|
|
|
|
> Ox.unserialize('a=-1&b=2.3&c=4,5', true)
|
|
|
|
{a: -1, b: 2.3, c: [4, 5]}
|
2012-01-04 07:42:48 +00:00
|
|
|
> Ox.unserialize('a=1&b=&c&a=0', true)
|
|
|
|
{a: 0}
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
|
|
|
Ox.unserialize = function(str, toNumber) {
|
|
|
|
var obj = {};
|
|
|
|
Ox.forEach(str.split('&'), function(val) {
|
|
|
|
if (val) {
|
|
|
|
var arr = val.split('=');
|
2012-01-04 07:42:48 +00:00
|
|
|
if (arr[1]) {
|
|
|
|
obj[arr[0]] = !toNumber ? arr[1]
|
|
|
|
: arr[1].indexOf(',') == -1 ? +arr[1]
|
|
|
|
: arr[1].split(',').map(function(val) {
|
|
|
|
return +val;
|
|
|
|
});
|
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return obj;
|
|
|
|
};
|