Skip to content

Bug: Word Count Shows Incorrect Value for Whitespace #1687

@pharmacist-sabot

Description

@pharmacist-sabot

Describe the bug

The word count statistic displays an incorrect value when the input contains only whitespace characters (spaces, tabs, newlines, etc.). Instead of showing 0, it shows a count of 2

File
text-statistics.vue (Line 14)

Current Behavior
When the textarea contains only whitespace:
Input: (3 spaces)
Expected word count: 0
Actual word count: 2

Root Cause
The current implementation uses

text === '' ? 0 : text.split(/\s+/).length

When text contains only whitespace, split(/\s+/) produces an array with empty strings

" ".split(/\s+/) returns ["", ""]

This results in length = 2 instead of 0

Proposed Fix
Use trim() to remove leading/trailing whitespace before checking and splitting

text.trim() === '' ? 0 : text.trim().split(/\s+/).length

What happened?

A bug happened!

System information

Browser at https://it-tools.tech/text-statistics

Where did you encounter the bug?

Public app (it-tools.tech)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtriage

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions