Nodejs String Insert insertAt(index, string)

Here you can find the source of insertAt(index, string)

Method Source Code

String.prototype.insertAt = function (index, string) {
   return [this.slice(0, index), string, this.slice(index)].join('');
};

String.prototype.bind = function (attr) {
   var str = this,
      regex = /(?:data\-bind\-(\w+)="(\w+)")/g,
      match,//from  w w w . j  av a 2  s. co  m
      matches = {};

   while (match = regex.exec(str)) {
      matches[match[1]] = match[2];
   }

   for (var key in matches) {
      if (matches.hasOwnProperty(key)) {
         if (key === 'content') {
            var angleBracketIndex = str.indexOf('>');
            str = str.insertAt(angleBracketIndex + 1, attr[matches[key]]);
         } else {
            var doubleQuotesIndex = str.lastIndexOf('\"');
            str = str.insertAt(doubleQuotesIndex + 1, ' ' + key + '\"=' + attr[matches[key]] + '\"');
         }
      }
   }

   return str;
};

var attributes1 = {name: 'Steven'};
console.log('<div data-bind-content="name"></div>'.bind(attributes1));

var attributes2 = {name: 'Elena', link: 'http://telerikacademy.com'};
console.log('<a data-bind-content="name" data-bind-href="link" data-bind-class="name"></\a>'.bind(attributes2));

Related

  1. insert(value, position)
    String.prototype.insert = String.prototype.insert || function (value, position) {
      if (!value) return this;
      position = position === 'end' ? this.length : position === 'start' ? 0 : position || 0
      return this.slice(0, position) + value + this.slice(position)
    
  2. insertAt( loc, strChunk )
    String.prototype.insertAt = function ( loc, strChunk ) {
        return (this.valueOf().substr( 0, loc )) + strChunk + (this.valueOf().substr( loc ))
    };
    
  3. insertAt(b,a)
    String.prototype.insertAt=function(b,a){
      return(this.valueOf().substr(0,b))+a+(this.valueOf().substr(b))
    };
    
  4. insertAt(idx, rem, text)
    String.prototype.insertAt = function(idx, rem, text){
      return (this.slice(0,idx) + text + this.slice(idx + Math.abs(rem)));
    };
    
  5. insertAt(index, string)
    String.prototype.insertAt=function(index, string) {
        return this.substr(0, index) + string + this.substr(index);
    };
    
  6. insertAt(index, string)
    String.prototype.insertAt = function(index, string) {
        return [this.slice(0, index), string, this.slice(index)].join('');
    };
    
  7. insertAt(index, stringInserted)
    String.prototype.insertAt = function (index, stringInserted) {
      if (index > 0)
        return this.substring(0, index) + stringInserted + this.substring(index, this.length);
      else
        return stringInserted + stthisring;
    };
    
  8. insertComma()
    String.prototype.insertComma = function () {
        var string = this;
        var temporary = "";
        while (string != (temporary = string.replace(/^([+-]?\d+)(\d\d\d)/, "$1,$2"))) {
            string = temporary;
        return string;