JSFiddle - React, Tailwind, and code Playground

by Oski Krawczyk

JavaScript

String.implement({
    substitute: function(object, regexp) {
      return String(this).replace(regexp || /\\?\{([^{}]+)\}/g, function(match, name) {
        var i, length, path, retStr, sub;
        if (match.charAt(0) === "\\") {
          return match.slice(1);
        }
        if (object[name] != null) {
          return object[name];
        }
        retStr = "";
        path = name.split(".");
        length = path.length;
        sub = object;
        if (length <= 1) {
          return retStr;
        }
        i = 0;
        while (i < length) {
          if ((sub = sub[path[i]]) === null) {
            return retStr;
          }
          i++;
        }
        return sub;
      });
    }
  });