is-binary.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.hasBinary = exports.isBinary = void 0;
  4. const withNativeArrayBuffer = typeof ArrayBuffer === "function";
  5. const isView = (obj) => {
  6. return typeof ArrayBuffer.isView === "function"
  7. ? ArrayBuffer.isView(obj)
  8. : obj.buffer instanceof ArrayBuffer;
  9. };
  10. const toString = Object.prototype.toString;
  11. const withNativeBlob = typeof Blob === "function" ||
  12. (typeof Blob !== "undefined" &&
  13. toString.call(Blob) === "[object BlobConstructor]");
  14. const withNativeFile = typeof File === "function" ||
  15. (typeof File !== "undefined" &&
  16. toString.call(File) === "[object FileConstructor]");
  17. /**
  18. * Returns true if obj is a Buffer, an ArrayBuffer, a Blob or a File.
  19. *
  20. * @private
  21. */
  22. function isBinary(obj) {
  23. return ((withNativeArrayBuffer && (obj instanceof ArrayBuffer || isView(obj))) ||
  24. (withNativeBlob && obj instanceof Blob) ||
  25. (withNativeFile && obj instanceof File));
  26. }
  27. exports.isBinary = isBinary;
  28. function hasBinary(obj, toJSON) {
  29. if (!obj || typeof obj !== "object") {
  30. return false;
  31. }
  32. if (Array.isArray(obj)) {
  33. for (let i = 0, l = obj.length; i < l; i++) {
  34. if (hasBinary(obj[i])) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. if (isBinary(obj)) {
  41. return true;
  42. }
  43. if (obj.toJSON &&
  44. typeof obj.toJSON === "function" &&
  45. arguments.length === 1) {
  46. return hasBinary(obj.toJSON(), true);
  47. }
  48. for (const key in obj) {
  49. if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. exports.hasBinary = hasBinary;