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

disparity

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

disparity - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

test/chars.html

7

CHANGELOG.md
# disparity changelog
## v1.3.0 (2015/04/01)
- add support for custom colors.
- update unified color scheme.
- simplify line splitting logic.
- improve way strings are colorized (avoids `\n` and `\r` chars).
## v1.2.0 (2015/04/01)

@@ -4,0 +11,0 @@

85

disparity.js

@@ -5,3 +5,2 @@ 'use strict';

var ansi = require('ansi-styles');
var hasAnsi = require('has-ansi');

@@ -15,2 +14,10 @@ // ---

exports.added = 'added';
exports.colors = {
charsRemoved: ansi.bgRed,
charsAdded: ansi.bgGreen,
removed: ansi.red,
added: ansi.green,
header: ansi.yellow,
section: ansi.magenta,
};

@@ -35,3 +42,4 @@ // ---

if (header == null) {
header = bgRed(exports.removed) + ' ' + bgGreen(exports.added) + '\n\n';
header = colorize(exports.removed, 'charsRemoved') + ' ' +
colorize(exports.added, 'charsAdded') + '\n\n';
}

@@ -42,15 +50,10 @@

var val = replaceInvisibleChars(c.value);
if (c.added) return bgGreen(val);
if (c.removed) return bgRed(val);
if (c.added) return colorize(val, 'charsAdded');
if (c.removed) return colorize(val, 'charsRemoved');
return val;
}).join('');
var endsWithLineBreak = (/\n$/m).test(diff);
var lines = diff.split('\n');
// this RegExp will include the '\n' char into the lines, easier to join()
var lines = diff.split(/^/m);
// if it ends with line break it would add an extra empty line at the end
if (endsWithLineBreak) {
lines.pop();
}
// add line numbers

@@ -64,34 +67,11 @@ var nChars = lines.length.toString().length;

