diff options
Diffstat (limited to 'tools/eslint/node_modules/ccount/index.js')
-rw-r--r-- | tools/eslint/node_modules/ccount/index.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tools/eslint/node_modules/ccount/index.js b/tools/eslint/node_modules/ccount/index.js new file mode 100644 index 00000000000..0d72d6e5276 --- /dev/null +++ b/tools/eslint/node_modules/ccount/index.js @@ -0,0 +1,46 @@ +/** + * @author Titus Wormer + * @copyright 2015 Titus Wormer + * @license MIT + * @module ccount + * @fileoverview Count characters. + */ + +'use strict'; + +/* Expose. */ +module.exports = ccount; + +/** + * Count how many characters `character` occur in `value`. + * + * @example + * ccount('foo(bar(baz)', '(') // 2 + * ccount('foo(bar(baz)', ')') // 1 + * + * @param {string} value - Content, coerced to string. + * @param {string} character - Single character to look + * for. + * @return {number} - Count. + * @throws {Error} - when `character` is not a single + * character. + */ +function ccount(value, character) { + var count = 0; + var index; + + value = String(value); + + if (typeof character !== 'string' || character.length !== 1) { + throw new Error('Expected character'); + } + + index = value.indexOf(character); + + while (index !== -1) { + count++; + index = value.indexOf(character, index + 1); + } + + return count; +} |