index.js 1.4 KB

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