Socket
Socket
Sign inDemoInstall

diff

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

diff - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

70

diff.js

@@ -157,3 +157,3 @@ /* See license.txt for terms of usage */

WordDiff.tokenize = function(value) {
return removeEmpty(value.split(/(\s+|\b)/g));
return removeEmpty(value.split(/(\s+|\b)/));
};

@@ -163,3 +163,3 @@

CssDiff.tokenize = function(value) {
return removeEmpty(value.split(/([{}:;,]|\s+)/g));
return removeEmpty(value.split(/([{}:;,]|\s+)/));
};

@@ -169,11 +169,3 @@

LineDiff.tokenize = function(value) {
var values = value.split(/\n/g),
ret = [];
for (var i = 0; i < values.length-1; i++) {
ret.push(values[i] + "\n");
}
if (values.length) {
ret.push(values[values.length-1]);
}
return ret;
return value.split(/^/m);
};

@@ -190,11 +182,28 @@

var ret = [];
ret.push("Index: " + fileName);
ret.push("===================================================================");
ret.push("--- " + fileName + "\t" + oldHeader);
ret.push("+++ " + fileName + "\t" + newHeader);
ret.push("--- " + fileName + (typeof oldHeader === "undefined" ? "" : "\t" + oldHeader));
ret.push("+++ " + fileName + (typeof newHeader === "undefined" ? "" : "\t" + newHeader));
var diff = LineDiff.diff(oldStr, newStr);
if (!diff[diff.length-1].value) {
diff.pop(); // Remove trailing newline add
}
diff.push({value: "", lines: []}); // Append an empty value to make cleanup easier
function contextLines(lines) {
return lines.map(function(entry) { return ' ' + entry; });
}
function eofNL(curRange, i, current) {
var last = diff[diff.length-2],
isLast = i === diff.length-2,
isLastOfType = i === diff.length-3 && (current.added === !last.added || current.removed === !last.removed);
// Figure out if this is the last line for the given file and missing NL
if (!/\n$/.test(current.value) && (isLast || isLastOfType)) {
curRange.push('\\ No newline at end of file');
}
}
var oldRangeStart = 0, newRangeStart = 0, curRange = [],

@@ -206,3 +215,3 @@ oldLine = 1, newLine = 1;

