Comparing version 0.0.3 to 0.0.4
82
diff3.js
// Copyright (c) 2006, 2008 Tony Garnock-Jones <tonyg@lshift.net> | ||
// Copyright (c) 2006, 2008 LShift Ltd. <query@lshift.net> | ||
// Copyright (c) 2016, 2022 Axosoft, LLC (www.gitkraken.com) | ||
// | ||
@@ -26,79 +27,2 @@ // Permission is hereby granted, free of charge, to any person | ||
function longestCommonSubsequence(file1, file2) { | ||
var diff = new onp(file1, file2); | ||
diff.compose(); | ||
var ses = diff.getses(); | ||
var root; | ||
var prev; | ||
var file1RevIdx = file1.length - 1, | ||
file2RevIdx = file2.length - 1; | ||
for (var i = ses.length - 1; i >= 0; --i) { | ||
if (ses[i].t === diff.SES_COMMON) { | ||
if (prev) { | ||
prev.chain = { | ||
file1index: file1RevIdx, | ||
file2index: file2RevIdx, | ||
chain: null | ||
}; | ||
prev = prev.chain; | ||
} else { | ||
root = { | ||
file1index: file1RevIdx, | ||
file2index: file2RevIdx, | ||
chain: null | ||
}; | ||
prev = root; | ||
} | ||
file1RevIdx--; | ||
file2RevIdx--; | ||
} else if (ses[i].t === diff.SES_DELETE) { | ||
file1RevIdx--; | ||
} else if (ses[i].t === diff.SES_ADD) { | ||
file2RevIdx--; | ||
} | ||
} | ||
var tail = { | ||
file1index: -1, | ||
file2index: -1, | ||
chain: null | ||
}; | ||
if (!prev) { | ||
return tail; | ||
} | ||
prev.chain = tail; | ||
return root; | ||
} | ||
function diffIndices(file1, file2) { | ||
// We apply the LCS to give a simple representation of the | ||
// offsets and lengths of mismatched chunks in the input | ||
// files. This is used by diff3_merge_indices below. | ||
var result = []; | ||
var tail1 = file1.length; | ||
var tail2 = file2.length; | ||
for (var candidate = longestCommonSubsequence(file1, file2); candidate !== null; candidate = candidate.chain) { | ||
var mismatchLength1 = tail1 - candidate.file1index - 1; | ||
var mismatchLength2 = tail2 - candidate.file2index - 1; | ||
tail1 = candidate.file1index; | ||
tail2 = candidate.file2index; | ||
if (mismatchLength1 || mismatchLength2) { | ||
result.push({ | ||
file1: [tail1 + 1, mismatchLength1], | ||
file2: [tail2 + 1, mismatchLength2] | ||
}); | ||
} | ||
} | ||
result.reverse(); | ||
return result; | ||
} | ||
function diff3MergeIndices(a, o, b) { | ||
@@ -118,4 +42,4 @@ // Given three files, A, O, and B, where both A and B are | ||
var m1 = diffIndices(o, a); | ||
var m2 = diffIndices(o, b); | ||
var m1 = new onp(o, a).compose(); | ||
var m2 = new onp(o, b).compose(); | ||
@@ -122,0 +46,0 @@ var hunks = []; |
124
onp.js
@@ -5,2 +5,3 @@ /* | ||
* Copyright (c) 2013 Tatsuhiko Kubo <cubicdaiya@gmail.com> | ||
* Copyright (c) 2016, 2022 Axosoft, LLC (www.gitkraken.com) | ||
* | ||
@@ -36,11 +37,5 @@ * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
reverse = false, | ||
ed = null, | ||
offset = m + 1, | ||
path = [], | ||
pathposi = [], | ||
ses = [], | ||
lcs = "", | ||
SES_DELETE = -1, | ||
SES_COMMON = 0, | ||
SES_ADD = 1; | ||
pathposi = []; | ||
@@ -63,19 +58,14 @@ var tmp1, | ||
var P = function (x, y, k) { | ||
var P = function (startX, startY, endX, endY, r) { | ||
return { | ||
'x' : x, | ||
'y' : y, | ||
'k' : k, | ||
startX, | ||
startY, | ||
endX, | ||
endY, | ||
r | ||
}; | ||
}; | ||
var seselem = function (elem, t) { | ||
return { | ||
'elem' : elem, | ||
't' : t, | ||
}; | ||
}; | ||
var snake = function (k, p, pp) { | ||
var r, x, y; | ||
var r, x, y, startX, startY; | ||
if (p > pp) { | ||
@@ -87,4 +77,4 @@ r = path[k-1+offset]; | ||
y = Math.max(p, pp); | ||
x = y - k; | ||
startY = y = Math.max(p, pp); | ||
startX = x = y - k; | ||
while (x < m && y < n && a[x] === b[y]) { | ||
@@ -95,58 +85,16 @@ ++x; | ||
path[k+offset] = pathposi.length; | ||
pathposi[pathposi.length] = new P(x, y, r); | ||
if (startX == x && startY == y) { | ||
path[k+offset] = r; | ||
} else { | ||
path[k+offset] = pathposi.length; | ||
pathposi[pathposi.length] = new P(startX, startY, x, y, r); | ||
} | ||
return y; | ||
}; | ||
var recordseq = function (epc) { | ||
var x_idx, y_idx, px_idx, py_idx, i; | ||
x_idx = y_idx = 1; | ||
px_idx = py_idx = 0; | ||
for (i=epc.length-1;i>=0;--i) { | ||
while(px_idx < epc[i].x || py_idx < epc[i].y) { | ||
if (epc[i].y - epc[i].x > py_idx - px_idx) { | ||
if (reverse) { | ||
ses[ses.length] = new seselem(b[py_idx], SES_DELETE); | ||
} else { | ||
ses[ses.length] = new seselem(b[py_idx], SES_ADD); | ||
} | ||
++y_idx; | ||
++py_idx; | ||
} else if (epc[i].y - epc[i].x < py_idx - px_idx) { | ||
if (reverse) { | ||
ses[ses.length] = new seselem(a[px_idx], SES_ADD); | ||
} else { | ||
ses[ses.length] = new seselem(a[px_idx], SES_DELETE); | ||
} | ||
++x_idx; | ||
++px_idx; | ||
} else { | ||
ses[ses.length] = new seselem(a[px_idx], SES_COMMON); | ||
lcs += a[px_idx]; | ||
++x_idx; | ||
++y_idx; | ||
++px_idx; | ||
++py_idx; | ||
} | ||
} | ||
} | ||
}; | ||
init(); | ||
return { | ||
SES_DELETE : -1, | ||
SES_COMMON : 0, | ||
SES_ADD : 1, | ||
editdistance : function () { | ||
return ed; | ||
}, | ||
getlcs : function () { | ||
return lcs; | ||
}, | ||
getses : function () { | ||
return ses; | ||
}, | ||
compose : function () { | ||
var delta, size, fp, p, r, epc, i, k; | ||
var delta, size, fp, p, r, i, k, lastStartX, lastStartY, result; | ||
delta = n - m; | ||
@@ -174,11 +122,37 @@ size = m + n + 3; | ||
r = path[delta+offset]; | ||
lastStartX = m; | ||
lastStartY = n; | ||
result = []; | ||
while (r !== -1) { | ||
let elem = pathposi[r]; | ||
if (m != elem.endX || n != elem.endY) { | ||
result.push({ | ||
file1: [ | ||
reverse ? elem.endY : elem.endX, | ||
reverse ? lastStartY - elem.endY : lastStartX - elem.endX | ||
], | ||
file2: [ | ||
reverse ? elem.endX : elem.endY, | ||
reverse ? lastStartX - elem.endX : lastStartY - elem.endY | ||
] | ||
}); | ||
} | ||
epc = []; | ||
while (r !== -1) { | ||
epc[epc.length] = new P(pathposi[r].x, pathposi[r].y, null); | ||
r = pathposi[r].k; | ||
lastStartX = elem.startX; | ||
lastStartY = elem.startY; | ||
r = pathposi[r].r; | ||
} | ||
recordseq(epc); | ||
if (lastStartX != 0 || lastStartY != 0) { | ||
result.push({ | ||
file1: [0, reverse ? lastStartY : lastStartX], | ||
file2: [0, reverse ? lastStartX : lastStartY] | ||
}) | ||
} | ||
result.reverse(); | ||
return result; | ||
} | ||
}; | ||
}; |
{ | ||
"name": "diff3", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A diff3 engine for nodejs", | ||
@@ -17,3 +17,3 @@ "main": "diff3.js", | ||
], | ||
"author": "Tyler Wanek", | ||
"author": "Tyler Wanek & Jacob Watson", | ||
"license": "MIT", | ||
@@ -20,0 +20,0 @@ "bugs": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11902
313
1