MES手机端
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.
 
 
 
 

70 regels
2.2 KiB

  1. 'use strict';
  2. var callBound = require('call-bind/callBound');
  3. var hasToStringTag = require('has-tostringtag/shams')();
  4. var hasOwn = require('hasown');
  5. var gOPD = require('gopd');
  6. /** @type {import('.')} */
  7. var fn;
  8. if (hasToStringTag) {
  9. /** @type {(receiver: ThisParameterType<typeof RegExp.prototype.exec>, ...args: Parameters<typeof RegExp.prototype.exec>) => ReturnType<typeof RegExp.prototype.exec>} */
  10. var $exec = callBound('RegExp.prototype.exec');
  11. /** @type {object} */
  12. var isRegexMarker = {};
  13. var throwRegexMarker = function () {
  14. throw isRegexMarker;
  15. };
  16. /** @type {{ toString(): never, valueOf(): never, [Symbol.toPrimitive]?(): never }} */
  17. var badStringifier = {
  18. toString: throwRegexMarker,
  19. valueOf: throwRegexMarker
  20. };
  21. if (typeof Symbol.toPrimitive === 'symbol') {
  22. badStringifier[Symbol.toPrimitive] = throwRegexMarker;
  23. }
  24. /** @type {import('.')} */
  25. // @ts-expect-error TS can't figure out that the $exec call always throws
  26. // eslint-disable-next-line consistent-return
  27. fn = function isRegex(value) {
  28. if (!value || typeof value !== 'object') {
  29. return false;
  30. }
  31. // eslint-disable-next-line no-extra-parens
  32. var descriptor = /** @type {NonNullable<typeof gOPD>} */ (gOPD)(/** @type {{ lastIndex?: unknown }} */ (value), 'lastIndex');
  33. var hasLastIndexDataProperty = descriptor && hasOwn(descriptor, 'value');
  34. if (!hasLastIndexDataProperty) {
  35. return false;
  36. }
  37. try {
  38. // eslint-disable-next-line no-extra-parens
  39. $exec(value, /** @type {string} */ (/** @type {unknown} */ (badStringifier)));
  40. } catch (e) {
  41. return e === isRegexMarker;
  42. }
  43. };
  44. } else {
  45. /** @type {(receiver: ThisParameterType<typeof Object.prototype.toString>, ...args: Parameters<typeof Object.prototype.toString>) => ReturnType<typeof Object.prototype.toString>} */
  46. var $toString = callBound('Object.prototype.toString');
  47. /** @const @type {'[object RegExp]'} */
  48. var regexClass = '[object RegExp]';
  49. /** @type {import('.')} */
  50. fn = function isRegex(value) {
  51. // In older browsers, typeof regex incorrectly returns 'function'
  52. if (!value || (typeof value !== 'object' && typeof value !== 'function')) {
  53. return false;
  54. }
  55. return $toString(value) === regexClass;
  56. };
  57. }
  58. module.exports = fn;