This function has a Cyclomatic Complexity of 11, exceeding the authorized limit of 10

if ((['ALL', ''].includes(this.accountnumber.value) ? true : ele.accountnumber === this.accountnumber.value) &&
        (['ALL', ''].includes(this.description.value) ? true : ele.description === this.description.value) &&
        (['ALL', ''].includes(this.country.value) ? true : ele.country === this.country.value) &&
        (!this.entryDate ? true : (this.entryDate === dateEntry)) && 
        (!this.editedDate ? true : (this.editedDate === dateEdited))) {
        return true;
      }

Answer №1

It can be beneficial to write additional code to gain a deeper understanding of the process.

An initial suggestion is to elaborate on the content within your if statement like this:

Keep in mind that using a simple || operator instead of ternary is also effective.

PS: I have used poor variable naming without considering the purpose behind your actions. When reusing the following code, ensure to change them and provide comments explaining your intentions.

const isA = [
 'ALL',
 '',
].includes(this.accountnumber.value) || ele.accountnumber === this.accountnumber.value;

const isB = [
  'ALL',
  '',
].includes(this.description.value) || ele.description === this.description.value;

const isC = [
  'ALL',
  '',
 ].includes(this.country.value) || ele.country === this.country.value;

const isD = !this.entryDate || this.entryDate === dateEntry;

const isE = !this.editedDate || this.editedDate === dateEdited;

if (isA && isB && isC && isD && isE) {
  return true;
}

Expanding upon this will highlight duplicated sections of code.

function checkCond(key, obj, arr = [
  'ALL',
  '',
]) {
  return arr.includes(this[key].value) || obj[key] === this[key].value; 
}

const isA = checkCond('accountnumber', ele);
const isB = checkCond('description', ele);
const isB = checkCond('country', ele);

const isD = !this.entryDate || this.entryDate === dateEntry;

const isE = !this.editedDate || this.editedDate === dateEdited;

if (isA && isB && isC && isD && isE) {
  return true;
}

Taking it one step further:

function checkCond(key, obj, arr = [
  'ALL',
  '',
]) {
  return arr.includes(this[key].value) || obj[key] === this[key].value; 
}

const conditions = [
  checkCond('accountnumber', ele),
  checkCond('description', ele),
  checkCond('country', ele),

  !this.entryDate || this.entryDate === dateEntry,
  !this.editedDate || this.editedDate === dateEdited,
];

if (conditions.every(x => x)) {
  return true;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a dynamic configuration for service instantiation in Angular 4/5

Currently working on a library that has an AuthModule with an AuthService for managing OAuth2 authentication using oidc-client-js. I want the application using this library to be able to set up the configuration for the OAuth client. One way to do this is ...

A guide to implementing Typescript Generics in modifier functions

I am searching for a solution to properly enforce strong typing in the following scenario. I believe Typescript Generics might be the way to go here. interface Person { name: string; age: number; } const person: Person = { name: "John", ...

What could be causing the type errors I am encountering while trying to resolve this Promise within a generic function?

I am attempting to implement additional types within this WebSocket protocol: type Action = { action: "change-or-create-state"; response: string; } | { action: "get-state"; response: string | null; }; /** * map an action to its response ...

Encountering a tuple type TypeScript error with a spread argument is far too frequent an occurrence

Encountering this error is becoming a frequent occurrence for me, and I am currently unable to resolve it. src/graphics.ts:105:55 - error TS2556: A spread argument must either have a tuple type or be passed to a rest parameter. 105 _queue.forEach((_ ...

Tips for including a dash or hyphen in an input field after two digits in Angular 4

Struggling to format the date of birth input with dashes manually when entered by the user. The desired output should resemble "08-18-2019," but I'm having difficulty achieving this. public dateOfBirth: { year: number; month: number; day: number }; ...

What is the best approach for defining variables in typescript?

Let's talk about creating a variable for a car: export class ICar { wheels: number; color: string; type: string; } So, which way is better to create the variable? Option one: const car = { wheels: 4, color: 'red', type: &apos ...

Unable to locate the reference for 'bootstrap'

After trying to implement Bootstrap toast in an Angular Component, the following error message is displayed: "Cannot find name 'bootstrap'" Here is the HTML code similar to the Bootstrap documentation: <div class="toast" r ...

Issues with CSS Styling not being applied properly on mobile devices in a React App deployed on Heroku

The Dilemma My React app is deployed on Heroku using create-react-app for bundling. The backend is a Node.js written in Typescript with node version 10.15.3. Locally, when I run the site using npm start, everything works perfectly. However, when I view t ...

Encountering an issue with importing from 'sockjs-client' in TypeScript

I am a beginner with Angular and TypeScript. To get started, I used npm to install the following package: npm install --save sockjs-client I attempted to import it in my chat.component.ts file like this: import * as SockJS from 'sockjs-client'; ...

Angular - Issue with Function Observable<number> in Development

Currently, I'm working on writing TypeScript code for a component. Within this TypeScript class, I have created a function that is meant to return a number representing the length of an array. My goal is to have this function work like an Observable. ...

There is no such property as formData.delete()

When I am using a FormData object to upload a file, I want to add functionality to delete the file from FormData. However, I encountered an error stating that the delete property does not exist on the FormData object. formData.delete(fileName) Code uplo ...

What is the best way to define the type of an object when the Key is already known?

If I have the code snippet below, how can I properly define the data object type based on the known value of data.type? In this scenario, I aim to assign a specific type to data when its type property is either "sms" or "email" const payload = '{&quo ...

filter failing to provide output

Issue with fetching partnername from the filter function, always returning undefined. administrationList = [ { "runid": 6, "partnerid": 2, "partnername": "test admin2", }, { "runid& ...

Is it Possible for Angular Layout Components to Render Content Correctly even with Deeply Nested ng-container Elements?

Within my Angular application, I have designed a layout component featuring two columns using CSS. Within this setup, placeholders for the aside and main content are defined utilizing ng-content. The data for both the aside and main sections is fetched fr ...

The element 'x' is not found within the 'unknown' type

I've been struggling with this issue. After searching through various sources like stackoverflow and github, I attempted a solution which involved adding a generic but I encountered the error message Expected 0 type arguments, but got 1. in relation t ...

The issue with Angular 2's router.navigate not functioning as expected within a nested JavaScript function

Consider the app module: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { RouterModule } from '@angul ...

Parsing errors occurred when using the ngFor template: Parser identified an unexpected token at a specific column

In my Angular CLI-built application, I have a component with a model named globalModel. This model is populated with user inputs from the previous page and displayed to the user in an HTML table on the current page. Here's how it's done: <inp ...

Increasing the value of a TypeScript variable using ngFor in an HTML template can enhance its usefulness

Can you dynamically increase a variable declared in your TypeScript file for each element generated by an ngFor loop in your HTML file? <tbody> <tr *ngFor="let item of invoiceItems"> <td>{{item.ItemName}}</td> <td>{ ...

Converting a JSON array into a TypeScript array

Looking to convert a JSON array into a TypeScript variable array. The JSON data retrieved from http://www.example.com/select.php: { "User":[ {"Name":"Luca M","ID":"1"}, {"Name":"Tim S","ID":"2"}, {"Name":"Lucas W","ID":"3"} ...

Displaying Information in Angular Modal Windows

I'm facing an issue while trying to create an edit button for a formGroup that is initially saved. When the user clicks on the adjacent button, a modal should open with editable data. However, I encountered this error and haven't been able to res ...