Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
chore(shallowCopy): handle arrays and primitives, and switch to using…
Browse files Browse the repository at this point in the history
… it where possible

In many cases, we want a shallow copy instead of a full copy

Closes #7618
  • Loading branch information
rodyhaddad committed May 30, 2014
1 parent 11170ae commit a87135b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
22 changes: 14 additions & 8 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,20 +799,26 @@ function copy(source, destination) {
}

/**
* Create a shallow copy of an object
* Creates a shallow copy of an object, an array or a primitive
*/
function shallowCopy(src, dst) {
dst = dst || {};
if (isArray(src)) {
dst = dst || [];

for(var key in src) {
// shallowCopy is only ever called by $compile nodeLinkFn, which has control over src
// so we don't need to worry about using our custom hasOwnProperty here
if (src.hasOwnProperty(key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) {
dst[key] = src[key];
for ( var i = 0; i < src.length; i++) {
dst[i] = src[i];
}
} else if (isObject(src)) {
dst = dst || {};

for (var key in src) {
if (hasOwnProperty.call(src, key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) {
dst[key] = src[key];
}
}
}

return dst;
return dst || src;
}


Expand Down
2 changes: 1 addition & 1 deletion src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function classDirective(name, selector) {
updateClasses(oldClasses, newClasses);
}
}
oldVal = copy(newVal);
oldVal = shallowCopy(newVal);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
// we need to work of an array, so we need to see if anything was inserted/removed
scope.$watch(function selectMultipleWatch() {
if (!equals(lastView, ctrl.$viewValue)) {
lastView = copy(ctrl.$viewValue);
lastView = shallowCopy(ctrl.$viewValue);
ctrl.$render();
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ function $HttpProvider() {
common: {
'Accept': 'application/json, text/plain, */*'
},
post: copy(CONTENT_TYPE_APPLICATION_JSON),
put: copy(CONTENT_TYPE_APPLICATION_JSON),
patch: copy(CONTENT_TYPE_APPLICATION_JSON)
post: shallowCopy(CONTENT_TYPE_APPLICATION_JSON),
put: shallowCopy(CONTENT_TYPE_APPLICATION_JSON),
patch: shallowCopy(CONTENT_TYPE_APPLICATION_JSON)
},

xsrfCookieName: 'XSRF-TOKEN',
Expand Down Expand Up @@ -874,7 +874,7 @@ function $HttpProvider() {
} else {
// serving from cache
if (isArray(cachedResp)) {
resolvePromise(cachedResp[1], cachedResp[0], copy(cachedResp[2]), cachedResp[3]);
resolvePromise(cachedResp[1], cachedResp[0], shallowCopy(cachedResp[2]), cachedResp[3]);
} else {
resolvePromise(cachedResp, 200, {}, 'OK');
}
Expand Down
2 changes: 1 addition & 1 deletion src/ng/sce.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ function $SceProvider() {
'document. See http://docs.angularjs.org/api/ng.$sce for more information.');
}

var sce = copy(SCE_CONTEXTS);
var sce = shallowCopy(SCE_CONTEXTS);

/**
* @ngdoc method
Expand Down
19 changes: 19 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,25 @@ describe('angular', function() {
expect(clone.hello).toBeUndefined();
expect(clone.goodbye).toBe("world");
});

it('should handle arrays', function() {
var original = [{}, 1],
clone = [];

var aCopy = shallowCopy(original);
expect(aCopy).not.toBe(original);
expect(aCopy).toEqual(original);
expect(aCopy[0]).toBe(original[0]);

expect(shallowCopy(original, clone)).toBe(clone);
expect(clone).toEqual(original);
});

it('should handle primitives', function() {
expect(shallowCopy('test')).toBe('test');
expect(shallowCopy(3)).toBe(3);
expect(shallowCopy(true)).toBe(true);
});
});

describe('elementHTML', function() {
Expand Down

0 comments on commit a87135b

Please sign in to comment.