UNPKG

voca

Version:

The ultimate JavaScript string library

43 lines (36 loc) 1.36 kB
'use strict'; require('./internal/is_nil.js'); require('./is_string.js'); var coerce_to_string = require('./internal/coerce_to_string.js'); var reduce = Array.prototype.reduce; /** * Counts the characters in `subject` for which `predicate` returns truthy. * * @function countWhere * @static * @since 1.0.0 * @memberOf Count * @param {string} [subject=''] The string to count characters. * @param {Function} predicate The predicate function invoked on each character with parameters `(character, index, string)`. * @param {Object} [context] The context to invoke the `predicate`. * @return {number} Returns the number of characters for which `predicate` returns truthy. * @example * v.countWhere('hola!', v.isAlpha); * // => 4 * * v.countWhere('2022', function(character, index, str) { * return character === '2'; * }); * // => 3 */ function countWhere(subject, predicate, context) { var subjectString = coerce_to_string.coerceToString(subject); if (subjectString === '' || typeof predicate !== 'function') { return 0; } var predicateWithContext = predicate.bind(context); return reduce.call(subjectString, function (countTruthy, character, index) { return predicateWithContext(character, index, subjectString) ? countTruthy + 1 : countTruthy; }, 0); } module.exports = countWhere;