mirror of
https://github.com/gchq/CyberChef.git
synced 2026-03-23 07:11:35 -07:00
Made word count slightly better
This commit is contained in:
parent
b3143451e6
commit
c2dce58db2
2 changed files with 10 additions and 10 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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": [
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue