fix(copy): fix handling of typed subarrays

Previously, it would return a copy of the whole original typed array, not its slice.
Now, the `byteOffset` and `length` are also preserved.

Fixes #14842

Closes #14845
This commit is contained in:
Igor Zhukov
2016-06-30 00:46:43 +03:00
committed by Georgios Kalpakas
parent 6a219ad8db
commit 6280ec89e3
2 changed files with 15 additions and 1 deletions
+1 -1
View File
@@ -908,7 +908,7 @@ function copy(source, destination) {
case '[object Uint8ClampedArray]':
case '[object Uint16Array]':
case '[object Uint32Array]':
return new source.constructor(copyElement(source.buffer));
return new source.constructor(copyElement(source.buffer), source.byteOffset, source.length);
case '[object ArrayBuffer]':
//Support: IE10
+14
View File
@@ -240,6 +240,20 @@ describe('angular', function() {
}
});
it("should handle Uint16Array subarray", function() {
if (typeof Uint16Array !== 'undefined') {
var arr = new Uint16Array(4);
arr[1] = 1;
var src = arr.subarray(1, 2);
var dst = copy(src);
expect(dst instanceof Uint16Array).toBeTruthy();
expect(dst.length).toEqual(1);
expect(dst[0]).toEqual(1);
expect(dst).not.toBe(src);
expect(dst.buffer).not.toBe(src.buffer);
}
});
it("should throw an exception if a Uint8Array is the destination", function() {
if (typeof Uint8Array !== 'undefined') {
var src = new Uint8Array();