/matt.js

Simple string formatter based on the sprintf implementation.

Primary LanguageJavaScript

Deprecation notice 🚨

This project is no longer being maintened by me. If you want to take ownership over it, just ping me.

Since I'm now using ES6 on my projects (thanks to Babel), I left sprintf-like approaches and fully embraced template strings. If you still want this, sprintf-js seem to cover all your needs (I personally have never used it tho).

matt.js Build Status

Simple string formatter based on the sprintf implementation.

Motivation

I've always been a huge fan of sprintf since the old ActionScript days (I even wrote an AS3 implementation of it). I really like its simplicity. Even the native console object has basic support for it. I just wanted a way to mix console's formatting support with named arguments.

Environment support

Works well with AMD, Node.js and the browser.

Install

For Node.js via NPM

$ npm install matt.js --save

For the browser via Bower

$ bower install matt.js --save

Usage

Basic substitution

var message = 'Hello %s! \
You\'re our visitor number %d! \
That means you just won %f pounds of Nutella!';

console.log(matt(message, 'John', 1000, 780.35));
// Hello John! You're our visitor number 1000! That means you just won 780.35 pounds of Nutella!

Named arguments

var message = 'Hello user! This is %(name)s %(surname)s. Check %(website)s for more info.',
    data = {
      name: 'John',
      surname: 'Doe',
      website: 'http://johndoe.com'
  };

console.log(matt(message, data));
// "Hello user! This is John Doe. Check http://johndoe.com for more info."

License

MIT © Rafael Rinaldi