How to use diff - 10 common examples

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 luckymarmot / API-Flow / testing / e2e / internal-swagger2 / e2e.spec.js View on Github external
const compare = (actual, expected = '{}') => {
  const delta = diff.diffJson(actual, expected)
  if (
    delta.length === 1 &&
    typeof delta[0].removed === 'undefined' &&
    typeof delta[0].added === 'undefined'
  ) {
    return true
  }

  /* eslint-disable no-console */
  console.log('\x1b[42m' +
    (new Array(6)).join('-------------\n') + '\x1b[0m')
  delta.forEach(part => {
    let color = 'grey'
    if (part.added) {
      color = 'green'
    }
github fourlabsldn / fl-form-builder / __tests__ / UiSnapshots.js View on Github external
const logDifference = (one, other) => {
  const diff = jsdiff.diffJson(one, other);

  diff.forEach((part) => {
    // green for additions, red for deletions
    // grey for common parts
    let color;
    if (part.added) {
      color = 'green';
    } else if (part.removed) {
      color = 'red';
    } else {
      color = 'grey';
    }

    process.stderr.write(colors[color](part.value));
  });
github TheFive / osmbc / import / createdb.js View on Github external
function coloredDiffLog(one,other) {
  should(typeof(one)=="string");
  should(typeof(other)=="string");


  var diff = jsdiff.diffWords(one, other);

  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.info();
}
github pomber / code-surfer / packs / standalone / src / differ.ts View on Github external
function myDiff(oldCode: string, newCode: string) {
  const changes = diffLines(oldCode || "", newCode);

  let oldIndex = -1;
  return changes.map(({ value, count, removed, added }) => {
    const lines = value.split(newlineRe);
    // check if last line is empty, if it is, remove it
    const lastLine = lines.pop();
    if (lastLine) {
      lines.push(lastLine);
    }
    const result = {
      oldIndex,
      lines,
      count,
      removed,
      added
    };
github TypeStrong / ts-node / src / bin.ts View on Github external
function _eval (service: Register, state: EvalState, input: string) {
  const lines = state.lines
  const isCompletion = !/\n$/.test(input)
  const undo = appendEval(state, input)
  let output: string

  try {
    output = service.compile(state.input, state.path, -lines)
  } catch (err) {
    undo()
    throw err
  }

  // Use `diff` to check for new JavaScript to execute.
  const changes = diffLines(state.output, output)

  if (isCompletion) {
    undo()
  } else {
    state.output = output
  }

  return changes.reduce((result, change) => {
    return change.added ? exec(change.value, state.path) : result
  }, undefined)
}
github mupchrch / split-diff / lib / compute-diff.js View on Github external
function _computeDiffChunks(oldText, newText, isWhitespaceIgnored) {
  var JsDiff = require('diff');

  // If the last line has changes, JsDiff doesn't return that.
  // Generally, content with new line ending are easier to calculate offsets for.
  if (oldText[oldText.length - 1] !== '\n' || newText[newText.length - 1] !== '\n') {
    oldText += '\n';
    newText += '\n';
  }

  var lineDiff;
  if (isWhitespaceIgnored == 'true') {
    lineDiff = JsDiff.diffTrimmedLines(oldText, newText);
  } else {
    lineDiff = JsDiff.diffLines(oldText, newText);
  }

  var chunks = [];
  var nextOffset = 0;
  var offset = 0;

  lineDiff.forEach( function(part) {
    var added = part.added,
      removed = part.removed,
      value = part.value;
    var count = part.count;//value.split('\n').length - 1;
    if (!added && !removed) {
      offset = nextOffset;
      nextOffset = 0;
    } else if (added) {
      nextOffset += count;
github juhovh / mocha-jenkins-reporter / lib / jenkins.js View on Github external
var actual = err.actual,
        expected = err.expected;

    var lines, msg = '';

    if (err.actual && err.expected) {
      // make sure actual and expected are strings
      if (!(typeof actual === 'string' || actual instanceof String)) {
        actual = JSON.stringify(err.actual);
      }

      if (!(typeof expected === 'string' || expected instanceof String)) {
        expected = JSON.stringify(err.expected);
      }

      var diffstr = diff.createPatch('string', actual, expected);
      lines = diffstr.split('\n').splice(4);
      msg += lines.map(cleanUp).filter(notBlank).join('\n');
    }

    if (options.junit_report_stack && err.stack) {
      if (msg) msg += '\n';
      lines = err.stack.split('\n').slice(1);
      msg += lines.map(cleanUp).filter(notBlank).join('\n');
    }

    return msg;
  }
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 Kinto / kinto-admin / src / utils.js View on Github external
export function diffJson(a: Object, b: Object, context: number = 3): string[] {
  // Number of lines to show above/below changes in diffs.
  const chunks = diff(a, b);
  return chunks.map((chunk, i) => {
    const isFirstChunk = i == 0;
    const isLastChunk = i == chunks.length - 1;

    // Remove empty lines.
    let lines = chunk.value.split("\n").filter(part => part !== "");

    if (!chunk.added && !chunk.removed) {
      // Truncate beginning of first chunk if larger than context.
      // The opening brace "{" does not count as context, hence +1.
      if (isFirstChunk && lines.length > context + 1) {
        lines = ["..."].concat(lines.slice(-context));
        // Truncate end of last chunk if larger than context.
        // The closing brace "}" does not count as context, hence +1.
      } else if (isLastChunk && lines.length > context + 1) {
        lines = lines.slice(0, context).concat(["..."]);