Skip to content

Commit

Permalink
fix(string/countWords): punctation chars
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkr committed Dec 7, 2021
1 parent 388bac5 commit 62e1460
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/string/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @param {String} str
* @returns {Number}
*/
const fn = str => str.trim().split(/\s+/g).length;
// const fn = str => str.trim().split(/\W+/g).length;
const fn = str => str.trim().split(/\s+/g).map(i => i.replace(/[\[\]?.,\/#!$%\^&\*;:{}=\"\-_~()…–—·'’]/g,"")).filter(i=>i).length;

export default fn;
10 changes: 10 additions & 0 deletions test/string/count-words.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ test('returns the amount of words in a string if the last chars are whitespaces'
let input = "foo bar baz ";
t.is( countWords(input), 3);
});

test('returns the amount of words in a string if the there are regular punctation marks and whitespaces', t => {
let input = "~ … ;,. ! ? [foo1] foo2 foo3-bar3 baz2 – — · / ( ) { } [ ] \" ' : ’ baz1";
t.is( countWords(input), 5);
});

test('returns the amount of words in a string if they are surounded by spaces', t => {
let input = "foo bar baz – — · / ( ) { } [ ] ~ … . , ; ! ? ' : ’ \" foobar [foo] ";
t.is( countWords(input), 5);
});

0 comments on commit 62e1460

Please sign in to comment.