“string”.supplant() a jewel from Douglas Crockford

Posted in development by Kris Gray on December 28th, 2007

I hope you all know about Douglas Crockfords website, but if you don’t, he has this wonderful little post called Remedial JavaScript. Which as you might imagine contains some basic JavaScript features, that were more or less, left out of the language. One of my favorites, is the supplant() prototype method. If your from a .NET or Java background you probably would recognize it better as String.Format().

Lets check out the prototype.

  1. String.prototype.supplant = function (o) {
  2. return this.replace(/{([^{}]*)}/g,
  3. function (a, b) {
  4. var r = o[b];
  5. return typeof r === 'string' || typeof r === 'number' ? r : a;
  6. }
  7. );
  8. };

This prototype takes in an object with properties matching the bracketed pairs in your string.

  1. "The date today is {todayDate}, thank you {name}".supplant(
  2.  
  3. { "todayDate" : new Date().toString(), "name" : "Clark Kent" }
  4.  
  5. );

Its just so beautiful and small, one can’t help but wonder how such it works.We have our traditional regex to match the bracketed keys in our string, which is easy enough, though the real magic here is discovering you can pass a function to the replace method, which allows you to do some custom magic on the replaced value.

He also does some additional type checking to be sure we are not assigning methods or anything else weird to our string values ([object Object] would look pretty stupid), but thats about it.  Something so easily implemented and useful, why isn’t it just part of the Language already?

2 comments

WordPress code plugin

Posted in development by Kris Gray on December 11th, 2007

So if your new to blogging, you wouldn’t really know how to make your code look great on your blog. Nor would you know how to go searching for the ideal plugin.

Well, search no longer, you desire iG:Syntax Hiliter.

It allows you to use tags like [language] … [/language] to produce a blog of formatted code. Here are some examples.

  1. function totallySweet() {
  2.  
  3. return "Kris Gray loves Six Apart";
  4.  
  5. }
  1. <h1>Daptiv rules!</h1>
  1. SELECT CompanyName FROM companiesTbl WHERE (Company == "Awesome")
  2.  
  3. -- Returned
  4.  
  5. Daptiv
  6.  
  7. Six Apart
3 comments

Google Charts new API

Posted in development by Kris Gray on December 8th, 2007

Google keeps releasing insane stuff, its really hard to believe the services they provide free of charge. This time its a Charting API accessible using just script or img tags.

http://code.google.com/apis/chart/

Take this chart here, which is a result of a chart query from this blog post.

Wakka Wakka Wakka

Using the collection of Google API’s its getting close to be able to mashup an actual enterprise level product that would definitely find a niche to fill.

I’d also like to mention that this was a project that a Google developer did in his 20% of free time, which allows me to say that not only does giving your developers 20% free time to work on whatever they please benefits your company, it benefits the entire world.

No comments

We are not amused about IE 7

Posted in development by Kris Gray on December 7th, 2007

About a week ago, Tony Chor posted on the IEBlog a post about how they were very happy with IE7 and gave a very rah rah speech about its quality attributes. You see the articles in print a lot, as reporters are generally happy to be complimentary to their guests, and maintain a good relationship with Microsoft. Though in the blog world you get things like.

# re: The First Year of IE7
Friday, November 30, 2007 3:32 PM by Joseph E. Davis
From the horrifically god-awful (IE6) to the merely depressingly buggy, nonstandard, and incomplete (IE7)… congratulations!

Direct quote from the end of the “A vulnerability report”
“ABOUT THE AUTHOR Jeff Jones is a Security Strategy Director in Microsoft’s Trustworthy Computing group.”
Once again, thanks for the un-biased reports!…

# re: The First Year of IE7
Friday, November 30, 2007 5:01 PM by “Internet Explorer has stopped working”
Sorry, I can’t get past the all-too-frequent IE 7 crashing or hanging at seemingly random times to appreciate anything you just posted.

Now every blog gets trolls, especially at Microsoft, but sometimes they are warrented, and the venom the developer community has for Microsoft and its insane browser process is very deserved.

Since FireFox can update issues and push out updates at any time, they can continually improve without any major release. Yet since Microsoft isn’t really willing to push mandatory updates of IE to its customers, it has to get everything right the first time. Which makes their process even more insane. The whole developer community can feel a big IE 8 dump coming in the near future stating “Its done!”, “Best IE Ever!” and “Developers will love it! Get it now!”. At which point you will have made thousands of developers who drive the web utterly frustrated and most likely give them a mental breakdown after the excitement of a new release turns to disappointment and disdain.

We just want to make sure you get it right. If your right then you’ve got nothing to hide, then you can ignore the trolls.

No comments