-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-manager.js
More file actions
57 lines (47 loc) · 1.07 KB
/
version-manager.js
File metadata and controls
57 lines (47 loc) · 1.07 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
// https://maxcode.dev/problems/version-manager/
class VersionManager {
constructor(initialVersion = '0.1.0') {
this.versions = [initialVersion];
}
major() {
this._registerVersion('major');
return this;
}
minor() {
this._registerVersion('minor');
return this;
}
patch() {
this._registerVersion('patch');
return this;
}
rollback() {
if(this.versions.length <= 1) {
throw new Error('Cannot rollback!');
}
this.versions.pop();
return this;
}
release() {
return this.versions.at(-1);
}
_registerVersion(nextVersion) {
const [currMajor, currMinor, currPatch] = this.versions.at(-1).split('.').map(Number);
const versioningMap = {
major: `${currMajor + 1}.0.0`,
minor: `${currMajor}.${currMinor + 1}.0`,
patch: `${currMajor}.${currMinor}.${currPatch + 1 }`
}
this.versions.push(versioningMap[nextVersion]);
}
}
const vm = new VersionManager("2.0.3");
console.log(
vm
.major() // "3.0.0"
.minor() // "3.1.0"
.minor() // "3.2.0"
.minor() // "3.3.0"
.patch() // "3.3.1"
.release()
);