count the unique occurrences of a word in a string

Usage no npm install needed!

<script type="module">
  import wordCataloguer from 'https://cdn.skypack.dev/word-cataloguer';



accepts a string and returns an array containing objects corresponding to each unique word

Build Status


$ npm install --save word-cataloguer


const wordCataloguer = require('word-cataloguer');

wordCataloguer('This module is really, really cool.');
[{word: 'really', count: 2},
{word: 'this', count: 1},
{word: 'module', count: 1},
{word: 'is', count: 1},
{word: 'cool', count: 1}


case-sensitive {caseSensitive: true}

By default all words are returned lower-case. Implement case-sensitive sorting with the following syntax:

wordCataloguer('Keep tHese biG, bIg GUY!', {caseSensitive: true});
{word: 'Keep', count: 1},
{word: 'tHese', count: 1},
{word: 'biG', count: 1},
{word: 'bIg', count: 1},
{word: 'GUY', count: 1}

{order: 'asc' || 'alpha'}

By default the array returned contains objects sorted by word-frequency in descending order. Return the objects in ascending order with the following syntax:

wordCataloguer('BOB SALLY JOE JOE JOHN JOE BOB!!!', {order: 'asc'});
{word: 'sally', count: 1},
{word: 'john', count: 1},
{word: 'bob', count: 2},
{word: 'joe', count: 3}

Alphabetical sorting is also supported:

wordCataloguer('d a b l f e'), {order: 'alpha'};
{word: a, count 1},
{word: b, count 1},
{word: d, count 1},
{word: e, count 1},
{word: f, count 1},
{word: l, count 1},

{compact: true}

A more compact output is also available:

wordCataloguer('This is much, much smaller.', {order: 'asc', compact: true});
[{w: 'This', c: '1'},
{w: 'is', c: '1'},
{w: 'smaller', c: 1},
{w: 'much', c: 2}]


Strings accepted by this module are stripped of punctuation and only a handful of symbols. Reference remove-punctuation for a list of symbols included in the removal process.


MIT © Christopher Howard