A strftime for Prototype

February 8th, 2008 . 0 comments

Object.extend(Date.prototype, {
  strftime: function(format) {
    var day = this.getUTCDay(), month = this.getUTCMonth();
    var hours = this.getUTCHours(), minutes = this.getUTCMinutes();
    function pad(num) { return num.toPaddedString(2); };

    return format.gsub(/\%([aAbBcdDHiImMpSwyY])/, function(part) {
      switch(part[1]) {
        case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break;
        case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break;
        case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break;
        case 'B': return $w("January February March April May June July August September October November December")[month]; break;
        case 'c': return this.toString(); break;
        case 'd': return this.getUTCDate(); break;
        case 'D': return pad(this.getUTCDate()); break;
        case 'H': return pad(hours); break;
        case 'i': return (hours === 12 || hours === 0) ? 12 : (hours + 12) % 12; break;
        case 'I': return pad((hours === 12 || hours === 0) ? 12 : (hours + 12) % 12); break;
        case 'm': return pad(month + 1); break;
        case 'M': return pad(minutes); break;
        case 'p': return hours > 11 ? 'PM' : 'AM'; break;
        case 'S': return pad(this.getUTCSeconds()); break;
        case 'w': return day; break;
        case 'y': return pad(this.getUTCFullYear() % 100); break;
        case 'Y': return this.getUTCFullYear().toString(); break;
      }
    }.bind(this));
  }
});

UPDATE: Bugs fixed. Thanks Andrew and Stephen

Also, some of my old and new code has been posted on GitHub. You might find something useful.

Continue reading article…

Widgets the YUI Way

December 27th, 2007 . 12 comments

Why are there so many lightbox implementations? Why are there numerous “versions” of widgets? Do you know which fork is the latest and greatest? Probably not, and for good reason. The current system sucks.

Continue reading article…

Your Momma's So Fat...Prototype vs. JQuery Edition

November 9th, 2007 . 28 comments

The time has come once again to clear the air of fallacious statements by the myriad of people comparing Prototype to JQuery. I’m all for comparison; I believe it’s healthy to have choices in life. The problem usually isn’t Prototype or JQuery, it’s the article comparing them.

Continue reading article…

Upgrading Radiant to Prototype 1.6

October 24th, 2007 . 0 comments

Mislav has written up an excellent overview on upgrading Radiant (which used 1.5) to Prototype 1.6. The article does a great job of showing how 1.6 is superior to 1.5.

Continue reading article…

Prototype Inheritance Updates

October 9th, 2007 . 0 comments

Defining classes and inheritance in Prototype–Incase you’ve missed it, Prototype 1.6 has a new inheritance model.

Continue reading article…

Understanding Scope and Binding in JavaScript

July 18th, 2007 . 11 comments

At the heart of binding, it’s merely a means to control execution scope—Function x executions in the scope of object y. It can be tough to grasp at first, but with the right amount of ninja references, anything can be explained so someone can understand it.

Continue reading article…

Prototype Bungee Book Released (And I'm late announcing it)

May 16th, 2007 . 0 comments

Prototype:The Bungee Book Lands —If you don’t know Prototype, get this book. If you know Prototype, get this book. It’s that good.

Continue reading article…

Shortcuts for the new Prototype DOM Builder

May 14th, 2007 . 0 comments
// Lowpro style (http://www.danwebb.net/2007/4/16/low-pro-0-4-released)
$w('a div p form').each(function(e) {
  window['$' + e] = function() {
    return new Element(e, arguments[0]);
  }
});
// $form({action: '/foo', method: 'post'});

Continue reading article…

New DOM Builder in Prototype

May 14th, 2007 . 0 comments

Let the DOM Cometh —New DOM builder in Prototype’s trunk.

Continue reading article…

PeepCode: Prototype Video Training

May 1st, 2007 . 0 comments

Prototype 1.5 Released and Documentation Site Live

January 18th, 2007 . 24 comments

We’ve worked really hard getting the official Prototype site up and running and it’s finally here, not to mention we’re also releasing 1.5!

Continue reading article…

Inject and Pluck: The Secret Sauce Behind Prototype's Enumerable

December 28th, 2006 . 15 comments

No misspelling in that title. Two of the most underused, albeit powerful, methods of Prototype are pluck and inject. Pluck provides an easy way to get at data, and inject is like the duct tape of Prototype—applicable in a variety of cases. Let’s look at how these methods can help tidy up our code and make our life a little easier.

Continue reading article…

Avoiding Bloat in Widgets

December 4th, 2006 . 18 comments

Widgets walk a fine line between abstractions and implementations. Implementation, in this case, is a practical solution chosen to perform a given function. The problems with widgets occur when the widget author walks too far in one direction, or worse, walks an outward spiral covering both directions. Both choices lead to script bloat and complexity thats hard to manage. You no longer have a simple solution for a defined problem, you have a complex solution for a variety of problems. The good news is there are ways to avoid both the bloat and complexity.

Continue reading article…

Prototype: A Call For Documentation

October 31st, 2006 . 10 comments

We’re hard at working getting the Prototype documentation site ready for launch. However, we know there is already a lot of great documentation scattered throughout the web. Instead of us rewriting a lot of this documentation, we’d like to ask that the community lend us a helping hand.

Continue reading article…

More Ruby in Prototype

September 26th, 2006 . 14 comments

It’s obvious that Ruby has been a big influence to Prototype. We’ve already talked about Enumerables, Arrays, and Hashes, and how powerful they are. Now, lets look at some new tools: inGroupsOf and eachSlice.

Continue reading article…