Skip to content

Commit d39026d

Browse files
committed
Add old() and new() functions.
1 parent 9fc87c1 commit d39026d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Diff for: Readme.md

+28
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ Becomes:
5555

5656
Converts a delta string back into a diff. This requires the original text.
5757

58+
### diff.old(diff)
59+
60+
Returns the old (source) text from a diff.
61+
62+
#### Example
63+
64+
The call:
65+
66+
diff.old([[0,"hi "], [-1,"joe"], [1,"sarah"], [0,", I "], [1,"dis"], [0,"like "], [-1,"cu"], [0,"p"], [1,"an"], [0,"cakes"]])
67+
68+
Returns:
69+
70+
'hi joe, I like cupcakes'
71+
72+
### diff.new(diff)
73+
74+
Returns the new (destination) text from a diff.
75+
76+
#### Example
77+
78+
The call:
79+
80+
diff.new([[0,"hi "], [-1,"joe"], [1,"sarah"], [0,", I "], [1,"dis"], [0,"like "], [-1,"cu"], [0,"p"], [1,"an"], [0,"cakes"]])
81+
82+
Returns:
83+
84+
'hi sarah, I dislike pancakes'
85+
5886
## License
5987

6088

Diff for: index.js

+24
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,28 @@ diff.fromDelta = function(oldText, delta) {
3838
return dmp.diff_fromDelta(oldText, delta);
3939
}
4040

41+
/**
42+
* Returns the old (source) text from a diff.
43+
* @param {Array} diff
44+
* @returns {String}
45+
* @api public
46+
*/
47+
48+
diff.old = function(diff) {
49+
var dmp = new diff_match_patch();
50+
return dmp.diff_text1(diff);
51+
}
52+
53+
/**
54+
* Returns the new (destination) text from a diff.
55+
* @param {Array} diff
56+
* @returns {String}
57+
* @api public
58+
*/
59+
60+
diff.new = function(diff) {
61+
var dmp = new diff_match_patch();
62+
return dmp.diff_text2(diff);
63+
}
64+
4165
module.exports = diff;

0 commit comments

Comments
 (0)