/
version.js
118 lines (116 loc) · 2.56 KB
/
version.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
/*!
* @author myax <mig_dj@hotmail.com>
* date 10/26/2018
* implement pa-dss version tag
*/
class Tag {
constructor() {
this._tagRegex = /\d+\.\d+\.\d+\.\d+\.\d+/;
this._major = 0;
this._minor = 0;
this._secure = 0;
this._crud = 0;
this._interface = 0;
}
/**
* @param {String} tag `''` tag version to parse
* @private
*/
parse(tag) {
if (typeof tag == "string" && this._tagRegex.test(tag.trim())) {
const tagElements = tag.split(".");
this._major = parseInt(tagElements[0]);
this._minor = parseInt(tagElements[1]);
this._secure = parseInt(tagElements[2]);
this._crud = parseInt(tagElements[3]);
this._interface = parseInt(tagElements[4]);
} else {
throw this.error("Invalid tag pattern", 1);
}
}
/**
* @param {String} message `Error` Error message
* @param {Number} code `0` Error code
* @private
*/
error(message = "Error", code = 0) {
return {
message: message,
code: code
};
}
/**
* increase tag version
* @param {Number} tag `` tag version
* @param {String} mode `` change mode
* @public
*/
up(tag, mode) {
return this.change(tag, mode, true);
}
/**
* decrease tag version
* @param {Number} tag `` tag version
* @param {String} mode `` change mode
* @public
*/
down(tag, mode) {
return this.change(tag, mode, false);
}
/**
* decrease tag version
* @param {Number} tag `` tag version
* @param {String} mode `` change mode ['major', 'minor', 'secure', 'crud', 'interface']
* @param {Boolean} inc `false` increase or decrease tag version
* @private
*/
change(tag, mode, inc) {
this.parse(tag);
const add = inc ? 1 : -1;
switch (mode) {
case "major":
{
this._major += add;
this._minor = 0;
this._secure = 0;
this._crud = 0;
this._interface = 0;
}
break;
case "minor":
{
this._minor += add;
this._secure = 0;
this._crud = 0;
this._interface = 0;
}
break;
case "secure":
{
this._secure += add;
}
break;
case "crud":
{
this._crud += add;
}
break;
case "interface":
{
this._interface += add;
}
break;
default:
break;
}
return [
this._major,
this._minor,
this._secure,
this._crud,
this._interface
].join(".");
}
}
// export default Tag;
module.exports = new Tag();