Get the count of how often substring
occurs in value
.
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
This is a tiny package that finds the count of a substring.
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.
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>
import {ccount} from 'ccount'
ccount('foo(bar(baz)', '(') // => 2
ccount('foo(bar(baz)', ')') // => 1
This package exports the identifier ccount
.
There is no default export.
Get the count of substring
in value
.
value
(string
) — content to search insubstring
(string
) — substring to look for, typically one character
Count of substring
s in value
(number
).
This package is fully typed with TypeScript. It exports no additional types.
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.
This package is safe.
wooorm/longest-streak
— count the longest repeating streak ofsubstring
s invalue
wooorm/direction
— detect directionality: left-to-right, right-to-left, or neutral
Yes please! See How to Contribute to Open Source.