How to use the diff.diffLines 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 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 iov-one / iov-core / packages / iov-cli / src / tsrepl.ts View on Github external
}
    }

    const undo = this.appendTypeScriptInput(tsInput);
    let output: string;

    try {
      // lineOffset unused at the moment (https://github.com/TypeStrong/ts-node/issues/661)
      output = this.typeScriptService.compile(this.evalData.input, this.evalPath);
    } catch (err) {
      undo();
      throw err;
    }

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

    if (isAutocompletionRequest) {
      undo();
    } else {
      // tslint:disable-next-line:no-object-mutation
      this.evalData.output = output;
    }

    // Execute new JavaScript. This may not necessarily be at the end only because e.g. an import
    // statement in TypeScript is compiled to no JavaScript until the imported symbol is used
    // somewhere. This btw. leads to a different execution order of imports than in the TS source.
    let lastResult: any;
    for (const added of changes.filter(change => change.added)) {
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
      lastResult = await executeJavaScriptAsync(added.value, this.evalFilename, this.context!);
    }
github dojo / framework / src / testing / support / assertRender.ts View on Github external
export function assertRender(actual: DNode | DNode[], expected: DNode | DNode[], message?: string): void {
	const parsedActual = formatDNodes(actual);
	const parsedExpected = formatDNodes(expected);
	const diffResult = diff.diffLines(parsedActual, parsedExpected);
	let diffFound = false;
	const parsedDiff = diffResult.reduce((result: string, part, index) => {
		if (part.added) {
			diffFound = true;
			result = `${result}(E)${part.value.replace(/\n\t/g, '\n(E)\t')}`;
		} else if (part.removed) {
			diffFound = true;
			result = `${result}(A)${part.value.replace(/\n\t/g, '\n(A)\t')}`;
		} else {
			result = `${result}${part.value}`;
		}
		return result;
	}, '\n');

	if (diffFound) {
		throw new Error(parsedDiff);
github dabbott / react-native-web-player / utils / Diff.js View on Github external
function diffLines(originalText, newText) {
    const lineDiff = diff.diffLines(originalText, newText, {
      newlineIsToken: true
    })

    const result = lineDiff.reduce(
      (result, change, index) => {
        if (change.removed) { return result }

        let { ranges, value } = result

        const beforeLines = value.split(newlineRegex)

        value += change.value

        const afterLines = value.split(newlineRegex)

        let beforeLineCount = beforeLines.length - 1
github jshint / fixmyjs / lib / cli.js View on Github external
function printDiff(a, b) {
  if (a == b) {
    return
  }

  var DARK = '\x1b[90m'
  var GREEN = '\x1b[32m'
  var RED = '\x1b[31m'
  var RESET = '\x1b[39m'

  var df = diff.diffLines(a, b)
  var content = fu.map(function (n) {
    var line = df[n]
    if (line.removed) {
      return RED + line.value
    } else if (line.added) {
      return GREEN + line.value
    } else {
      return DARK + line.value
    }
  }, Object.keys(df))
  console.log(content.join(RESET + '\n'), RESET)
}
github fossasia / susper.com / node_modules / ts-node / dist / _bin.js View on Github external
function _eval(input, context) {
    var lines = EVAL_INSTANCE.lines;
    var isCompletion = !/\n$/.test(input);
    var undo = appendEval(input);
    var output;
    try {
        output = service.compile(EVAL_INSTANCE.input, EVAL_PATH, -lines);
    }
    catch (err) {
        undo();
        throw err;
    }
    var changes = diff_1.diffLines(EVAL_INSTANCE.output, output);
    if (isCompletion) {
        undo();
    }
    else {
        EVAL_INSTANCE.output = output;
    }
    var result;
    for (var _i = 0, changes_1 = changes; _i < changes_1.length; _i++) {
        var change = changes_1[_i];
        if (change.added) {
            var script = new vm_1.Script(change.value, EVAL_FILENAME);
            result = script.runInNewContext(context);
        }
    }
    return result;
}
github ChuckJonas / sfdx-git-packager / src / commands / git / package.ts View on Github external
function hasNonWhitespaceChanges(a: string, b: string) {
  const diffResults = jsdiff.diffLines(a, b, { ignoreWhitespace: true, newlineIsToken: true });
  for (const result of diffResults) {
    if (result.added || result.removed) {
      return true;
    }
  }
  return false;
}
github slushjs / gulp-conflict / index.js View on Github external
function diffFiles (newFile, oldFilePath) {
  if (newFile.isStream()) {
    error('Diff does not support file streams');
  }
  try {
    var content = fs.readFileSync(oldFilePath, 'utf8');
    var differences = diff.diffLines(content, String(newFile.contents));
    log('File differences: ' + gutil.colors.bgGreen('added') + ' ' + gutil.colors.bgRed('removed') + '\n\n' + differences.map(formatPart).join(''));
  } catch (err) {
    error('Reading old file for diff failed with: ' + err.message);
  }
}