startsWith.js 1017 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var baseClamp = require('./_baseClamp'),
  2. baseToString = require('./_baseToString'),
  3. toInteger = require('./toInteger'),
  4. toString = require('./toString');
  5. /**
  6. * Checks if `string` starts with the given target string.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 3.0.0
  11. * @category String
  12. * @param {string} [string=''] The string to inspect.
  13. * @param {string} [target] The string to search for.
  14. * @param {number} [position=0] The position to search from.
  15. * @returns {boolean} Returns `true` if `string` starts with `target`,
  16. * else `false`.
  17. * @example
  18. *
  19. * _.startsWith('abc', 'a');
  20. * // => true
  21. *
  22. * _.startsWith('abc', 'b');
  23. * // => false
  24. *
  25. * _.startsWith('abc', 'b', 1);
  26. * // => true
  27. */
  28. function startsWith(string, target, position) {
  29. string = toString(string);
  30. position = position == null
  31. ? 0
  32. : baseClamp(toInteger(position), 0, string.length);
  33. target = baseToString(target);
  34. return string.slice(position, position + target.length) == target;
  35. }
  36. module.exports = startsWith;