How to use the diff.diffWords 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 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 slidewiki / slidewiki-platform / components / Deck / Diffview / diff_funcs.js View on Github external
const handleTEXT = (oldText, newText, source) => {

    const oldStr = oldText.text,
        newStr = newText.text;
    let root = toHTML(source);

    let color = '',
        container = null,
        diff = jsdiff.diffWords(oldStr, newStr),
        temp = document.createElement('div');


    diff.forEach((part) => {
        // green for additions, red for deletions
        color = part.added ? 'ins' : part.removed ? 'del' : '';
        container = document.createElement('span');
        container.className = color;
        container.appendChild(document.createTextNode(part.value));
        temp.appendChild(container);
    });

    const markedText = temp.innerHTML;

    $(root).find(`*:contains(${oldStr})`)
            .filter(function() { return $(this).text() === oldStr; })
github axross / tap-diff / sources / index.js View on Github external
if (expected_type === 'object') {
      const delta = jsondiffpatch.diff(actual[failed_test_number], expected[failed_test_number])
      const output = jsondiffpatch.formatters.console.format(delta)
      println(output, 4)

    } else if (expected_type === 'array') {
      const compared = diffJson(actual, expected)
        .map(writeDiff)
        .join('');

      println(compared, 4);
    } else if (expected === 'undefined' && actual === 'undefined') {
      ;
    } else if (expected_type === 'string') {
      const compared = diffWords(actual, expected)
        .map(writeDiff)
        .join('');

      println(compared, 4);
    } else {
      println(
        chalk.red.inverse(actual) + chalk.green.inverse(expected),
        4
      );
    }
  };
github prisma / lift / src / utils / printDatamodelDiff.ts View on Github external
return chalk.greenBright(charChange.value)
              })
              .join('')
          }
        }
        return chalk.greenBright(change.value)
      }
      if (change.removed) {
        if (
          change.value.split('\n').length <= 2 &&
          index > 0 &&
          changes[index + 1] &&
          changes[index + 1].added &&
          changes[index + 1].value.split('\n').length <= 2
        ) {
          const charChanges = diffWords(change.value, changes[index + 1].value)

          if (charChanges.length < change.value.length - 3) {
            return charChanges
              .map(charChange => {
                if (charChange.removed) {
                  return strongRed(charChange.value)
                }
                if (charChange.added) {
                  return ''
                }

                return chalk.redBright(charChange.value)
              })
              .join('')
          }
        }
github mybuddymichael / linter-elm-make / lib / old-formatting.js View on Github external
function formatExpectedType(expected, actual) {
  const diff = jsdiff.diffWords(formatType(actual), formatType(expected));
  return (
    '<div class="linter-elm-make-details-expected-type">' +
    diff
      .map(part =&gt; {
        if (part.added) {
          return (
            '<span class="linter-elm-make-expected-type-diff-changed">' +
            _.escape(part.value) +
            '</span>'
          );
        } else if (!part.removed) {
          return '<span>' + _.escape(part.value) + '</span>';
        }
        return '';
      })
      .join('') +</div>
github plone / volto / src / components / manage / Diff / DiffField.jsx View on Github external
const DiffField = ({ one, two, view, schema }) => {
  let parts;
  if (schema.widget) {
    switch (schema.widget) {
      case 'richtext':
        parts = diffWords(one.data, two.data);
        break;
      case 'datetime':
        parts = diffWords(
          moment(one).format('LLLL'),
          moment(two).format('LLLL'),
        );
        break;
      case 'textarea':
      default:
        parts = diffWords(one, two);
        break;
    }
  } else if (schema.type === 'boolean') {
    parts = diffWords(one ? 'Yes' : 'No', two ? 'Yes' : 'No');
  } else if (schema.type === 'array') {
    parts = diffWords(join(one, ', '), join(two, ', '));
github firebase / oss-bot / functions / src / template.ts View on Github external
areStringsEqual(a: string, b: string) {
    const diffs = diff.diffWords(a, b);
    for (const d of diffs) {
      if (d.added || d.removed) {
        return false;
      }
    }

    return true;
  }
}
github yeoman / generator / lib / util / conflicter.js View on Github external
if (file.binary === undefined) {
      file.binary = binaryDiff.isBinary(file.path, file.contents);
    }

    const actual = fs.readFileSync(path.resolve(filepath));

    if (!(contents instanceof Buffer)) {
      contents = Buffer.from(contents || '', 'utf8');
    }

    if (file.binary) {
      return actual.toString('hex') !== contents.toString('hex');
    }

    if (this.ignoreWhitespace) {
      file.changes = jsdiff.diffWords(
        actual.toString(),
        contents.toString(),
        this.diffOptions
      );
    } else {
      file.changes = jsdiff.diffLines(
        actual.toString(),
        contents.toString(),
        this.diffOptions
      );
    }

    const changes = file.changes;
    return changes.length > 1 || changes[0].added || changes[0].removed;
  }
github plone / volto / src / components / manage / Diff / DiffField.jsx View on Github external
const DiffField = ({ one, two, view, schema }) =&gt; {
  let parts;
  if (schema.widget) {
    switch (schema.widget) {
      case 'richtext':
        parts = diffWords(one.data, two.data);
        break;
      case 'datetime':
        parts = diffWords(
          moment(one).format('LLLL'),
          moment(two).format('LLLL'),
        );
        break;
      case 'textarea':
      default:
        parts = diffWords(one, two);
        break;
    }
  } else if (schema.type === 'boolean') {
    parts = diffWords(one ? 'Yes' : 'No', two ? 'Yes' : 'No');
  } else if (schema.type === 'array') {
    parts = diffWords(join(one, ', '), join(two, ', '));
  } else {
    parts = diffWords(one, two);
  }
  return (
    
        
          {schema.title}
        
      <table>
      </table>
github zanata / zanata-platform / server / zanata-frontend / src / app / editor / components / TextDiff / index.js View on Github external
function compare (text1, text2) {
  return diffWords(text1, text2, { ignoreCase: true })
}