How to use the diff-match-patch.DIFF_INSERT function in diff-match-patch

To help you get started, we’ve selected a few diff-match-patch 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 DefinitelyTyped / DefinitelyTyped / types / diff-match-patch / diff-match-patch-tests.ts View on Github external
diffs.forEach((diff) => {
        const operation = diff[0]; // Operation (insert, delete, equal)
        const text = diff[1]; // Text of change

        switch (operation) {
            case DiffMatchPatch.DIFF_INSERT:
                pattern += "I";
                break;
            case DiffMatchPatch.DIFF_DELETE:
                pattern += "D";
                break;
            case DiffMatchPatch.DIFF_EQUAL:
                pattern += "E";
                break;
        }

        changes += text;
    });
}
github DefinitelyTyped / DefinitelyTyped / types / diff-match-patch / diff-match-patch-tests.ts View on Github external
pattern += "I";
                break;
            case DiffMatchPatch.DIFF_DELETE:
                pattern += "D";
                break;
            case DiffMatchPatch.DIFF_EQUAL:
                pattern += "E";
                break;
        }

        changes += text;
    });
}

const DIFF_DELETE: number = DiffMatchPatch.DIFF_DELETE;
const DIFF_INSERT: number = DiffMatchPatch.DIFF_INSERT;
const DIFF_EQUAL: number = DiffMatchPatch.DIFF_EQUAL;
const dmp = new DiffMatchPatch.diff_match_patch();

// DIFF TEST FUNCTIONS

function testDiffCommonPrefix() {
    assertEquals(0, dmp.diff_commonPrefix('abc', 'xyz'));
}

function testDiffCommonSuffix() {
    assertEquals(0, dmp.diff_commonSuffix('abc', 'xyz'));
}

function testDiffCommonOverlap() {
    assertEquals(0, dmp.diff_commonOverlap_('', 'abcd'));
}
github imnapo / react-native-cn-richtext-editor / src / CNTextInput.js View on Github external
this.checkKeyPressAndroid = 0;

    this.avoidAndroidIssueWhenPressSpaceText = '';
    this.justToolAdded = false;
    this.state = {
      selectedTag: 'body',
      selection: { start: 0, end: 0 },
      avoidUpdateText: false,
    };

    this.dmp = new DiffMatchPatch();
    this.oldText = '';
    this.reCalculateTextOnUpate = false;
    // You can also use the following properties:
    DiffMatchPatch.DIFF_DELETE = -1;
    DiffMatchPatch.DIFF_INSERT = 1;
    DiffMatchPatch.DIFF_EQUAL = 0;
  }
github buildbuddy-io / buildbuddy / app / compare / diff_chunk.tsx View on Github external
render() {
    const marker = this.props.chunk.marker;
    switch (marker) {
      case DiffMatchPatch.DIFF_INSERT:
        return this.renderChunk(
          this.renderAdded.bind(this),
          /* collapsedLabel= */ "added",
          /* expandButtonClass= */ "added",
          MIN_COLLAPSED_CHANGED_REGION_NUM_LINES
        );
      case DiffMatchPatch.DIFF_DELETE:
        return this.renderChunk(
          this.renderRemoved.bind(this),
          /* collapsedLabel= */ "removed",
          /* expandButtonClass= */ "removed",
          MIN_COLLAPSED_CHANGED_REGION_NUM_LINES
        );
      case DiffMatchPatch.DIFF_EQUAL:
      default:
        return this.renderChunk(
github inkandswitch / capstone / src / components / TextEditor.tsx View on Github external
// Otherwise find the diff between the current and desired contents, and
    // apply corresponding editor ops to close them.
    const dmp = new DiffMatchPatch.diff_match_patch()
    const diff = dmp.diff_main(oldContent, newContent)
    // The diff lib doesn't give indicies so we need to compute them ourself as
    // we go along.
    let at = 0
    for (let i = 0; i < diff.length; i += 1) {
      const [type, str] = diff[i]
      switch (type) {
        case DiffMatchPatch.DIFF_EQUAL: {
          at += str.length
          break
        }
        case DiffMatchPatch.DIFF_INSERT: {
          const fromPos = codeMirrorDoc.posFromIndex(at)
          codeMirrorDoc.replaceRange(str, fromPos, undefined, "automerge")
          at += str.length
          break
        }
        case DiffMatchPatch.DIFF_DELETE: {
          const fromPos = codeMirrorDoc.posFromIndex(at)
          const toPos = codeMirrorDoc.posFromIndex(at + str.length)
          codeMirrorDoc.replaceRange("", fromPos, toPos, "automerge")
          break
        }
        default: {
          throw new Error(`Did not expect diff type ${type}`)
        }
      }
    }
github Webstrates / Webstrates / client / webstrates / coreOpCreator.js View on Github external
patch.diffs.forEach(function([type, value]) {
			switch (type) {
				case diffMatchPatch.DIFF_DELETE:
					ops.push({ sd: value, p: [...path, offset] });
					break;
				case diffMatchPatch.DIFF_INSERT:
					ops.push({ si: value, p: [...path, offset] });
					// falls through intentionally
				case diffMatchPatch.DIFF_EQUAL:
					offset += value.length;
					break;
				default: throw Error(`Unsupported operation type: ${type}`);
			}
		});
	});
github choerodon / devops-service / react / components / yamlEditor / merge.js View on Github external
(function () {
  "use strict";
  const CodeMirror = require("codemirror/lib/codemirror");
  var diff_match_patch = require("diff-match-patch");
  var DIFF_EQUAL = diff_match_patch.DIFF_EQUAL;
  var DIFF_INSERT = diff_match_patch.DIFF_INSERT;
  var DIFF_DELETE = diff_match_patch.DIFF_DELETE;

  var Pos = CodeMirror.Pos;
  var svgNS = "http://www.w3.org/2000/svg";

  function DiffView(mv, type) {
    this.mv = mv;
    this.type = type;
    this.classes =
      type == "left"
        ? {
          chunk: "CodeMirror-merge-l-chunk",
          start: "CodeMirror-merge-l-chunk-start",
          end: "CodeMirror-merge-l-chunk-end",
          insert: "CodeMirror-merge-l-inserted",
          del: "CodeMirror-merge-l-deleted",
github Adslot / adslot-ui / src / components / PrettyDiff / index.jsx View on Github external
const getTextClass = diffType => {
    switch (diffType) {
      case DiffMatchPatch.DIFF_DELETE:
        return 'pretty-diff-component-delete';
      case DiffMatchPatch.DIFF_INSERT:
        return 'pretty-diff-component-insert';
      default:
        return 'pretty-diff-component-equal';
    }
  };
github translate / pootle / pootle / static / js / shared / utils / diff.js View on Github external
export default function diff(a, b) {
  const result = dmp.diff_main(a, b);
  const html = [];

  dmp.diff_cleanupSemantic(result);

  for (let i = 0; i < result.length; i++) {
    const op = result[i][0];
    const text = highlightRO(result[i][1]);
    if (op === DMP.DIFF_INSERT) {
      html[i] = `<span class="diff-insert">${text}</span>`;
    } else if (op === DMP.DIFF_DELETE) {
      html[i] = `<span class="diff-delete">${text}</span>`;
    } else if (op === DMP.DIFF_EQUAL) {
      html[i] = text;
    }
  }

  return html.join('');
}

diff-match-patch

npm package for https://github.com/google/diff-match-patch

Apache-2.0
Latest version published 4 years ago

Package Health Score

71 / 100
Full package analysis