Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

text-tools-js

A tiny, zero-dependency text utility library for JavaScript: word/character/sentence counting with reading-time estimate, plus 8 case-conversion modes (UPPERCASE, lowercase, Title Case, Sentence case, camelCase, snake_case, kebab-case, aLtErNaTe). No build step, no external libraries.

Live demo → · Full-featured version on roversia.it →

Why

Word counting and case conversion are things every text-editing tool eventually needs, and the logic is small enough that pulling in a dependency for it is usually overkill. This package is the whole thing in one small file, tested against real edge cases (empty input, whitespace-only text, punctuation in identifiers).

Features

  • 📊 Word, character, sentence, and paragraph counting
  • ⏱️ Reading-time estimate with configurable words-per-minute
  • 🔤 8 case-conversion modes in one function
  • 📦 Zero dependencies, no build step
  • 🌐 Works as ESM, CommonJS, or plain <script> tag

Install

Copy src/text-tools.js into your project, or clone this repo. No npm package yet — open an issue if you'd find one useful.

Usage

As an ES module

import { countText, convertCase } from './text-tools.js';

countText('Hello world. How are you?');
// { words: 5, characters: 25, charactersNoSpaces: 21, sentences: 2, paragraphs: 1, readingTimeMinutes: 1 }

convertCase('hello world', 'title');     // 'Hello World'
convertCase('hello world', 'camel');     // 'helloWorld'
convertCase('Hello World!', 'snake');    // 'hello_world'

Plain <script> tag (no bundler)

<script src="text-tools.js"></script>
<script>
  const stats = window.TextTools.countText(document.querySelector('textarea').value);
</script>

API

countText(text, options?)

Returns { words, characters, charactersNoSpaces, sentences, paragraphs, readingTimeMinutes }.

Option Type Default Description
wordsPerMinute number 200 Reading speed used for the time estimate
countText('First paragraph.\n\nSecond paragraph.');
// { words: 4, characters: 34, ..., paragraphs: 2 }

Reading time is always at least 1 minute for any non-empty text, and 0 for empty input.

convertCase(text, mode)

Converts text to one of 8 case styles. Throws on an unknown mode.

Mode Example (hello world)
upper HELLO WORLD
lower hello world
title Hello World
sentence Hello world
camel helloWorld
snake hello_world
kebab hello-world
alternate hElLo WoRlD

CASE_MODES

Array of the 8 valid mode strings, useful for building a UI (dropdown, tabs, etc.) without hardcoding the list.

FAQ

How is reading time calculated? words / wordsPerMinute, rounded to the nearest minute, with a floor of 1 minute for any non-empty text. Default reading speed is 200 words per minute — a common average for adult silent reading.

How are sentences counted? By splitting on ., !, or ?, then filtering out empty fragments. This is a simple heuristic — it doesn't handle abbreviations like "Dr." or decimal numbers specially, so very technical text may over-count sentences slightly.

Why no npm package? Keeping this dependency-free and copy-pasteable on purpose. Open an issue if there's demand for one.

Related

This is one of 39+ free browser-based tools I maintain at roversia.it. The full-featured version includes a live UI with instant stats as you type.

License

MIT © Andrea Roversi

Releases

Packages

Contributors

Languages