Home > Software design >  How can I pass a condition into my function?
How can I pass a condition into my function?

Time:01-27

this is my function: I want to pass a condition to change my "selectedConformityStatus" in line 7.

const externalFilter = function () {
  if (!__externalFilter) {
    __externalFilter = {
      gridApi: undefined,
      selectedSubjects: [],
      selectedGeneralRequirement: "my_responsibility",
      selectedConformityStatus: [
        "conforms",
        "no_conforms",
        "not_applicable",
        "initial_status",
      ],
      selectedAnalysisResultStatus: "all",
      isExternalFilterPresent: function (params) {
        return (
          (this.selectedSubjects && this.selectedSubjects.length > 0) ||
          this.selectedGeneralRequirement !== "all" ||
          this.selectedConformityStatus.length > 0 ||
          this.selectedAnalysisResultStatus !== "all"
        );
      },
      doesExternalFilterPass: function (node) {
        const systemOperatorId = _.get(this, "loggedUser.systemOperatorId");

        let systemOperator_AllRequirements = false,
          systemOperator_Requirements = [];

        if (systemOperatorId) {
          const systemOperatorEntry = _.get(
            this.project,
            "systemOperators",
            []
          ).find((s) => _.get(s, "systemOperator._id") === systemOperatorId);

          if (systemOperatorEntry) {
            systemOperator_AllRequirements =
              systemOperatorEntry.allRequirements === "yes";
            systemOperator_Requirements =
              systemOperatorEntry.requirements || [];
          }
        }

        const subjectCondition =
          !this.selectedSubjects ||
          this.selectedSubjects.length === 0 ||
          this.selectedSubjects.includes(
            _.get(
              node,
              "data.dataOverride.subject._id",
              _.get(
                node,
                "data.dataOverride.subject",
                _.get(node, "data.requirement.subject._id")
              )
            )
          );
        const generalRequirementCondition =
          !this.selectedGeneralRequirement ||
          this.selectedGeneralRequirement === "all" ||
          (this.selectedGeneralRequirement === "my_responsibility" &&
            (systemOperator_AllRequirements === true ||
              systemOperator_Requirements.some(
                (r) =>
                  _.get(node, "data.requirement.generalRequirement._id") ===
                  r._id
              ))) ||
          _.get(node, "data.requirement.generalRequirement._id") ===
            this.selectedGeneralRequirement;
        const conformityStatusCondition =
          !this.selectedConformityStatus ||
          this.selectedConformityStatus.length === 0 ||
          this.selectedConformityStatus.includes(
            _.get(node, "data.conformityStatus")
          );
        const conformityAnalysisResultStatus =
          !this.selectedAnalysisResultStatus ||
          this.selectedAnalysisResultStatus === "all" ||
          (this.selectedAnalysisResultStatus === "all_awainting_analysis" &&
            [
              "awaiting_analysis",
              "awaiting_correction",
              "awaiting_review",
            ].includes(_.get(node, "data.currentStatus"))) ||
          _.get(node, "data.currentStatus") ===
            this.selectedAnalysisResultStatus;

        return (
          subjectCondition &&
          generalRequirementCondition &&
          conformityStatusCondition &&
          conformityAnalysisResultStatus
        );
      },
      setGridApi: function (params) {
        if (params && params.gridApi) {
          this.gridApi = params.gridApi;
        }
      },
      onChangeSubjects: function (subjects) {
        this.selectedSubjects = subjects || [];
        this.onFilterChanged();
      },
      onChangeGeneralRequirement: function (item) {
        this.selectedGeneralRequirement = item && item.value;
        this.onFilterChanged();
      },
      onChangeConformityStatus: function (value) {
        this.selectedConformityStatus = value;
        this.onFilterChanged();
      },
      onChangeAnalysisResultStatus: function (item) {
        this.selectedAnalysisResultStatus = item && item.value;
        this.onFilterChanged();
      },
      onFilterChanged: function (item) {
        if (this.gridApi) {
          this.gridApi.onFilterChanged();
        }
      },
    };
  }
  return __externalFilter;
};

I want to pass a condition to change my "selectedConformityStatus" in line 7. Like,

if (__checklist.project.analysisData?.actionPlan == 'yes') {
  let selectedConformityStatus: [
    "conforms",
    "no_conforms",
    "not_applicable",
    "initial_status",
  ]
} else {
  selectedConformityStatus: [
    "conforms",
    "no_conforms",
    "not_applicable"
  ]
}

I allready tried like that, pass the condition with the !__externalFilter), but the project brokes.

