asserts.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. export function isPromiseMethod(x) {
  2. if (x === "then" || x === "catch" || x === "finally") {
  3. return true;
  4. }
  5. return false;
  6. }
  7. export function isKVMap(x) {
  8. if (typeof x !== "object" || x == null) {
  9. return false;
  10. }
  11. const prototype = Object.getPrototypeOf(x);
  12. return ((prototype === null ||
  13. prototype === Object.prototype ||
  14. Object.getPrototypeOf(prototype) === null) &&
  15. !(Symbol.toStringTag in x) &&
  16. !(Symbol.iterator in x));
  17. }
  18. export const isAsyncIterable = (x) => x != null &&
  19. typeof x === "object" &&
  20. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  21. typeof x[Symbol.asyncIterator] === "function";
  22. export const isIteratorLike = (x) => x != null &&
  23. typeof x === "object" &&
  24. "next" in x &&
  25. typeof x.next === "function";
  26. const GeneratorFunction = function* () { }.constructor;
  27. export const isGenerator = (x) =>
  28. // eslint-disable-next-line no-instanceof/no-instanceof
  29. x != null && typeof x === "function" && x instanceof GeneratorFunction;
  30. export const isThenable = (x) => x != null &&
  31. typeof x === "object" &&
  32. "then" in x &&
  33. typeof x.then === "function";
  34. export const isReadableStream = (x) => x != null &&
  35. typeof x === "object" &&
  36. "getReader" in x &&
  37. typeof x.getReader === "function";