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.

58 lines
1.0 KiB

  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(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. if (obj.toJSON) {
  35. obj = obj.toJSON();
  36. }
  37. for (var key in obj) {
  38. if (obj.hasOwnProperty(key) && _hasBinary(obj[key])) {
  39. return true;
  40. }
  41. }
  42. }
  43. return false;
  44. }
  45. return _hasBinary(data);
  46. }