1 | ;
|
2 | var isString = require('./string'),
|
3 | handleCurry = require('./utils/handleCurry');
|
4 |
|
5 | /**
|
6 | * Checks whether a string starts with a given prefix
|
7 | *
|
8 | * @function startsWith
|
9 | *
|
10 | * @example
|
11 | * var is = require('predicates');
|
12 | *
|
13 | * var isProfessor = is.startsWith('Prof. ');
|
14 | * isProfessor('Prof. Bend Ovah'); // true
|
15 | * // same as
|
16 | * is.startsWith('Prof. ', 'Prof. Bend Ovah'); // true
|
17 | *
|
18 | * isProfessor('Dr. Here U\' Are'); // false
|
19 | *
|
20 | * @param {String} prefix
|
21 | * @param {String} [value]
|
22 | * @throws {TypeError} if prefix is not a string
|
23 | * @throws {Error} if prefix is empty
|
24 | * @returns {(Boolean|Predicate)} returns bool if at least two arguments provided, otherwise a predicate
|
25 | */
|
26 | module.exports = function startsWith(prefix) {
|
27 | if (!isString(prefix)) {
|
28 | throw new TypeError('Prefix must be a string');
|
29 | }
|
30 | if (prefix === '') {
|
31 | throw new Error('Prefix cannot be empty');
|
32 | }
|
33 |
|
34 | return handleCurry.call(this, arguments, function startsWithPredicate(value) {
|
35 | return isString(value) && value.indexOf(prefix) === 0;
|
36 | });
|
37 | };
|