Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sanity/diff-match-patch

Package Overview
Dependencies
Maintainers
36
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sanity/diff-match-patch - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1

16

dist/index.d.ts

@@ -14,14 +14,2 @@ /**

/**
* Takes a `patches` array as produced by diff-match-patch and adjusts the
* `start1` and `start2` properties so that they refer to UTF-8 index instead
* of a UCS-2 index.
*
* @param patches - The patches to adjust
* @param base - The base string to use for counting bytes
* @returns A new array of patches with adjusted indicies
* @beta
*/
export declare function adjustIndiciesToUtf8(patches: Patch[], base: string): Patch[]
/**
* Merge a set of patches onto the text. Returns patched text, as well as a

@@ -221,2 +209,4 @@ * list of true/false values indicating which patches were applied.

start2: number
utf8Start1: number
utf8Start2: number
length1: number

@@ -238,3 +228,3 @@ length2: number

/**
* Create a textual representation of a
* Create a textual representation of a patch.
*

@@ -241,0 +231,0 @@ * @param patch - Patch to stringify

109

dist/index.js

@@ -956,2 +956,4 @@ function isHighSurrogate(char) {

start2,
utf8Start1: start1,
utf8Start2: start2,
length1: 0,

@@ -992,35 +994,2 @@ length2: 0,

}
function adjustIndiciesToUtf8(patches, base) {
let byteOffset = 0;
let idx = 0;
function advanceTo(target) {
for (; idx < target; idx++) {
const codePoint = base.codePointAt(idx);
if (typeof codePoint === "undefined") {
throw new Error("Failed to get codepoint");
}
byteOffset += utf8len(codePoint);
if (codePoint > 65535) {
idx++;
}
}
if (idx !== target) {
throw new Error("Failed to determine byte offset");
}
return byteOffset;
}
const adjusted = [];
for (const patch of patches) {
adjusted.push({
diffs: patch.diffs.map(diff => cloneDiff(diff)),
start1: advanceTo(patch.start1),
start2: advanceTo(patch.start2),
length1: patch.length1,
length2: patch.length2,
utf8Length1: patch.utf8Length1,
utf8Length2: patch.utf8Length2
});
}
return adjusted;
}
function adjustIndiciesToUcs2(patches, base) {

@@ -1033,3 +1002,3 @@ let byteOffset = 0;

if (typeof codePoint === "undefined") {
throw new Error("Failed to get codepoint");
return idx;
}

@@ -1054,2 +1023,4 @@ byteOffset += utf8len(codePoint);

start2: advanceTo(patch.start2),
utf8Start1: patch.utf8Start1,
utf8Start2: patch.utf8Start2,
length1: patch.length1,

@@ -1109,7 +1080,9 @@ length2: patch.length2,

let charCount2 = 0;
let utf8Count1 = 0;
let utf8Count2 = 0;
let prepatchText = textA;
let postpatchText = textA;
for (let x = 0; x < diffs.length; x++) {
const diffType = diffs[x][0];
const diffText = diffs[x][1];
const currentDiff = diffs[x];
const [diffType, diffText] = currentDiff;
const diffTextLength = diffText.length;

@@ -1120,6 +1093,8 @@ const diffByteLength = countUtf8Bytes(diffText);

patch.start2 = charCount2;
patch.utf8Start1 = utf8Count1;
patch.utf8Start2 = utf8Count2;
}
switch (diffType) {
case DIFF_INSERT:
patch.diffs[patchDiffLength++] = diffs[x];
patch.diffs[patchDiffLength++] = currentDiff;
patch.length2 += diffTextLength;

@@ -1132,3 +1107,3 @@ patch.utf8Length2 += diffByteLength;

patch.utf8Length1 += diffByteLength;
patch.diffs[patchDiffLength++] = diffs[x];
patch.diffs[patchDiffLength++] = currentDiff;
postpatchText = postpatchText.substring(0, charCount2) + postpatchText.substring(charCount2 + diffTextLength);

@@ -1138,3 +1113,3 @@ break;

if (diffTextLength <= 2 * options.margin && patchDiffLength && diffs.length !== x + 1) {
patch.diffs[patchDiffLength++] = diffs[x];
patch.diffs[patchDiffLength++] = currentDiff;
patch.length1 += diffTextLength;

@@ -1152,2 +1127,3 @@ patch.length2 += diffTextLength;

charCount1 = charCount2;
utf8Count1 = utf8Count2;
}

@@ -1161,5 +1137,7 @@ }

charCount1 += diffTextLength;
utf8Count1 += diffByteLength;
}
if (diffType !== DIFF_DELETE) {
charCount2 += diffTextLength;
utf8Count2 += diffByteLength;
}

@@ -1171,3 +1149,3 @@ }

}
return adjustIndiciesToUtf8(patches, textA);
return patches;
}

@@ -1193,2 +1171,4 @@ function addContext(patch, text, opts) {

}
const prefixLength = prefix.length;
const prefixUtf8Length = countUtf8Bytes(prefix);
let suffixEnd = patch.start2 + patch.length1 + padding;

@@ -1202,8 +1182,12 @@ if (suffixEnd < text.length && isLowSurrogate(text[suffixEnd])) {

}
patch.start1 -= prefix.length;
patch.start2 -= prefix.length;
patch.length1 += prefix.length + suffix.length;
patch.length2 += prefix.length + suffix.length;
patch.utf8Length1 += prefix.length + suffix.length;
patch.utf8Length2 += prefix.length + suffix.length;
const suffixLength = suffix.length;
const suffixUtf8Length = countUtf8Bytes(suffix);
patch.start1 -= prefixLength;
patch.start2 -= prefixLength;
patch.utf8Start1 -= prefixUtf8Length;
patch.utf8Start2 -= prefixUtf8Length;
patch.length1 += prefixLength + suffixLength;
patch.length2 += prefixLength + suffixLength;
patch.utf8Length1 += prefixUtf8Length + suffixUtf8Length;
patch.utf8Length2 += prefixUtf8Length + suffixUtf8Length;
}

@@ -1270,2 +1254,4 @@ function levenshtein(diffs) {

p.start2 += paddingLength;
p.utf8Start1 += paddingLength;
p.utf8Start2 += paddingLength;
}

@@ -1278,2 +1264,4 @@ let patch = patches[0];

patch.start2 -= paddingLength;
patch.utf8Start1 -= paddingLength;
patch.utf8Start2 -= paddingLength;
patch.length1 += paddingLength;

@@ -1284,6 +1272,9 @@ patch.length2 += paddingLength;

} else if (paddingLength > diffs[0][1].length) {
const extraLength = paddingLength - diffs[0][1].length;
diffs[0][1] = nullPadding.substring(diffs[0][1].length) + diffs[0][1];
const firstDiffLength = diffs[0][1].length;
const extraLength = paddingLength - firstDiffLength;
diffs[0][1] = nullPadding.substring(firstDiffLength) + diffs[0][1];
patch.start1 -= extraLength;
patch.start2 -= extraLength;
patch.utf8Start1 -= extraLength;
patch.utf8Start2 -= extraLength;
patch.length1 += extraLength;

@@ -1482,4 +1473,4 @@ patch.length2 += extraLength;

utf8Length2,
start1,
start2,
utf8Start1,
utf8Start2,
diffs

@@ -1489,15 +1480,15 @@ } = patch;

if (utf8Length1 === 0) {
coords1 = "".concat(start1, ",0");
coords1 = "".concat(utf8Start1, ",0");
} else if (utf8Length1 === 1) {
coords1 = "".concat(start1 + 1);
coords1 = "".concat(utf8Start1 + 1);
} else {
coords1 = "".concat(start1 + 1, ",").concat(utf8Length1);
coords1 = "".concat(utf8Start1 + 1, ",").concat(utf8Length1);
}
let coords2;
if (utf8Length2 === 0) {
coords2 = "".concat(start2, ",0");
coords2 = "".concat(utf8Start2, ",0");
} else if (utf8Length2 === 1) {
coords2 = "".concat(start2 + 1);
coords2 = "".concat(utf8Start2 + 1);
} else {
coords2 = "".concat(start2 + 1, ",").concat(utf8Length2);
coords2 = "".concat(utf8Start2 + 1, ",").concat(utf8Length2);
}

@@ -1541,2 +1532,3 @@ const text = ["@@ -".concat(coords1, " +").concat(coords2, " @@\n")];

patch.start1--;
patch.utf8Start1--;
patch.length1 = 1;

@@ -1549,2 +1541,3 @@ patch.utf8Length1 = 1;

patch.start1--;
patch.utf8Start1--;
patch.utf8Length1 = toInt(m[2]);

@@ -1555,2 +1548,3 @@ patch.length1 = patch.utf8Length1;

patch.start2--;
patch.utf8Start2--;
patch.length2 = 1;

@@ -1563,2 +1557,3 @@ patch.utf8Length2 = 1;

patch.start2--;
patch.utf8Start2--;
patch.utf8Length2 = toInt(m[4]);

@@ -1606,3 +1601,3 @@ patch.length2 = patch.utf8Length2;

}
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, adjustIndiciesToUcs2, adjustIndiciesToUtf8, apply as applyPatches, cleanupEfficiency, cleanupSemantic, diff as makeDiff, make as makePatches, match, parse as parsePatch, stringifyPatch, stringify as stringifyPatches };
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, adjustIndiciesToUcs2, apply as applyPatches, cleanupEfficiency, cleanupSemantic, diff as makeDiff, make as makePatches, match, parse as parsePatch, stringifyPatch, stringify as stringifyPatches };
//# sourceMappingURL=index.js.map
{
"name": "@sanity/diff-match-patch",
"version": "3.0.0",
"version": "3.0.1",
"description": "Robust diff, match and patch algorithms to perform operations required for synchronizing plain text",

@@ -5,0 +5,0 @@ "sideEffects": false,

@@ -23,3 +23,3 @@ // Diff

// UTF-8 / UCS-2 utils (beta)
export {adjustIndiciesToUcs2, adjustIndiciesToUtf8} from './utils/utf8Indices.js'
// UCS-2 utils (beta)
export {adjustIndiciesToUcs2} from './utils/utf8Indices.js'

@@ -25,2 +25,4 @@ import {DIFF_EQUAL} from '../diff/diff.js'

p.start2 += paddingLength
p.utf8Start1 += paddingLength
p.utf8Start2 += paddingLength
}

@@ -36,2 +38,4 @@

patch.start2 -= paddingLength // Should be 0.
patch.utf8Start1 -= paddingLength // Should be 0.
patch.utf8Start2 -= paddingLength // Should be 0.
patch.length1 += paddingLength

@@ -43,6 +47,9 @@ patch.length2 += paddingLength

// Grow first equality.
const extraLength = paddingLength - diffs[0][1].length
diffs[0][1] = nullPadding.substring(diffs[0][1].length) + diffs[0][1]
const firstDiffLength = diffs[0][1].length
const extraLength = paddingLength - firstDiffLength
diffs[0][1] = nullPadding.substring(firstDiffLength) + diffs[0][1]
patch.start1 -= extraLength
patch.start2 -= extraLength
patch.utf8Start1 -= extraLength
patch.utf8Start2 -= extraLength
patch.length1 += extraLength

@@ -49,0 +56,0 @@ patch.length2 += extraLength

@@ -110,3 +110,2 @@ /* eslint-disable max-depth */