var eof = endsWithLineBreak ? '\n' : '';
return header + lines.join('\n') + eof;
return header + lines.join('');
}
function bgGreen(str) {
return colorize(str, ansi.bgGreen);
function colorize(str, colorId) {
var color = exports.colors[colorId];
// avoid highlighting the "\n" (would highlight till the end of the line)
return str.replace(/[^\n\r]+/g, color.open + '$&' + color.close);
}
function bgRed(str) {
return colorize(str, ansi.bgRed);
}
function green(str) {
return colorize(str, ansi.green);
}
function red(str) {
return colorize(str, ansi.red);
}
function yellow(str) {
return colorize(str, ansi.yellow);
}
function colorize(str, color) {
// we need to split the lines to avoid highlighting the "\n" (would highlight
// till the end of the line)
return str.split('\n').map(function(s) {
return color.open + s + color.close;
}).join('\n');
}
function replaceInvisibleChars(str) {

@@ -113,3 +93,3 @@ return str

function hasDiff(line, i) {
if (diffMap[i] || hasAnsi(line)) {
if (diffMap[i] || hasCharDiff(line)) {
diffMap[i] = true;

@@ -146,2 +126,11 @@ return true;

function hasCharDiff(line) {
return line.indexOf(exports.colors.charsAdded.open) !== -1 ||
line.indexOf(exports.colors.charsRemoved.open) !== -1;
}
function escapeRegExp(str) {
return str.replace(/\W/g,'\\$&');
}
function unified(oldStr, newStr, filePathOld, filePathNew) {

@@ -152,8 +141,14 @@ if (newStr === oldStr) {

var changes = unifiedNoColor(oldStr, newStr, filePathOld, filePathNew)
.replace(/^\-.*/gm, red('$&'))
.replace(/^\+.*/gm, green('$&'))
.replace(/^@@.+/gm, yellow('$&'));
var changes = unifiedNoColor(oldStr, newStr, filePathOld, filePathNew);
// this RegExp will include all the `\n` chars into the lines, easier to join
var lines = changes.split(/^/m);
return changes;
// we avoid colorizing the line breaks
var start = colorize(lines.slice(0, 2).join(''), 'header');
var end = lines.slice(2).join('')
.replace(/^\-.*/gm, colorize('$&', 'removed'))
.replace(/^\+.*/gm, colorize('$&', 'added'))
.replace(/^@@.+@@/gm, colorize('$&', 'section'));
return start + end;
}

@@ -160,0 +155,0 @@

{
"name": "disparity",
"version": "1.2.0",
"version": "1.3.0",
"description": "Colorized string diff ideal for text/code that spans through multiple lines",

@@ -34,4 +34,3 @@ "main": "disparity.js",

"ansi-styles": "^2.0.1",
"diff": "^1.3.2",
"has-ansi": "^1.0.3"
"diff": "^1.3.2"
},

@@ -38,0 +37,0 @@ "jshintConfig": {

@@ -100,2 +100,23 @@ # disparity

### colors:Object
Object containing references to all the colors used by disparity.
If you want a different output than `ansi` you can replace the color values:
```js
// wrap blocks into custom tags
disparity.colors = {
// chars diff
charsRemoved: { open: '<bggreen>', close: '</bggreen>' },
charsAdded: { open: '<bgred>', close: '</bgred>' },
// unified diff
removed: { open: '<red>', close: '</red>' },
added: { open: '<green>', close: '</green>' },
header: { open: '<yellow>', close: '</yellow>' },
section: { open: '<magenta>', close: '</magenta>' }
};
```
## CLI

@@ -102,0 +123,0 @@

@@ -7,3 +7,3 @@ removed added

4 | function dolor(){<LF>
5 |   amet();<LF>
5 |  amet();<LF>
6 | }<LF>

@@ -10,0 +10,0 @@ 7 | <LF>

@@ -17,8 +17,8 @@ 'use strict';

// not using assert because it is easier to understand what is wrong
console.error('disparity.' + name + '() failure!');
console.error('=== expected result:');
console.error(expected);
console.error('=== actual result:');
console.error(diff);
process.exit(1);
process.stderr.write('disparity.' + name + '() failure!\n');
process.stderr.write('=== expected result:\n');
process.stderr.write(expected);
process.stderr.write('=== actual result:\n');
process.stderr.write(diff);
throw new Error('assertion error');
}

@@ -59,2 +59,29 @@ }

// custom colors
// =============
var _oldColors = disparity.colors;
// wrap blocks into custom tags
disparity.colors = {
// chars diff
charsRemoved: { open: '<bggreen>', close: '</bggreen>' },
charsAdded: { open: '<bgred>', close: '</bgred>' },
// unified diff
removed: { open: '<red>', close: '</red>' },
added: { open: '<green>', close: '</green>' },
header: { open: '<yellow>', close: '</yellow>' },
section: { open: '<magenta>', close: '</magenta>' }
};
diff = disparity.chars(file1, file2);
expected = readFile('chars.html');
compare(diff, expected, 'chars.html');
diff = disparity.unified(file1, file2, 'test/file1.js', 'test/file2.js');
expected = readFile('unified.html');
compare(diff, expected, 'unified.html');
disparity.colors = _oldColors;
// cli.parse

@@ -61,0 +88,0 @@ // =========

@@ -1,4 +0,4 @@

--- test/file1.js removed
+++ test/file2.js added
@@ -1,9 +1,8 @@
--- test/file1.js removed
+++ test/file2.js added
@@ -1,9 +1,8 @@
var foo = "bar";

@@ -14,3 +14,3 @@

var a = function() {
@@ -15,7 +14,7 @@
@@ -15,7 +14,7 @@
thisWontShowOnDiff();

@@ -17,0 +17,0 @@ thisShouldBeFirstLineOfLine19Diff();

@@ -1,4 +0,4 @@

--- removed
+++ added
@@ -1,9 +1,8 @@
--- removed
+++ added
@@ -1,9 +1,8 @@
var foo = "bar";

@@ -14,3 +14,3 @@

var a = function() {
@@ -15,7 +14,7 @@
@@ -15,7 +14,7 @@
thisWontShowOnDiff();

@@ -17,0 +17,0 @@ thisShouldBeFirstLineOfLine19Diff();

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