How to use diff-match-patch - 10 common examples

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
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'));
}

function testDiffHalfMatch() {
github DefinitelyTyped / DefinitelyTyped / types / diff-match-patch / diff-match-patch-tests.ts View on Github external
function testDiffMainEach() {
    const oldValue = "hello world, how are you?";
    const newValue = "hello again world. how have you been?";

    const diffEngine = new DiffMatchPatch.diff_match_patch();
    const diffs = diffEngine.diff_main(oldValue, newValue);
    diffEngine.diff_cleanupSemantic(diffs);

    let changes = "";
    let pattern = "";

    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";
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
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 DefinitelyTyped / DefinitelyTyped / types / diff-match-patch / diff-match-patch-tests.ts View on Github external
function testPatchObj() {
    // Patch Object.
    const p = new DiffMatchPatch.patch_obj();
    assertEquals(null, p.start1);
    assertEquals(null, p.start2);

    p.start1 = 20;
    p.start2 = 21;
    p.length1 = 18;
    p.length2 = 17;
    p.diffs = [[DIFF_EQUAL, 'jump'], [DIFF_DELETE, 's'], [DIFF_INSERT, 'ed'], [DIFF_EQUAL, ' over '], [DIFF_DELETE, 'the'], [DIFF_INSERT, 'a'], [DIFF_EQUAL, '\nlaz']];
    const strp = p.toString();
    assertEquals('@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n  over \n-the\n+a\n %0Alaz\n', strp);
}
github facebookarchive / atom-ide-ui / modules / atom-ide-debugger-python / VendorLib / vs-py-debugger / out / client / common / editor.js View on Github external
break;
            case dmp.DIFF_INSERT:
                if (edit === null) {
                    edit = new Edit(EditAction.Insert, start);
                }
                else if (edit.action === EditAction.Delete) {
                    edit.action = EditAction.Replace;
                }
                // insert and replace edits are all relative to the original state
                // of the document, so inserts should reset the current line/character
                // position to the start.
                line = start.line;
                character = start.character;
                edit.text += diffs[i][1];
                break;
            case dmp.DIFF_EQUAL:
                if (edit !== null) {
                    edits.push(edit);
                    edit = null;
                }
                break;
        }
    }
    if (edit !== null) {
        edits.push(edit);
    }
    return edits;
}
function getTempFileWithDocumentContents(document) {
github imnapo / react-native-cn-richtext-editor / src / CNTextInput.js View on Github external
this.textLength = 0;
    this.upComingStype = null;
    this.androidSelectionJump = 0;

    this.AvoidAndroidIssueWhenPressSpace = 0;
    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 freedomexio / rocketx-condenser / src / app / redux / TransactionSaga.js View on Github external
permlink = s;
        }

        // ensure permlink conforms to STEEMIT_MAX_PERMLINK_LENGTH
        if (permlink.length > 255) {
            permlink = permlink.substring(0, 255);
        }
    } else {
        permlink = Math.floor(Date.now() / 1000).toString(36);
    }

    return permlink;
}

import diff_match_patch from 'diff-match-patch';
const dmp = new diff_match_patch();

export function createPatch(text1, text2) {
    if (!text1 && text1 === '') return undefined;
    const patches = dmp.patch_make(text1, text2);
    const patch = dmp.patch_toText(patches);
    return patch;
}

function* error_custom_json({ operation: { id, required_posting_auths } }) {
    if (id === 'follow') {
        const follower = required_posting_auths[0];
        yield put(
            globalActions.update({
                key: ['follow', 'getFollowingAsync', follower, 'loading'],
                updater: () => null,
            })
github busyorg / busy / src / client / vendor / steemitHelpers.js View on Github external
import base58 from 'bs58';
import getSlug from 'speakingurl';
import secureRandom from 'secure-random';
import diff_match_patch from 'diff-match-patch';
import steemAPI from '../steemAPI';
import formatter from '../helpers/steemitFormatter';
import _ from 'lodash';
import {
  HF21_TIME,
  DEFAULT_CURATION_REWARD_PERCENT,
  HF21_CURATION_REWARD_PERCENT,
} from '../helpers/constants';

const dmp = new diff_match_patch();
/**
 * This function is extracted from steemit.com source code and does the same tasks with some slight-
 * adjustments to meet our needs. Refer to the main one in case of future problems:
 * https://github.com/steemit/steemit.com/blob/edac65e307bffc23f763ed91cebcb4499223b356/app/redux/TransactionSaga.js#L340
 *
 */
export const createCommentPermlink = (parentAuthor, parentPermlink) => {
  let permlink;

  // comments: re-parentauthor-parentpermlink-time
  const timeStr = new Date().toISOString().replace(/[^a-zA-Z0-9]+/g, '');
  const newParentPermlink = parentPermlink.replace(/(-\d{8}t\d{9}z)/g, '');
  permlink = `re-${parentAuthor}-${newParentPermlink}-${timeStr}`;

  if (permlink.length > 255) {
    // STEEMIT_MAX_PERMLINK_LENGTH

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