Skip to content
Browse files

Update format of node-for-everyday-things.

  • Loading branch information...
1 parent baca343 commit 2381ca5d6463daed5011959e32acdebceffce0be @creationix committed Apr 14, 2010
View
6 .gitmodules
@@ -0,0 +1,6 @@
+[submodule "articles/step-of-conductor/step"]
+ path = articles/step-of-conductor/step
+ url = git://github.com/creationix/step.git
+[submodule "articles/step-of-conductor/conductor"]
+ path = articles/step-of-conductor/conductor
+ url = git://github.com/creationix/conductor.git
View
66 articles/node-for-everyday-things.markdown
@@ -1,6 +1,7 @@
Title: NodeJS for everyday things
Author: R. S. Doiel
Date: Fri Feb 19 2010 14:20:20 GMT-0800 (PST)
+Node: v0.1.90
Everyday things:
> Those little programs you write quickly to get something done like counting pages in a text document.
@@ -9,7 +10,6 @@ Everyday things:
Recently I was writing an essay and I needed to calculate a page count. My text editor was great at giving me a word count. I used the result and divided it by 350 to ball park my page count. This became problematic because I'm lousy at doing math in my head. Next I decided to chain some Unix commands together to do the job. I played with Bash, Unix's wc and cut commands I came up with this:
- #!sh
#!/usr/bin/env bash
let x=$(wc -w $1 | cut -d\ -f 1)/350
echo "Page count for $1 is $x";
@@ -18,70 +18,8 @@ This was nice. It was short. It worked but I wanted to procrastinate a little mo
The page count problems are basically a simple analysis of text with some accounting. Could I quickly write a program using node to do something so mundane? Yes and it was surprisingly straight forward. I fired up node-repl and started playing around before typing up this:
- #!/usr/bin/env node
- /**
- * Calculate the approximate the number of "pages" in a text document based on
- * a word count of 350 words per page.
- */
+<node-for-everyday-things/word-count.js*>
- var sys = require('sys'),
- fs = require('fs');
-
- (function () {
- /* Include some instructions for when I forget how this works. */
- USAGE = function (message) {
- var error_code = 0;
- sys.puts("\n USAGE: pagecount FILENAME\n" +
- "\n" + " Show the estimated page count of a file based on 350 words per page.\n" +
- " FILENAME should be the name of a utf-8 encoded text file.\n" +
- "\b\n Example:\n\t\tpagecount MyFile.txt\n\n Estimates the page count of MyFile.txt\n");
-
- if (message !== undefined) {
- sys.puts(message);
- error_code = 1;
- }
- process.exit(error_code);
- };
-
- if (process.argv.length < 3) {
- USAGE();
- }
-
- /* PageCount() analyze the file and displays the results */
- PageCount = function (filename) {
- fs.stat(filename, function (stat_error, stat) {
- if (stat_error) {
- USAGE("ERROR: " + filename + ", " + stat_error);
- }
-
- if (stat.isFile()) {
- fs.readFile(filename, 'utf8', function (read_error, content) {
- var subtotal_words = 0;
- if (read_error) {
- USAGE("ERROR: Can't read " + filename + ". " + read_error);
- }
- /* Replace all non-letter characters with a single space. */
- subtotal_words = content.replace(/\W+|\s+/gm,' ').split(' ').length;
- page_count = (subtotal_words/350);
- if (Number(page_count).toFixed(0) <= 1) {
- sys.puts(filename + "(" + subtotal_words + " words): " +
- Number(page_count * 100).toFixed(0) + "% of one page.");
- } else {
- sys.puts(filename + "(" + subtotal_words + " words):" +
- Number(page_count).toFixed(2) + " pages.");
- }
- });
- } else {
- USAGE("ERROR: " + filename + " is not a file.");
- }
- });
- };
-
- /* For each file I want to tally up call PageCount() */
- for (var i = 2; i < process.argv.length; i += 1) {
- PageCount(process.argv[i]);
- }
- })();
`pagecount` reports the plain text version of this essay is 636 words or 1.82 pages including the source code examples. It's a little longer than my shell script. On the other hand it is easier to read and with a little modification I can embed it as a web service or put it into a web page.
View
63 articles/node-for-everyday-things/word-count.js
@@ -0,0 +1,63 @@
+#!/usr/bin/env node
+/**
+ * Calculate the approximate the number of "pages" in a text document based on
+ * a word count of 350 words per page.
+ */
+
+var sys = require('sys'),
+ fs = require('fs');
+
+/* Include some instructions for when I forget how this works. */
+function USAGE(message) {
+ var error_code = 0;
+ sys.puts("\n USAGE: pagecount FILENAME\n\n" +
+ " Show the estimated page count of a file based on 350 words per page.\n" +
+ " FILENAME should be the name of a utf-8 encoded text file.\n" +
+ "\b\n" +
+ " Example:\n\t\tpagecount MyFile.txt\n\n Estimates the page count of MyFile.txt\n");
+
+ if (message !== undefined) {
+ sys.puts(message);
+ error_code = 1;
+ }
+ process.exit(error_code);
+};
+
+/* PageCount() analyze the file and displays the results */
+function PageCount(filename) {
+ fs.stat(filename, function (stat_error, stat) {
+ if (stat_error) {
+ USAGE("ERROR: " + filename + ", " + stat_error);
+ }
+
+ if (stat.isFile()) {
+ fs.readFile(filename, 'utf8', function (read_error, content) {
+ var subtotal_words = 0;
+ if (read_error) {
+ USAGE("ERROR: Can't read " + filename + ". " + read_error);
+ }
+ /* Replace all non-letter characters with a single space. */
+ subtotal_words = content.replace(/\W+|\s+/gm,' ').split(' ').length;
+ page_count = (subtotal_words/350);
+ if (Number(page_count).toFixed(0) <= 1) {
+ sys.puts(filename + "(" + subtotal_words + " words): " +
+ Number(page_count * 100).toFixed(0) + "% of one page.");
+ } else {
+ sys.puts(filename + "(" + subtotal_words + " words):" +
+ Number(page_count).toFixed(2) + " pages.");
+ }
+ });
+ } else {
+ USAGE("ERROR: " + filename + " is not a file.");
+ }
+ });
+};
+
+if (process.argv.length < 3) {
+ USAGE();
+}
+
+/* For each file I want to tally up call PageCount() */
+for (var i = 2; i < process.argv.length; i += 1) {
+ PageCount(process.argv[i]);
+}
1 articles/step-of-conductor/conductor
@@ -0,0 +1 @@
+Subproject commit c00ff7a6baa87909fcc35890a7c2fe98b524a7b8
View
BIN articles/step-of-conductor/example1.dot.png
Deleted file not rendered
1 articles/step-of-conductor/step
@@ -0,0 +1 @@
+Subproject commit d63ae6d5d655daae4bd69f4c4c567749b6b07978
View
2 articles/step-of-conductor/step3.js
@@ -1,4 +1,4 @@
-var Step = require('step')
+var Step = require('step');
Step(
function loadData() {
View
2 articles/tasks-and-prompts.markdown
@@ -1,6 +1,8 @@
Title: Tasks and Prompts -- Implementing Simple Work Queues
Author: R. S. Doiel
Date: Sun, 28 Feb 2010 00:50:22 GMT
+Node: v0.1.90
+
Sometimes all you really need is orderly execution not blocking I/O to get the job done. Tasks and prompts is a light weight implementation of the work queue design pattern.

0 comments on commit 2381ca5

Please sign in to comment.
Something went wrong with that request. Please try again.