From: Feross Aboukhadijeh Date: Fri, 16 Feb 2018 08:08:40 +0000 (-0800) Subject: convert isArrayBuffer() to generic isInstance() function X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=a1667c5f6f9ffc417f046d10b58ded873bb524c7;p=buffer.git convert isArrayBuffer() to generic isInstance() function --- diff --git a/index.js b/index.js index b2f6676..4b98dea 100644 --- a/index.js +++ b/index.js @@ -120,7 +120,8 @@ function from (value, encodingOrOffset, length) { ) } - if (isArrayBuffer(value) || (value && isArrayBuffer(value.buffer))) { + if (isInstance(value, ArrayBuffer) || + (value && isInstance(value.buffer, ArrayBuffer))) { return fromArrayBuffer(value, encodingOrOffset, length) } @@ -383,7 +384,7 @@ function byteLength (string, encoding) { if (Buffer.isBuffer(string)) { return string.length } - if (ArrayBuffer.isView(string) || isArrayBuffer(string)) { + if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) { return string.byteLength } if (typeof string !== 'string') { @@ -1729,12 +1730,12 @@ function blitBuffer (src, dst, offset, length) { return i } -// ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check -// but they should be treated as valid. See: https://github.com/feross/buffer/issues/166 -function isArrayBuffer (obj) { - return obj instanceof ArrayBuffer || - (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' && - typeof obj.byteLength === 'number') +// ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass +// the `instanceof` check but they should be treated as of that type. +// See: https://github.com/feross/buffer/issues/166 +function isInstance (obj, type) { + return obj instanceof type || + (obj != null && obj.constructor != null && obj.constructor.name === type.name) } function numberIsNaN (obj) {