UNPKG

1.06 kBJavaScriptView Raw
1'use strict';
2
3var isString = require('./string'),
4 handleCurry = require('./utils/handleCurry');
5
6/**
7 * Checks whether a string ends with a given suffix
8 *
9 * @function endsWith
10 *
11 * @example
12 * var is = require('predicates');
13 *
14 * var isYelling = is.endsWith('!');
15 *
16 * isYelling('shut up!'); // true
17 * // same as
18 * is.endsWith('!', 'shut up!'); // true
19 * isYelling('be quiet please'); // false
20 *
21 * @param {String} suffix
22 * @param {String} [value]
23 * @throws {TypeError} if suffix is not a string
24 * @throws {Error} if suffix is empty
25 * @returns {(Boolean|Predicate)} returns bool if at least two arguments provided, otherwise a predicate
26 */
27module.exports = function endsWith(suffix) {
28 if (!isString(suffix)) {
29 throw new TypeError('Suffix must be a string');
30 }
31 if (suffix === '') {
32 throw new Error('Suffix cannot be empty');
33 }
34
35 return handleCurry.call(this, arguments, function endsWithPredicate(value) {
36 return isString(value) && value.indexOf(suffix) === value.length - suffix.length;
37 });
38};
\No newline at end of file