diff --git a/src/core/operations/WordCount.mjs b/src/core/operations/WordCount.mjs index a94eb0ac9..a34e88f90 100644 --- a/src/core/operations/WordCount.mjs +++ b/src/core/operations/WordCount.mjs @@ -63,7 +63,9 @@ class WordCount extends Operation { for (let j = 0; j < inputArray.length; j++) { // Trim whitespace and replace punctuation - const word = inputArray[j].replace(/(?:!|"|#|\$|%|&|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|\?|@|\[|\\|\]|\^|_|`|\{|\||\}|~|£)/g, "").trim(); + const word = inputArray[j].replace(/[!"#\$%&\(\)\*\+,-\.\/:;<=>\?@\[\\\]\^_`\{\\}~£\|]/g, "").trim(); + + // If empty string or ', then skip if (word === "" || /[']+/.test(word)) { @@ -100,13 +102,11 @@ class WordCount extends Operation { // Process output to string let output = "WORD,COUNT\n"; - for (let k = 0; k < order.length; k++) { - output = output + order[k] + "," + counter[order[k]] + "\n"; - } + output = output + order.map(entry => `${entry},${counter[entry]}`).join('\n'); // Add total counter at the bottom if (args[1]) { - output = output + "TOTAL," + total; + output = output + "\nTOTAL," + total; } return output; diff --git a/tests/operations/tests/WordCount.mjs b/tests/operations/tests/WordCount.mjs index bc518de2d..4d210260e 100644 --- a/tests/operations/tests/WordCount.mjs +++ b/tests/operations/tests/WordCount.mjs @@ -9,7 +9,7 @@ TestRegister.addTests([ { "name": "Word Count: Empty test 1", "input": "", - "expectedOutput": "WORD,COUNT\nTOTAL,0", + "expectedOutput": "WORD,COUNT\n\nTOTAL,0", "recipeConfig": [ { @@ -21,7 +21,7 @@ TestRegister.addTests([ { "name": "Word Count: Empty test 2", "input": "", - "expectedOutput": "WORD,COUNT\nTOTAL,0", + "expectedOutput": "WORD,COUNT\n\nTOTAL,0", "recipeConfig": [ { @@ -81,7 +81,7 @@ TestRegister.addTests([ { "name": "Word Count: Count test 3", "input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world", - "expectedOutput": "WORD,COUNT\nhello,2\nworld,3\n", + "expectedOutput": "WORD,COUNT\nhello,2\nworld,3", "recipeConfig": [ { @@ -93,7 +93,7 @@ TestRegister.addTests([ { "name": "Word Count: Count test 4", "input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world", - "expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n", + "expectedOutput": "WORD,COUNT\nworld,3\nhello,2", "recipeConfig": [ { @@ -105,7 +105,7 @@ TestRegister.addTests([ { "name": "Word Count: Different delimiter test", "input": "Hello, World\nhello, world \n''!@£$%^&*()_+=-[]{};'|:/.,<>? world", - "expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n", + "expectedOutput": "WORD,COUNT\nworld,3\nhello,2", "recipeConfig": [ {