const externalFilter = function () {
  if (!__externalFilter && __checklist.project.analysisData?.actionPlan == 'yes') {
    __externalFilter = {
      gridApi: undefined,
      selectedSubjects: [],
      selectedGeneralRequirement: "my_responsibility",
      selectedConformityStatus: [
        "conforms",
        "no_conforms",
        "not_applicable",
        "initial_status",
      ],
      selectedAnalysisResultStatus: "all",
      isExternalFilterPresent: function (params) {
        return (
          (this.selectedSubjects && this.selectedSubjects.length > 0) ||
          this.selectedGeneralRequirement !== "all" ||
          this.selectedConformityStatus.length > 0 ||
          this.selectedAnalysisResultStatus !== "all"
        );
      },
      doesExternalFilterPass: function (node) {
        const systemOperatorId = _.get(this, "loggedUser.systemOperatorId");

        let systemOperator_AllRequirements = false,
          systemOperator_Requirements = [];

        if (systemOperatorId) {
          const systemOperatorEntry = _.get(
            this.project,
            "systemOperators",
            []
          ).find((s) => _.get(s, "systemOperator._id") === systemOperatorId);

          if (systemOperatorEntry) {
            systemOperator_AllRequirements =
              systemOperatorEntry.allRequirements === "yes";
            systemOperator_Requirements =
              systemOperatorEntry.requirements || [];
          }
        }

        const subjectCondition =
          !this.selectedSubjects ||
          this.selectedSubjects.length === 0 ||
          this.selectedSubjects.includes(
            _.get(
              node,
              "data.dataOverride.subject._id",
              _.get(
                node,
                "data.dataOverride.subject",
                _.get(node, "data.requirement.subject._id")
              )
            )
          );
        const generalRequirementCondition =
          !this.selectedGeneralRequirement ||
          this.selectedGeneralRequirement === "all" ||
          (this.selectedGeneralRequirement === "my_responsibility" &&
            (systemOperator_AllRequirements === true ||
              systemOperator_Requirements.some(
                (r) =>
                  _.get(node, "data.requirement.generalRequirement._id") ===
                  r._id
              ))) ||
          _.get(node, "data.requirement.generalRequirement._id") ===
            this.selectedGeneralRequirement;
        const conformityStatusCondition =
          !this.selectedConformityStatus ||
          this.selectedConformityStatus.length === 0 ||
          this.selectedConformityStatus.includes(
            _.get(node, "data.conformityStatus")
          );
        const conformityAnalysisResultStatus =
          !this.selectedAnalysisResultStatus ||
          this.selectedAnalysisResultStatus === "all" ||
          (this.selectedAnalysisResultStatus === "all_awainting_analysis" &&
            [
              "awaiting_analysis",
              "awaiting_correction",
              "awaiting_review",
            ].includes(_.get(node, "data.currentStatus"))) ||
          _.get(node, "data.currentStatus") ===
            this.selectedAnalysisResultStatus;

        return (
          subjectCondition &&
          generalRequirementCondition &&
          conformityStatusCondition &&
          conformityAnalysisResultStatus
        );
      },
      setGridApi: function (params) {
        if (params && params.gridApi) {
          this.gridApi = params.gridApi;
        }
      },
      onChangeSubjects: function (subjects) {
        this.selectedSubjects = subjects || [];
        this.onFilterChanged();
      },
      onChangeGeneralRequirement: function (item) {
        this.selectedGeneralRequirement = item && item.value;
        this.onFilterChanged();
      },
      onChangeConformityStatus: function (value) {
        this.selectedConformityStatus = value;
        this.onFilterChanged();
      },
      onChangeAnalysisResultStatus: function (item) {
        this.selectedAnalysisResultStatus = item && item.value;
        this.onFilterChanged();
      },
      onFilterChanged: function (item) {
        if (this.gridApi) {
          this.gridApi.onFilterChanged();
        }
      },
    };
  } else {
    __externalFilter = {
      gridApi: undefined,
      selectedSubjects: [],
      selectedGeneralRequirement: "my_responsibility",
      selectedConformityStatus: [
        "conforms",
        "no_conforms",
        "not_applicable",
      ],
      selectedAnalysisResultStatus: "all",
      isExternalFilterPresent: function (params) {
        return (
          (this.selectedSubjects && this.selectedSubjects.length > 0) ||
          this.selectedGeneralRequirement !== "all" ||
          this.selectedConformityStatus.length > 0 ||
          this.selectedAnalysisResultStatus !== "all"
        );
      },
      doesExternalFilterPass: function (node) {
        const systemOperatorId = _.get(this, "loggedUser.systemOperatorId");

        let systemOperator_AllRequirements = false,
          systemOperator_Requirements = [];

        if (systemOperatorId) {
          const systemOperatorEntry = _.get(
            this.project,
            "systemOperators",
            []
          ).find((s) => _.get(s, "systemOperator._id") === systemOperatorId);

          if (systemOperatorEntry) {
            systemOperator_AllRequirements =
              systemOperatorEntry.allRequirements === "yes";
            systemOperator_Requirements =
              systemOperatorEntry.requirements || [];
          }
        }

        const subjectCondition =
          !this.selectedSubjects ||
          this.selectedSubjects.length === 0 ||
          this.selectedSubjects.includes(
            _.get(
              node,
              "data.dataOverride.subject._id",
              _.get(
                node,
                "data.dataOverride.subject",
                _.get(node, "data.requirement.subject._id")
              )
            )
          );
        const generalRequirementCondition =
          !this.selectedGeneralRequirement ||
          this.selectedGeneralRequirement === "all" ||
          (this.selectedGeneralRequirement === "my_responsibility" &&
            (systemOperator_AllRequirements === true ||
              systemOperator_Requirements.some(
                (r) =>
                  _.get(node, "data.requirement.generalRequirement._id") ===
                  r._id
              ))) ||
          _.get(node, "data.requirement.generalRequirement._id") ===
            this.selectedGeneralRequirement;
        const conformityStatusCondition =
          !this.selectedConformityStatus ||
          this.selectedConformityStatus.length === 0 ||
          this.selectedConformityStatus.includes(
            _.get(node, "data.conformityStatus")
          );
        const conformityAnalysisResultStatus =
          !this.selectedAnalysisResultStatus ||
          this.selectedAnalysisResultStatus === "all" ||
          (this.selectedAnalysisResultStatus === "all_awainting_analysis" &&
            [
              "awaiting_analysis",
              "awaiting_correction",
              "awaiting_review",
            ].includes(_.get(node, "data.currentStatus"))) ||
          _.get(node, "data.currentStatus") ===
            this.selectedAnalysisResultStatus;

        return (
          subjectCondition &&
          generalRequirementCondition &&
          conformityStatusCondition &&
          conformityAnalysisResultStatus
        );
      },
      setGridApi: function (params) {
        if (params && params.gridApi) {
          this.gridApi = params.gridApi;
        }
      },
      onChangeSubjects: function (subjects) {
        this.selectedSubjects = subjects || [];
        this.onFilterChanged();
      },
      onChangeGeneralRequirement: function (item) {
        this.selectedGeneralRequirement = item && item.value;
        this.onFilterChanged();
      },
      onChangeConformityStatus: function (value) {
        this.selectedConformityStatus = value;
        this.onFilterChanged();
      },
      onChangeAnalysisResultStatus: function (item) {
        this.selectedAnalysisResultStatus = item && item.value;
        this.onFilterChanged();
      },
      onFilterChanged: function (item) {
        if (this.gridApi) {
          this.gridApi.onFilterChanged();
        }
      },
    };
  }
  return __externalFilter;
};

