Skip to content

Commit

Permalink
Fix/wordcount whitespace only error (#123)
Browse files Browse the repository at this point in the history
* Addresses error thrown when the results of matchWord or matchCharacters returns null. Includes test for the case

* Changes as per @tofumatt

* Going full ternary
  • Loading branch information
ryanwelcher authored and aduth committed May 7, 2018
1 parent aaa44c2 commit 4892728
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/wordcount/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export function count( text, type, userSettings ) {
const settings = loadSettings( type, userSettings );
if ( text ) {
let matchRegExp = settings[ type + 'RegExp' ];
if ( 'words' === settings.type ) {
return matchWords( text, matchRegExp, settings ).length;
} else {
return matchCharacters( text, matchRegExp, settings ).length;
}
const results = ( 'words' === settings.type ) ?
matchWords( text, matchRegExp, settings ) :
matchCharacters( text, matchRegExp, settings );

return results ? results.length : 0;
}
}
7 changes: 7 additions & 0 deletions packages/wordcount/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ describe( 'WordCounter', () => {
words: 1,
characters_excluding_spaces: 5,
characters_including_spaces: 6
},
{
message: "Empty Tags",
string: "<p></p>",
words: 0,
characters_excluding_spaces: 0,
characters_including_spaces: 0
}
];

Expand Down

0 comments on commit 4892728

Please sign in to comment.