easy-richtextarea 4.0.1 → 4.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +0 -8
  2. package/example.js +1012 -595
  3. package/lib/browser.js +9 -9
  4. package/lib/constants.js +13 -4
  5. package/lib/contentCompare.js +34 -0
  6. package/lib/example/richTextarea.js +2 -2
  7. package/lib/example/view.js +18 -9
  8. package/lib/keyCodes.js +13 -0
  9. package/lib/main.js +1 -1
  10. package/lib/operation/delete.js +101 -52
  11. package/lib/operation/empty.js +21 -6
  12. package/lib/operation/insert.js +78 -31
  13. package/lib/operations/fromJSON.js +4 -4
  14. package/lib/operations/generate.js +11 -11
  15. package/lib/operations/transform.js +3 -7
  16. package/lib/richTextarea.js +244 -156
  17. package/lib/selection.js +55 -13
  18. package/lib/types.js +13 -13
  19. package/lib/undoBuffer.js +105 -0
  20. package/package.json +3 -2
  21. package/src/browser.js +3 -3
  22. package/src/constants.js +1 -0
  23. package/src/contentCompare.js +34 -0
  24. package/src/example/richTextarea.js +11 -9
  25. package/src/example/view.js +28 -10
  26. package/src/keyCodes.js +3 -0
  27. package/src/main.js +1 -0
  28. package/src/operation/delete.js +135 -58
  29. package/src/operation/empty.js +19 -14
  30. package/src/operation/insert.js +97 -39
  31. package/src/operations/fromJSON.js +4 -4
  32. package/src/operations/generate.js +17 -13
  33. package/src/operations/transform.js +3 -3
  34. package/src/richTextarea.js +316 -185
  35. package/src/selection.js +53 -9
  36. package/src/types.js +6 -6
  37. package/src/undoBuffer.js +73 -0
  38. package/test/content/transform.js +15 -17
  39. package/test/helpers.js +27 -21
  40. package/test/operation/delete.js +187 -147
  41. package/test/operation/empty.js +3 -5
  42. package/test/operation/insert.js +134 -118
  43. package/test/operations/generate.js +25 -29
  44. package/test/operations/transform.js +37 -98
  45. package/lib/stringCompare.js +0 -33
  46. package/src/stringCompare.js +0 -33
@@ -4,183 +4,199 @@ const { assert } = require("chai"),
4
4
  { arrayUtilities } = require("necessary"),
5
5
  { EmptyOperation, DeleteOperation, InsertOperation } = require("../../lib/main"); ///
6
6
 
7
- const helpers = require("../helpers");
8
-
9
7
  const { first, second } = arrayUtilities;
10
8
 
