Skip to content
/ ccount Public

Count how often a substring occurs

License

Notifications You must be signed in to change notification settings

wooorm/ccount

Repository files navigation

ccount

Build Coverage Downloads Size

Get the count of how often substring occurs in value.

Contents

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 in
  • substring (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

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer