#479 ✓not_for_core
kazuyoshi tlacaelel

sprintf feature on string!

Reported by kazuyoshi tlacaelel | December 5th, 2008 @ 02:00 PM

PROBLEM:

Sometimes you have a string that has to be formatted in a specific way with different values in between.

SOLUTION:

I Added a new feature to the String data type. called sprintf (very simple) that allows you to predefine a template which can be merged with any other kind of data!

EXAMPLE:

var template = 'Hello %s!' template.sprintf('There') // => 'Hello There!' template.sprintf('John') // => 'Hello John!'

var template = 'User %s, has %s messages!' template.sprintf('John', 5) // => User John, has 5 messages!

template.sprintf('Cindy', 9) // => User Cindy, has 9 messages!

Comments and changes to this ticket

  • kazuyoshi tlacaelel

    kazuyoshi tlacaelel December 5th, 2008 @ 02:08 PM

    Whoops!

    javascript should look like this

    
    
    var template = 'Hello %s!'
    
    template.sprintf('There') // => 'Hello There!'
    template.sprintf('John') // => 'Hello John!'
    
    
    var template = 'User %s, has %s messages!'
    
    template.sprintf('John', 5)
    // => User John, has 5 messages!
    
    template.sprintf('Cindy', 9)
    // => User Cindy, has 9 messages!
    
    
  • kazuyoshi tlacaelel

    kazuyoshi tlacaelel December 5th, 2008 @ 02:09 PM

    • Assigned user set to “Sam Stephenson”

    Assigned ticket to "Sam Stephenson".

  • Juriy Zaytsev

    Juriy Zaytsev December 5th, 2008 @ 03:21 PM

    • State changed from “new” to “enhancement”

    It behaves a bit weird in some cases:

    
    
    'Test %s'.sprintf('foo', 'bar');
    // I would expect "Test foo" but it returns 'Test %s'
    
    
    'Test %s %s'.sprintf('foo');
    // I would expect "Test foo %s" or "Test foo" but it returns 'Test %s %s'
    
  • Juriy Zaytsev

    Juriy Zaytsev December 5th, 2008 @ 04:40 PM

    Maybe something like this:

    
    String.prototype.sprintf = function() {
      var i = 0, args = arguments;
      return this.replace(/%s/g, function(m) {
        var value = (i in args) ? args[i] : m;
        i++;
        return value;
      });
    };
    
  • Nick Stakenburg

    Nick Stakenburg December 5th, 2008 @ 06:53 PM

    Or maybe stick with Template, it's syntax is nicer.

    
    new Template('User #{name}, has #{messages} messages!').evaluate({
      name: 'John',
      messages: 5
    });
    // "User John has 5 messages!"
    
  • Tobie Langel

    Tobie Langel December 5th, 2008 @ 08:58 PM

    • State changed from “enhancement” to “not_for_core”

    Ya, that really doubles up with the Template class. It's a nice add-on but definitely not something for core.

  • Juriy Zaytsev

    Juriy Zaytsev December 5th, 2008 @ 10:54 PM

    Hmm, sometimes I actually find templates a bit too verbose (comparing to sprintf-like syntax). Besides, sprintf avoids object creation.

    
    '5 #{fruit1}, 10 #{fruit2}, 13 #{fruit3}'.interpolate({
      fruit1: 'bananas',
      fruit2: 'apples',
      fruit3: 'oranges'
    });
    
    // vs
    
    '5 %s, 10 %s, 13 %s'.sprintf('bananas', 'apples', 'oranges');
    
  • kazuyoshi tlacaelel

    kazuyoshi tlacaelel December 8th, 2008 @ 02:39 PM

    There is nothing wrong with the code below.

    
    
    'Test %s'.sprintf('foo', 'bar');
    // I would expect "Test foo" but it returns 'Test %s'
    
    

    Basically if the given parameters don't match the "bindings" in the template string, no changes will be made at all and the template string is returned instead.

    Please look at the sprintf tests under String ; )

    Hmm.. Yup, I like the Template but must agree that its a bit verbose.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Attachments

Pages