如何简化这段代码以降低复杂度?

由于有很多 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;

}


Smart猫小萌
浏览 131回答 2
2回答

潇潇雨雨

好吧,你的代码想起来很简单,但是很长!!如果你想要示例,我有 javascript vanilla 代码:function parse (value) {    var list = (value + "").trim().split("-").reverse();    if (list != 0 && list.every(x => !isNaN(x))) {        return {            year: parseInt(list[0]),            month: parseInt(list[1]) || undefined,            day: parseInt(list[2]) || undefined,        }    }    return undefined;}// below for testing[    "08-11-2020",    "11-2020",    "2020",    ""].forEach(x => {console.log(x, parse(x))});不幸的是,我没有任何实现打字稿代码的经验,但上面的 JavaScript 代码可能会对你有所帮助。实际上我缩短了代码,因为 javascript 带来的优点(缺点?),也许转换为 typescript 不会有太大区别!附加打字稿代码:在 typescriptlang.org 上type NgbDateStruct = {    year?: number;    month?: number;    day?: number;};function parse (value: string) : NgbDateStruct {    var list = (value + "").trim().split("-").reverse().map(x => x || -1).map(Number);    if (list.length > 0 && list.every(x => !isNaN(Number(x))) && list.every(x => x > -1)) {        return {            year: list[0] || undefined,            month: list[1] || undefined,            day: list[2] || undefined,        }    }    return undefined as any;}// below for testing[    "08-11-2020",    "11-2020",    "2020",    "",    "nothing"].forEach(x => {console.log(x, parse(x))});

长风秋雁

一种选择是使用正则表达式来匹配数字,可能会散布-s:const getDateObj = value => {  const match = value.match(/(^\d+)(?:-(\d+)(?:-(\d+))?)?$/);  if (!match) return undefined;  const [, first, second, third] = match.filter(group => group !== undefined).map(Number);  if (third) {    // All were matched    return {      year: third,      month: second,      day: first    };  } else if (second) {    // Only first two were matched    return {      year: second,      month: first    }  }  return {    year: first  }};console.log(getDateObj('31-12-2000'));console.log(getDateObj('12-2000'));console.log(getDateObj('2000'));console.log(getDateObj('incorrect'));在 TypeScript 语法中:type NgbDateStruct = {    year: number;    month?: number;    day?: number;};const getDateObj = (value: string): NgbDateStruct | undefined => {    const match = value.match(/(^\d+)(?:-(\d+)(?:-(\d+))?)?$/);    if (!match) return undefined;    const [, first, second, third] = match.filter(group => group !== undefined).map(Number);    if (third) {        // All were matched        return {            year: third,            month: second,            day: first,        };    }    if (second) {        // Only first two were matched        return {            year: second,            month: first,        };    }    return {        year: first,    };};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript