-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndoRedo.js
More file actions
281 lines (250 loc) · 7.13 KB
/
Copy pathUndoRedo.js
File metadata and controls
281 lines (250 loc) · 7.13 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Description:
*
* 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 redos.
*
*
* Kata URL: https://www.codewars.com/kata/531489f2bb244a5b9f00077e
*
* @param {*} object
* @returns
*/
function undoRedo(object) {
let history = [];
let undoAction = [];
let step = 0;
let undocounter = 0;
let redocounter = 0;
function deepClone(object) {
return JSON.parse(JSON.stringify(object));
}
return {
set: function (key, value) {
history.push(deepClone(object));
object[key] = value;
history.push(deepClone(object));
step++;
},
get: function (key) {
return object[key];
},
del: function (key) {
delete object[key];
history.push(deepClone(object));
step++;
},
undo: function () {
if (undocounter === step) {
return new Error("Nothing to undo");
} else {
// const newOBJState = deepClone(object);
undoAction.push(object.pop());
undocounter++;
step--;
}
},
redo: function () {
if (redocounter === step) {
throw new Error("Nothing to redo.");
} else {
try {
object.push(undoAction.pop());
redocounter++;
undocounter--;
step++;
} catch (error) {
return error;
}
}
},
};
}
/*
function undoRedo(object) {
let history = [];
history.push(JSON.parse(JSON.stringify(object)));
let undocounter = 0;
let redocounter = 0;
return {
set: function (key, value) {
object[key] = value;
history.push(JSON.parse(JSON.stringify(object)));
undocounter++;
redocounter = undocounter;
},
get: function (key) {
return object[key];
},
del: function (key) {
delete object[key];
history.push(JSON.parse(JSON.stringify(object)));
undocounter++;
redocounter = undocounter;
},
undo: function () {
if (undocounter <= 0) {
return new Error("Nothing to undo");
} else {
redocounter = undocounter;
undocounter--;
object = JSON.parse(JSON.stringify(history[undocounter]));
}
},
redo: function () {
if (redocounter === undocounter) {
throw new Error("Nothing to redo.");
} else {
try {
undocounter = redocounter;
object = JSON.parse(JSON.stringify(history[redocounter]));
redocounter++;
} catch (error) {
return error;
}
}
},
};
}
*/
//_______________________________;
// var obj = {
// x: 1,
// y: 2,
// };
// var unRe = undoRedo(obj);
// console.log((unRe.get("x"), 1, "The get method returns the value of a key"));
// unRe.set("x", 3);
// console.log((unRe.get("x"), 3, "The set method change the value of a key"));
// console.log(
// "##################################################################"
// );
// var obj = {
// x: 1,
// y: 2,
// };
// var unRe = undoRedo(obj);
// unRe.set("y", 10);
// console.log(unRe.get("y"), 10, "The get method returns the value of a key");
// unRe.undo();
// console.log(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) {
// console.log(unRe.get("y"), 2);
// }
// console.log("###########################################################");
// var obj = {
// x: 1,
// y: 2,
// };
// var unRe = undoRedo(obj);
// unRe.set("y", 10);
// console.log(unRe.get("y"), 10, "The get method returns the value of a key");
// unRe.undo();
// console.log(unRe.get("y"), 2, "The undo method restores the previous state");
// unRe.redo();
// console.log(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) {
// console.log(unRe.get("y"), 10);
// }
// console.log("############################################################");
// 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);
// console.log(unRe.get("y"), 100, "The get method returns the value of a key");
// console.log(unRe.get("x"), 50, "The get method returns the value of a key");
// unRe.undo();
// console.log(unRe.get("x"), 150, "The undo method restores the previous state");
// console.log(unRe.get("y"), 100, "The y key stays the same");
// unRe.redo();
// console.log(unRe.get("x"), 50, "Undo the x value");
// console.log(unRe.get("y"), 100, "The y key stays the same");
// unRe.undo();
// unRe.undo();
// console.log(unRe.get("x"), 1, "Undo the x value");
// console.log(unRe.get("y"), 100, "The y key stays the same");
// unRe.undo();
// unRe.undo();
// console.log(unRe.get("y"), 2, "Undo the y value");
// console.log(unRe.get("x"), 1, "The x key stays the same");
// try {
// unRe.undo();
// Test.expect(false, "It should have thrown an exception");
// } catch (e) {
// console.log(unRe.get("y"), 2, "There is nothing to undo");
// }
// unRe.redo();
// unRe.redo();
// unRe.redo();
// unRe.redo();
// console.log(unRe.get("y"), 100, "y key redo state");
// console.log(unRe.get("x"), 50, "y key redo state");
// try {
// unRe.redo();
// console.log(false, "It should have thrown an exception");
// } catch (e) {
// console.log(unRe.get("y"), 100, "There is nothing to redo");
// }
// console.log("###########################################################");
// var obj = {
// x: 1,
// y: 2,
// };
// var unRe = undoRedo(obj);
// unRe.set("z", 10);
// console.log(unRe.get("z"), 10, "A new key has been added");
// unRe.undo();
// console.log(unRe.get("z"), undefined, "The z key should not exist");
// unRe.redo();
// console.log(unRe.get("z"), 10, "A new key has been added");
// console.log("'#########################################################'");
// var obj = {
// x: 1,
// y: 2,
// };
// var unRe = undoRedo(obj);
// unRe.del("x");
// console.log(unRe.get("x"), undefined, "The x key should not exist");
// console.log(!obj.hasOwnProperty("x"), "The x key should be deleted");
// unRe.undo();
// console.log(unRe.get("x"), 1, "A new key has been added");
// unRe.redo();
// console.log(unRe.get("x"), undefined, "The x key should not exist");
// console.log(!obj.hasOwnProperty("x"), "The x key should be deleted");