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