由于有很多 if 语句,下面的代码非常复杂。如何简化这部分代码?
把最新的else if改成无条件的else语句,代码会不会简化?它会以同样的方式起作用吗?
parse(value: string): NgbDateStruct {
if (value) {
const date = value.trim().split('-');
if (date.length === 1 && isNumber(date[0])) {
return {
year: toInteger(date[0]),
month: undefined as any,
day: undefined as any
};
} else if (date.length === 2 && isNumber(date[0]) && isNumber(date[1])) {
return {
year: toInteger(date[1]),
month: toInteger(date[0]),
day: undefined as any
};
} else if (date.length === 3 && isNumber(date[0]) && isNumber(date[1]) && isNumber(date[2])) {
return {
year: toInteger(date[2]),
month: toInteger(date[1]),
day: toInteger(date[0])
};
}
}
return undefined as any;
}
潇潇雨雨
长风秋雁
相关分类