Last active
August 29, 2015 13:58
-
-
Save anasnakawa/10375718 to your computer and use it in GitHub Desktop.
because i don't like string concatination
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
/** | |
* string parser with data passed as arguments | |
* | |
* @param {arguments} | |
* @return {string} | |
* | |
* example: | |
* -------- | |
* 'i used both [0] & [1], however [1] looks good so far'.parse( 'iphone', 'andoird' ); // "i used both iphone & andoird, however andoird looks good so far" | |
* 'the weather now in [0] seems to be [1]'.parse( 'Dubai', function() { return 'very hot'; }); // "the weather now in Dubai seems to be very hot" | |
*/ | |
String.prototype.parse = function() { | |
var args = arguments; | |
return this.replace( /\[([0-9]+)\]/g, function( token, match ) { | |
var index = parseInt( match, 10 ); | |
var data = args[ index ]; | |
return isNaN( index ) || data == null ? token: ( typeof data !== 'function' ? data: data() ); | |
}); | |
} | |
/** | |
* string parser with data passed as an object literal | |
* | |
* @param {object} data | |
* @return {string} | |
* | |
* example: | |
* -------- | |
* 'i used both {mob1} & {mob2}, however {mob2} looks good so far'.tmpl({ mob1: 'iphone', mob2: 'andoird' }); // "i used both iphone & andoird, however andoird looks good so far" | |
* 'the weather now in {city} seems to be {temp}'.tmpl({ city: 'Dubai', temp: function() { return 'very hot'; } }); // "the weather now in Dubai seems to be very hot" | |
*/ | |
String.prototype.tmpl = function( data ) { | |
return this.replace( /{([A-Za-z0-9_$\-]+)}/g, function ( token, match ) { | |
return ( typeof data[ match ] !== 'function' ) ? data[ match ] : data[ match ](); | |
}); | |
} |
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
/** | |
* in case you don't like extending native objects | |
*/ | |
/** | |
* tiny string parser | |
* | |
* @param {string} str | |
* @param {arguments} | |
* @return {string} | |
* | |
* example: | |
* -------- | |
* parse( 'i used both [0] & [1], however [1] looks good so far', 'iphone', 'andoird' ); // "i used both iphone & andoird, however andoird looks good so far" | |
* parse( 'the weather now in [0] seems to be [1]', 'Dubai', function() { return 'very hot'; }); // "the weather now in Dubai seems to be very hot" | |
*/ | |
function parse( str ) { | |
var args = Array.prototype.slice.call( arguments, 1 ); | |
return str.replace( /\[([0-9]+)\]/g, function( token, match ) { | |
var index = parseInt( match, 10 ); | |
var data = args[ index ]; | |
return isNaN( index ) || data == null ? token: ( typeof data !== 'function' ? data: data() ); | |
}); | |
} | |
/** | |
* template | |
* | |
* @param {string} str | |
* @param {object} data | |
* @return {string} | |
* | |
* example: | |
* -------- | |
* tmpl( 'i used both {mob1} & {mob2}, however {mob2} looks good so far', { mob1: 'iphone', mob2: 'andoird' }); // "i used both iphone & andoird, however andoird looks good so far" | |
* tmpl( 'the weather now in {city} seems to be {temp}', { city: 'Dubai', temp: function() { return 'very hot'; } }); // "the weather now in Dubai seems to be very hot" | |
*/ | |
function tmpl( str, data ) { | |
return str.replace( /{([A-Za-z0-9_$\-]+)}/g, function ( token, match ) { | |
return ( typeof data[ match ] == 'function' ) ? data[ match ]() : data[ match ]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment