From e7fc81263beb0f042a15d7ba912aca1e49ecb595 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Sat, 28 Aug 2021 02:23:32 +0900 Subject: [PATCH] Update isDataView --- src/helper/is.mjs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/helper/is.mjs b/src/helper/is.mjs index 456a2b1e..b21b76b9 100644 --- a/src/helper/is.mjs +++ b/src/helper/is.mjs @@ -14,16 +14,6 @@ export function isObjectLike(value) { return value !== null && typeof value === "object"; } -const toString = Object.prototype.toString; - -/** - * @param {unknown} value - * @returns {value is DataView} - */ -export function isDataView(value) { - return ArrayBuffer.isView(value) && toString.call(value) === "[object DataView]"; -} - // Inspired by util.types implementation of Node.js const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array).prototype; const getTypedArrayPrototypeSybolToStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get; @@ -44,6 +34,28 @@ export function isUint16Array(value) { return getTypedArrayPrototypeSybolToStringTag.call(value) === "Uint16Array"; } +const toString = Object.prototype.toString; + +/** + * @param {unknown} value + * @returns {value is DataView} + */ + export function isDataView(value) { + if (!ArrayBuffer.isView(value)) { + return false; + } + + if (isTypedArray(value)) { + return false; + } + + if (toString.call(value) !== "[object DataView]") { + return false; + } + + return true; +} + /** * @param {unknown} value * @returns {value is ArrayBuffer}