forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartsWith.js
36 lines (35 loc) · 878 Bytes
/
startsWith.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Checks if `string` starts with the given target string.
*
* @since 3.0.0
* @category String
* @param {string} [string=''] The string to inspect.
* @param {string} [target] The string to search for.
* @param {number} [position=0] The position to search from.
* @returns {boolean} Returns `true` if `string` starts with `target`,
* else `false`.
* @see endsWith, includes
* @example
*
* startsWith('abc', 'a')
* // => true
*
* startsWith('abc', 'b')
* // => false
*
* startsWith('abc', 'b', 1)
* // => true
*/
function startsWith(string, target, position) {
const { length } = string
position = position == null ? 0 : position
if (position < 0) {
position = 0
}
else if (position > length) {
position = length
}
target = `${target}`
return string.slice(position, position + target.length) == target
}
export default startsWith