-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.js
45 lines (38 loc) · 1.19 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { unifiedDiff } from 'difflib';
import { format } from 'util';
import { compose } from 'recompose';
export const defaultOptions = {
originalFileName: 'Unknown-File-Name',
updatedFileName: 'Unknown-File-Name',
inputFormat: 'diff',
outputFormat: 'side-by-side',
showFiles: false,
matching: 'none',
matchWordsThreshold: 0.25,
matchingMaxComparisons: 2500,
maxLineSizeInBlockForComparison: 200,
maxLineLengthHighlight: 10000,
renderNothingWhenEmpty: false
};
const compare = ({ past, current, options }) => {
const nextOptions = { ...defaultOptions, ...options };
const pastArray = past.split(/\r|\n|\r\n/);
const currentArray = current.split(/\r|\n|\r\n/);
const diffArray = unifiedDiff(pastArray, currentArray, {
fromfile: nextOptions.originalFileName,
tofile: nextOptions.updatedFileName
});
const diffString = format(
'diff --git %s %s\n%s',
nextOptions.originalFileName,
nextOptions.updatedFileName,
diffArray.join('\n')
);
return {
diffString,
options: nextOptions
};
};
export const genPrettyHtml = ({ diffString, options }) =>
require('diff2html').html(diffString, options);
export default compose(genPrettyHtml, compare);