mirror of
https://github.com/nodejs/node.git
synced 2026-12-06 10:48:45 -05:00
* install eslint-plugin-markdown * add doc/.eslintrc.yaml * add `<!-- eslint-disable rule -->` or `<!-- eslint-disable -->` for the rest of problematic code * .js files in doc folder added to .eslintignore * update Makefile and vcbuild.bat PR-URL: https://github.com/nodejs/node/pull/12563 Refs: https://github.com/nodejs/node/pull/12557#issuecomment-296015032 Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
47 lines
923 B
JavaScript
47 lines
923 B
JavaScript
/**
|
|
* @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;
|
|
}
|