-
-
Save mckamey/981871 to your computer and use it in GitHub Desktop.
Indexed string formatting
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
/** | |
* Populates a format string with a ordered list of values | |
* | |
* e.g. format('This is an ordered list: {0}, {1}, {1}, {2}', 1, 'a', false) | |
* => 'This is an ordered list: 1, a, a, false' | |
* | |
* @param {string} s string format with numbered placeholders | |
* @param {...string|number|boolean} a ordered list of values | |
* @return {string} formatted string | |
*/ | |
function (s) { | |
var a = arguments; | |
return s.replace(/\{(\d+)\}/g, function (f, k) { | |
return (++k < a.length) ? a[k] : f; | |
}); | |
} |
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(s){var a=arguments;return s.replace(/\{(\d+)\}/g,function(f,k){return ++k<a.length?a[k]:f})} |
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
Copyright (c) 2011 Stephen M. McKamey, http://mck.me | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
{ | |
"name": "indexed_string_format", | |
"description": "Populates a format string with a ordered list of values", | |
"keywords": [ | |
"string", | |
"format", | |
"list" | |
] | |
} |
Thanks for the suggestion. I incorporated it but added in the test to default to not replacing if that value doesn't exist as I liked that from your other suggestion.
(s,...a)=>s.replace(/{(\d+)}/g,(f,k)=>a[k])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That was a little tougher to shorten than your last one, but I managed, too:
function(s){var a=arguments;return s.replace(/{(\d+)}/g,function(f,k){return a[++k]})}
Regards, atk