trimLeft.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var baseToString = require('../internal/baseToString'),
  2. charsLeftIndex = require('../internal/charsLeftIndex'),
  3. isIterateeCall = require('../internal/isIterateeCall'),
  4. trimmedLeftIndex = require('../internal/trimmedLeftIndex');
  5. /**
  6. * Removes leading whitespace or specified characters from `string`.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @category String
  11. * @param {string} [string=''] The string to trim.
  12. * @param {string} [chars=whitespace] The characters to trim.
  13. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  14. * @returns {string} Returns the trimmed string.
  15. * @example
  16. *
  17. * _.trimLeft(' abc ');
  18. * // => 'abc '
  19. *
  20. * _.trimLeft('-_-abc-_-', '_-');
  21. * // => 'abc-_-'
  22. */
  23. function trimLeft(string, chars, guard) {
  24. var value = string;
  25. string = baseToString(string);
  26. if (!string) {
  27. return string;
  28. }
  29. if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
  30. return string.slice(trimmedLeftIndex(string));
  31. }
  32. return string.slice(charsLeftIndex(string, (chars + '')));
  33. }
  34. module.exports = trimLeft;