current.lines = lines;
if (current.added || current.removed) {

@@ -215,8 +224,10 @@ if (!oldRangeStart) {

if (prev) {
curRange.push.apply(curRange, prev.lines.slice(-4).map(function(entry) { return " " + entry; }));
oldRangeStart -= 4;
newRangeStart -= 4;
curRange = contextLines(prev.lines.slice(-4));
oldRangeStart -= curRange.length;
newRangeStart -= curRange.length;
}
}
curRange.push.apply(curRange, lines.map(function(entry) { return (current.added?"+":"-") + entry; }));
eofNL(curRange, i, current);
if (current.added) {

@@ -229,5 +240,6 @@ newLine += lines.length;

if (oldRangeStart) {
if (lines.length <= 8 && i < diff.length-1) {
// Overlapping
curRange.push.apply(curRange, lines.map(function(entry) { return " " + entry; }));
// Close out any changes that have been output (or join overlapping)
if (lines.length <= 8 && i < diff.length-2) {
// Overlapping
curRange.push.apply(curRange, contextLines(lines));
} else {

@@ -241,3 +253,6 @@ // end the range and output

ret.push.apply(ret, curRange);
ret.push.apply(ret, lines.slice(0, contextSize).map(function(entry) { return " " + entry; }));
ret.push.apply(ret, contextLines(lines.slice(0, contextSize)));
if (lines.length <= 4) {
eofNL(ret, i, current);
}

@@ -251,7 +266,4 @@ oldRangeStart = 0; newRangeStart = 0; curRange = [];

}
if (diff.length > 1 && !/\n$/.test(diff[diff.length-2].value)) {
ret.push("\\ No newline at end of file\n");
}
return ret.join("\n");
return ret.join('\n') + '\n';
},

@@ -258,0 +270,0 @@

{
"name": "diff",
"version": "1.0.1",
"version": "1.0.2",
"description": "A javascript text diff implementation.",

@@ -27,3 +27,3 @@ "keywords": [

"engines": {
"node": ">=0.1.9"
"node": ">=0.3.1"
},

@@ -30,0 +30,0 @@ "main": "./diff",

const VERBOSE = false;
var assert = require('assert'),
diff = require('diff');
diff = require('../diff');

@@ -12,3 +12,3 @@ function log() {

diffResult = diff.diffWords("New Value", "New ValueMoreData");
assert.eql(
assert.equal(
"New <ins>ValueMoreData</ins><del>Value</del>",

@@ -19,3 +19,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New Value ", "New ValueMoreData ");
assert.eql(
assert.equal(
"New <ins>ValueMoreData</ins><del>Value</del> ",

@@ -29,3 +29,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New :Value:Test", "New ValueMoreData ");
assert.eql(
assert.equal(
"New <ins>ValueMoreData </ins><del>:Value:Test</del>",

@@ -35,3 +35,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New Value:Test", "New Value:MoreData ");
assert.eql(
assert.equal(
"New Value:<ins>MoreData </ins><del>Test</del>",

@@ -41,3 +41,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New Value-Test", "New Value:MoreData ");
assert.eql(
assert.equal(
"New Value<ins>:MoreData </ins><del>-Test</del>",

@@ -47,3 +47,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New Value", "New Value:MoreData ");
assert.eql(
assert.equal(
"New Value<ins>:MoreData </ins>",

@@ -57,3 +57,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New Value", "New Value");
assert.eql(
assert.equal(
"New Value",

@@ -63,3 +63,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New Value", "New Value");
assert.eql(
assert.equal(
"New Value",

@@ -69,3 +69,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("", "");
assert.eql(
assert.equal(
"",

@@ -79,4 +79,4 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("New Value", "");
assert.eql(1, diffResult.length, "Empty diff result length");
assert.eql(
assert.equal(1, diffResult.length, "Empty diff result length");
assert.equal(
"<del>New Value</del>",

@@ -86,3 +86,3 @@ diff.convertChangesToXML(diffResult),

diffResult = diff.diffWords("", "New Value");
assert.eql(
assert.equal(
"<ins>New Value</ins>",

@@ -94,7 +94,9 @@ diff.convertChangesToXML(diffResult),

// With without anchor (the Heckel algorithm error case)
diffResult = diff.diffWords("New Value New Value", "Value Value New New");
assert.eql(
"<ins>Value</ins><del>New</del> Value New <ins>New</ins><del>Value</del>",
diff.convertChangesToXML(diffResult),
"No anchor diffResult Value");
exports['No anchor'] = function() {
diffResult = diff.diffWords("New Value New Value", "Value Value New New");
assert.eql(
"<ins>Value</ins><del>New</del> Value New <ins>New</ins><del>Value</del>",
diff.convertChangesToXML(diffResult),
"No anchor diffResult Value");
};

@@ -106,3 +108,3 @@ // CSS Diff

".test2, #value2 .test {\nmargin-top:50px;\nmargin-right:-400px;\n}");
assert.eql(
assert.equal(
"<ins>.test2</ins><del>.test</del>,<del>#value</del> <ins>#value2 </ins>.test<ins> </ins>{<ins>\n"

@@ -120,3 +122,3 @@ + "margin-top</ins><del>margin-left</del>:50px;<ins>\n</ins>"

"line\nnew value\nline");
assert.eql(
assert.equal(
"line\n<ins>new value\n</ins><del>old value\n</del>line",

@@ -128,3 +130,3 @@ diff.convertChangesToXML(diffResult),

"line\nvalue\nline");
assert.eql(
assert.equal(
"line\nvalue\nline",

@@ -138,3 +140,3 @@ diff.convertChangesToXML(diffResult),

log("diffResult", diff.convertChangesToXML(diffResult));
assert.eql(
assert.equal(
"line\n<ins>value\n</ins><del>value \n</del>line",

@@ -145,2 +147,114 @@ diff.convertChangesToXML(diffResult),

// Patch creation with diff at EOF
exports['lastLineChanged'] = function() {
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,3 +1,4 @@\n'
+ ' line2\n'
+ ' line3\n'
+ '+line4\n'
+ ' line5\n',
diff.createPatch('test', 'line2\nline3\nline5\n', 'line2\nline3\nline4\nline5\n', 'header1', 'header2'));
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,3 +1,4 @@\n'
+ ' line2\n'
+ ' line3\n'
+ ' line4\n'
+ '+line5\n',
diff.createPatch('test', 'line2\nline3\nline4\n', 'line2\nline3\nline4\nline5\n', 'header1', 'header2'));
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,4 @@\n'
+ ' line1\n'
+ ' line2\n'
+ ' line3\n'
+ '+line44\n'
+ '-line4\n',
diff.createPatch('test', 'line1\nline2\nline3\nline4\n', 'line1\nline2\nline3\nline44\n', 'header1', 'header2'));
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,5 @@\n'
+ ' line1\n'
+ ' line2\n'
+ ' line3\n'
+ '+line44\n'
+ '+line5\n'
+ '-line4\n',
diff.createPatch('test', 'line1\nline2\nline3\nline4\n', 'line1\nline2\nline3\nline44\nline5\n', 'header1', 'header2'));
};
exports['EOFNL'] = function() {
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,4 @@\n'
+ ' line1\n'
+ ' line2\n'
+ ' line3\n'
+ '+line4\n'
+ '\\ No newline at end of file\n'
+ '-line4\n',
diff.createPatch('test', 'line1\nline2\nline3\nline4\n', 'line1\nline2\nline3\nline4', 'header1', 'header2'));
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,4 @@\n'
+ ' line1\n'
+ ' line2\n'
+ ' line3\n'
+ '+line4\n'
+ '-line4\n'
+ '\\ No newline at end of file\n',
diff.createPatch('test', 'line1\nline2\nline3\nline4', 'line1\nline2\nline3\nline4\n', 'header1', 'header2'));
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,4 @@\n'
+ '+line1\n'
+ '-line11\n'
+ ' line2\n'
+ ' line3\n'
+ ' line4\n'
+ '\\ No newline at end of file\n',
diff.createPatch('test', 'line11\nline2\nline3\nline4', 'line1\nline2\nline3\nline4', 'header1', 'header2'));
assert.eql(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,5 +1,5 @@\n'
+ '+line1\n'
+ '-line11\n'
+ ' line2\n'
+ ' line3\n'
+ ' line4\n'
+ ' line4\n',
diff.createPatch('test', 'line11\nline2\nline3\nline4\nline4\nline4\nline4', 'line1\nline2\nline3\nline4\nline4\nline4\nline4', 'header1', 'header2'));
};
exports['Large Test'] = function() {

@@ -374,4 +488,4 @@ var random = 42;

log("diffResult remove length: " + removeCount);
assert.eql(largeTest.replace(/s+/g, ""), removeChanges.join("").replace(/s+/g, ""), "New Diff results match");
assert.eql(largeNewValue.replace(/s+/g, ""), addChanges.join("").replace(/s+/g, ""), "Old Diff results match");
assert.equal(largeTest.replace(/s+/g, ""), removeChanges.join("").replace(/s+/g, ""), "New Diff results match");
assert.equal(largeNewValue.replace(/s+/g, ""), addChanges.join("").replace(/s+/g, ""), "Old Diff results match");
};

@@ -506,6 +620,5 @@

diffResult = diff.createPatch("testFileName", oldFile, newFile, "Old Header", "New Header");
assert.eql(
assert.equal(
expectedResult,
diffResult,
"Patch diffResult Value");
diffResult);

@@ -516,6 +629,5 @@ expectedResult =

+ "--- testFileName\tOld Header\n"
+ "+++ testFileName\tNew Header\n"
+ "\\ No newline at end of file\n";
+ "+++ testFileName\tNew Header\n";
diffResult = diff.createPatch("testFileName", oldFile, oldFile, "Old Header", "New Header");
assert.eql(
assert.equal(
expectedResult,

@@ -522,0 +634,0 @@ diffResult,

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