How to use the diff.diffTrimmedLines 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 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;
github dsherret / ts-morph / packages / ts-morph / scripts / verification / ensureDeclarationFilesNotChanged.ts View on Github external
async function compareFiles(baseLineFilePath: string, outputtedFilePath: string) {
    const baselineFilePromise = readFile(baseLineFilePath);
    const outputtedFilePromise = readFile(outputtedFilePath);

    const baseLineResult = await baselineFilePromise;
    const outputtedFileResult = await outputtedFilePromise;

    const results = jsdiff.diffTrimmedLines(baseLineResult, outputtedFileResult).filter(result => result.added || result.removed);
    if (results.length > 0) {
        console.log(`There were differences in the declaration file (${outputtedFilePath}). `
            + "Please review these changes and then confirm them by running `yarn overwrite-declaration-files`.");
        for (const result of results) {
            const text = result.added ? chalk.green(result.value) : chalk.red(result.value);
            console.log(text);
        }
        process.exit(1);
    }
}
github centrehq / centre-tokens / validate / contractDiff.js View on Github external
function diffText(code1, code2) {
    const diffOutput = diff.diffTrimmedLines(code1, code2);
    for (var i = 0; i < diffOutput.length; i++) {
        var diffLine = diffOutput[i];
		if (diffLine.added) {
	  	    process.stdout.write(chalk.green(`+ ${diffLine.value}`));
	    } else if (diffLine.removed) {
			process.stdout.write(chalk.red(`- ${diffLine.value}`));
	    }
	}
}
github elasticio / haproxy-srv / start.js View on Github external
return new Promise(function (resolve, reject) {
            var originalConfig = fs.existsSync(configurationFile) ? fs.readFileSync(configurationFile, 'utf8') : '';
            debug('Merging template');
            var newConfig = template(context);
            var diff = jsdiff.diffTrimmedLines(originalConfig, newConfig, {ignoreWhitespace: true});
            if (diff.length > 1 || (diff[0].added || diff[0].removed)) {
                info('Configuration changes detected, diff follows');
                info(jsdiff.createPatch(configurationFile, originalConfig, newConfig, 'previous', 'new'));
                info('Writing configuration file filename=%s', configurationFile);
                ;
                fs.writeFileSync(configurationFile, newConfig, 'utf8');
                info('Configuration file updated filename=%s', configurationFile);
                if (haproxyRunning) {
                    info('Configuration changes were detected reloading the HAProxy');
                    haproxy.reload(function (err, reloaded, cmd) {
                        if (err) {
                            info("HAProxy reload failed error=%s cmd=%s", err, cmd);
                            return reject(err);
                        }
                        info('Triggered configuration reload reloaded=%s cmd=%s', reloaded, cmd);
                        resolve();
github gchq / CyberChef / src / core / operations / legacy / Diff.js View on Github external
}

        switch (diffBy) {
            case "Character":
                diff = JsDiff.diffChars(samples[0], samples[1]);
                break;
            case "Word":
                if (ignoreWhitespace) {
                    diff = JsDiff.diffWords(samples[0], samples[1]);
                } else {
                    diff = JsDiff.diffWordsWithSpace(samples[0], samples[1]);
                }
                break;
            case "Line":
                if (ignoreWhitespace) {
                    diff = JsDiff.diffTrimmedLines(samples[0], samples[1]);
                } else {
                    diff = JsDiff.diffLines(samples[0], samples[1]);
                }
                break;
            case "Sentence":
                diff = JsDiff.diffSentences(samples[0], samples[1]);
                break;
            case "CSS":
                diff = JsDiff.diffCss(samples[0], samples[1]);
                break;
            case "JSON":
                diff = JsDiff.diffJson(samples[0], samples[1]);
                break;
            default:
                return "Invalid 'Diff by' option.";
        }
github sszczep / Flo / server / GloEvents / cards.js View on Github external
const fields = [];

    if(oldCard.name !== newCard.name) {
      fields.push({
        name: i18next.t('keywords.name'),
        value:
`${i18next.t('keywords.new')}: ${newCard.name}
${i18next.t('keywords.old')}: ${oldCard.name}`
      });
    }

    if(oldCard.description.text === undefined) oldCard.description.text = '';
    if(newCard.description.text === undefined) newCard.description.text = '';
    if(oldCard.description.text !== newCard.description.text) {
      const diffObj = JsDiff.diffTrimmedLines(oldCard.description.text, newCard.description.text);

      fields.push({
        name: i18next.t('keywords.description'),
        value:
`\`\`\`diff
${diffToText(diffObj)}
\`\`\``
      });
    }

    if(oldCard.due_date !== newCard.due_date) {
      fields.push({
        name: i18next.t('keywords.dueDate'),
        value:
`${i18next.t('keywords.new')}: ${newCard.due_date}
${i18next.t('keywords.old')}: ${oldCard.due_date}`