Skip to content

Commit

Permalink
Update isDataView
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Aug 27, 2021
1 parent 74cf54a commit e7fc812
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/helper/is.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
Expand Down

0 comments on commit e7fc812

Please sign in to comment.