I guess it's pretty simple, but I'm not beeing able to figure out.

CodePudding user response:

You can just create an array and declare its value in your condition

    let selectedConformityStatusArray: [];    
    if (__checklist.project.analysisData?.actionPlan == 'yes') {
          selectedConformityStatusArray: [
            "conforms",
            "no_conforms",
            "not_applicable",
            "initial_status",
          ]
        } else {
          selectedConformityStatusArray: [
            "conforms",
            "no_conforms",
            "not_applicable"
          ]
        }

then in your code just add the array to your selectedConformityStatus key as value

      gridApi: undefined,
      selectedSubjects: [],
      selectedGeneralRequirement: "my_responsibility",
      selectedConformityStatus: selectedConformityStatusArray,

CodePudding user response:

In JavaScript, you can pass a condition as an argument to a function by defining a parameter in the function definition and passing the condition as a value when you call the function.

function myFunction(condition) {
  if (condition) {
    console.log('The condition is true');
  } else {
    console.log('The condition is false');
  }
}

myFunction(5 > 3); // The condition is true
myFunction(5 < 3); // The condition is false

You can also use the ternary operator to accomplish this.

function myFunction(condition) {
   return condition ? 'The condition is true' : 'The condition is false';
}
console.log(myFunction(5 > 3)); // The condition is true
console.log(myFunction(5 < 3)); // The condition is false

You can also use arrow function

const myFunction = condition => condition ? 'The condition is true' : 'The condition is false';
console.log(myFunction(5 > 3)); // The condition is true
console.log(myFunction(5 < 3)); // The condition is false

  • Related