add comment; fix Ox.extend if called with two arguments, add test
This commit is contained in:
parent
3ad31125b3
commit
5eb1b96e4a
1 changed files with 8 additions and 5 deletions
|
@ -6,14 +6,16 @@ Ox.extend <function> Extends an object with one or more other objects
|
||||||
{a: 1, b: 2, c: 3}
|
{a: 1, b: 2, c: 3}
|
||||||
> Ox.extend({a: 1}, 'b', 2)
|
> Ox.extend({a: 1}, 'b', 2)
|
||||||
{a: 1, b: 2}
|
{a: 1, b: 2}
|
||||||
|
> Ox.extend({a: 1}, 'b')
|
||||||
|
{a: 1, b: void 0}
|
||||||
@*/
|
@*/
|
||||||
Ox.extend = function(object) {
|
Ox.extend = function(object) {
|
||||||
var args = Ox.slice(arguments, 1);
|
var args = Ox.slice(arguments, 1);
|
||||||
if (args.length == 2 && !Ox.isObject(args[0])) {
|
if (!Ox.isObject(args[0])) {
|
||||||
args = [Ox.makeObject(args)];
|
args = [Ox.makeObject(args)];
|
||||||
}
|
}
|
||||||
Ox.forEach(args, function(argument, i) {
|
Ox.forEach(args, function(arg, i) {
|
||||||
Ox.forEach(argument, function(value, key) {
|
Ox.forEach(arg, function(value, key) {
|
||||||
object[key] = value;
|
object[key] = value;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -115,13 +117,14 @@ Ox.makeObject <f> Takes an array and returns an object
|
||||||
> (function() { return Ox.makeObject(arguments); }())
|
> (function() { return Ox.makeObject(arguments); }())
|
||||||
{}
|
{}
|
||||||
@*/
|
@*/
|
||||||
|
// FIXME: rename to toObject
|
||||||
Ox.makeObject = function(array) {
|
Ox.makeObject = function(array) {
|
||||||
var ret = {};
|
var ret = {};
|
||||||
if (Ox.isObject(array[0])) {
|
if (Ox.isObject(array[0])) {
|
||||||
// ({foo: 'bar'})
|
// [{foo: 'bar'}]
|
||||||
ret = array[0];
|
ret = array[0];
|
||||||
} else if (array.length) {
|
} else if (array.length) {
|
||||||
// ('foo', 'bar')
|
// ['foo', 'bar']
|
||||||
ret[array[0]] = array[1];
|
ret[array[0]] = array[1];
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|
Loading…
Reference in a new issue