Monday, August 29, 2011

Report the name of a JavaScript function

I do a LOT of JS development lately: straight up HTML5, PhoneGap for Android and iPhone and Node.JS on the server side. One command line I use in all environments is console.log.
console.log allows you to print out a string, a variable and even a complex object (just use JSON.stringify on it first) to the console view of:
  • Developer Tools in IE8 and higher (hit F12)
  • Developer Tools in Chrome (different keys for different OSs – nice, Google)
  • Firebug in Firefox (install as an extension)
Since advanced JS uses a lot of event calls, anonymous functions and multiple files, it’s hard at times to figure out where things go wrong. So I’ve gotten into the habit of adding an entrance printout at the beginning of each function. But it has gotten cumbersome and annoying after a while, so I searched for a more elegant solution. I found this StackOverflow question, and based on the replies, constructed this code:

var fname = /^function\s+([^(]+)/.exec(arguments.callee.toString())[1];

Now, this looks like a cumbersome code to paste into each and every function, so I wanted to write a general funnction I can call anywhere to print the current function name. The problem is, if you put this code into a function, say reportFunctionName() and call it, you get "getFunctionName" printed out. The answer was in the same SO post:

function reportName() {
  var fname = /^function\s+([^(]+)/.exec(reportName.caller.toString())[1];

Note the use of caller instead of callee. Also note both methods would not work well for anonymous functions (duh - no name -> nothing to print :)

Finally, I started using Github's Gist feature to collect all my code snippets, so you can find both code pieces mentioned here and here respectively.

Monday, August 15, 2011

Ultraviolet Pig

If you got to this post hoping to read about the “Angry Birds” sequel “Ultraviolent Pigs” – you’re in the wrong place - please go to the Rovio siteSmile.
This is just another crazy electronics project I set myself to.

It all started while I was zombie-surfing the DealExtreme site. If you’re not familiar with the concept, it means browsing after 1am, no good reason other then to eke a couple of more minutes of browsing before the brain shuts down completely. Instead of muttering “brrraaaaains!” you mutter “beeedd”. Some of my best (and worst) ideas hit me during zombie surfing. But I digress.

So, as I said, while zs-ing DealExtreme, I came a cross a too-stupid-not-to-buy item: a pig LED flashlight for $0.78. And free shipping to boot! What’s the catch here, I wondered? And what the hell will I do with this useless, clearly non-kosher flashlight?

The Ultraviolet Pig
Here piggy piggy!

So I browsed further, and for $2.93 I got a bag full of ultraviolet LEDs.

20 UV LEDs
Not the best UV LEDs out there - but the cheapest
After 20-25 days (hey, you 'pay' for free shipping, you get it delivered by a lame horse that has to swim across the Pacific blindfolded), I finally got an envelope containing the items.

It took less than 5 minutes to take the pig apart (please don’t ever associate this sentence with me Smile) and replace the 2 simple LEDs with the UV ones. And that’s when the fun starts.

Other than enabling me to see hidden marks on a $20 bills and a driver’s license, the most useful thing you can do with UV light is protect your stuff.
Do you have a favorite chair that you think gets replaced with a crappy one when you leave your desk? Did someone swipe your beloved Swingline stapler and you can’t prove it? Just mark your stuff with UV ink – completely invisible to the naked eye – but not to good ol’ Mr. UVP. Observe:

Now you don't

This is a simple metal mint box, easily swiped and easy to deny. But when you turn UVP on it, the truth comes to light, in 3 colors (you can also see the UV pens in the photo):

Now you see it

So, yes, not really practical unless you can retrieve the stolen goods, but quite good at identifying your stuff, or secretly communicating under the nose of pig-less people (think about passing notes in a boring meeting).

The stats:
  • Usefulness: 3/5 stars – not that amazing, and not too techy. Not a single line of code needed
  • Fun factor: 5/5. Oh yeah!
  • How easy it is to build: very. Just get your pig, LEDs (you need at least 2) and follow your instincts. Or watch this youtube video where someone demonstrates how to take it apart
  • Total project cost: $3.71 – and I still have about 18 LEDs waiting for the next 9 pigs

Your eyes cannot see it well, hence your pupils will not shrink properly to protect them. Few minutes are OK, more is not recommended.