Skip to content

Instantly share code, notes, and snippets.

@ruslanys
Created March 11, 2019 11:21
Show Gist options
  • Save ruslanys/4f12c3d9c8a2cfcf55ac8385c17b4fd4 to your computer and use it in GitHub Desktop.
Save ruslanys/4f12c3d9c8a2cfcf55ac8385c17b4fd4 to your computer and use it in GitHub Desktop.

Revisions

  1. ruslanys created this gist Mar 11, 2019.
    31 changes: 31 additions & 0 deletions unique-words.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/usr/bin/env node

    var fs = require('fs');

    // arg
    const filePath = process.argv[2];

    // read file
    let content = fs.readFileSync(filePath, 'utf8');

    // process content
    let processedContent = content
    .replace('\n', ' ')
    .replace(/\W/g, ' ');

    let words = processedContent.split(' ');
    let set = new Set();

    for (let i = 0; i < words.length; i++) {
    let word = words[i];
    let processedWord = word.trim().toLocaleLowerCase();

    if (processedWord.length === 0) {
    continue;
    }

    set.add(processedWord)
    }

    // output
    console.log('Unique words: ' + set.size);