Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#21 calculate y coordinate on a given table #42

Merged

Conversation

deletidev
Copy link
Collaborator

No description provided.

@@ -73,3 +79,75 @@ export const calculateRelationXCoordinate = (
xOrigin: calculateRelationXCoordinateOrigin(tableOrigin, tableDestination),
xDestination: calculateRelationXCoordinateEnd(tableOrigin, tableDestination),
});

const seekField = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since SeekResult is only going to be used inside this function, we can define the interface just before the SeekFielddeclaration instead of the vm, why this approach? We place in the vms entities that we expect that are going to be used in several files, in this case is rare that this interface could be used outside this file.

+ interface seekResult {
+  found: boolean;
+ parentCollapsed: boolean;
+  YPosition: number;
+ }

const seekField = (

Note you can export in case is needed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

yDestination: number;
}

export interface seekResult {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove seekResult from this file

one thing to take into account interfaces should be always uppercase

- export interface seekResult {
+ export interface SeekResult {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

@@ -47,3 +47,14 @@ export interface XRelationCoords {
xOrigin: number;
xDestination: number;
}

export interface YRelationCoords {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do the same with this interface as with SeekResult, but we will need to export this interface

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

) {
const result = seekField(fieldId, newYPosition, field.children);
found = result.found;
newYPosition = parentCollapsed ? newYPosition : result.YPosition;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment:

// If the current node was already collapsed, ignore the child YPosition offset (Opa rulez !)
newYPosition = parentCollapsed ? newYPosition : result.YPosition;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

for (let i = 0; i < fields.length && !found; i++) {
const field = fields[i];
if (field.id === fieldId) {
found = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are just returning a value we can get rid of

- found = true:

And pass found: true in the object it self

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

found,
parentCollapsed,
YPosition: newYPosition,
};
Copy link
Member

@brauliodiez brauliodiez Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could create a helper function

const buildFieldFoundResponse = (parentCollapsed: boolean, YPosition: number) => ({
  found: true,
  parentCollapsed,
  YPosition
});

Then we can replace and simplify:

if (field.id === fieldId) {
-      found = true;
- 
-     return {
-        found,
-        parentCollapsed,
-       YPosition: newYPosition,
-    };
+  return buildFieldFoundResponse(parentCollapsed, YNewPosition);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

} else {
if (!parentCollapsed) {
newYPosition += ROW_HEIGHT;
}
Copy link
Member

@brauliodiez brauliodiez Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reduce the cyclomatic complexity (Hadouken)

image

Let's create a helped method:

const addFieldRowHeight = (YPosition : number, parentCollapsed : boolean) : number => (!parentColapsed) newYPosition + ROW_HEIGHT : newYPosition;

Then in this code:

   } else {
+    newYPosition = addFieldRowHeight(newYPosition, parentCollapsed);
-      if (!parentCollapsed) {
-        newYPosition += ROW_HEIGHT;
-      }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed


if (!parentCollapsed && field.isCollapsed) {
parentCollapsed = true;
}
Copy link
Member

@brauliodiez brauliodiez Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing her we could create a helper

const isParentCollapsedOrCurrentNodeCollapsed = (parentCollapsed : boolean, currentFieldCollapsed : boolean) => (!parentCollapsed && field.isCollapsed) true : parentCollapsed;
+ const parentCollapsed = isParentOrCurrentNodeCollapsed(parentCollapsed, field.isCollapsed);
- if (!parentCollapsed && field.isCollapsed) {
-        parentCollapsed = true;
- }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

field.type === 'object' &&
field.children &&
field.children.length > 0
) {
Copy link
Member

@brauliodiez brauliodiez Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is complex, we can wrap it into a helper function, something like:

const doesFieldContainsChildren = (field: Field) => (field.type === 'object' && field.children &&  field.children.length > 0); 

Then the if statement gets simplified and just by reading the name of the function we know what it does:

     if (
+       doesFieldContainsChildren(field);
-        field.type === 'object' &&
-        field.children &&
-        field.children.length > 0
     ) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

found,
parentCollapsed,
YPosition: newYPosition,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we could make use of buildFieldFoundResponse that we created before

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed

@brauliodiez brauliodiez merged commit 742a7ee into main Jan 4, 2024
1 check passed
@brauliodiez brauliodiez deleted the feature/#21-Calculate-Y-Coordinate-on-a-given-table branch January 4, 2024 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants