index.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var callBound = require('call-bound');
  3. /** @type {(receiver: ThisParameterType<typeof String.prototype.valueOf>, ...args: Parameters<typeof String.prototype.valueOf>) => ReturnType<typeof String.prototype.valueOf>} */
  4. var $strValueOf = callBound('String.prototype.valueOf');
  5. /** @type {import('.')} */
  6. var tryStringObject = function tryStringObject(value) {
  7. try {
  8. $strValueOf(value);
  9. return true;
  10. } catch (e) {
  11. return false;
  12. }
  13. };
  14. /** @type {(receiver: ThisParameterType<typeof Object.prototype.toString>, ...args: Parameters<typeof Object.prototype.toString>) => ReturnType<typeof Object.prototype.toString>} */
  15. var $toString = callBound('Object.prototype.toString');
  16. var strClass = '[object String]';
  17. var hasToStringTag = require('has-tostringtag/shams')();
  18. /** @type {import('.')} */
  19. module.exports = function isString(value) {
  20. if (typeof value === 'string') {
  21. return true;
  22. }
  23. if (!value || typeof value !== 'object') {
  24. return false;
  25. }
  26. return hasToStringTag ? tryStringObject(value) : $toString(value) === strClass;
  27. };