isString.js 969 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var isArray = require('./isArray'),
  2. isObjectLike = require('./isObjectLike');
  3. /** `Object#toString` result references. */
  4. var stringTag = '[object String]';
  5. /** Used for built-in method references. */
  6. var objectProto = Object.prototype;
  7. /**
  8. * Used to resolve the
  9. * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  10. * of values.
  11. */
  12. var objectToString = objectProto.toString;
  13. /**
  14. * Checks if `value` is classified as a `String` primitive or object.
  15. *
  16. * @static
  17. * @since 0.1.0
  18. * @memberOf _
  19. * @category Lang
  20. * @param {*} value The value to check.
  21. * @returns {boolean} Returns `true` if `value` is correctly classified,
  22. * else `false`.
  23. * @example
  24. *
  25. * _.isString('abc');
  26. * // => true
  27. *
  28. * _.isString(1);
  29. * // => false
  30. */
  31. function isString(value) {
  32. return typeof value == 'string' ||
  33. (!isArray(value) && isObjectLike(value) && objectToString.call(value) == stringTag);
  34. }
  35. module.exports = isString;