← 全部工具

disparity

热度 65 更新于 开发与构建

Colorized string diff ideal for text/code that spans through multiple lines

npmauto-collected

安装

npm
npm install -g disparity

通过 npm 安装。

disparity

Colorized string diff ideal for text/code that spans through multiple lines.

This is basically just a wrapper around diff and ansi-styles + line numbers and omitting lines that don't have changes and/or that wouldn't help user identify the diff "context".

We also replace some invisible chars to make it easier to understand what really changed from one file to another:

  • \r becomes <CR
  • \n becomes <LF
  • \t becomes <tab

Created mainly to be used by esformatter and other tools that might need to display a nice looking diff of source files.

API

var disparity = require('disparity');

chars(oldStr, newStr[, opts]):String

Diffs two blocks of text, comparing character by character and returns a String with ansi color codes.

var diff = disparity.chars(file1, file2);
console.log(diff);

Will return an empty string if oldStr === newStr;

// default options
var opts = {
  // how many lines to display before/after a line that contains diffs
  context: 3,
  // file paths displayed just before the diff
  paths: [disparity.removed, disparity.added]
};

unified(oldStr, newStr[, opts]):String

Returns ansi colorized unified diff.

Will return an empty string if oldStr === newStr;

var diff = disparity.unified(file1, file2, {
  paths: ['test/file1.js', 'test/file2.js']
});
console.log(diff);

unifiedNoColor(oldStr, newStr[, opts]):String