-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundo-slash-redo.js
88 lines (79 loc) · 2.38 KB
/
undo-slash-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
// 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:
// 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.
function undoRedo(object) {
var history = [];
var historyIndex = 0;
var historySize = 0;
var SetCmd = function (key, before, after) {
this.key = key;
this.before = before;
this.after = after;
};
SetCmd.prototype.do = function () {
object[this.key] = this.after;
historyIndex++;
};
SetCmd.prototype.undo = function () {
object[this.key] = this.before;
historyIndex--;
};
var DelCmd = function (key, before) {
this.key = key;
this.before = before;
};
DelCmd.prototype.do = function () {
delete object[this.key];
historyIndex++;
};
DelCmd.prototype.undo = function () {
object[this.key] = this.before;
historyIndex--;
};
return {
// set(key, value) Assigns the value to the key. If the key does
// not exist, creates it.
set: function (key, value) {
history[historyIndex] = new SetCmd(key, object[key], value);
history[historyIndex].do();
historySize = historyIndex;
},
// get(key) Returns the value associated to the key.
get: function (key) {
return object[key];
},
// del(key) removes the key from the object.
del: function (key) {
history[historySize] = new DelCmd(key, object[key]);
history[historySize].do();
historySize = historyIndex;
},
// undo() Undo the last operation (set or del) on the
// object. Throws an exception if there is no operation to undo.
undo: function () {
if(historyIndex <= 0) {
throw "No more undo";
} else {
history[historyIndex - 1].undo();
}
},
// redo() Redo the last undo operation (redo is only possible
// after an undo). Throws an exception if there is no operation to
// redo.
redo: function () {
if(historyIndex >= historySize) {
throw "No more redo";
} else {
history[historyIndex].do();
}
}
};
}
var obj = {x: 1, y:2};
var unRe = undoRedo(obj);
console.log(unRe.get('x'));
unRe.set('x', 2);
console.log(unRe.get('x'));