Skip to content

Commit 814da9b

Browse files
committed
新增:版本检测和匹配工具
1 parent cd12ea8 commit 814da9b

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

electron/lib/util.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,56 @@ export const ShellUtil = {
216216
return `"${p}"`
217217
}
218218
}
219+
220+
221+
export const VersionUtil = {
222+
/**
223+
* 检测版本是否匹配
224+
* @param v string
225+
* @param match string 如 * 或 >=1.0.0 或 >1.0.0 或 <1.0.0 或 <=1.0.0 或 1.0.0
226+
*/
227+
match(v: string, match: string) {
228+
if (match === '*') {
229+
return true
230+
}
231+
if (match.startsWith('>=') && this.ge(v, match.substring(2))) {
232+
return true
233+
}
234+
if (match.startsWith('>') && this.gt(v, match.substring(1))) {
235+
return true
236+
}
237+
if (match.startsWith('<=') && this.le(v, match.substring(2))) {
238+
return true
239+
}
240+
if (match.startsWith('<') && this.lt(v, match.substring(1))) {
241+
return true
242+
}
243+
return this.eq(v, match)
244+
},
245+
compare(v1: string, v2: string) {
246+
const v1Arr = v1.split('.')
247+
const v2Arr = v2.split('.')
248+
for (let i = 0; i < v1Arr.length; i++) {
249+
const v1Num = parseInt(v1Arr[i])
250+
const v2Num = parseInt(v2Arr[i])
251+
if (v1Num > v2Num) {
252+
return 1
253+
} else if (v1Num < v2Num) {
254+
return -1
255+
}
256+
}
257+
return 0
258+
},
259+
gt(v1: string, v2: string) {
260+
return VersionUtil.compare(v1, v2) > 0
261+
},
262+
ge(v1: string, v2: string) {
263+
return VersionUtil.compare(v1, v2) >= 0
264+
},
265+
lt(v1: string, v2: string) {
266+
return VersionUtil.compare(v1, v2) < 0
267+
},
268+
le: (v1: string, v2: string) => {
269+
return VersionUtil.compare(v1, v2) <= 0
270+
}
271+
}

src/lib/util.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ export const EncodeUtil = {
6363
}
6464

6565
export const VersionUtil = {
66+
/**
67+
* 检测版本是否匹配
68+
* @param v string
69+
* @param match string 如 * 或 >=1.0.0 或 >1.0.0 或 <1.0.0 或 <=1.0.0 或 1.0.0
70+
*/
71+
match(v: string, match: string) {
72+
if (match === '*') {
73+
return true
74+
}
75+
if (match.startsWith('>=') && this.ge(v, match.substring(2))) {
76+
return true
77+
}
78+
if (match.startsWith('>') && this.gt(v, match.substring(1))) {
79+
return true
80+
}
81+
if (match.startsWith('<=') && this.le(v, match.substring(2))) {
82+
return true
83+
}
84+
if (match.startsWith('<') && this.lt(v, match.substring(1))) {
85+
return true
86+
}
87+
return this.eq(v, match)
88+
},
6689
compare(v1: string, v2: string) {
6790
const v1Arr = v1.split('.')
6891
const v2Arr = v2.split('.')

0 commit comments

Comments
 (0)