undefinedfix
Sign in

How can optimization be better if the repeatability of these functions is too high?

CircleOnCircles edited in Thu, 30 Jun 2022

html :

<ng-container *ngIf="oCode_test1(table.oCode)'">
    <span>{{aaa}}:</span>
</ng-container>
<ng-container *ngIf="oCode_test2(table.oCode)'">
    <span>{{bbb}}:</span>
</ng-container>
<ng-container *ngIf="oCode_test3(table.oCode)'">
    <span>{{ccc}}:</span>
</ng-container>

ts :

  oCode_test1(oCode){
    if(oCode == 'aa' || oCode == 'aa1st' || oCode == 'aah2' ||
       oCode == 'aaq1' || oCode == 'aaq3' || oCode == 'aaq4'){
         return true
       }
       return false
  }

  oCode_test2(oCode){
    if(oCode == 'qq' || oCode == 'qq1st' || oCode == 'qqh2' ||
       oCode == 'qqq1' || oCode == 'qqq3' || oCode == 'qqq4'){
         return true
       }
       return false
  }

  oCode_test3(oCode){
    if(oCode == 'ww' || oCode == 'ww1st' || oCode == 'wwh2' ||
       oCode == 'wwq1' || oCode == 'wwq3' || oCode == 'wwq4'){
         return true
       }
       return false
  }

How to optimize the repeatability of TS? Is there a way to combine the three functions into one?

4 Replies
acnol
commented on Thu, 30 Jun 2022
public readonly testCaseOne = ['aa', 'aa1st', 'aah2', 'aaq1', 'aaq3', 'aaq4'];
public oCode_test(oCode, testCase) {
    return testCase.includes(oCode);
}
<ng-container *ngIf="oCode_test(table.oCode, testCaseOne)">
    <span>{{aaa}}:</span>
</ng-container>
Paul
commented on Thu, 30 Jun 2022
    if (oCode.startsWith('aa')) {
        return ['aa', 'aa1st', ...].includes(oCOde)
    } else if (oCode.startsWith('qq') {
        return ['qq', 'qq1st', ...].includes(oCode)
    } else if (oCode.startsWith('ww')) {
        return ['ww', 'ww1st', ...].includes(oCode)
    } else {
        return false
    }
KirkSu
commented on Fri, 01 Jul 2022

Pseudo code, to the effect that

<ng-container>
    <span>{{oCode_test(table.oCode)}}:</span>
</ng-container>

oCode_test(oCode){
    switch oCode
        case 'aa'
        case 'aa1s'
        return aa;
        break;
        case 'qq':
        case 'qq1s':
        return bb;
        break;
        case 'ww':
        case 'ww1s':
        return cc;
        break;
        default:
        break;
}
aleonjhon
commented on Fri, 01 Jul 2022
function oCode_test1(oCode){
    // 以一个小写字母且重复一次为开头, 后跟1st|h2|q1|q3|q4或者空, 为结尾
    return /^([a-z])\1(1st|h2|q1|q3|q4|)$/.test(oCode)
}
lock This question has been locked and the reply function has been disabled.