From 1df9870ea0f1fb89d2a84940464f4c4cfef053c1 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Fri, 6 Dec 2019 16:28:35 -0800 Subject: [PATCH] Add fromArrayView for TypedArrays and DataViews. Fast path for Uint8Array. --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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') -- 2.34.1