UNPKG

1.49 kBJavaScriptView Raw
1var Merge = exports.Merge = function(str) {
2 var _conflicts = 0, _text = {}, _sections = null;
3 var section = 0;
4 var status = Merge.STATUS_BOTH;
5
6 Object.defineProperty(this, "conflicts", { get: function() { return _conflicts; }, set: function(value) { _conflicts = value; }, enumerable: true});
7 Object.defineProperty(this, "text", { get: function() { return _text; }, set: function(value) { _text = value; }, enumerable: true});
8 Object.defineProperty(this, "sections", { get: function() { return _sections; }, set: function(value) { _sections = value; }, enumerable: true});
9
10 var lines = str.split("\n");
11 lines.forEach(function(line) {
12 if(line.match(/^<<<<<<< (.*?)/)) {
13 status = Merge.STATUS_OURS;
14 _conflicts = _conflicts + 1;
15 section = section + 1;
16 } else if(line == '=======') {
17 status = Merge.STATUS_THEIRS;
18 } else if(line.match(/^>>>>>>> (.*?)/)) {
19 status = Merge.STATUS_BOTH;
20 section = section + 1;
21 } else {
22 _text[section] = _text[section] == null ? {} : _text[section];
23 _text[section][status] = _text[section][status] == null ? [] : _text[section][status];
24 _text[section][status].push(line);
25 }
26 });
27
28 // Let's set the values
29 _text = Object.keys(_text).map(function(key) {
30 return _text[key];
31 });
32 // Set the number of sections
33 _sections = _text.length;
34}
35
36// Static function
37Merge.STATUS_BOTH = 'both';
38Merge.STATUS_OURS = 'ours';
39Merge.STATUS_THEIRS = 'theirs';