Created
September 10, 2014 05:37
-
-
Save alanrsoares/a9b94d5827bcf5759d78 to your computer and use it in GitHub Desktop.
// source http://jsbin.com/hatoka/4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://static.jsbin.com/js/vendor/traceur.js"></script> | |
<meta charset="utf-8" | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
(function(){ | |
'use strict'; | |
var formatter = (function(){ | |
// api interface | |
return { | |
format: format | |
}; | |
// implementation | |
function onMatch(data){ | |
return function(match, key){ | |
return data[key] || match; | |
}; | |
} | |
function formatData(input, data){ | |
return input.replace(/{(\w+)}/g, onMatch(data)); | |
} | |
function formatArgs(input, data){ | |
return input.replace(/{(\d+)}/g, onMatch(data)); | |
} | |
function format() { | |
var args = arguments, self = this; | |
return (typeof args[0] === 'object') | |
? formatData(self, args[0]) | |
: formatArgs(self, args); | |
} | |
}()); | |
if (!String.prototype.format) { | |
String.prototype.format = formatter.format; | |
} | |
}()); | |
console.log("test {0} - {1}".format("{foo}", "{bar}") | |
.format({foo:"bar", bar:"baz"})); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">(function(){ | |
'use strict'; | |
var formatter = (function(){ | |
// api interface | |
return { | |
format: format | |
}; | |
// implementation | |
function onMatch(data){ | |
return function(match, key){ | |
return data[key] || match; | |
}; | |
} | |
function formatData(input, data){ | |
return input.replace(/{(\w+)}/g, onMatch(data)); | |
} | |
function formatArgs(input, data){ | |
return input.replace(/{(\d+)}/g, onMatch(data)); | |
} | |
function format() { | |
var args = arguments, self = this; | |
return (typeof args[0] === 'object') | |
? formatData(self, args[0]) | |
: formatArgs(self, args); | |
} | |
}()); | |
if (!String.prototype.format) { | |
String.prototype.format = formatter.format; | |
} | |
}()); | |
console.log("test {0} - {1}".format("{foo}", "{bar}") | |
.format({foo:"bar", bar:"baz"})); | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
'use strict'; | |
var formatter = (function(){ | |
// api interface | |
return { | |
format: format | |
}; | |
// implementation | |
function onMatch(data){ | |
return function(match, key){ | |
return data[key] || match; | |
}; | |
} | |
function formatData(input, data){ | |
return input.replace(/{(\w+)}/g, onMatch(data)); | |
} | |
function formatArgs(input, data){ | |
return input.replace(/{(\d+)}/g, onMatch(data)); | |
} | |
function format() { | |
var args = arguments, self = this; | |
return (typeof args[0] === 'object') | |
? formatData(self, args[0]) | |
: formatArgs(self, args); | |
} | |
}()); | |
if (!String.prototype.format) { | |
String.prototype.format = formatter.format; | |
} | |
}()); | |
console.log("test {0} - {1}".format("{foo}", "{bar}") | |
.format({foo:"bar", bar:"baz"})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment