Bower - Specify Components Directory

You would think that Bower would allow you to specify where you want the components to be installed from within your bower.json. Well, just like me you would have thought wrong...

As I found out you need to create a .bowerrc file in your projects root. You can find the specs for this file on the Bower Config page.

In my example I only needed to specify where I would like Bower to install the dependencies.

  "directory" : "public/components"

Now when I run the 'bower install' command I will have my dependencies right right where I want them.

Ruby Array of Symbols Shorthand

I am a big fan of Ruby shorthand and I use them every time I get the opportunity. I just ran across one that I have never used before. This syntax allows you to create an array of symbols.

%i(array of symbols shall follow)
 => [:array, :of, :symbols, :shall, :follow] 

I have to admit it is nothing mind blowing, but none the less useful. It will allow you to do things like define a list of symbols and itterate over them pretty seemlessly.

%i(array of symbols shall follow).each{|s| puts s}
 => [:array, :of, :symbols, :shall, :follow] 

window.location hash

I have been developing for quite a while now (6 years at the time of writing), and it never ceases to amaze me the amount of things left to learn. I thought that I had learned all that I needed to from the DOM World and today I was thrown right back in with something I had never had the need to use before.

I was working on a page that has a javascript tab system. The downside of tab systems like that is they do not maintain state. When you trigger a full page reload you need some way to determine what tab you should show. I figured an easy way would be to detect an anchor reference in the URL. This actually proved to be a fairly simple task.

$(function() {
	var anchor = window.location.hash;

	if (anchor) {
		// Code for tab activation here...

To me, this is pretty cool. I thought I would need to come up with some regular expression to match the URL anchor reference. Just goes to show that no matter how long you work with something you always have room for improvement.

Also, I would like to point out how invaluable our developer tools are. I stumbled upon this method while playing around in the Chrome Javascript Console. It has the helpful feature of showing all methods and properties off of an object is has built, very very handy.

Testing for empty strings

Alomst every applicaiton you will make with a Javascript front end will do some work that needs user validation. The primary benefit of doing validation on the client side is that you do not have to make the user wait in order to find out there was an issue.

This is a common practice when submitting forms via AJAX. You want to make sure all the ducks are in a row before you let them send out the dogs of war. Also, please take note that I am only talking about the client side behavior in this post. It is assumed (and absolutely neccessary) to verify all the input on your chosen back end before any work is done with the tainted data.

I always use a regular expression to test for an empty string. I find this better than testing against a string of zero length because you can compensate for a string that has whitespace (that would throw off the zero length test).

var empty = /^$|^\s+$/;
var yourString = 'your source here...';

if (empty.test(yourString)) {
  // the passed string was empty
} else {
  // the passed string was not empty

If you know your regular expressions this should need no special explination. On the other had if you are not too familiar I have added comments to explain:

// creates a new regular expression using the thie literal syntax.
// could also have been created with
// var empty = new RegExp(/^$|^\s+$/);
var empty = /^$|^\s+$/;

// ^ matches the beginnig of a string
// $ matches the end of a string.
// so ^$ means a string that starts and immediattly ends

// | is the regular express or, like this or that.
// \s matchies a single empty character
// + means match at least 1 but can match unlimited after that.
// so ^\s+$ means a string that starts and then has at least one empty
// character followed by either many more empty characters or the end of the string.

// The final regular express is ^$|^\s+$
// Which means capture an empty string or a string with one or multiple white spaces

var yourString = 'your source here...';

// them regular expression test method will return a boolean true for a match
// and a boolean false for a negative match
if (empty.test(yourString)) {
  // the passed string was empty
} else {
  // the passed stirng was not empty

PS. I wrote a contrived function to test this feeding in regular expressions.

var empty = /^(\s+)?$/;
var empty2 = ^$|^\s+$;
var empty3 = /^\s*$/;

function testForEmpty(regexp) {
  console.log("'': " + regexp.test(''));
  console.log("' ': " + regexp.test(' '));
  console.log("'     ': " + regexp.test('     '));
  console.log("'    a': " + regexp.test('    a'));

//'': true
//' ': true
//'     ': true
//'    a': false

What is a bare Git repository?

I have started a quest to know more in depth detail about the tool that I have been using every day for the last couple of years. I have wanted to understand more about Git so I am reading the documentation a little bit at a time.

Looking at the init method I noticed there was a --bare switch you could pass to it.

  Create a bare repository. If GIT_DIR environment is not set, it is set to the current working directory.

What does that mean? No information is provided to determine what the difference is between a normal and a bare repository. The documentation had failed me.

I had to look outside of the documentation to find an answer to this. I could have used this as an excuse to take the time and look at the source code for Git and figure out what the differences are but I just did not have the time. I found a blog post written by Jon Saints who at some point had the same question as me. His post summarized that the bare repo did not contain the working copy of your code. The bare repo is merely a shell for the delta and changes made by you and other developers and it is meant ot be housed on the central server.

I am still unclear on how that use cas would find its way into my day to day workflow. I don't think that I will ever diretly house my own git repositories for updating. I would much rather use a service like Github or Bitbucket for that.

I guess I can chaulk this up to the "Good to know" but not really relavant category. If you happent to have any real world use cases I would love to hear about them. Feel free to contact me.