/
index.ts
307 lines (226 loc) · 8.55 KB
/
index.ts
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const exclude = <O extends object>(object: O, keys: (keyof O)[]) => Object.fromEntries(Object.entries(object).filter(([k]) => !keys.includes(k as keyof O)));
class Snapshot<S extends object> {
#id: string;
#snapshot: S;
#message: string;
constructor(snapshot: S, message?: string) {
if (typeof snapshot !== "object" || snapshot === null) throw new Error("The snapshot must be an object");
if (typeof message !== "undefined") {
if (typeof message !== "string") throw new Error("Commit message must be a string.");
if (message.length > 65536) throw new Error("Commit message cannot be over 65536 characters.");
}
//@ts-ignore
this.#id = (
Date.now().toString(16) +
[...(typeof message === "string" ? message : "")].reduce((a, b) => a + b.charCodeAt(0), 0).toString(16) +
Math.floor(Math.random() * 4294967296).toString(16)
).padEnd(64, "0");
this.#snapshot = snapshot;
this.#message = typeof message === "string" ? message : "";
}
get id() {
return this.#id;
}
get snapshot() {
return this.#snapshot;
}
get message() {
return this.#message;
}
}
class Branch {
#name: string;
#snapshots: Snapshot<any>[];
constructor(name: string, snapshot?: Snapshot<any>, message?: string) {
this.#name = name;
this.#snapshots = typeof snapshot === "object" ? [new Snapshot(Object.assign({}, snapshot), message)] : [];
}
commit(snapshot: Snapshot<any>, message?: string) {
this.#snapshots.push(new Snapshot(Object.assign({}, snapshot), message));
return this;
}
revert(id?: string) {
if (this.#snapshots.length <= 1) throw new Error("Already at earliest change.");
if (typeof id === "string") {
const index = this.#snapshots.findIndex((s) => s.id === id);
if (index < 0) throw new Error("Cannot revert because there is no snapshot with that id.");
this.#snapshots = this.#snapshots.slice(0, index);
} else this.#snapshots.pop();
return this.latest;
}
get name() {
return this.#name;
}
find(id: string) {
return this.#snapshots.find((s) => s.id === id);
}
get latest() {
return this.#snapshots[this.#snapshots.length - 1];
}
}
class History {
#branches = new Map();
#current;
constructor(snapshot: Snapshot<any>, opts?: { master?: string; message?: string }) {
let master = "master";
if (typeof opts === "object" && opts) {
if (typeof opts.master === "string" && opts.master) master = opts.master;
}
this.#branches.set(master, new Branch(master, snapshot, opts && opts.message));
this.#current = this.#branches.get(master).latest.id;
}
get current() {
return this.#current;
}
get currentSnapshot() {
return this.find(this.#current)!.snapshot;
}
checkout(name: string) {
if (!this.#branches.has(name)) throw new Error("Branch doesn't exist.");
this.#current = this.#branches.get(name).latest.id;
return this;
}
branch(name: string, snapshot: Snapshot<any>, opts?: { m: boolean; b: boolean }) {
if (this.#branches.has(name) && !(opts && opts.m)) throw new Error("Branch already exists.");
const branch = new Branch(name, snapshot);
this.#branches.set(name, branch);
if (opts && opts.b) this.#current = branch.latest.id;
return this;
}
commit(snapshot: Snapshot<any>, message?: string) {
const { branch } = this.find(this.#current)!;
branch.commit(snapshot, message);
this.#current = branch.latest.id;
return this;
}
revert(id: string) {
const { branch } = this.find(this.#current)!;
const snapshot = branch.revert(id);
this.#current = snapshot.id;
return snapshot;
}
find(id: string) {
for (const branch of [...this.#branches.values()]) {
const snapshot = branch.find(id);
if (snapshot)
return {
branch,
snapshot,
};
}
return;
}
}
export default class Zed<T extends object> {
#object?: object;
#history: History;
//@ts-ignore
static #Simple = class Simple<T extends object> {
#object?: object;
#snapshots: object[];
constructor(object?: T) {
if (this.constructor === Simple) {
if (!object) throw new Error("Cannot instantiate an instance of Zed.Simple without an object.");
if (typeof object !== "object" || object === null) throw new Error("Zed.Simple requires an object to track.");
this.#object = object;
this.#snapshots = [Object.assign({}, this.#object)];
} else {
this.#snapshots = [Object.assign({}, exclude(this, ["latest", "commit", "revert"]))];
}
}
get latest() {
if (this.constructor === Simple) return this.#object;
return this;
}
commit() {
if (this.constructor === Simple) this.#snapshots.push(Object.assign({}, this.#object));
else this.#snapshots.push(Object.assign({}, exclude(this, ["latest", "commit", "revert"])));
return this;
}
revert(times?: number, force?: boolean) {
if (typeof times === "number") {
if (times > this.#snapshots.length - 1 && !force) throw new Error("Can't revert that many times.");
this.#snapshots = this.#snapshots.slice(0, Math.max(1, this.#snapshots.length - times));
} else this.#snapshots.pop();
if (this.constructor === Simple) this.#object = this.#snapshots[this.#snapshots.length - 1];
else
Object.entries(this.#snapshots[this.#snapshots.length - 1]).forEach(([k, v]) => {
//@ts-ignore
this[k] = v;
});
return this;
}
};
constructor(
object: T | any,
opts?: {
master?: string | undefined;
message?: string | undefined;
}
) {
if (this.constructor === Zed) {
if (!object) throw new Error("Cannot instantiate an instance of Zed without an object.");
if (typeof object !== "object" || object === null) throw new Error("Zed requires an object to track.");
this.#object = object;
this.#history = new History(object, opts);
} else {
//@ts-ignore
this.#history = new History(Object.assign({}, exclude(this, ["latest", "commit", "revert"])), object);
}
}
get latest() {
if (this.constructor === Zed) return this.#object;
return this;
}
checkout(name: string) {
this.#history.checkout(name);
if (this.constructor === Zed) this.#object = this.#history.currentSnapshot.snapshot;
else
Object.entries(this.#history.currentSnapshot.snapshot).forEach(([k, v]) => {
//@ts-ignore
this[k] = v;
});
return this;
}
branch(
name: string,
opts?: {
m: boolean;
b: boolean;
}
) {
if (this.constructor === Zed) {
//@ts-ignore
this.#history.branch(name, this.#object, opts);
this.#object = this.#history.currentSnapshot.snapshot;
} else {
//@ts-ignore
this.#history.branch(name, Object.assign({}, exclude(this, ["latest", "commit", "revert"])), opts);
Object.entries(this.#history.currentSnapshot.snapshot).forEach(([k, v]) => {
//@ts-ignore
this[k] = v;
});
}
return this;
}
commit(message: string) {
//@ts-ignore
if (this.constructor === Zed) this.#history.commit(this.#object, message);
//@ts-ignore
else this.#history.commit(Object.assign({}, exclude(this, ["latest", "commit", "revert"])), message);
return this;
}
revert(id: string) {
this.#history.revert(id);
if (this.constructor === Zed) this.#object = this.#history.currentSnapshot.snapshot;
else
Object.entries(this.#history.currentSnapshot.snapshot).forEach(([k, v]) => {
//@ts-ignore
this[k] = v;
});
return this;
}
static get Simple() {
return this.#Simple;
}
}