You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.2 KiB

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