Skip to content

Commit c2dce58

Browse files
committed
Made word count slightly better
1 parent b314345 commit c2dce58

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/core/operations/WordCount.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class WordCount extends Operation {
6363
for (let j = 0; j < inputArray.length; j++) {
6464

6565
// Trim whitespace and replace punctuation
66-
const word = inputArray[j].replace(/(?:!|"|#|\$|%|&|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|\?|@|\[|\\|\]|\^|_|`|\{|\||\}|~|£)/g, "").trim();
66+
const word = inputArray[j].replace(/[!"#\$%&\(\)\*\+,-\.\/:;<=>\?@\[\\\]\^_`\{\\}~£\|]/g, "").trim();
67+
68+
6769

6870
// If empty string or ', then skip
6971
if (word === "" || /[']+/.test(word)) {
@@ -100,13 +102,11 @@ class WordCount extends Operation {
100102

101103
// Process output to string
102104
let output = "WORD,COUNT\n";
103-
for (let k = 0; k < order.length; k++) {
104-
output = output + order[k] + "," + counter[order[k]] + "\n";
105-
}
105+
output = output + order.map(entry => `${entry},${counter[entry]}`).join('\n');
106106

107107
// Add total counter at the bottom
108108
if (args[1]) {
109-
output = output + "TOTAL," + total;
109+
output = output + "\nTOTAL," + total;
110110
}
111111

112112
return output;

tests/operations/tests/WordCount.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ TestRegister.addTests([
99
{
1010
"name": "Word Count: Empty test 1",
1111
"input": "",
12-
"expectedOutput": "WORD,COUNT\nTOTAL,0",
12+
"expectedOutput": "WORD,COUNT\n\nTOTAL,0",
1313

1414
"recipeConfig": [
1515
{
@@ -21,7 +21,7 @@ TestRegister.addTests([
2121
{
2222
"name": "Word Count: Empty test 2",
2323
"input": "",
24-
"expectedOutput": "WORD,COUNT\nTOTAL,0",
24+
"expectedOutput": "WORD,COUNT\n\nTOTAL,0",
2525

2626
"recipeConfig": [
2727
{
@@ -81,7 +81,7 @@ TestRegister.addTests([
8181
{
8282
"name": "Word Count: Count test 3",
8383
"input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
84-
"expectedOutput": "WORD,COUNT\nhello,2\nworld,3\n",
84+
"expectedOutput": "WORD,COUNT\nhello,2\nworld,3",
8585

8686
"recipeConfig": [
8787
{
@@ -93,7 +93,7 @@ TestRegister.addTests([
9393
{
9494
"name": "Word Count: Count test 4",
9595
"input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
96-
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n",
96+
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2",
9797

9898
"recipeConfig": [
9999
{
@@ -105,7 +105,7 @@ TestRegister.addTests([
105105
{
106106
"name": "Word Count: Different delimiter test",
107107
"input": "Hello, World\nhello, world \n''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
108-
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n",
108+
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2",
109109

110110
"recipeConfig": [
111111
{

0 commit comments

Comments
 (0)