Word Density Counter In Vanilla JavaScript – density.js

Category: Javascript , Text | October 17, 2019
Author: jenstornell 504 views Go to website October 17, 2019 MIT

Description:

density.js is a pure JavaScript library that can be used to count and calculate how many times your words appear in the HTML/Text.

How to use it:

`<script src="density.js"></script>`

2. Define the text in which you want to count the word density.

```const html = `<h1>A html heading</h1>
<p>Here is a html paragraph</p>`;```

3. Output the results.

```// Simple sorted
results = density.getSorted(html);
console.log(results);
// Simple unsorted
results = density.getUnsorted(html);
console.log(results);```
```// results in
0: {count: 2, word: "a"}
1: {count: 2, word: "html"}
3: {count: 1, word: "here"}
4: {count: 1, word: "is"}
5: {count: 1, word: "paragraph"}```

4. Specify an array of stop words to ignore.

```results = density.getSorted(html, {
stopwords: ["here is"]
});
console.log(results);```

5. With selected words.

```results = density.getSorted(html, {
});
console.log(results);```

6. Specify the number of words on each row. Default: 1.

```results = density.getSorted(html, {
words: 2
});
console.log(results);```

7. Specify the minimum number of characters. Default: 0 (disable).

```results = density.getSorted(html, {
characters: 2
});
console.log(results);```