npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

countable

v3.0.1

Published

Countable is a JavaScript library to add live paragraph-, word- and character-counting to an HTML element.

Downloads

8,481

Readme

Countable

Build Status Code Climate Latest Tag License

Countable is a JavaScript function to add live paragraph-, word- and character-counting to an HTML element. Countable is a zero-dependency library and comes in at 1KB when minified and gzipped.

View the Demo

Installation

The preferred method of installation is npm or yarn.

npm i --save-dev countable
yarn add --dev countable

Alternatively, you can download the latest zipball or copy the script directly.

Usage

Countable is available as a Node / CommonJS module, an AMD module and as a global. All methods are accessed on the Countable object directly.

Callbacks

The on and count methods both accept a callback. The given callback is then called whenever needed with a single parameter that carries all the relevant data. this is bound to the current element. Take the following code for an example.

Countable.count(document.getElementById('text'), counter => console.log(this, counter))
=> <textarea id="text"></textarea>, { all: 0, characters: 0, paragraphs: 0, words: 0 }

Property | Meaning ---------- | -------------------------------------------------------------------------------------------- paragraphs | The number of paragraphs. Paragraphs can be separated by either a soft or a hard (two line breaks) return. To use hard returns, set the corresponding option (hardReturns). sentences | The number of sentences. Sentences are separated by a sentence-terminating character. words | The number of words. Words are split using spaces. characters | The number of characters (without spaces). This contains all non-whitespace characters. all | The number of characters including whitespace. This is the total number of all characters in the element.

Countable.on(elements, callback, options)

Bind the callback to all given elements. The callback gets called every time the element's value or text is changed.

Countable.on(area, counter => console.log(counter))

Countable.off(elements)

Remove the bound callback from all given elements.

Countable.off(area)

Countable.count(elements, callback, options)

Similar to Countable.on(), but the callback is only executed once, there are no events bound.

Countable.once(area, counter => console.log(counter))

Countable.enabled(elements)

Checks the live-counting functionality is bound to the given.

Countable.enabled(area)

Options

Countable.on() and Countable.count() both accept a third argument, an options object that allows you to change how Countable treats certain aspects of your element's text.

{
  hardReturns: false,
  stripTags: false,
  ignore: []
}

By default, paragraphs are split by a single return (a soft return). By setting hardReturns to true, Countable splits paragraphs after two returns.

Depending on your application and audience, you might need to strip HTML tags from the text before counting it. You can do this by setting stripTags to true.

Sometimes it is desirable to ignore certain characters. These can be included in an array and passed using the ignore option.

Browser Support

Countable supports all modern browsers. Full ES5 support is expected, as are some ES6 features, namely let and const.