ccount
Get the count of how often substring occurs in value.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This is a tiny package that finds the count of a substring.
When should I use this?
I find this particularly useful when generating code, for example, when building a string that can either be double or single quoted. I use this utility to choose single quotes when double quotes are used more frequently, and double quotes otherwise.
Install
This package is ESM only. In Node.js (version 14.14+, 16.0+), install with npm:
npm install ccount
In Deno with esm.sh:
import {ccount} from 'https://esm.sh/ccount@2'
In browsers with esm.sh:
<script type="module">
import {ccount} from 'https://esm.sh/ccount@2?bundle'
</script>
Use
import {ccount} from 'ccount'
ccount('foo(bar(baz)', '(') // => 2
ccount('foo(bar(baz)', ')') // => 1
API
This package exports the identifier ccount.
There is no default export.
ccount(value, character)
Get the count of substring in value.
Parameters
value(string) — content to search insubstring(string) — substring to look for, typically one character
Returns
Count of substrings in value (number).
Types
This package is fully typed with TypeScript. It exports no additional types.
Compatibility
This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. It also works in Deno and modern browsers.
Security
This package is safe.
Related
wooorm/longest-streak— count the longest repeating streak ofsubstrings invaluewooorm/direction— detect directionality: left-to-right, right-to-left, or neutral
Contribute
Yes please! See How to Contribute to Open Source.