How to use the diff.diffChars function in diff

To help you get started, we’ve selected a few diff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github maxogden / data-plumber / compare.js View on Github external
var differ = through(function(ch) {
    lines++
  
    if (ch[0] === ch[1]) linesMatched++
  
    if (lines < cutoff) {
      var diff = jsDiff.diffChars(ch[1], ch[0])
  
      diff.forEach(function(part){
        var color = 'green'
        if (part.added) color = 'brightRedBg white'
        if (part.removed) color = 'brightRedBg white'
        // var color = 'grey'
        // if (part.added) color = 'green'
        // if (part.removed) color = 'red'
        process.stdout.write(styled(color, part.value))
      })

      console.log('')
    }
  }, function() {
    differ.queue(null)
github apache / cordova-docs / tools / lib / docs_comparer.js View on Github external
DocsComparer.prototype.compareDomSubset = function (sourceDom, targetDom, subset, relativePath, options) {
        var sourceHtml = sourceDom(subset).html();
        var targetHtml = targetDom(subset).html();
        var diff;
        var changed = false;

        diff = jsdiff.diffChars(sourceHtml, targetHtml);
        diff.forEach(function (part) {
            changed = part.added || part.removed;
        });
        if (changed) {
            console.error("Subset of DOM '" + subset + "' for path " + relativePath + ' is different.');
            if (options.verbose > 0) {
                diff.forEach(function (part) {
                    // green for additions, red for deletions
                    // grey for common parts
                    var color = part.added ? 'green' : (part.removed ? 'red' : 'grey');
                    process.stderr.write(part.value[color]);
                });

                console.log();
            }
        }
github byte-foundry / prototypo / app / scripts / components / handlegrip / handlegrip-text.components.jsx View on Github external
updateCacheTextArray(newText) {
		let {textArray, lastKey} = this.state;
		let currentIndex = 0;

		diffChars(
			textArray.map(keyValue => keyValue[1]).join(''),
			newText,
		).forEach(({added, removed, count, value}) => {
			if (removed) {
				textArray = count
					? [
						...textArray.slice(0, currentIndex),
						...textArray.slice(currentIndex + count),
					]
					: []; // if count is undefined, it means text is empty
				return;
			}

			if (added) {
				textArray = [
					...textArray.slice(0, currentIndex),
github yabwe / words / src / js / words.js View on Github external
updateState: function (element) {
		var nextStr = Util.createHTMLWordString(element), // text representation of current DOM
			currStr = this.doc.toString(), // text representation of the current tree
			diff = JsDiff.diffChars(currStr, nextStr), // JsDiff of two strings
			index = 0;

		/* Iterate through array of diffs detected by JSDiff
		 *
		 * Each 'diff' is an object that represents a part of the string:
		 *
		 * {
		 *    added: true/undefined (True if this represents a chunk of text that was added)
		 *    removed: true/undefined (True if this represents a chunk of text that was removed)
		 *    value: string (The chunk of text this diff covers)
		 *    count: int (The length of this chunk of text)
		 * }
		 *
		 * NOTE: If added and removed are both undefined this chunk of text text was unchanged
		 */
		diff.forEach(function (action) {
github TheFive / osmbc / model / logModule.js View on Github external
Change.prototype.htmlDiffText = function htmlDiffText(maxChars) {
  debug("htmlDiffText");
  var from = "";
  var to = "";

  if (this.from) from = String(this.from);
  if (this.to) to = String(this.to);

  if (from.length > 2000 || to.length > 2000) {
    return "Disabled for texts longer than 2000 chars.";
  }

  // first check on only spaces
  var diff = jsdiff.diffChars(from, to);
  var onlySpacesAdd = true;
  var onlySpacesDel = true;
  diff.forEach(function(part) {
    var partOnlySpace = (part.value.trim() === "");
    if (part.removed) {
      onlySpacesAdd = false;
      if (!partOnlySpace) onlySpacesDel = false;
    }

    if (part.added) {
      onlySpacesDel = false;
      if (!partOnlySpace) onlySpacesAdd = false;
    }
  });
  if (onlySpacesAdd) {
    return '<span class="osmbc-inserted">ONLY SPACES ADDED</span>';
github lamartire / sharec / packages / sharec-tester / src / core / files / diff.js View on Github external
function createFilesDiff({ fixtureFile, targetFile }) {
  const filesDiff = diff.diffChars(targetFile.source, fixtureFile.source)

  if (!filesDiff || filesDiff.length === 0) {
    return null
  }

  const outputDiff = filesDiff.reduce((acc, part) => {
    if (part.added) {
      return acc + chalk.bgGreen(chalk.black(part.value))
    } else if (part.removed) {
      return acc + chalk.bgRed(chalk.black(part.value))
    }

    return acc + part.value
  }, '')

  return {
github tbroadley / github-spellcheck-cli / lib / spellcheck.js View on Github external
function onlyPeriodsAdded({ misspelling, suggestions: [suggestion] }) {
  if (!suggestion) {
    return false;
  }

  const diff = diffChars(misspelling, suggestion);
  return _(diff).filter('removed').isEmpty() &&
         _(diff).filter('added').every(({ value }) => /^\.+$/.test(value));
}
github Adornis / typescript-compiler / meteor-typescript / script-snapshot.js View on Github external
getChangeRange(oldSnapshot) {
    if (!oldSnapshot) return undefined;

    const diffs = jsdiff.diffChars(oldSnapshot.text, this.text);
    if (diffs.length) {
      let ind = 0;
      let changes = [];
      for (let i = 0; i &lt; diffs.length; i++) {
        const diff = diffs[i];

        if (diff.added) {
          changes.push(ts.createTextChangeRange(ts.createTextSpan(ind, 0), diff.count));
          ind += diff.count;
          continue;
        }

        if (diff.removed) {
          changes.push(ts.createTextChangeRange(ts.createTextSpan(ind, diff.count), 0));
          continue;
        }
github lovetheory / nkc2 / nkc_modules / jaderender.js View on Github external
function htmlDiff(earlier,later){
  var diff = jsdiff.diffChars(earlier,later)
  var outputHTML = ''

  diff.forEach(function(part){
    var stylestr = part.added?'DiffAdded':part.removed?'DiffRemoved':null
    part.value = render.plain_render(part.value)
    outputHTML += (stylestr?`<span class="${stylestr}">${part.value}</span>`:part.value)
  })

  return outputHTML
}
github autowp / autowp / assets / controllers / info / text / index.ts View on Github external
function doDiff() {
            
            var diff = JsDiff.diffChars(self.prev.text, self.current.text);
            
            var fragment = document.createDocumentFragment();
            for (var i=0; i &lt; diff.length; i++) {

                if (diff[i].added &amp;&amp; diff[i + 1] &amp;&amp; diff[i + 1].removed) {
                    var swap = diff[i];
                    diff[i] = diff[i + 1];
                    diff[i + 1] = swap;
                }

                var node;
                if (diff[i].removed) {
                    node = document.createElement('del');
                    node.appendChild(document.createTextNode(diff[i].value));
                } else if (diff[i].added) {
                    node = document.createElement('ins');