Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vjs.extend method #1909

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,3 +849,22 @@ vjs.arr.forEach = function(array, callback, thisArg) {

return array;
};

vjs.extend = function (obj) {
var arg, i, k;
for (i = 1; i < arguments.length; i++) {
arg = arguments[i];
for (k in arg) {
if (arg.hasOwnProperty(k)) {
if (vjs.obj.isPlain(obj[k]) && vjs.obj.isPlain(arg[k])) {
obj[k] = vjs.extend({}, obj[k], arg[k]);
} else if (vjs.obj.isPlain(arg[k])) {
obj[k] = vjs.extend({}, arg[k]);
} else {
obj[k] = arg[k];
}
}
}
}
return obj;
};
243 changes: 243 additions & 0 deletions test/unit/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,246 @@ test('should loop through each element of an array', function() {
}
});
});

test('should copy a given object', function(){
var obj = {
person: {
name: 'John',
surname: 'Doe',
location: {
country: 'EEUU',
city: 'New York'
}
}
};

var objCopy = vjs.obj.copy(obj);
notEqual(objCopy, obj);
deepEqual(objCopy, obj);
});

test('should do a deep merge of primitive values', function(){
var obj1 = {
num1: 1,
num2: 1,
str1: 'one',
str2: 'one',
bool1: true,
bool2: true
};

var obj2 = {
num2: 2,
str2: 'two',
bool2: false
};

deepEqual(vjs.obj.deepMerge(obj1, obj2), {
num1: 1,
num2: 2,
str1: 'one',
str2: 'two',
bool1: true,
bool2: false
});
});

test('should override arrays on when deep merging two objs', function () {
var obj1 = {
array1: ['one', 'two', 'three']
};

var obj2 = {
array1: [1, 2, 3]
};

deepEqual(vjs.obj.deepMerge(obj1, obj2), {
array1: [1, 2, 3]
});
});

test('should merge nested objs when you do a deep merge of two objs', function () {
var obj1 = {
person: {
name: 'John',
surname: 'Doe',
location: {
country: 'EEUU',
city: 'New York'
}
}
};

var obj2 = {
person: {
name: 'Susan',
location: {
city: 'San Francisco'
}
}
};

deepEqual(vjs.obj.deepMerge(obj1, obj2), {
person: {
name: 'Susan',
surname: 'Doe',
location: {
country: 'EEUU',
city: 'San Francisco'
}
}
});
});

test('should be possible to extend different option maps of primitive types', function () {
var obj1 = {
num1: 1,
num2: 1,
str1: 'one',
str2: 'one',
bool1: true,
bool2: true
};
var obj2 = {
num2: 2,
str2: 'two',
bool2: false
};

var extendedObj = {};

vjs.extend(extendedObj, obj1, obj2);

deepEqual(extendedObj, {
num1: 1,
num2: 2,
str1: 'one',
str2: 'two',
bool1: true,
bool2: false
});
});

test('should be override arrays when you extend option maps', function () {
var obj1 = {
array1: ['one', 'two', 'three']
};
var obj2 = {
array1: [1, 2, 3]
};
var extendedObj = {};

vjs.extend(extendedObj, obj1, obj2);
deepEqual(extendedObj, {
array1: [1, 2, 3]
});
});

test('should be able be able to merge nested objs when you extend', function(){
var obj1 = {
person: {
name: 'John',
surname: 'Doe',
location: {
country: 'EEUU',
city: 'New York'
}
}
};

var obj2 = {
person: {
name: 'Susan',
location: {
city: 'San Francisco'
}
}
};
var extendedObj = {};

vjs.extend(extendedObj, obj1, obj2);
deepEqual(extendedObj, {
person: {
name: 'Susan',
surname: 'Doe',
location: {
country: 'EEUU',
city: 'San Francisco'
}
}
});

//A Modification on an extended obj should not modify the original objs
extendedObj.person.name = 'Matt';
extendedObj.person.location.city = 'Washington D.C.';

equal(obj1.person.name, 'John');
equal(obj1.person.location.city, 'New York');

equal(obj2.person.name, 'Susan');
equal(obj2.person.location.city, 'San Francisco');
});

test('should when extending preserve the original obj', function () {
var obj1 = {
person: {
name: 'John',
surname: 'Doe',
location: {
country: 'EEUU',
city: 'New York'
}
}
};

var obj2 = {
person: {
name: 'Susan',
location: {
city: 'San Francisco'
}
}
};
var extendedObj = {};

vjs.extend(extendedObj, obj1, obj2);
deepEqual(obj1, {
person: {
name: 'John',
surname: 'Doe',
location: {
country: 'EEUU',
city: 'New York'
}
}
});

deepEqual(obj2, {
person: {
name: 'Susan',
location: {
city: 'San Francisco'
}
}
});
});

test('should when extending not preserve any reference to the original objs', function () {
var obj1 = {
person: {
name: 'Susan',
location: {
city: 'San Francisco'
}
}
};
var extendedObj = {};

vjs.extend(extendedObj, obj1);

extendedObj.person.name = 'Matt';
extendedObj.person.location.city = 'Washington D.C.';

equal(obj1.person.name, 'Susan');
equal(obj1.person.location.city, 'San Francisco');
});