初始化项目
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* 语义化版本工具(1.3.4)。
|
||||
*
|
||||
* 只实现插件兼容性判断所需的最小子集:解析 major.minor.patch 与兼容性比较。
|
||||
* 纯函数,不依赖平台 API,可独立测试。
|
||||
*/
|
||||
export class SemVerUtil {
|
||||
/** 解析版本号;非法返回 undefined。 */
|
||||
static parse(version: string): number[] | undefined {
|
||||
if (version.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const parts: string[] = version.split('.');
|
||||
if (parts.length !== 3) {
|
||||
return undefined;
|
||||
}
|
||||
const out: number[] = [];
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const text: string = parts[i];
|
||||
if (text.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
for (let j = 0; j < text.length; j++) {
|
||||
const code: number = text.charCodeAt(j);
|
||||
if (code < 48 || code > 57) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
out.push(Number(text));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件要求的 API 版本是否可被当前版本满足。
|
||||
* 规则:主版本必须相同(不兼容变更需升主版本);要求的 minor/patch 不得高于当前。
|
||||
*/
|
||||
static isCompatible(required: string, current: string): boolean {
|
||||
const req: number[] | undefined = SemVerUtil.parse(required);
|
||||
const cur: number[] | undefined = SemVerUtil.parse(current);
|
||||
if (req === undefined || cur === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (req[0] !== cur[0]) {
|
||||
return false;
|
||||
}
|
||||
if (req[1] > cur[1]) {
|
||||
return false;
|
||||
}
|
||||
if (req[1] === cur[1] && req[2] > cur[2]) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user