delta -= parsed[x].length2 - parsed[x].length1
// @todo byte count?
} else {

@@ -113,0 +112,0 @@ // Found a match. :)

@@ -10,4 +10,8 @@ import type {Diff} from '../diff/diff.js'

diffs: Diff[]
start1: number
start2: number
utf8Start1: number
utf8Start2: number
length1: number

@@ -52,4 +56,8 @@ length2: number

diffs: [],
start1,
start2,
utf8Start1: start1,
utf8Start2: start2,
length1: 0,

@@ -56,0 +64,0 @@ length2: 0,

@@ -5,3 +5,3 @@ import {cleanupSemantic, cleanupEfficiency} from '../diff/cleanup.js'

import {isLowSurrogate} from '../utils/surrogatePairs.js'
import {adjustIndiciesToUtf8, countUtf8Bytes} from '../utils/utf8Indices.js'
import {countUtf8Bytes} from '../utils/utf8Indices.js'
import {MAX_BITS} from './constants.js'

@@ -98,2 +98,3 @@ import {createPatchObject, type Patch} from './createPatchObject.js'

const patches = []
let patch = createPatchObject(0, 0)

@@ -103,2 +104,5 @@ let patchDiffLength = 0 // Keeping our own length var is faster in JS.

let charCount2 = 0 // Number of characters into the textB string.
let utf8Count1 = 0 // Number of utf-8 bytes into the textA string.
let utf8Count2 = 0 // Number of utf-8 bytes into the textB string.
// Start with textA (prepatchText) and apply the diffs until we arrive at

@@ -109,5 +113,6 @@ // textB (postpatchText). We recreate the patches one by one to determine

let postpatchText = textA
for (let x = 0; x < diffs.length; x++) {
const diffType = diffs[x][0]
const diffText = diffs[x][1]
const currentDiff = diffs[x]
const [diffType, diffText] = currentDiff
const diffTextLength = diffText.length

@@ -120,2 +125,4 @@ const diffByteLength = countUtf8Bytes(diffText)

patch.start2 = charCount2
patch.utf8Start1 = utf8Count1
patch.utf8Start2 = utf8Count2
}

@@ -125,3 +132,3 @@

case DIFF_INSERT:
patch.diffs[patchDiffLength++] = diffs[x]
patch.diffs[patchDiffLength++] = currentDiff
patch.length2 += diffTextLength

@@ -135,3 +142,3 @@ patch.utf8Length2 += diffByteLength

patch.utf8Length1 += diffByteLength
patch.diffs[patchDiffLength++] = diffs[x]
patch.diffs[patchDiffLength++] = currentDiff
postpatchText =

@@ -144,3 +151,3 @@ postpatchText.substring(0, charCount2) +

// Small equality inside a patch.
patch.diffs[patchDiffLength++] = diffs[x]
patch.diffs[patchDiffLength++] = currentDiff
patch.length1 += diffTextLength

@@ -163,2 +170,3 @@ patch.length2 += diffTextLength

charCount1 = charCount2
utf8Count1 = utf8Count2
}

