The repeatability of these function is too high, how to optimize it better?

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
  }

the part of ts feels too repetitive. How to optimize it? is there a way to merge 3 function into one?


function oCode_test1(oCode){
    // , 1st|h2|q1|q3|q4, 
    return /^([a-z])\1(1st|h2|q1|q3|q4|)$/.test(oCode)
}
  • The question about Angular CanActivate

    export class AuthGuard implements CanActivate { constructor(private PassportService: PassportService, private CookieService: CookieService, private router: Router) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {...

Menu