Difference between revisions of "WebFund 2016W: Tutorial 1"

From Soma-notes
Jump to navigation Jump to search
Line 1: Line 1:
'''This tutorial is not yet finalized.'''
In this tutorial you will be studying and extending two implementations of simplegrep, included below.  grep is a standard UNIX utility for returning lines from a text file that match a given pattern.
In this tutorial you will be studying and extending two implementations of simplegrep, included below.  grep is a standard UNIX utility for returning lines from a text file that match a given pattern.


Line 10: Line 8:
Then, do all of the above with the async version.  Make sure to use writeFile(), not writeFileSync()!
Then, do all of the above with the async version.  Make sure to use writeFile(), not writeFileSync()!


You may find the following methods useful when working with arrays:[https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push push()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop pop()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift shift()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join join].
You may find the following methods useful when working with arrays:[https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push push()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop pop()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift shift()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join join()].


==Code==
==Code==

Revision as of 17:16, 12 January 2016

In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern.

First, with the sync version, try the following:

  • Make the search pattern into a regular expression.
  • Give it an additional argument that causes matched lines to be output to a new file. You'll need to use writeFileSync() from the Node fs module. When finished, output "All done!" to the console.
  • Make the output lines sorted in alphabetical order.

Then, do all of the above with the async version. Make sure to use writeFile(), not writeFileSync()!

You may find the following methods useful when working with arrays:push(), pop(), shift(), unshift(), and join().

Code

simplegrep_sync.js

Downloadable version.

var fs = require('fs');

if (process.argv.length < 4) {
    console.error('Not enough parameters given. Try this: ' +
                  '"node simplegrep_sync term filename.txt"'); 
    process.exit(1);
}

var searchterm = process.argv[2];
var filename = process.argv[3];

var rawContents = fs.readFileSync(filename, 'utf-8');
var lines = rawContents.split('\n');

for (i = 0; i < lines.length; i++) {
    if (lines[i].indexOf(searchterm) > -1) {
        console.log(lines[i]);
    }
}

simplegrep_async.js

Downloadable version.

var fs = require('fs');

if (process.argv.length < 4) {
    console.error('Not enough parameters given. Try this: ' +
                  '"node simplegrep_async term filename.txt"'); 
    process.exit(1);
}

var searchterm = process.argv[2];
var filename = process.argv[3];

var returnMatches = function(err, rawContents) {

    var lines = rawContents.split('\n');

    lines.forEach(function(theLine) {
        if (theLine.indexOf(searchterm) > -1) {
            console.log(theLine);
        }
    });
}

fs.readFile(filename, 'utf-8', returnMatches);