String#interpolate(values, syntax) -> String - Node.js String

Node.js examples for String:Template

Description

String#interpolate(values, syntax) -> String

Demo Code


/**/*from w ww .jav  a 2 s . co m*/
 * String#interpolate(values, syntax) -> String
 *
 * Based on String#supplant() by Douglas Crawford:
 *
 *   http://javascript.crockford.com/remedial.html
 *
**/
if (!String.prototype.interpolate)
{
  String.prototype.interpolate = function (values, syntax)
  {
    if (!syntax) syntax = /{([^{}]*)}/g
    return this.replace(syntax,
      function (a, b)
      {
        var r = values[b];
        return typeof r === "string" || typeof r === "number" ? r : a;
      }
    );
  }
}

Related Tutorials