Thursday, June 16, 2016

Browserify Winston

1. The problem.

Winston 1.0 and still 2.0 uses fs.readdirSync() which is not supported by brfs and the other goto fs transform modules.

2. The solution.

Browserify lets us write our own transforms. Thus if we take the MVP path, we can write two files one, that substitutes require("mockLogger") in for require("winston") out of every src file, and the second file to define the "mockLogger".

Assume we have file called browserTest.js as follows:

// browserTest.js
var winston = require('winston')
console.log('I am browserified!')
winston.info('I am info!')
winston.debug('I am info!')
winston.silly('I am info!')
winston.error('I am info!')

If we have a mockLogger.js file as follows:

// mockLogger.js
module.exports = {
  error: function(e) {
    console.log(e)
  },
  warn: function(d) {console.log(d)},
  verbose: function(d) {console.log(d)},
  info: function(d) {console.log(d)},
  debug: function(d) {console.log(d)},
  silly: function(d) {console.log(d)},
};

Then we can define a transform file called wtc.js like this:

// wtc.js
var through = require('through2')

module.exports = function(file) {
  return through(function(buf, enc, next) {
    this.push(buf.toString('utf8').replace(/require\(.winston.\).*(;)?/g,'require("./mockLogger");'))
    next()
  })
}


And the final browserify cli command:

browserify browserTest.js -t ./wtc.js > bundle.js

If you like you can run the above command without specifying the "-t ./wtc.js" part and if you simply try to run "node bundle.js" you will see a "TypeError: fs.readdirSync is not a function" Error.

Of course you don't really want to rely on the cli for production software. In that case you can forgo the transformer and just map the module substitution in your package.json like so:

//package.json
{
  "browser": {
    "winston": "./mockLogger"
  },
  "scripts" {
    "browersify": "browserify -e browserTest.js -o bundle.js"
  }
}

Your command then becomes:

npm run browserify

Sunday, June 1, 2014

Asychronous NodeJs

I have been doing a lot of work with Cordova lately and struggled a little to get my head around some of the event driven concepts of NodeJs, Promises, and Callbacks. This became apparent when trying to test my NodeJs with Jasmine. I had written a method much like this one:
exports.doAsync = function(my_str) {
  fs.exists(fileName, function() {
    // fileName, callback declared elsewhere
    fs.writeFile(fileName, my_str, callback);
  });
}
Even though I knew somewhere deep down in my heart that Callbacks meant asychronous I still wrote my test like so:
it("should be an async method", function () {
  //setup
  doAsync(param1);
  expect(fs.writeFile).toBeCalled();
});
This test failed everytime because I had no mechanism in place to tell test code when that first callback had been executed. You'll notice my first mistake was writing the function prototype first, which did not include a callback parameter. This anti-BDD stunt influenced my presuppositions when writing the test. To make it work correctly, I needed the following changes:
// In my file.js
exports.doAsync = function(my_str, win) {
  fs.exists(fileName, function() {
    // fileName, callback declared elsewhere
    fs.writeFile(fileName, my_str, callback);
  });
  function callback(err) {
    win(err);
  }
}
// in file.spec.js
it("should be an async method", function (done) {
  //setup
  doAsync(param1, function() {
    expect(fs.writeFile).toBeCalled();
    done();
  });
});
This is a bit contrived because to make this work you'd need a bit more setup. (You can view the source from which this is derived in the file around here). However, in this post I simply want to share a mental tool that has helped me grasp these ideas and detect my conceptual errors quicker.
I worked in a restaurant for a few years and so I imagine our single-threaded Javascript engine as a kitchen with only one cook. The cook, Long John Silver, actually has to do many things quickly to handle all the orders his waiters are taking on the floor. If we just opened and I only have one table whose ordered our famous Eggs and Salsa, I can put in the order and go have a chat with Long John while the eggs cook. When the dish is done, I could end the chat and deliver them to the customer and everyone tips 20%, even though this has been synchronous and inefficient. Obviously, when we're at the peak dinner hours everyone works asynchronously. John will do many other things while waiting for the eggs to cook.
In my test case above, I was essentially saying, "Long John, can you make me Eggs and Salsa?" He gruffly responds, "Sure" and because he's forgotten the Salsa on previous eggs, I immediately begin to ask, "Did they come with Salsa?" (past tense). He's just gotten the eggs in the pan so it hardly makes sense to ask a past-tense question about a future state of the eggs. So then, I either have to check back every five minutes asking the past tense question expect(fs.writeFile).toBeCalled() or I can simply have him text me when the meal is ready. Using the Callback or the Promise is like saying, "Just text me when your done."
This analogy works in a few other respects. If you want to test the process of how he makes eggs, you can give him fake veggies so he doesn't have to go search for them in the fridge (module dependencies or data). Either way, you can only check his work when he's finished with it.
Just remember, one cook, one thread, so you'll need to be making sure you have ways for your cook to notify you when he's done. Otherwise everyone waits in line.