11
9
  describe("InsertOperation", () => {
12
10
  describe("transform", () => {
13
- it("transforms a second insert operation at position 0, leaving it in the same position", () => {
14
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
15
- insertOperation2 = InsertOperation.fromStringAndPosition("b", 0),
16
- transformedOperations = insertOperation1.transformOperation(insertOperation2);
11
+ it("leaves an empty operation intact", () => {
12
+ const insertOperation1 = InsertOperation.fromContentAndPosition("nhTY", 1),
13
+ emptyOperation2 = EmptyOperation.fromNothing(),
14
+ transformedOperations = insertOperation1.transformOperation(emptyOperation2);
17
15
 
18
16
  assert.lengthOf(transformedOperations, 1);
19
17
 
20
18
  const firstTransformedOperation = first(transformedOperations),
21
19
  transformedOperation = firstTransformedOperation, ///
22
20
  transformedOperationJSON = transformedOperation.toJSON(),
23
- expectedTransformedOperation = insertOperation2.clone(),
21
+ expectedTransformedOperation = emptyOperation2.clone(),
24
22
  expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
25
23
 
26
24
  assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
27
25
  });
28
26
 
29
- it("transforms a second, lexicographically lesser insert operation at position 1, leaving it in the same position", () => {
30
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
31
- insertOperation2 = InsertOperation.fromStringAndPosition(" ", 1),
32
- transformedOperations = insertOperation1.transformOperation(insertOperation2);
27
+ describe("to the left of another insert", () => {
28
+ it("moves the other insert to the right by its own length", () => {
29
+ const insertOperation1 = InsertOperation.fromContentAndPosition("iUYHgt", 2),
30
+ insertOperation2 = InsertOperation.fromContentAndPosition("ahBGFRtq", 10),
31
+ transformedOperations = insertOperation1.transformOperation(insertOperation2);
33
32
 
34
- assert.lengthOf(transformedOperations, 1);
33
+ assert.lengthOf(transformedOperations, 1);
35
34
 
36
- const firstTransformedOperation = first(transformedOperations),
37
- transformedOperation = firstTransformedOperation, ///
38
- transformedOperationJSON = transformedOperation.toJSON(),
39
- expectedTransformedOperation = insertOperation2.clone(),
40
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
35
+ const firstTransformedOperation = first(transformedOperations),
36
+ transformedOperation = firstTransformedOperation, ///
37
+ transformedOperationJSON = transformedOperation.toJSON(),
38
+ expectedTransformedOperation = InsertOperation.fromContentAndPosition("ahBGFRtq", 16),
39
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
41
40
 
42
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
41
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
42
+ });
43
43
  });
44
44
 
45
- it("transforms a second, equal insert operation, leaving it in the same position", () => {
46
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
47
- insertOperation2 = insertOperation1.clone(),
48
- transformedOperations = insertOperation1.transformOperation(insertOperation2);
45
+ describe("at the same position whilst lexicographically less than another insert", () => {
46
+ it("moves the other insert to the right by its own length", () => {
47
+ const insertOperation1 = InsertOperation.fromContentAndPosition("acnhJU", 4),
48
+ insertOperation2 = InsertOperation.fromContentAndPosition("xKId", 4),
49
+ transformedOperations = insertOperation1.transformOperation(insertOperation2);
49
50
 
50
- assert.lengthOf(transformedOperations, 1);
51
+ assert.lengthOf(transformedOperations, 1);
51
52
 
52
- const firstTransformedOperation = first(transformedOperations),
53
- transformedOperation = firstTransformedOperation, ///
54
- transformedOperationJSON = transformedOperation.toJSON(),
55
- expectedTransformedOperation = insertOperation1.clone(),
56
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
53
+ const firstTransformedOperation = first(transformedOperations),
54
+ transformedOperation = firstTransformedOperation, ///
55
+ transformedOperationJSON = transformedOperation.toJSON(),
56
+ expectedTransformedOperation = InsertOperation.fromContentAndPosition("xKId", 10),
57
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
57
58
 
58
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
59
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
60
+ });
59
61
  });
60
62
 
61
- it("transforms a second, lexicographically greater insert operation at position 1 to position 2", () => {
62
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
63
- insertOperation2 = InsertOperation.fromStringAndPosition("b", 1),
64
- transformedOperations = insertOperation1.transformOperation(insertOperation2);
63
+ describe("at the same position whilst lexicographically greater than another insert", () => {
64
+ it("leaves the other insert intact", () => {
65
+ const insertOperation1 = InsertOperation.fromContentAndPosition("xcnhJU", 4),
66
+ insertOperation2 = InsertOperation.fromContentAndPosition("aKId", 4),
67
+ transformedOperations = insertOperation1.transformOperation(insertOperation2);
65
68
 
66
- assert.lengthOf(transformedOperations, 1);
69
+ assert.lengthOf(transformedOperations, 1);
67
70
 
68
- const firstTransformedOperation = first(transformedOperations),
69
- transformedOperation = firstTransformedOperation, ///
70
- transformedOperationJSON = transformedOperation.toJSON(),
71
- expectedTransformedOperation = InsertOperation.fromStringAndPosition("b", 2),
72
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
71
+ const firstTransformedOperation = first(transformedOperations),
72
+ transformedOperation = firstTransformedOperation, ///
73
+ transformedOperationJSON = transformedOperation.toJSON(),
74
+ expectedTransformedOperation = InsertOperation.fromContentAndPosition("aKId", 4),
75
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
73
76
 
74
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
77
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
78
+ });
75
79
  });
76
80
 
77
- it("transforms a second insert operation at position 2 to position 3", () => {
78
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
79
- insertOperation2 = InsertOperation.fromStringAndPosition("b", 2),
80
- transformedOperations = insertOperation1.transformOperation(insertOperation2);
81
+ describe("at the same position whilst lexicographically equal to another insert", () => {
82
+ it("leaves the other insert intact", () => {
83
+ const insertOperation1 = InsertOperation.fromContentAndPosition("xcnhJU", 4),
84
+ insertOperation2 = InsertOperation.fromContentAndPosition("xcnhJU", 4),
85
+ transformedOperations = insertOperation1.transformOperation(insertOperation2);
81
86
 
82
- assert.lengthOf(transformedOperations, 1);
87
+ assert.lengthOf(transformedOperations, 1);
83
88
 
84
- const firstTransformedOperation = first(transformedOperations),
85
- transformedOperation = firstTransformedOperation, ///
86
- transformedOperationJSON = transformedOperation.toJSON(),
87
- expectedTransformedOperation = InsertOperation.fromStringAndPosition("b", 3),
88
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
89
+ const firstTransformedOperation = first(transformedOperations),
90
+ transformedOperation = firstTransformedOperation, ///
91
+ transformedOperationJSON = transformedOperation.toJSON(),
92
+ expectedTransformedOperation = InsertOperation.fromContentAndPosition("xcnhJU", 4),
93
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
89
94
 
90
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
95
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
96
+ });
91
97
  });
92
98
 
93
- it("transforms an empty operation, leaving it the same", () => {
94
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
95
- emptyOperation2 = EmptyOperation.fromNothing(),
96
- transformedOperations = insertOperation1.transformOperation(emptyOperation2);
99
+ describe("to the right of another insert", () => {
100
+ it("leaves the other insert intact", () => {
101
+ const insertOperation1 = InsertOperation.fromContentAndPosition("kiuj", 14),
102
+ insertOperation2 = InsertOperation.fromContentAndPosition("xcnhJU", 4),
103
+ transformedOperations = insertOperation1.transformOperation(insertOperation2);
97
104
 
98
- assert.lengthOf(transformedOperations, 1);
105
+ assert.lengthOf(transformedOperations, 1);
99
106
 
100
- const firstTransformedOperation = first(transformedOperations),
101
- transformedOperation = firstTransformedOperation, ///
102
- transformedOperationJSON = transformedOperation.toJSON(),
103
- expectedTransformedOperation = emptyOperation2.clone(),
104
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
107
+ const firstTransformedOperation = first(transformedOperations),
108
+ transformedOperation = firstTransformedOperation, ///
109
+ transformedOperationJSON = transformedOperation.toJSON(),
110
+ expectedTransformedOperation = InsertOperation.fromContentAndPosition("xcnhJU", 4),
111
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
105
112
 
106
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
113
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON)
114
+ });
107
115
  });
108
116
 
109
- it("transforms a delete operation at position 0 of length 1, leaving it the same position", () => {
110
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
111
- deleteOperation2 = DeleteOperation.fromLengthAndPosition(1, 0),
112
- transformedOperations = insertOperation1.transformOperation(deleteOperation2);
117
+ describe("to the left of a delete", () => {
118
+ it("moves the other delete to the right by its own length", () => {
119
+ const insertOperation1 = InsertOperation.fromContentAndPosition("jabgTF", 2),
120
+ deleteOperation2 = DeleteOperation.fromContentAndPosition("cHYg", 10),
121
+ transformedOperations = insertOperation1.transformOperation(deleteOperation2);
113
122
 
114
- assert.lengthOf(transformedOperations, 1);
123
+ assert.lengthOf(transformedOperations, 1);
115
124
 
116
- const firstTransformedOperation = first(transformedOperations),
117
- transformedOperation = firstTransformedOperation, ///
118
- transformedOperationJSON = transformedOperation.toJSON(),
119
- expectedTransformedOperation = deleteOperation2.clone(),
120
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
125
+ const firstTransformedOperation = first(transformedOperations),
126
+ transformedOperation = firstTransformedOperation, ///
127
+ transformedOperationJSON = transformedOperation.toJSON(),
128
+ expectedTransformedOperation = DeleteOperation.fromContentAndPosition("cHYg", 16),
129
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
121
130
 
122
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON);
131
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON);
132
+ });
123
133
  });
124
134
 
125
- it("transforms a delete operation at position 0 of length 2, splitting it into two delete operations of length 1, the first at position 0 and the second at position 1", () => {
126
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
127
- deleteOperation2 = DeleteOperation.fromLengthAndPosition(2, 0),
128
- transformedOperations = insertOperation1.transformOperation(deleteOperation2);
129
-
130
- assert.lengthOf(transformedOperations, 2);
135
+ describe("in the same position as a delete", () => {
136
+ it("moves the other delete to the right by its own length", () => {
137
+ const insertOperation1 = InsertOperation.fromContentAndPosition("jabgTF", 2),
138
+ deleteOperation2 = DeleteOperation.fromContentAndPosition("cHYg", 2),
139
+ transformedOperations = insertOperation1.transformOperation(deleteOperation2);
131
140
 
132
- const firstTransformedOperation = first(transformedOperations),
133
- secondTransformedOperation = second(transformedOperations),
134
- firstTransformedOperationJSON = firstTransformedOperation.toJSON(),
135
- secondTransformedOperationJSON = secondTransformedOperation.toJSON(),
136
- firstExpectedTransformedOperation = DeleteOperation.fromLengthAndPosition(1, 0),
137
- secondExpectedTransformedOperation = DeleteOperation.fromLengthAndPosition(1, 1),
138
- firstExpectedTransformedOperationJSON = firstExpectedTransformedOperation.toJSON(),
139
- secondExpectedTransformedOperationJSON = secondExpectedTransformedOperation.toJSON();
140
-
141
- assert.deepEqual(firstTransformedOperationJSON, firstExpectedTransformedOperationJSON);
142
- assert.deepEqual(secondTransformedOperationJSON, secondExpectedTransformedOperationJSON);
143
- });
141
+ assert.lengthOf(transformedOperations, 1);
144
142
 
145
- it("transforms a delete operation at position 1 of length 1 to position 2", () => {
146
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
147
- deleteOperation2 = DeleteOperation.fromLengthAndPosition(1, 1),
148
- transformedOperations = insertOperation1.transformOperation(deleteOperation2);
149
-
150
- assert.lengthOf(transformedOperations, 1);
143
+ const firstTransformedOperation = first(transformedOperations),
144
+ transformedOperation = firstTransformedOperation, ///
145
+ transformedOperationJSON = transformedOperation.toJSON(),
146
+ expectedTransformedOperation = DeleteOperation.fromContentAndPosition("cHYg", 8),
147
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
151
148
 
152
- const firstTransformedOperation = first(transformedOperations),
153
- transformedOperation = firstTransformedOperation, ///
154
- transformedOperationJSON = transformedOperation.toJSON(),
155
- expectedTransformedOperation = DeleteOperation.fromLengthAndPosition(1, 2),
156
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
149
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON);
150
+ });
151
+ });
157
152
 
158
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON);
153
+ describe("strictly overlapped by a delete", () => {
154
+ it("splits the delete in two", () => {
155
+ const insertOperation1 = InsertOperation.fromContentAndPosition("XsAz", 4),
156
+ deleteOperation2 = DeleteOperation.fromContentAndPosition("xhytgGTY", 2),
157
+ transformedOperations = insertOperation1.transformOperation(deleteOperation2);
158
+
159
+ assert.lengthOf(transformedOperations, 2);
160
+
161
+ const firstTransformedOperation = first(transformedOperations),
162
+ secondTransformedOperation = second(transformedOperations),
163
+ firstTransformedOperationJSON = firstTransformedOperation.toJSON(),
164
+ secondTransformedOperationJSON = secondTransformedOperation.toJSON(),
165
+ firstExpectedTransformedOperation = DeleteOperation.fromContentAndPosition("xh", 2),
166
+ firstExpectedTransformedOperationJSON = firstExpectedTransformedOperation.toJSON(),
167
+ secondExpectedTransformedOperation = DeleteOperation.fromContentAndPosition("ytgGTY", 6),
168
+ secondExpectedTransformedOperationJSON = secondExpectedTransformedOperation.toJSON();
169
+
170
+ assert.deepEqual(firstTransformedOperationJSON, firstExpectedTransformedOperationJSON);
171
+ assert.deepEqual(secondTransformedOperationJSON, secondExpectedTransformedOperationJSON);
172
+ });
159
173
  });
160
174
 
161
- it("transforms a delete operation at position 2 of length 1 to position 3", () => {
162
- const insertOperation1 = InsertOperation.fromStringAndPosition("a", 1),
163
- deleteOperation2 = DeleteOperation.fromLengthAndPosition(1, 2),
164
- transformedOperations = insertOperation1.transformOperation(deleteOperation2);
175
+ describe("to the right of a delete", () => {
176
+ it("leaves the delete intact", () => {
177
+ const insertOperation1 = InsertOperation.fromContentAndPosition("juhY", 10),
178
+ deleteOperation2 = DeleteOperation.fromContentAndPosition("xhyTYa", 2),
179
+ transformedOperations = insertOperation1.transformOperation(deleteOperation2);
165
180
 
166
- assert.lengthOf(transformedOperations, 1);
181
+ assert.lengthOf(transformedOperations, 1);
167
182
 
168
- const firstTransformedOperation = first(transformedOperations),
169
- transformedOperation = firstTransformedOperation, ///
170
- transformedOperationJSON = transformedOperation.toJSON(),
171
- expectedTransformedOperation = DeleteOperation.fromLengthAndPosition(1, 3),
172
- expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
183
+ const firstTransformedOperation = first(transformedOperations),
184
+ transformedOperation = firstTransformedOperation, ///
185
+ transformedOperationJSON = transformedOperation.toJSON(),
186
+ expectedTransformedOperation = DeleteOperation.fromContentAndPosition("xhyTYa", 2),
187
+ expectedTransformedOperationJSON = expectedTransformedOperation.toJSON();
173
188
 
174
- assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON);
189
+ assert.deepEqual(transformedOperationJSON, expectedTransformedOperationJSON);
190
+ });
175
191
  });
176
192
  });
177
193
 
178
194
  describe("transformContent", () => {
179
195
  it("inserts the requisite characters into the content", () => {
180
- const content = helpers.content(),
181
- emptyOperation = InsertOperation.fromStringAndPosition("123", 0),
196
+ const content = "adsfxvbc",
197
+ emptyOperation = InsertOperation.fromContentAndPosition("123", 1),
182
198
  transformedContent = emptyOperation.transformContent(content),
183
- expectedContent = `123${content}`;
199
+ expectedContent = "a123dsfxvbc";
184
200
 
185
201
  assert.equal(transformedContent, expectedContent);
186
202
  });
@@ -190,7 +206,7 @@ describe("InsertOperation", () => {
190
206
  it("transforms from and to JSON, leaving the operation unchanged", () => {
191
207
  const expectedJSON = {
192
208
  "type": "insert",
193
- "string": "a",
209
+ "content": "a",
194
210
  "position": 1
195
211
  },
196
212
  json = InsertOperation.fromJSON(expectedJSON).toJSON();
@@ -2,34 +2,30 @@
2
2
 
3
3
  const { assert } = require("chai"),
4
4
  { arrayUtilities } = require("necessary"),
5
- { types, InsertOperation, DeleteOperation, generateOperations } = require("../../lib/main"); ///
5
+ { InsertOperation, DeleteOperation, generateOperations } = require("../../lib/main"); ///
6
6
 
7
- const helpers = require("../helpers");
8
-
9
- const{ first, second } = arrayUtilities,
10
- { insertType, deleteType } = types;
7
+ const{ first, second } = arrayUtilities;
11
8
 
12
9
  describe("generateOperations", () => {
13
- describe("the working and editable contents are the same", () => {
10
+ describe("the contents are the same", () => {
14
11
  it("generates a zero length array of operations", () => {
15
- const workingContent = helpers.content(),
16
- editableContent = workingContent,
17
- operations = generateOperations(workingContent, editableContent);
12
+ const content = "ngYtFRdeaQ",
13
+ operations = generateOperations(content, content);
18
14
 
19
15
  assert.lengthOf(operations, 0);
20
16
  });
21
17
  });
22
18
 
23
- describe("the the working content is a subset of the editable content", () => {
19
+ describe("the first content is a subset of the second content", () => {
24
20
  it("generates a array of operations containing an insert operation", () => {
25
- const workingContent = helpers.content(),
26
- editableContent = `123${workingContent}`,
27
- operations = generateOperations(workingContent, editableContent);
21
+ const contentA = "juhGTre",
22
+ contentB = "juhJHuyGTre",
23
+ operations = generateOperations(contentA, contentB);
28
24
 
29
25
  assert.lengthOf(operations, 1);
30
26
 
31
27
  const firstOperation = first(operations),
32
- insertOperation = InsertOperation.fromStringAndPosition("123", 0),
28
+ insertOperation = InsertOperation.fromContentAndPosition("JHuy", 3),
33
29
  operation = firstOperation, ///
34
30
  expectedOperation = insertOperation, ///
35
31
  operationJSON = operation.toJSON(),
@@ -39,16 +35,16 @@ describe("generateOperations", () => {
39
35
  });
40
36
  });
41
37
 
42
- describe("the editable content is a subset of the the working content", () => {
38
+ describe("the second content is a subset of first content", () => {
43
39
  it("generates array of operations containing a delete operation", () => {
44
- const editableContent = helpers.content(),
45
- workingContent = `xyz${editableContent}`,
46
- operations = generateOperations(workingContent, editableContent);
40
+ const contentA = "ayHTgvcregd",
41
+ contentB = "ayHTegd",
42
+ operations = generateOperations(contentA, contentB);
47
43
 
48
44
  assert.lengthOf(operations, 1);
49
45
 
50
46
  const firstOperation = first(operations),
51
- deleteOperation = DeleteOperation.fromLengthAndPosition(3, 0),
47
+ deleteOperation = DeleteOperation.fromContentAndPosition("gvcr", 4),
52
48
  operation = firstOperation, ///
53
49
  expectedOperation = deleteOperation, ///
54
50
  operationJSON = operation.toJSON(),
@@ -58,11 +54,11 @@ describe("generateOperations", () => {
58
54
  });
59
55
  });
60
56
 
61
- describe("the the working and editable contents are different", () => {
57
+ describe("the contents are different", () => {
62
58
  it("generates array of operations containing one delete operation followed by an insert operation", () => {
63
- const workingContent = helpers.content(),
64
- editableContent = helpers.content(),
65
- operations = generateOperations(workingContent, editableContent);
59
+ const contentA = "khbgytrcGFRDwr",
60
+ contentB = "khb12buhGTFRDwr",
61
+ operations = generateOperations(contentA, contentB);
66
62
 
67
63
  assert.lengthOf(operations, 2);
68
64
 
@@ -70,13 +66,13 @@ describe("generateOperations", () => {
70
66
  secondOperation = second(operations),
71
67
  firstOperationJSON = firstOperation.toJSON(),
72
68
  secondOperationJSON = secondOperation.toJSON(),
73
- firstOperationType = firstOperationJSON["type"],
74
- secondOperationType = secondOperationJSON["type"],
75
- expectedFirstOperationType = deleteType, ///
76
- expectedSecondOperationType = insertType; ///
69
+ expectedFirstOperation = DeleteOperation.fromContentAndPosition("gytrcG", 3),
70
+ expectedSecondOperation = InsertOperation.fromContentAndPosition("12buhGT", 3),
71
+ expectedFirstOperationJSON = expectedFirstOperation.toJSON(),
72
+ expectedSecondOperationType = expectedSecondOperation.toJSON();
77
73
 
78
- assert.deepEqual(firstOperationType, expectedFirstOperationType);
79
- assert.deepEqual(secondOperationType, expectedSecondOperationType);
74
+ assert.deepEqual(firstOperationJSON, expectedFirstOperationJSON);
75
+ assert.deepEqual(secondOperationJSON, expectedSecondOperationType);
80
76
  });
81
77
  });
82
78
  });
@@ -1,126 +1,65 @@
1
1
  "use strict";
2
2
 
3
3
  const { assert } = require("chai"),
4
- { transformContent, operationsFromJSON, transformOperations } = require("../../lib/main"); ///
4
+ { transformContent, transformOperations } = require("../../lib/main"); ///
5
5
 
6
6
  const helpers = require("../helpers");
7
7
 
8
8
  describe("transformOperations", () => {
9
- describe("for two empty sequences of operations", () => {
10
- it("The intentions are preserved", () => {
11
- const firstOperationsJSON = [],
12
- secondOperationsJSON = [],
13
- firstOperations = operationsFromJSON(firstOperationsJSON),
14
- secondOperations = operationsFromJSON(secondOperationsJSON);
15
-
16
- assertIntentionsPreserved(firstOperations, secondOperations);
9
+ describe("for two sequences each containing a hundred operations", () => {
10
+ it("The intentions are preserved 1", () => {
11
+ assertIntentionsPreserved();
17
12
  });
18
- });
19
13
 
20
- describe("for an array containing one operation and an empty sequence", () => {
21
- it("The intentions are preserved", () => {
22
- const firstOperationsJSON = [{
23
- "type": "insert",
24
- "string": "xyz",
25
- "position": 1
26
- }],
27
- secondOperationsJSON = [],
28
- firstOperations = operationsFromJSON(firstOperationsJSON),
29
- secondOperations = operationsFromJSON(secondOperationsJSON);
30
-
31
- assertIntentionsPreserved(firstOperations, secondOperations);
14
+ it("The intentions are preserved 2", () => {
15
+ assertIntentionsPreserved();
32
16
  });
33
- });
34
17
 
35
- describe("for two sequences each containing one operation", () => {
36
- it("The intentions are preserved", () => {
37
- const firstOperationsJSON = [{
38
- "type": "insert",
39
- "string": "xyz",
40
- "position": 1
41
- }],
42
- secondOperationsJSON = [{
43
- "type": "delete",
44
- "length": 2,
45
- "position": 1
46
- }],
47
- firstOperations = operationsFromJSON(firstOperationsJSON),
48
- secondOperations = operationsFromJSON(secondOperationsJSON);
49
-
50
- assertIntentionsPreserved(firstOperations, secondOperations);
18
+ it("The intentions are preserved 3", () => {
19
+ assertIntentionsPreserved();
51
20
  });
52
- });
53
21
 
54
- describe("for an array containing one operation and a sequence containing two operations", () => {
55
- it("The intentions are preserved", () => {
56
- const firstOperationsJSON = [{
57
- "type": "insert",
58
- "string": "xyz",
59
- "position": 1,
60
- }],
61
- secondOperationsJSON = [{
62
- "type": "insert",
63
- "string": "abc",
64
- "position": 3
65
- },
66
- {
67
- "type": "delete",
68
- "length": 2,
69
- "position": 1,
70
- }],
71
- firstOperations = operationsFromJSON(firstOperationsJSON),
72
- secondOperations = operationsFromJSON(secondOperationsJSON);
73
-
74
- assertIntentionsPreserved(firstOperations, secondOperations);
22
+ it("The intentions are preserved 4", () => {
23
+ assertIntentionsPreserved();
75
24
  });
76
- });
77
25
 
78
- describe("for two sequences each containing two operations", () => {
79
- it("The intentions are preserved", () => {
80
- const firstOperationsJSON = [{
81
- "type": "delete",
82
- "length": 6,
83
- "position": 0
84
- },
85
- {
86
- "type": "insert",
87
- "string": "JQ",
88
- "position": 0
89
- }],
90
- secondOperationsJSON = [{
91
- "type": "delete",
92
- "length": 4,
93
- "position": 0
94
- },
95
- {
96
- "type": "insert",
97
- "string": "bkW",
98
- "position": 0
99
- }],
100
- firstOperations = operationsFromJSON(firstOperationsJSON),
101
- secondOperations = operationsFromJSON(secondOperationsJSON);
102
-
103
- assertIntentionsPreserved(firstOperations, secondOperations);
26
+ it("The intentions are preserved 6", () => {
27
+ assertIntentionsPreserved();
104
28
  });
105
- });
106
29
 
107
- describe("for two sequences each containing a hundred operations", () => {
108
- it("The intentions are preserved", () => {
109
- const content = helpers.content(100),
110
- firstOperations = helpers.operations(content, 100),
111
- secondOperations = helpers.operations(content, 100);
30
+ it("The intentions are preserved 6", () => {
31
+ assertIntentionsPreserved();
32
+ });
112
33
 
113
- assertIntentionsPreserved(firstOperations, secondOperations);
34
+ it("The intentions are preserved 7", () => {
35
+ assertIntentionsPreserved();
36
+ });
37
+
38
+ it("The intentions are preserved 8", () => {
39
+ assertIntentionsPreserved();
40
+ });
41
+
42
+ it("The intentions are preserved 9", () => {
43
+ assertIntentionsPreserved();
44
+ });
45
+
46
+ it("The intentions are preserved 10", () => {
47
+ assertIntentionsPreserved();
114
48
  });
115
49
  });
116
50
  });
117
51
 
118
- function assertIntentionsPreserved(firstOperations, secondOperations) {
119
- const content = helpers.content(100),
120
- transformedFirstOperations = transformOperations(firstOperations, secondOperations),
52
+ function assertIntentionsPreserved() {
53
+ const content = helpers.content(1000);
54
+
55
+ let firstOperations = helpers.operations(content, 100),
56
+ secondOperations = helpers.operations(content, 100);
57
+
58
+ const transformedFirstOperations = transformOperations(firstOperations, secondOperations),
121
59
  transformedSecondOperations = transformOperations(secondOperations, firstOperations);
122
60
 
123
61
  firstOperations = firstOperations.concat(transformedSecondOperations);
62
+
124
63
  secondOperations = secondOperations.concat(transformedFirstOperations);
125
64
 
126
65
  const firstTransformedContent = transformContent(content, firstOperations),
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "default", {
6
- enumerable: true,
7
- get: function() {
8
- return stringCompare;
9
- }
10
- });
11
- var _constants = require("./constants");
12
- function stringCompare(stringA, stringB) {
13
- if (stringA === _constants.EMPTY_STRING && stringB === _constants.EMPTY_STRING) {
14
- return false;
15
- }
16
- if (stringA === _constants.EMPTY_STRING) {
17
- return true;
18
- }
19
- if (stringB === _constants.EMPTY_STRING) {
20
- return false;
21
- }
22
- var codePointA = stringA.codePointAt(0), codePointB = stringB.codePointAt(0);
23
- if (codePointA < codePointB) {
24
- return true;
25
- }
26
- if (codePointA > codePointB) {
27
- return false;
28
- }
29
- var subStringA = stringA.substring(1), subStringB = stringB.substring(1);
30
- return stringCompare(subStringA, subStringB);
31
- }
32
-
33
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9zdHJpbmdDb21wYXJlLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIlwidXNlIHN0cmljdFwiO1xuXG5pbXBvcnQgeyBFTVBUWV9TVFJJTkcgfSBmcm9tIFwiLi9jb25zdGFudHNcIjtcblxuZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24gc3RyaW5nQ29tcGFyZShzdHJ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