@@ -174,7 +182,10 @@ }

charCount1 += diffTextLength
utf8Count1 += diffByteLength
}
if (diffType !== DIFF_DELETE) {
charCount2 += diffTextLength
utf8Count2 += diffByteLength
}
}
// Pick up the leftover patch if not empty.

@@ -186,3 +197,3 @@ if (patchDiffLength) {

return adjustIndiciesToUtf8(patches, textA)
return patches
}

@@ -231,2 +242,5 @@

const prefixLength = prefix.length
const prefixUtf8Length = countUtf8Bytes(prefix)
// Add the suffix.

@@ -245,11 +259,16 @@

const suffixLength = suffix.length
const suffixUtf8Length = countUtf8Bytes(suffix)
// Roll back the start points.
patch.start1 -= prefix.length
patch.start2 -= prefix.length
patch.start1 -= prefixLength
patch.start2 -= prefixLength
patch.utf8Start1 -= prefixUtf8Length
patch.utf8Start2 -= prefixUtf8Length
// Extend the lengths.
patch.length1 += prefix.length + suffix.length
patch.length2 += prefix.length + suffix.length
patch.utf8Length1 += prefix.length + suffix.length
patch.utf8Length2 += prefix.length + suffix.length
patch.length1 += prefixLength + suffixLength
patch.length2 += prefixLength + suffixLength
patch.utf8Length1 += prefixUtf8Length + suffixUtf8Length
patch.utf8Length2 += prefixUtf8Length + suffixUtf8Length
}