Wednesday, May 15, 2013

Groovy Tutorial Pt. 2

The most underrated question ever: Why?

I hope you've asked this question. "Why learn Groovy or any other programming language?" There are many reasons. Take it from Will I Am. In short, as Cars became ubiquitous throughout the world, it became a necessary life skill to learn how to drive. As computers rise and the availability and use of data, programming is, and will be, the new driver's license. At least, Wolfram thinks so.

Identify The Problem

All around us we repeat work by hand when we could be automating it. For instance, many times we have two sets of data that need to be linked. Consider a file with the following contents:
item name, relationship, other item name
Tree, Parent Of,Branch
Branch,,Leaf,Child Of,Branch
You might recognize this as a CSV file. These files are often a text result of excel spreadsheets or output from some other program. With this file we want to match up the relationships of each task. Let's start with the conceptual organization of this data. Obviously we want to treat each line as it's own set of information with three distinct parts. In Java we would use Classes to represent this data. In Groovy we can write a class like so:
// title, relationship, other
public class Thing {
    public String title
    public String rel_type
    public String o_title
}
This gives us a way to represent many Thing's. The public operator is saying "let anyone use this data I'm about to define". Then it defines the data type and data name. When you are at the mall, you might be a public Human jake. We first define a class (the formal container to hold the data) and then three String's. Each String can hold a string of letters or digits (characters). When then give each String a different name since we have three distinct pieces of textual data from our CSV file. Later I'll show you how to read the data from an actual text file, but for now let's just represent the data in a way that lets us manipulate it easily. The most basic way to create this data for use is as follows:
// Create a new instance of the Thing
Thing a_thing = new Thing()
// Assign values to the class members.
a_thing.title = 'Tree'
a_thing.rel_type = 'Parent Of'
a_thing.o_title = 'Branch'
In line two we are creating a new container, a new well-defined backpack. In lines 4-6 we use the dot operator "." to peak inside the Thing called a_thing and give each attribute its own value. To test if everything is working as expected print the object.
println a_thing
Ut oh.. what happened? What is that thing (pun intended)? That is called the "address of" our created Thing, which you really don't need to know anything about. Every time you run the program that will look different. 'Nough said. So how do we easily view the data we just created? Groovy actually provides a method called dump() that will look inside your object and try to give you a textual representation of it's contents.
println a_thing.dump()
// title=Tree rel_type=Parent Of o_title=Branch
Now that looks pretty sweet. You should get an output like what's in the comment in line 2 above. But wait- where does dump() come from and what else is out there? Well, to understand where dump() comes from we have to give a short intro to Object Oriented Programming. Remember in Grammar class, when you learned that a Noun was a person, place, or thing? This means that everything tangible has a sort of root attribute: it is a noun. All nouns have some kind of name, some kind of weight, and other things that describe them all. In contrast, not all nouns have eyeballs. Humans, which are nouns, have eyeballs. So this way of qualifying everything under a single root and then recursively grouping, like the Latin names of living creatures, is core to OOP. In Groovy, that root "noun" is called an Object. All objects have a set of core functions like dump(), each(), and toString(). So that's where babies- I mean dump()'s come from: logical groupings of data.
Checkpoint: Add a int  member to the Thing class and dump the contents.

