autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
76 lines (75 loc) • 2.02 kB
JavaScript
export class Quality {
adaptation;
completeness;
documentation;
grade;
overall;
constructor(props = {}) {
/** 内容完整度 (0-1) */
this.completeness = props.completeness ?? 0;
/** 项目适配度 (0-1) */
this.adaptation = props.adaptation ?? 0;
/** 文档清晰度 (0-1) */
this.documentation = props.documentation ?? 0;
/** 综合分 (0-1) */
this.overall = props.overall ?? 0;
/** 等级 A-F */
this.grade = props.grade || Quality.calcGrade(this.overall);
}
/** 从任意输入构造 Quality */
static from(input) {
if (input instanceof Quality) {
return input;
}
if (typeof input === 'string') {
try {
input = JSON.parse(input);
}
catch {
return new Quality();
}
}
return new Quality((input || {}));
}
/** 从 3 维度计算综合分 */
recalculate() {
this.overall =
Math.round(((this.completeness + this.adaptation + this.documentation) / 3) * 100) / 100;
this.grade = Quality.calcGrade(this.overall);
return this;
}
/**
* 根据分数计算等级
* @param score 0-1
*/
static calcGrade(score) {
if (score >= 0.9) {
return 'A';
}
if (score >= 0.75) {
return 'B';
}
if (score >= 0.6) {
return 'C';
}
if (score >= 0.4) {
return 'D';
}
return 'F';
}
/** 转换为 wire format JSON */
toJSON() {
return {
completeness: this.completeness,
adaptation: this.adaptation,
documentation: this.documentation,
overall: this.overall,
grade: this.grade,
};
}
/** 从 wire format 创建 */
static fromJSON(data) {
return Quality.from(data);
}
}
export default Quality;