An JavaScript string formatter that is mostly compatible with ES6 template strings.
In a browser:
<script src="format.js"></script>
Via bower:
bower install es6ish-string-format
This method will attach itself to the String prototype. That means that once you include it, you can use it like this:
var test = 'test';
console.log('this is a ${test}'.format({ test: test }));
// = 'this is a test'
It can also access object attributes, like so:
var user = { username: 'cdmckay' };
console.log('hello, ${user.username}'.format({ user: user }));
// = 'hello, cdmckay'
This format method was designed to have a migration path to ES6 template strings. So, if you wanted to convert
the above example to an ES6 template string, simple replace the quotes with backticks and delete the .format(...)
part:
var user = { username: 'cdmckay' };
console.log(`hello, ${user.username}`);
// = 'hello, cdmckay'
Cameron McKay |
This library is available under the MIT license.