@@ -33,2 +33,3 @@ import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT} from '../diff/diff.js'

patch.start1--
patch.utf8Start1--
patch.length1 = 1

@@ -41,2 +42,3 @@ patch.utf8Length1 = 1

patch.start1--
patch.utf8Start1--
// The patch itself will contain the UTF-8 length

@@ -50,2 +52,3 @@ patch.utf8Length1 = toInt(m[2])

patch.start2--
patch.utf8Start2--
patch.length2 = 1

@@ -58,2 +61,3 @@ patch.utf8Length2 = 1

patch.start2--
patch.utf8Start2--
// The patch itself will contain the UTF-8 length

@@ -60,0 +64,0 @@ patch.utf8Length2 = toInt(m[4])

@@ -16,3 +16,3 @@ import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT} from '../diff/diff.js'

/**
* Create a textual representation of a
* Create a textual representation of a patch.
*

@@ -24,11 +24,11 @@ * @param patch - Patch to stringify

export function stringifyPatch(patch: Patch): string {
const {utf8Length1, utf8Length2, start1, start2, diffs} = patch
const {utf8Length1, utf8Length2, utf8Start1, utf8Start2, diffs} = patch
let coords1: string
if (utf8Length1 === 0) {
coords1 = `${start1},0`
coords1 = `${utf8Start1},0`
} else if (utf8Length1 === 1) {
coords1 = `${start1 + 1}`
coords1 = `${utf8Start1 + 1}`
} else {
coords1 = `${start1 + 1},${utf8Length1}`
coords1 = `${utf8Start1 + 1},${utf8Length1}`
}

@@ -38,7 +38,7 @@

if (utf8Length2 === 0) {
coords2 = `${start2},0`
coords2 = `${utf8Start2},0`
} else if (utf8Length2 === 1) {
coords2 = `${start2 + 1}`
coords2 = `${utf8Start2 + 1}`
} else {
coords2 = `${start2 + 1},${utf8Length2}`
coords2 = `${utf8Start2 + 1},${utf8Length2}`
}

@@ -45,0 +45,0 @@

@@ -26,54 +26,2 @@ import {cloneDiff} from '../diff/clone.js'

* Takes a `patches` array as produced by diff-match-patch and adjusts the
* `start1` and `start2` properties so that they refer to UTF-8 index instead
* of a UCS-2 index.
*
* @param patches - The patches to adjust
* @param base - The base string to use for counting bytes
* @returns A new array of patches with adjusted indicies
* @beta
*/
export function adjustIndiciesToUtf8(patches: Patch[], base: string): Patch[] {
let byteOffset = 0
let idx = 0 // index into the input.
function advanceTo(target: number) {
for (; idx < target; idx++) {
const codePoint = base.codePointAt(idx)
if (typeof codePoint === 'undefined') {
throw new Error('Failed to get codepoint')
}
byteOffset += utf8len(codePoint)
// This is encoded as a surrogate pair.
if (codePoint > 0xffff) {
idx++
}
}
if (idx !== target) {
throw new Error('Failed to determine byte offset')
}
return byteOffset
}
const adjusted: Patch[] = []
for (const patch of patches) {
adjusted.push({
diffs: patch.diffs.map((diff) => cloneDiff(diff)),
start1: advanceTo(patch.start1),
start2: advanceTo(patch.start2),
length1: patch.length1,
length2: patch.length2,
utf8Length1: patch.utf8Length1,
utf8Length2: patch.utf8Length2,
})
}
return adjusted
}
/**
* Takes a `patches` array as produced by diff-match-patch and adjusts the
* `start1` and `start2` properties so that they refer to UCS-2 index instead

@@ -95,3 +43,5 @@ * of a UTF-8 index.

if (typeof codePoint === 'undefined') {
throw new Error('Failed to get codepoint')
// Reached the end of the base string - the indicies won't be correct,
// but we also cannot advance any further to find a closer index.
return idx
}

@@ -122,2 +72,4 @@

start2: advanceTo(patch.start2),
utf8Start1: patch.utf8Start1,
utf8Start2: patch.utf8Start2,
length1: patch.length1,

@@ -124,0 +76,0 @@ length2: patch.length2,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc