简体   繁体   English

如何将此 Angular/TS 代码重构为几行以获得更好的性能和可读性?

[英]How to refactor this Angular/TS code into few lines for better performance & readability?

I have a getBaseUrl() method which assigns a string value to this.baseURL depending on the serviceType value.我有一个getBaseUrl()方法,它根据serviceType值将字符串值分配给this.baseURL

getBaseUrl(serviceType: string, network?: string) {
    this.network = network;

    const PROXY_13541_NEEDED_SERVICE_TYPES = ["pipInventory"];
    const PROXY_13561_NEEDED_SERVICE_TYPES = [
      "pipChangeManager",
      "pubChangeManagerdbw",
      "pubChangeManager",
      "pubChangeManager alert",
      "pipChangeManagerBulkvalidate",
    ];
    const PROXY_13651_NEEDED_SERVICE_TYPES = [
      "elineChangeManagerSubscribe",
      "elineChangeManager",
      "elanChangeManager",
      "elanChangeManagerBulkvalidate",
    ];
    const PROXY_13661_NEEDED_SERVICE_TYPES = [
      "enum-values-by-pagetype",
      "dnmCommon",
      "self-service",
      "dnmEline",
      "dnm",
      "dnmDelphiTestResult",
      "chatbot",
    ];
    const PROXY_13396_NEEDED_SERVICE_TYPES = ["pnp"];
    const PROXY_13621_NEEDED_SERVICE_TYPES = [
      "utilizationExportController",
      "utilizationExportController_pub",
      "bulkUtilizationPub",
    ];
    const PROXY_13751_NEEDED_SERVICE_TYPES = ["ccds"];
    const PROXY_13551_NEEDED_SERVICE_TYPES = ["routerservice"];
    const PROXY_13571_NEEDED_SERVICE_TYPES = [
      "pipAnalyticscheduled",
      "pipAnalytics",
      "pipAnalyticsNTD",
      "pipAnalyticsBulk",
    ];
    const PROXY_14150_NEEDED_SERVICE_TYPES = [
      "12and13workflow",
      "12and13workflowstatus",
      "dnmworkflow",
    ];
    const PROXY_14160_NEEDED_SERVICE_TYPES = ["errorManagement"];
    const PROXY_13601_NEEDED_SERVICE_TYPES = [
      "dnmActivationSupport",
      "activation",
    ];
    const PROXY_13631_NEEDED_SERVICE_TYPES = [
      "pubInventory",
      "pubInventorySubInprogress",
      "pubInventoryAccessSpeed",
    ];
    const PROXY_7303_NEEDED_SERVICE_TYPES = ["provRestBridge"];
    const PROXY_13271_NEEDED_SERVICE_TYPES = ["vams"];
    const PROXY_13822_NEEDED_SERVICE_TYPES = ["ucpe"];
    const PROXY_13641_NEEDED_SERVICE_TYPES = [
      "ethernetEline",
      "ethernetElineBulk",
      "ethernetAccess",
      "ethernetElan",
      "ethernetTest",
    ];
    const PROXY_13721_NEEDED_SERVICE_TYPES = ["sciChangeManager"];
    const PROXY_13711_NEEDED_SERVICE_TYPES = ["scicatalog"];
    const PROXY_13784_NEEDED_SERVICE_TYPES = ["livedashboard"];
    const PROXY_13182_NEEDED_SERVICE_TYPES = ["1mmsService"];
    const PROXY_13171_NEEDED_SERVICE_TYPES = [
      "pipVNS",
      "upiServices",
      "webComponent",
      "",
    ];

    this.options = this.getOptions();

    if (PROXY_13541_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13541/";
    } else if (PROXY_13561_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13561/";
    } else if (PROXY_13651_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13651/";
    } else if (PROXY_13661_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13661/";
    } else if (PROXY_13396_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13396/";
      this.options = this.getVnsOptions();
    } else if (PROXY_13621_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13621/";
    } else if (PROXY_13751_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13751/";
    } else if (PROXY_13551_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13551/";
    } else if (PROXY_13571_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13571/";
    } else if (PROXY_14150_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_14158/";
    } else if (PROXY_14160_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_14160/";
    } else if (PROXY_13601_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13601/";
    } else if (PROXY_13631_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13631/";
    } else if (PROXY_7303_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_7303/";
    } else if (PROXY_13271_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13271/";
      this.options = this.getVnsOptions();
    } else if (PROXY_13822_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13641/";
    } else if (PROXY_13641_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13721";
    } else if (PROXY_13721_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13721/";
    } else if (PROXY_13711_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13711/";
    } else if (PROXY_13784_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13784/";
    } else if (PROXY_13182_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13182/";
    } else if (PROXY_13171_NEEDED_SERVICE_TYPES.includes(serviceType)) {
      this.baseURL = "/PROXY_13171/";
      this.options = this.getVnsOptions();
    } else {
      this.baseURL = "/PROXY_UNDEFINED/";
      this.options = this.getVnsOptions();
    }
  }

I am expecting the method to be refactored in such a way that the method has我期望以该方法具有的方式重构该方法

  1. Few number of lines行数少
  2. Best performance and最佳性能和
  3. Good readability可读性好

Please help me on this...请帮我解决这个...

Follow below step and you will find better code structure, easy readability and Best performance.按照下面的步骤,您会发现更好的代码结构、易读性和最佳性能。

  1. Create separate file for your serviceTypeMap variable named service-type.enum.ts and put your array there as like below为名为service-type.enum.ts的 serviceTypeMap 变量创建单独的文件,并将数组放在那里,如下所示

Enums allow a developer to define a set of named constants.枚举允许开发人员定义一组命名常量。 Using enums can make it easier to document intent, or create a set of distinct cases.使用枚举可以更轻松地记录意图,或创建一组不同的案例。 TypeScript provides both numeric and string-based enums. TypeScript 提供基于数字和字符串的枚举。

 export enum ServiceTypeMap = { PROXY_13541: ["pipInventory"], PROXY_13561: [ "pipChangeManager", "pubChangeManagerdbw", "pubChangeManager", "pubChangeManager alert", "pipChangeManagerBulkvalidate" ], PROXY_13651: [ "elineChangeManagerSubscribe", "elineChangeManager", "elanChangeManager", "elanChangeManagerBulkvalidate" ], PROXY_13661: [ "enum-values-by-pagetype", "dnmCommon", "self-service", "dnmEline", "dnm", "dnmDelphiTestResult", "chatbot" ], PROXY_13396: ["pnp"], PROXY_13621: [ "utilizationExportController", "utilizationExportController_pub", "bulkUtilizationPub" ], PROXY_13751: ["ccds"], PROXY_13551: ["routerservice"], PROXY_13571: [ "pipAnalyticscheduled", "pipAnalytics", "pipAnalyticsNTD", "pipAnalyticsBulk" ], PROXY_14150: [ "12and13workflow", "12and13workflowstatus", "dnmworkflow" ], PROXY_14160: ["errorManagement"], PROXY_13601: [ "dnmActivationSupport", "activation" ], PROXY_13631: [ "pubInventory", "pubInventorySubInprogress", "pubInventoryAccessSpeed" ], PROXY_7303: ["provRestBridge"], PROXY_13271: ["vams"], PROXY_13822: ["ucpe"], PROXY_13641: [ "ethe.netEline", "ethe.netElineBulk", "ethe.netAccess", "ethe.netElan", "ethe.netTest" ], PROXY_13721: ["sciChangeManager"], PROXY_13711: ["scicatalog"], PROXY_13784: ["livedashboard"], PROXY_13182: ["1mmsService"], PROXY_13171: [ "pipVNS", "upiServices", "webComponent", "" ] };

  1. Let's define getBaseUrl() method now现在让我们定义 getBaseUrl() 方法

 /* Import your enum */ public getBaseUrl(serviceType: string,.network?: string) { this.network =.network; for (const [baseURL, neededServiceTypes] of Object.entries(ServiceTypeMap)) { if (neededServiceTypes.includes(serviceType)) { this.baseURL = `/${baseURL}/`; break; } } this.options = this.getOptions(); }

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM