index.js (1508B)
1 /* global Blob File */ 2 3 /* 4 * Module requirements. 5 */ 6 7 var isArray = require('isarray'); 8 9 var toString = Object.prototype.toString; 10 var withNativeBlob = typeof Blob === 'function' || 11 typeof Blob !== 'undefined' && toString.call(Blob) === '[object BlobConstructor]'; 12 var withNativeFile = typeof File === 'function' || 13 typeof File !== 'undefined' && toString.call(File) === '[object FileConstructor]'; 14 15 /** 16 * Module exports. 17 */ 18 19 module.exports = hasBinary; 20 21 /** 22 * Checks for binary data. 23 * 24 * Supports Buffer, ArrayBuffer, Blob and File. 25 * 26 * @param {Object} anything 27 * @api public 28 */ 29 30 function hasBinary (obj) { 31 if (!obj || typeof obj !== 'object') { 32 return false; 33 } 34 35 if (isArray(obj)) { 36 for (var i = 0, l = obj.length; i < l; i++) { 37 if (hasBinary(obj[i])) { 38 return true; 39 } 40 } 41 return false; 42 } 43 44 if ((typeof Buffer === 'function' && Buffer.isBuffer && Buffer.isBuffer(obj)) || 45 (typeof ArrayBuffer === 'function' && obj instanceof ArrayBuffer) || 46 (withNativeBlob && obj instanceof Blob) || 47 (withNativeFile && obj instanceof File) 48 ) { 49 return true; 50 } 51 52 // see: https://github.com/Automattic/has-binary/pull/4 53 if (obj.toJSON && typeof obj.toJSON === 'function' && arguments.length === 1) { 54 return hasBinary(obj.toJSON(), true); 55 } 56 57 for (var key in obj) { 58 if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) { 59 return true; 60 } 61 } 62 63 return false; 64 }