简体   繁体   中英

Simplifying writing this JSON-object in TypeScript

I am looking to remove the many lines of duplicate code. The issue is basically to generate a JSON.

The last line in the search criteria either has a "hasValue"-attribute or a "value"-attribute. It's written in TypeScript and I can't use the {} notation for writing JavaScript code there.

The code works as is, but 95% is duplicate code because of the requirement of a different attribute in case of hasValue=false.

Is there a much more concise way to write it?

export const valueSearch = (value: string, hasEmptyValue: boolean, profile: ProfileHolder, attributeId: string, objectTypeId: string, typeName: string = "ValueSearch") => {
    if (!hasValue) {
        return {
            "__typename": "SearchFromProfile",
            "profileHolder": {
                "__typename": profile.__typename,
                "Id": profile.Id
            },
            "search": {
                "__typename": "HasValue",
                "objectTypeId": objectTypeId,
                "attributeId": attributeId,
                "hasValue": false
            }
        };
    }
    else {
        return {
            "__typename": "SearchFromProfile",
            "profileHolder": {
                "__typename": profile.__typename,
                "Id": profile.Id
            },
            "search": {
                "__typename": typeName,
                "objectTypeId": objectTypeId,
                "attributeId": attributeId,
                "value": value
            }
        };
    }
};

There are basically two fields difference between the two objets, so you can compute that difference and spread it on top of a common base:

export const valueSearch = (
  value: string,
  hasEmptyValue: boolean,
  profile: ProfileHolder,
  attributeId: string,
  objectTypeId: string,
  typeName: string = "ValueSearch"
) => {
  const diff = hasValue
    ? {
      "value": value,
      "__typename": typeName
    }
    : {
      "hasValue": false,
      "__typename": "HasValue"
    };

  return {
    "__typename": "SearchFromProfile",
    "profileHolder": {
      "__typename": profile.__typename,
      "Id": profile.Id
    },
    "search": {
      "__typename": "HasValue",
      "objectTypeId": objectTypeId,
      "attributeId": attributeId,
      ...diff
    }
  };
};

Create an object without that properties outside the if and add them later inside the check.

export const valueSearch = (value: string, hasEmptyValue: boolean, profile: ProfileHolder, attributeId: string, objectTypeId: string, typeName: string = "ValueSearch") => {
    const obj = {
        "__typename": "SearchFromProfile",
        "profileHolder": {
            "__typename": profile.__typename,
            "Id": profile.Id
        },
        "search": {
            "__typename": "HasValue",
            "objectTypeId": objectTypeId,
            "attributeId": attributeId
        }
    };
    if (!hasValue) {
        obj.search.hasValue = false
    }
    else {
        obj.search.value = value
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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