Skip to content

Commit

Permalink
fixed bug Cannot assign to read only property '0'
Browse files Browse the repository at this point in the history
  • Loading branch information
alykoshin committed Mar 24, 2016
1 parent d41a8ff commit 0682b3a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @returns {object}
*/
var _assign = function(target, source) {
console.log('_assign(): source[nextKey]:', source[nextKey], '; target[nextKey]:', target[nextKey]);

for (var nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
Expand All @@ -29,6 +30,7 @@ var _assign = function(target, source) {
typeof s === 'string' ||
s === null
) {
console.log('scalar: nextKey:', nextKey, '; source[nextKey]:', source[nextKey], '; target[nextKey]:', target[nextKey]);
target[nextKey] = s;

} else if (typeof s === 'function') {
Expand All @@ -41,10 +43,12 @@ var _assign = function(target, source) {
target[nextKey] = new RegExp(s);

} else if (Array.isArray(s)) {
console.log('array: nextKey:', nextKey, '; source[nextKey]:', source[nextKey], '; target[nextKey]:', target[nextKey]);
var arr = s.slice();
var i = arr.length;
while (i--){
arr[i] = _assign(arr[i], s[i]);
//arr[i] = _assign(arr[i], s[i]);
arr[i] = ( _assign({ value: arr[i] }, { value: s[i] }) ).value; // workaround: pass by reference
}
target[nextKey] = arr;

Expand Down Expand Up @@ -75,17 +79,18 @@ var _assign = function(target, source) {
* @returns {object}
*/
var assign = function (target /* sources */) {
console.log('assign:', arguments);

if (target === undefined || target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
//var output = Object(target);
var output = { value: target };
var output = { value: target }; // workaround: pass by reference

for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== undefined && source !== null) {
var input = { value: source };
var input = { value: source }; // workaround: pass by reference
output = _assign(output, input);

}
Expand Down
51 changes: 51 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,55 @@ describe('mini-deep-assign', function () {
expect(deepCopy(target, source)).eql(res);
});

it('should copy arrays 1', function () {
var source = {
a: [ 1, 2, 3 ],
b: [ [1, 2], 3 ],
};
var target = {};
var res = source;
expect(deepCopy(target, source)).eql(res);
console.log(source);
});


it('should copy arrays 2', function () {
var source = {
delivery : ['M','W','F']
};
var target = {};
var res = source;
expect(deepCopy(target, source)).eql(res);
console.log(source);
});


it('should copy arrays 3', function () {
var source =
{
'_id' : 3,
'type' : 'food',
'item' : 'Super Dark Chocolate',
'classification' : { 'dept' : 'grocery', 'category' : 'chocolate'},
'vendor' : {
'primary' : {
'name' : 'Marsupial Vending Co',
'address' : 'Wallaby Rd',
'delivery' : ['M','W','F']
},
'secondary':{
'name' : 'Intl. Chocolatiers',
'address' : 'Cocoa Plaza',
'delivery' : ['Sa']
}
}
}
;
var target = {};
var res = source;
expect(deepCopy(target, source)).eql(res);
console.log(source);
});


});

0 comments on commit 0682b3a

Please sign in to comment.