-
Notifications
You must be signed in to change notification settings - Fork 0
/
undo-redo.js
228 lines (189 loc) · 7.07 KB
/
undo-redo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* http://www.codewars.com/kata/531489f2bb244a5b9f00077e/
*
The purpose of this kata is to implement the undoRedo function.
This function takes an object and returns an object that has these actions to be performed on the object passed as a parameter:
set(key, value) Assigns the value to the key. If the key does not exist, creates it.
get(key) Returns the value associated to the key.
del(key) removes the key from the object.
undo() Undo the last operation (set or del) on the object. Throws an exception if there is no operation to undo.
redo() Redo the last undo operation (redo is only possible after an undo). Throws an exception if there is no operation to redo.
After set() or del() are called, there is nothing to redo.
All actions must affect to the object passed to undoRedo(object) function. So you can not work with a copy of the object.
Any set/del after an undo should disallow new undos.
*
*/
let tests = require('./lib/framework.js');
let Test = tests.Test, describe = tests.describe, it = tests.it, before = tests.before, after = tests.after;
function undoRedo(object) {
var history = {
undo: [],
redo: []
};
function copy(obj) {
return Object.assign({}, obj);
}
function assign(newObj) {
var key;
for (key in object) {
if (!object.hasOwnProperty(key)) continue;
if (!newObj[key]) {
delete object[key];
}
}
return Object.assign(object, newObj);
}
return {
set: function(key, value) {
if (object[key] && object[key] == value) {
return ;
}
history.undo.push(copy(object));
object[key] = value;
history.redo = [];
},
get: function(key) {
return object[key];
},
del: function(key) {
history.undo.push(copy(object));
delete object[key];
history.redo = [];
},
undo: function() {
if (!history.undo.length) {
throw new Error('No undo available');
}
history.redo.push(copy(object));
assign(history.undo.pop());
},
redo: function() {
if (!history.redo.length) {
throw new Error('No redo available');
}
history.undo.push(copy(object));
assign(history.redo.pop());
}
};
}
Test.describe('tests', function() {
Test.it('get/set tests', function() {
var obj = {
x: 1,
y: 2
};
var unRe = undoRedo(obj);
Test.assertEquals(unRe.get('x'), 1, 'The get method returns the value of a key');
unRe.set('x', 3);
Test.assertEquals(unRe.get('x'), 3, 'The set method change the value of a key');
});
Test.it('simple undo', function() {
var obj = {
x: 1,
y: 2
};
var unRe = undoRedo(obj);
unRe.set('y', 10);
Test.assertEquals(unRe.get('y'), 10, 'The get method returns the value of a key');
unRe.undo();
Test.assertEquals(unRe.get('y'), 2, 'The undo method restores the previous state');
try {
unRe.undo();
Test.expect(false, 'It should have thrown an exception');
} catch (e) {
Test.assertEquals(unRe.get('y'), 2);
}
});
Test.it('simple redo', function() {
var obj = {
x: 1,
y: 2
};
var unRe = undoRedo(obj);
unRe.set('y', 10);
Test.assertEquals(unRe.get('y'), 10, 'The get method returns the value of a key');
unRe.undo();
Test.assertEquals(unRe.get('y'), 2, 'The undo method restores the previous state');
unRe.redo();
Test.assertEquals(unRe.get('y'), 10, 'The undo method restores the previous state');
try {
unRe.redo();
Test.expect(false, 'It should have thrown an exception');
} catch (e) {
Test.assertEquals(unRe.get('y'), 10);
}
});
Test.it('undo/redo', function() {
var obj = {
x: 1,
y: 2
};
var unRe = undoRedo(obj);
unRe.set('y', 10);
unRe.set('y', 100);
unRe.set('x', 150);
unRe.set('x', 50);
Test.assertEquals(unRe.get('y'), 100, 'The get method returns the value of a key');
Test.assertEquals(unRe.get('x'), 50, 'The get method returns the value of a key');
unRe.undo();
Test.assertEquals(unRe.get('x'), 150, 'The undo method restores the previous state');
Test.assertEquals(unRe.get('y'), 100, 'The y key stays the same');
unRe.redo();
Test.assertEquals(unRe.get('x'), 50, 'Undo the x value');
Test.assertEquals(unRe.get('y'), 100, 'The y key stays the same');
unRe.undo();
unRe.undo();
Test.assertEquals(unRe.get('x'), 1, 'Undo the x value');
Test.assertEquals(unRe.get('y'), 100, 'The y key stays the same');
unRe.undo();
unRe.undo();
Test.assertEquals(unRe.get('y'), 2, 'Undo the y value');
Test.assertEquals(unRe.get('x'), 1, 'The x key stays the same');
try {
unRe.undo();
Test.expect(false, 'It should have thrown an exception');
} catch (e) {
Test.assertEquals(unRe.get('y'), 2, 'There is nothing to undo');
}
unRe.redo();
unRe.redo();
unRe.redo();
unRe.redo();
Test.assertEquals(unRe.get('y'), 100, 'y key redo state');
Test.assertEquals(unRe.get('x'), 50, 'y key redo state');
try {
unRe.redo();
Test.expect(false, 'It should have thrown an exception');
} catch (e) {
Test.assertEquals(unRe.get('y'), 100, 'There is nothing to redo');
}
});
Test.it('new key', function() {
var obj = {
x: 1,
y: 2
};
var unRe = undoRedo(obj);
unRe.set('z', 10);
Test.assertEquals(unRe.get('z'), 10, 'A new key has been added');
unRe.undo();
Test.assertEquals(unRe.get('z'), undefined, 'The z key should not exist');
unRe.redo();
Test.assertEquals(unRe.get('z'), 10, 'A new key has been added');
});
Test.it('delete key', function() {
var obj = {
x: 1,
y: 2
};
var unRe = undoRedo(obj);
unRe.del('x');
Test.assertEquals(unRe.get('x'), undefined, 'The x key should not exist');
Test.expect(!obj.hasOwnProperty('x'), 'The x key should be deleted');
unRe.undo();
Test.assertEquals(unRe.get('x'), 1, 'A new key has been added');
unRe.redo();
Test.assertEquals(unRe.get('x'), undefined, 'The x key should not exist');
Test.expect(!obj.hasOwnProperty('x'), 'The x key should be deleted');
});
});