Abstract & Simplify

Now, in Groovy we have a few ways to simplify this classic Java approach. Instead of listing the parameters one by one, we can just call out each one in the constructor of the object like so:
Thing my_thing = new Thing(title: 'Tree', o_title: 'Branch')
println my_thing.dump()
You'll notice this time that we chose not to set a relationship. In the output you see a null value as you should expect. So we've just minimized the number of lines of code and it's just as readable.
Now, we've talked about Lists, but if we stare at our Thing class we see that it resembles a phone book (where names map to numbers). Each attribute name maps to a String value. So we can get a further abstract view of the data and represent the object creation with a Map:
def my_thing = [ title:'Tree', o_title: 'Branch' ] as Thing
println my_thing.dump()
Notice the syntax of the map. Where a List was [], a Map is [:]. One the left side of the colon is your key on the right side is your value. Consdier a phonebook: left side is a name of a person and the right is his phone number (or vise versa). The structure of the mapping is up to you and your needs. We don't need the new Java keyword because Groovy by default creates a new List or Map when you use this syntax. You also see that we can replace Thing with the keyword def and everything works peachy. It works because we've thrown in the clause 'as Thing'. as is a keyword in Groovy that lets you duck-type or cast an object from one type to another.
Checkpoint: Remove the as Thing clause and see how the output changes. Hint: remove the .dump() call to get cleaner output. Next, add a rel_type of "Parent Of" to the map. Finally, add the as Thing statement back and compare outputs.
Let's talk about this Map. To access the members of a Class we use the dot operator in Groovy right? Well, to access the keys of a Map we can use it in the very same way as seen below in line 2. In fact, we can pull this information out of our map object in a bunch of different ways.
def my_thing = [ title:'Tree', o_title: 'Branch' ]
println "The title: " + my_thing.title
println "The title: " + my_thing.'title'
println "The title: " + my_thing['title']
def title = 'title'
println "The title: " + my_thing[title]
But wait a minute, if we can describe our data in the form of map, and we don't loose any data or any ability to manipulate the data, why do we have this whole public class Thing blah blah blah!?
Excellent question, and the answer is... we don't! I showed you the concept of Classes because Java was built on an Object Oriented paradigm and the sooner you begin to think of your data as just objects with relationships to each other, the better programmer you'll be. Finally note that in many cases of complex systems it's good to have well defined types, but for simple scripting like what we're doing Lists and Maps can get almost any job done.
Checkpoint: Represent all the data in our text file as three maps.

Answer:
def tree = [id: 1, title:'Tree', o_title: 'Branch', rel_type: 'Parent Of']
def branch = [id: 2, title:'Branch', o_title: null, rel_type: null]
def leaf = [id: 3, title:'Leaf', o_title: 'Branch', rel_type: 'Child Of']

Monday, April 22, 2013

Groovy Tutorial Pt 1.9

Installing Groovy

Java Prereq

To do real applications you'll need to install Groovy and Java on your machine. Go here to get Java 1.7 SE (Standard Edition) SDK (Software Developers Kit) and download an appropriate version. I am running Windows 7 so I'll use the highlighted one. If you don't know which one to get, ask Google. Just so you know, the JDK refers to Java Developers Kit, and GDK is Groovy Developers Kit. It's not rocket science.
Follow the installer to do all standard settings. It will install a few different tools, most of which you won't use, hence, we won't talk about them.

Get Your Groove On

Next, go here and download the latest binary of Groovy. For this, I'll be using 2.1.3.

You can also use the installer, but I'll be dealing with the binary for generalized instructions. Once you've downloaded the binary you're going to unzip the contents into a file location. Mine is going to go into "C:\Program Files\Java". Right Click on the zip in your downloads folder and extract to a directory.
If you have any trouble, just use the "extract to here" menu option and copy the files to your directory of choice. Here's my groovy install:


Next, you'll need to set your environment variables to find the location of the Groovy install. Edit the system environment variables and add GROOVY_HOME and JAVA_HOME to point to your SDK's. Follow the snapshots below.
If you pull up the start menu you can search for environment settings, otherwise go to the Control Panel and look for menus that will let you to editing these properties.

