MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / versionCompare

Function versionCompare

src/pkg/utils/semver.ts:14–49  ·  view source on GitHub ↗
(version1: string, version2: string)

Source from the content-addressed store, hash-verified

12// 参考 https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/version/format
13// 考虑 UserScript 的脚本号设计进行简化及修改
14export const versionCompare = (version1: string, version2: string): VersionCompare => {
15 // -1 if version1 < version2
16 // 0 if version1 == version2
17 // 1 if version1 > version2
18 const v1 = version1.split(".").map((e) => e.split(/(\D+)/g));
19 const v2 = version2.split(".").map((e) => e.split(/(\D+)/g));
20 const maxI = Math.max(v1.length, v2.length) - 1;
21 let i = -1;
22 while (++i <= maxI) {
23 const e1 = v1[i] || [];
24 const e2 = v2[i] || [];
25 const maxJ = Math.max(e1.length, e2.length) - 1;
26 let j = -1;
27 while (++j <= maxJ) {
28 let w1: number | string = e1[j] || "";
29 let w2: number | string = e2[j] || "";
30 if (+(w1 || "0") === 0 && +(w2 || "0") === 0) continue; // 空值与零值等价
31 const isCharCmp = (j & 1) === 1;
32 if (!isCharCmp) {
33 // 数值对比
34 w1 = +w1 || 0;
35 w2 = +w2 || 0;
36 if (w1 === w2) continue;
37 return w1 < w2 ? -1 : 1;
38 }
39 if (!w1 && w2) return 1;
40 if (!w2 && w1) return -1;
41 // 忽略非英文字符的变更进行对比
42 const w1c = w1.replace(/[^a-zA-Z]/g, ".").toLowerCase(); // 不区分大小写
43 const w2c = w2.replace(/[^a-zA-Z]/g, ".").toLowerCase(); // 不区分大小写
44 const c = w1c.localeCompare(w2c);
45 if (c) return c;
46 }
47 }
48 return 0;
49};

Callers 4

AppFunction · 0.90
twoWayTestFunction · 0.90
checkSkillUpdateMethod · 0.90
lteverFunction · 0.85

Calls

no outgoing calls

Tested by 1

twoWayTestFunction · 0.72