From: Koushik Dutta Date: Sat, 7 Dec 2019 00:28:35 +0000 (-0800) Subject: Add fromArrayView for TypedArrays and DataViews. Fast path for Uint8Array. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=1df9870ea0f1fb89d2a84940464f4c4cfef053c1;p=buffer.git Add fromArrayView for TypedArrays and DataViews. Fast path for Uint8Array. --- diff --git a/index.js b/index.js index 19b0468..cb712b1 100644 --- a/index.js +++ b/index.js @@ -127,7 +127,7 @@ function from (value, encodingOrOffset, length) { } if (ArrayBuffer.isView(value)) { - return fromArrayLike(value) + return fromArrayView(value) } if (value == null) { @@ -269,6 +269,14 @@ function fromArrayLike (array) { return buf } +function fromArrayView (arrayView) { + if (isInstance(arrayView, Uint8Array)) { + var copy = Uint8Array.from(arrayView) + return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength) + } + return fromArrayLike(arrayView) +} + function fromArrayBuffer (array, byteOffset, length) { if (byteOffset < 0 || array.byteLength < byteOffset) { throw new RangeError('"offset" is outside of buffer bounds')