Global.String.prototype.diff (mod, separator)
function compares a string with the one passed as argument using diff

  • Parameter String:
    String to compare against String object value
  • Parameter String:
    Optional regular expression string to use for splitting. If not defined, newlines will be used.
  • Returns
    Object Array containing one JS object for each line with the following properties: .num Line number .value String line if unchanged .deleted Obj Array containing deleted lines .inserted Obj Array containing added lines
Sourcecode in /home/hannes/helma/modules/core/String.js:
547:   String.prototype.diff = function(mod, separator) {
548:       // if no separator use line separator
549:       var regexp = (typeof(separator) == "undefined") ?
550:           new RegExp("\r\n|\r|\n") :
551:           new RegExp(separator);
552:       // split both strings into arrays
553:       var orig = this.split(regexp);
554:       var mod = mod.split(regexp);
555:       // create the Diff object
556:      var diff = new Packages.helma.util.Diff(orig, mod);
557:      // get the diff.
558:      var d = diff.diff();
559:      if (!d)
560:          return null;
561:  
562:      var max = Math.max(orig.length, mod.length);
563:      var result = new Array();
564:      for (var i=0;i<max;i++) {
565:          var line = result[i];
566:          if (!line) {
567:              line = new Object();
568:              line.num = (i+1);
569:              result[i] = line;
570:          }
571:          if (d && i == d.line1) {
572:              if (d.deleted) {
573:                  var del = new Array();
574:                  for (var j=d.line0; j<d.line0+d.deleted; j++)
575:                      del[del.length] = orig[j];
576:                  line.deleted = del;
577:              }
578:              if (d.inserted) {
579:                  var ins = new Array();
580:                  for (var j=d.line1; j<d.line1+d.inserted; j++)
581:                      ins[ins.length] = mod[j];
582:                  line.inserted = ins;
583:              }
584:              i = d.line1 + d.inserted -1;
585:              d = d.link;
586:          } else {
587:              line.value = mod[i];
588:          }
589:      }
590:      return result;
591:  }