Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Created December 3, 2013 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasBurleson/7779666 to your computer and use it in GitHub Desktop.
Save ThomasBurleson/7779666 to your computer and use it in GitHub Desktop.
String building function that support index-based or key-based token substitutions; e.g. supplant( "Hello {firstName} {lastName}", user );
# Make sure the supplant() method is available!
# Now supports dot notation for complex object value substitutions
# e.e. {foo.boo.moo.uff ...}
String::supplant = (o) ->
this.replace /\{([^{}]*)\}/g, (a, b) ->
p = b.split('.')
r = o
# Descend the property chain "b" to resolve the end-chain value
try
for own s of p
r = r[p[s]]
catch e
r = a
return if (r is 'string') or (r is 'number') then r else a
# return `(r === 'string') || (r ==='number') ? r : a`
@ThomasBurleson
Copy link
Author

Here is the JavaScript version:

/**
 * @author      Thomas Burleson
 * @date        November, 2013
 * @description
 *
 *  String supplant global utility (similar to but more powerful than sprintf() ).
 *
 *  Usages:
 *
 *      var user = {
 *              first : "Thomas",
 *              last  : "Burleson",
 *              address : {
 *                  city : "West Des Moines",
 *                  state: "Iowa"
 *              },
 *              contact : {
 *                  email : "ThomasBurleson@Gmail.com"
 *                  url   : "http://www.gridlinked.info"
 *              }
 *          },
 *          message = "Hello Mr. {first} {last}. How's life in {address.city}, {address.state} ?";
 *
 *     return supplant( message, user );
 *
 *
 * @author Thomas Burleson
 *
 */
(function( define ) {
    "use strict";

        // supplant() method from Crockfords `Remedial Javascript`

        var supplant =  function( template, values, pattern ) {
            pattern = pattern || /\{([^\{\}]*)\}/g;

            return template.replace(pattern, function(a, b) {
                var p = b.split('.'),
                    r = values;

                try {
                    for (var s in p) { r = r[p[s]];  }
                } catch(e){
                    r = a;
                }

                return (typeof r === 'string' || typeof r === 'number') ? r : a;
            });
        };


        // supplant() method from Crockfords `Remedial Javascript`
        Function.prototype.method = function (name, func) {
            this.prototype[name] = func;
            return this;
        };

        String.method("supplant", function( values, pattern ) {
            var self = this;
            return supplant(self, values, pattern);
        });


        // Publish this global function...
        String.supplant = supplant;

        window.supplant = supplant;
}( ));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment