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

Commit 8d26238

Browse files
committedMay 30, 2014
chore(shallowCopy): handle arrays and primitives, and switch to using it where possible
In many cases, we want a shallow copy instead of a full copy Closes #7618
1 parent b7cb454 commit 8d26238

File tree

6 files changed

+40
-15
lines changed

6 files changed

+40
-15
lines changed
 

‎src/Angular.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -799,20 +799,26 @@ function copy(source, destination) {
799799
}
800800

801801
/**
802-
* Create a shallow copy of an object
802+
* Creates a shallow copy of an object, an array or a primitive
803803
*/
804804
function shallowCopy(src, dst) {
805-
dst = dst || {};
805+
if (isArray(src)) {
806+
dst = dst || [];
806807

807-
for(var key in src) {
808-
// shallowCopy is only ever called by $compile nodeLinkFn, which has control over src
809-
// so we don't need to worry about using our custom hasOwnProperty here
810-
if (src.hasOwnProperty(key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) {
811-
dst[key] = src[key];
808+
for ( var i = 0; i < src.length; i++) {
809+
dst[i] = src[i];
810+
}
811+
} else if (isObject(src)) {
812+
dst = dst || {};
813+
814+
for (var key in src) {
815+
if (hasOwnProperty.call(src, key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) {
816+
dst[key] = src[key];
817+
}
812818
}
813819
}
814820

815-
return dst;
821+
return dst || src;
816822
}
817823

818824

‎src/ng/directive/ngClass.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function classDirective(name, selector) {
7878
updateClasses(oldClasses, newClasses);
7979
}
8080
}
81-
oldVal = copy(newVal);
81+
oldVal = shallowCopy(newVal);
8282
}
8383
}
8484
};

‎src/ng/directive/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
283283
// we need to work of an array, so we need to see if anything was inserted/removed
284284
scope.$watch(function selectMultipleWatch() {
285285
if (!equals(lastView, ctrl.$viewValue)) {
286-
lastView = copy(ctrl.$viewValue);
286+
lastView = shallowCopy(ctrl.$viewValue);
287287
ctrl.$render();
288288
}
289289
});

‎src/ng/http.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ function $HttpProvider() {
111111
common: {
112112
'Accept': 'application/json, text/plain, */*'
113113
},
114-
post: copy(CONTENT_TYPE_APPLICATION_JSON),
115-
put: copy(CONTENT_TYPE_APPLICATION_JSON),
116-
patch: copy(CONTENT_TYPE_APPLICATION_JSON)
114+
post: shallowCopy(CONTENT_TYPE_APPLICATION_JSON),
115+
put: shallowCopy(CONTENT_TYPE_APPLICATION_JSON),
116+
patch: shallowCopy(CONTENT_TYPE_APPLICATION_JSON)
117117
},
118118

119119
xsrfCookieName: 'XSRF-TOKEN',
@@ -946,7 +946,7 @@ function $HttpProvider() {
946946
} else {
947947
// serving from cache
948948
if (isArray(cachedResp)) {
949-
resolvePromise(cachedResp[1], cachedResp[0], copy(cachedResp[2]), cachedResp[3]);
949+
resolvePromise(cachedResp[1], cachedResp[0], shallowCopy(cachedResp[2]), cachedResp[3]);
950950
} else {
951951
resolvePromise(cachedResp, 200, {}, 'OK');
952952
}

‎src/ng/sce.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ function $SceProvider() {
737737
'document. See http://docs.angularjs.org/api/ng.$sce for more information.');
738738
}
739739

740-
var sce = copy(SCE_CONTEXTS);
740+
var sce = shallowCopy(SCE_CONTEXTS);
741741

742742
/**
743743
* @ngdoc method

‎test/AngularSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ describe('angular', function() {
219219
expect(clone.hello).toBeUndefined();
220220
expect(clone.goodbye).toBe("world");
221221
});
222+
223+
it('should handle arrays', function() {
224+
var original = [{}, 1],
225+
clone = [];
226+
227+
var aCopy = shallowCopy(original);
228+
expect(aCopy).not.toBe(original);
229+
expect(aCopy).toEqual(original);
230+
expect(aCopy[0]).toBe(original[0]);
231+
232+
expect(shallowCopy(original, clone)).toBe(clone);
233+
expect(clone).toEqual(original);
234+
});
235+
236+
it('should handle primitives', function() {
237+
expect(shallowCopy('test')).toBe('test');
238+
expect(shallowCopy(3)).toBe(3);
239+
expect(shallowCopy(true)).toBe(true);
240+
});
222241
});
223242

224243
describe('elementHTML', function() {

0 commit comments

Comments
 (0)