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

Commit

Permalink
fix(copy): fix handling of typed subarrays
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zhukov authored and gkalpak committed Jul 4, 2016
1 parent 6bee655 commit 1645924
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Angular.js
Expand Up @@ -909,7 +909,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
Expand Down
14 changes: 14 additions & 0 deletions test/AngularSpec.js
Expand Up @@ -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();
Expand Down

0 comments on commit 1645924

Please sign in to comment.