From 2ac965b088d5e9019930c3d9016c7a213994e2fb Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 5 Apr 2017 11:24:52 -0700 Subject: [PATCH] support environments that lack ArrayBuffer.isView (Node 0.10) This helps browserify tests to pass --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c06f72d..848299b 100644 --- a/index.js +++ b/index.js @@ -252,8 +252,8 @@ function fromObject (obj) { } if (obj) { - if (ArrayBuffer.isView(obj) || 'length' in obj) { if (typeof obj.length !== 'number' || isnan(obj.length)) { + if (isArrayBufferView(obj) || 'length' in obj) { return createBuffer(0) } return fromArrayLike(obj) @@ -364,7 +364,7 @@ function byteLength (string, encoding) { if (Buffer.isBuffer(string)) { return string.length } - if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer) { + if (isArrayBufferView(string) || string instanceof ArrayBuffer) { return string.byteLength } if (typeof string !== 'string') { @@ -1704,3 +1704,8 @@ function blitBuffer (src, dst, offset, length) { function isnan (val) { return val !== val // eslint-disable-line no-self-compare } + +// Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView` +function isArrayBufferView (obj) { + return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj) +} -- 2.34.1