forked from component/patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (62 loc) · 1.56 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var diff_match_patch = require('./diff_match_patch');
/**
* Creates a patch.
* @param {String} old (optional)
* @param {Array} diff
* @returns {Array}
* @api public
*/
function patch() {
var dmp = new diff_match_patch();
if (arguments.length == 1) {
// single argument, diff
return dmp.patch_make(arguments[0]);
} else {
if (typeof arguments[1] == 'string') {
// two arguments, old and delta
var d = dmp.diff_fromDelta(arguments[0], arguments[1]);
return dmp.patch_make(arguments[0], d);
} else {
// two arguments, old and diff
return dmp.patch_make(arguments[0], arguments[1]);
}
}
}
/**
* Applies a patch to a piece of text, and optionally reports what pieces
* of the patch were succesfully applied.
* @param {Array} patch
* @param {String} text
* @param {Boolean} report (optional)
* @returns {String, Array}
* @api public
*/
patch.apply = function(patch, text, report) {
var dmp = new diff_match_patch();
if (report) {
return dmp.patch_apply(patch, text);
} else {
return dmp.patch_apply(patch, text)[0];
}
}
/**
* Converts a patch to a string representation
* @param {Array} patch
* @returns {String}
* @api public
*/
patch.toText = function(patch) {
var dmp = new diff_match_patch();
return dmp.patch_toText(patch);
}
/**
* Converts a string representation of a patch back into a patch
* @param {String} text
* @returns {Array}
* @api public
*/
patch.fromText = function(text) {
var dmp = new diff_match_patch();
return dmp.patch_fromText(text);
}
module.exports = patch;