Skip to content

An ES5 JavaScript string formatter compatible with ES6-style template strings

Notifications You must be signed in to change notification settings

process-street/es6ish-string-format

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ES6-ish String formatter

An JavaScript string formatter that is mostly compatible with ES6 template strings.

Installation

In a browser:

<script src="format.js"></script>

Via bower:

bower install es6ish-string-format

Notes

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'

Author

twitter/cdmckay
Cameron McKay

License

This library is available under the MIT license.

About

An ES5 JavaScript string formatter compatible with ES6-style template strings

Resources

Stars

Watchers

Forks

Packages

No packages published