Nodejs String Template template(objVar)

Here you can find the source of template(objVar)

Method Source Code

/**/*from ww w.  ja  v  a  2  s .com*/
* Extended String object For Espruino
* Copyright (c) 2014 Anar Software LLC. < http://anars.com >
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see < http://www.gnu.org/licenses/ >
*
*/

/*

Extended Array object For Espruino

This script extends Array object, adds new functions

Created 11 July 2014
by Kay Anar

*/

String.prototype.template = function (objVar)
{
   var strVar = this;
   if(typeof objVar === "object")
   {
      var length = strVar.length;
      var start = -1;
      var key = "";
      for(var index = 0; index < length; index++)
      {
         if (strVar.charAt(index) == '{' && start == -1)
            start = index;
         else if (strVar.charAt(index) == '}' && start != -1)
         {
            strVar = strVar.substring(0, start) + objVar[key] + strVar.substring(index + 1);
            start = -1;
            key = "";
            length = strVar.length;
         }
         else if (start != -1)
         {
            key += strVar.charAt(index);
         }
      }
   }
   return(strVar);
};

print("Free memory {free} bytes of total {total} bytes.".template(process.memory()));
// prints Free memory 1716 bytes of total 1800 bytes.

Related

  1. template()
    String.prototype.template = function () {
      var current = this;
      function safeRegexEscape(str) {
        return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
      for (var i = 0; i < arguments.length; i++) {
        var arg = arguments[i];
        current = current.replace(new RegExp(safeRegexEscape("%%")), arg);
      return current;
    };
    
  2. template(fn, object)
    String.prototype.template = function (fn, object) {'use strict';
      var
        hasTransformer = typeof fn === 'function',
        stringify = JSON.stringify,
        re = /\$\{([\S\s]*?)\}/g,
        strings = [],
        values = hasTransformer ? [] : strings,
        i = 0,
        str,
    ...
    
  3. template(map)
    String.prototype.template=function(map){
        return $(this.replace(/{([^}]+)}/g, function(m,g) {
            console.log(map, m,g)
            var segments = g.split('.');
            var val = map;
            for(var i in segments) {
                val=val[segments[i]];
            return val;
    ...
    
  4. tmpl(d)
    String.prototype.tmpl = function(d) {
      return this.replace(/\$\$([^$]+)\$\$/g,
        Function('d', 'return function(_,s){with(d) return eval(s)}')(d||{}))
    };
    
  5. tmpl(o)
    String.prototype.tmpl = function(o) {
      return this.replace(/\{([^{}]*)\}/g, function(a, b) {
        var r = o[b];
        return typeof r === 'string' || typeof r === 'number' ? r : a;
      });
    };
    
  6. tmpl(obj)
    String.prototype.tmpl = function(obj){
        var str, keys, _do;
        str = this.toString();
        if(!obj || typeof obj !== "object"){
            return str;
        keys = Object.keys(obj);
        _do = function(key){
            var rgx = new RegExp("#{"+key+"}", "g");
    ...