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}
|
|
|
|
@*/
|
2012-05-19 09:39:25 +00:00
|
|
|
Ox.extend = function(obj) {
|
2012-05-21 20:04:22 +00:00
|
|
|
Ox.forEach(Ox.slice(arguments, 1), function(arg, i) {
|
2011-10-07 01:04:47 +00:00
|
|
|
Ox.forEach(arg, function(val, key) {
|
|
|
|
obj[key] = val;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2012-05-25 07:32:11 +00:00
|
|
|
/*@
|
|
|
|
Ox.getset <f> Generic getter and setter function
|
|
|
|
See examples for details.
|
|
|
|
# Usage --------------------------------------------------------------------
|
|
|
|
Ox.getset(options, args=[]) -> <o> all options
|
|
|
|
Ox.getset(options, args=[key]) -> <*> options[key]
|
|
|
|
Ox.getset(options, args=[key, value], callback, that) -> <f|o> context
|
|
|
|
sets options[key] to value and calls fn(key, value)
|
|
|
|
if the key/value pair was added or modified
|
|
|
|
Ox.getset(options, args=[{key: value}], callback, that) -> <f|o> that
|
|
|
|
sets multiple options and calls fn(key, value)
|
|
|
|
for every key/value pair that was added or modified
|
|
|
|
# Arguments ----------------------------------------------------------------
|
|
|
|
options <obj> Options object (key/value pairs)
|
|
|
|
args <arr> The arguments "array" of the caller function
|
|
|
|
callback <fun> Callback function
|
|
|
|
The callback is called for every key/value pair that was added or
|
|
|
|
modified.
|
|
|
|
key <s> Key
|
|
|
|
value <*> Value
|
|
|
|
that <obj> The this object of the caller function (for chaining)
|
|
|
|
# Examples -----------------------------------------------------------------
|
|
|
|
<script>
|
|
|
|
Ox.test.object = new function() {
|
|
|
|
var options = {},
|
|
|
|
setOption = function(key, value) {
|
|
|
|
// handle added or modified options
|
|
|
|
},
|
|
|
|
that = this;
|
|
|
|
that.options = function() {
|
|
|
|
return Ox.getset(options, arguments, setOption, that);
|
|
|
|
};
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
> Ox.test.object.options("key", "val").options("key")
|
|
|
|
"val"
|
|
|
|
> Ox.test.object.options({foo: "foo", bar: "bar"}).options()
|
|
|
|
{"key": "val", "foo": "foo", "bar": "bar"}
|
|
|
|
@*/
|
|
|
|
Ox.getset = function(object, args, callback, that) {
|
|
|
|
var object_ = Ox.clone(object), ret;
|
|
|
|
if (args.length == 0) {
|
|
|
|
// []
|
|
|
|
ret = object_;
|
|
|
|
} else if (args.length == 1 && !Ox.isObject(args[0])) {
|
|
|
|
// [key]
|
|
|
|
ret = Ox.clone(object[args[0]]);
|
|
|
|
} else {
|
|
|
|
// [key, val] or [{key: val, ...}]
|
|
|
|
args = Ox.makeObject(args);
|
|
|
|
object = Ox.extend(object, args);
|
|
|
|
Ox.forEach(args, function(value, key) {
|
|
|
|
if (!object_ || !Ox.isEqual(object_[key], value)) {
|
|
|
|
callback && callback(key, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ret = that;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2012-05-19 09:39:25 +00:00
|
|
|
Ox.hasOwn = function(obj, val) {
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, val)
|
|
|
|
};
|
|
|
|
|
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) {
|
2012-03-30 14:11:29 +00:00
|
|
|
if (v === val) {
|
2011-10-27 18:50:23 +00:00
|
|
|
key = k;
|
2012-05-25 07:32:11 +00:00
|
|
|
Ox.Break();
|
2011-10-27 18:50:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return key;
|
|
|
|
};
|
|
|
|
|
2012-05-25 07:32:11 +00:00
|
|
|
/*@
|
|
|
|
Ox.makeObject <f> Takes an array and returns an object
|
|
|
|
<code>Ox.makeObject</code> is a helper for functions with two alternative
|
|
|
|
signatures like <code>('key', 'val')</code> and <code>({key: 'val'})</code>.
|
|
|
|
> (function() { return Ox.makeObject(arguments); }({foo: 1, bar: 2}))
|
|
|
|
{foo: 1, bar: 2}
|
|
|
|
> (function() { return Ox.makeObject(arguments); }('foo', 1))
|
|
|
|
{foo: 1}
|
|
|
|
> (function() { return Ox.makeObject(arguments); }('foo'))
|
|
|
|
{foo: void 0}
|
|
|
|
> (function() { return Ox.makeObject(arguments); }())
|
|
|
|
{}
|
|
|
|
@*/
|
|
|
|
Ox.makeObject = function(array) {
|
|
|
|
var ret = {};
|
|
|
|
if (Ox.isObject(array[0])) {
|
|
|
|
// ({foo: 'bar'})
|
|
|
|
ret = array[0];
|
|
|
|
} else if (array.length) {
|
|
|
|
// ('foo', 'bar')
|
|
|
|
ret[array[0]] = array[1];
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2012-01-07 07:20:02 +00:00
|
|
|
/*@
|
|
|
|
Ox.methods <f> Returns a sorted list of all method names of an object
|
|
|
|
> Ox.methods({a: [], b: false, f: function() {}, n: 0, o: {}, s: ''})
|
|
|
|
['f']
|
|
|
|
@*/
|
2012-05-21 20:04:22 +00:00
|
|
|
Ox.methods = function(obj, includePrototype) {
|
|
|
|
var key, keys;
|
|
|
|
if (includePrototype) {
|
|
|
|
keys = [];
|
|
|
|
for (var key in obj) {
|
|
|
|
keys.push(key);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keys = Object.keys(obj);
|
|
|
|
}
|
|
|
|
return keys.filter(function(key) {
|
2012-01-07 07:20:02 +00:00
|
|
|
return Ox.isFunction(obj[key]);
|
|
|
|
}).sort();
|
|
|
|
};
|
|
|
|
|
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 = {};
|
2012-05-22 07:11:26 +00:00
|
|
|
str.split('&').forEach(function(val) {
|
2011-10-07 01:04:47 +00:00
|
|
|
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;
|
|
|
|
};
|