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
This commit is contained in:
rodyhaddad
2014-05-28 12:02:37 -07:00
parent b7cb454546
commit 8d26238664
6 changed files with 40 additions and 15 deletions

View File

@@ -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;
}

View File

@@ -78,7 +78,7 @@ function classDirective(name, selector) {
updateClasses(oldClasses, newClasses);
}
}
oldVal = copy(newVal);
oldVal = shallowCopy(newVal);
}
}
};

View File

@@ -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();
}
});

View File

@@ -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',
@@ -946,7 +946,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');
}

View File

@@ -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

View File

@@ -219,6 +219,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() {