You'll have to add these one by one, and remember the "Variable value" is the path to your specific installation. If you extracted the zip file into the downloads folder, you'll have to point there. Lastly, you should have a "Path" system variable already defined. Edit that variable and add on "%groovy_home%%\bin;%java_home%\bin". Every variable is denoted by % signs and are separated with a semicolon. The \bin folder is where all your executable files (called binaries) are, that why you have to add it to the Path variable.

Now, open up your command line (cmd.exe) and check your installation details. Your commands should show the same kind of output that mine does. If it does not show the same output, something went wrong and you should ask Google or give me a comment below and I'll try to help.


For the grand finale, enter "groovyConsole" into the command line.


This little sucker is going to be your best friend. From here on out we'll be using this to write pilot code and develop an app. Later on we'll probably move to full scale IDE so that you can send your app to all your friends.

Checkpoint: Go ahead and run a few commands from the previous tutorial to make the Groovy Console an acquaintance.

Monday, April 15, 2013

Groovy Tutorial Pt 1

Nothing sinks in a concept like applying it to the real word. In this blog, I'll attempt to teach even a non-CS layman Groovy by building an application from the ground up.

Setup:
See a web based version of the Groovy Console to use as your sandbox. When using the web based version don't forget to switch tabs to "Output".

Lesson 1: Building Blocks

Let's do some fun stuff. Open up your Groovy Console (or web console) and enter in the lines:
println 'I <3 coding.'
System.out.println("I <3 coding.");
You will get some output that displays two messages to the user. Next do this:
def num = 2
def three = 3
println "Lucky number " + (num + three)
If your coming from a Java background, you'll recognize the second 'I <3 coding' statement. This is straight Java syntax. Groovy is almost 100% backwards compatible with all Java syntax. This is handy information if you can't figure out how to do something in Groovy, just do it in Java! The first statement 'prints a line of text to standard out'. println is actually a function, and your giving it a single string: "I <3 coding" as a parameter.
The second code block is more fun. As you figured out, your declaring a variable (remember algebra?) called num and giving it a value of 2. def is like saying "define this thing for me so I use it in the future". Groovy is loosely typed. You don't have to say "define an integer" or "define a string". Groovy will figure out the details for you. Line 3 will give "Lucky number 5".
Checkpoint: Write two or three println's and figure out how to break the syntax.
Next, try this:

def num = 2
def nums = [num, 3, 4]
nums.add(5)
nums.each { a -> println "Hello $a" }

Line 2 enters the realm of data structures. These are just specially designed backpacks that hold your data. Here, we define a List with the syntax of [].  This particular backpack holds one variable and two integers. In the last line of code we use a method called each to iterate through each element in the list and print it to the console. You can call each on every variable you ever define (or declare). Not all variables (or objects) will act the same though. The two braces {} define a closure, which is a method (a function) that is created and called on the fly. The it-> is saying "expect a single parameter and call it a, then run everything between the -> and }". The statements after the pointing arrow can span as many lines as they need to. But how does it know to do this to each value in the list?? The each method needs you to define a closure as a single parameter because it plans to grab each element in order and, in turn, pass each element to that closure you've defined. In Java you would write something like:
for (int i = 0; i < nums.size(); i++) {
  System.out.println("Hello " + nums.get(i));
}
Finally we have a new string defined as "Hello $a". This is common scripting syntax where you can tell Groovy to replace $a in the string with the value of a. In Groovy, there is always multiple ways to accomplish the same thing. For instance, you can add elements to a list by doing a "right shift": nums << 5.
Checkpoint: Swap out the double quotes with single quotes to see a key syntax difference. 
Add elements to your list.
Try the same each closure on a string (ie "a string".each { ... })

If you have specific examples in mind of something you want to do, the Cookbook Examples page is a great resource. Next time we'll make sure you install Groovy on your machine and we'll write a small console application that you can run on any machine with Java installed.
Groovy on, young and faithful warrior.

Mission

To encourage and teach the layman to code like a boss.