index.js 1.5 KB

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