8000 feat(core): add `first` and `last` methods to `Collection` class by rubiin · Pull Request #6056 · mikro-orm/mikro-orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(core): add first and last methods to Collection class #6056

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

rubiin
Copy link
Contributor
@rubiin rubiin commented Sep 22, 2024

Adds methods on collection which is just a syntatic sugars

Copy link
codecov bot commented Sep 22, 2024

Codecov Report

Attention: Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 99.79%. Comparing base (5b866fa) to head (d5f30bd).
Report is 317 commits behind head on master.

Files with missing lines Patch % Lines
packages/core/src/entity/ArrayCollection.ts 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6056      +/-   ##
==========================================
- Coverage   99.79%   99.79%   -0.01%     
==========================================
  Files         264      264              
  Lines       18753    18765      +12     
  Branches     4655     4413     -242     
==========================================
+ Hits        18715    18726      +11     
- Misses         38       39       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rubiin rubiin changed the title feat(collections): add first and last feat(collections): add first,last and pop Sep 22, 2024
@rubiin rubiin changed the title feat(collections): add first,last and pop feat(collections): add first and last Sep 22, 2024
@@ -337,6 +337,10 @@ export class ArrayCollection<T extends object, O extends object> {
return true;
}

first(): T | undefined {
return this.getItems()[0];
Copy link
Member

Choose a reason for hiding this comment

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

this could be done more effectively, now you convert the internal items set to an array just to select its first value, you can do this[0] to read the first value instead (plus check the loaded state first)

Copy link
Contributor Author
@rubiin rubiin Sep 23, 2024

Choose a reason for hiding this comment

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

  first(): T | undefined {
    return this.items.values().next().value;
  }

How about this one. This doesnt convert it into array

Copy link
Member

Choose a reason for hiding this comment

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

yes, but why? your code feels more complex than my proposal (and you still need to check the loaded state).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

first(): T | undefined {
    return this[0];
  }

I guess you meant this

Copy link
Member

Choose a reason for hiding this comment

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

yes, plus this.checkInitialized(); before that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure thing. Will push the updates

@@ -345,6 +349,10 @@ export class ArrayCollection<T extends object, O extends object> {
return this.count() === 0;
}

last(): T | undefined {
return this.getItems()[this.items.size - 1];
Copy link
Member

Choose a reason for hiding this comment

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

same here, you can just check the loaded state and 8000 return this[this.length - 1]. in fact this.length is a getter that will check the loaded state, so here it should be enough to just do this

Suggested change
return this.getItems()[this.items.size - 1];
return this[this.length - 1];

Comment on lines 341 to 344
if (this.isInitialized()) {
return this[0];
}
return undefined;
Copy link
Member

Choose a reason for hiding this comment

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

8000

the method should throw if the collection is not initialized, same for the last one. i would move the checkInitialized to ArrayCollection (and mark it as protected) and do this instead

Suggested change
if (this.isInitialized()) {
return this[0];
}
return undefined;
this.checkInitialized();
return this[0];

then the Collection implementations can be removed completely

Copy link
Contributor Author

Choose a reason for hiding this comment

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

checkInitialized doesnt exist on ArrayCollection class. I have the checkInitialized on the collection one though

  override first(): T | undefined {
    this.checkInitialized();
    return super.first();
  }

Copy link
Member

Choose a reason for hiding this comment

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

yes, and I just told you to move that method down
image

@B4nan
Copy link
Member
B4nan commented Sep 23, 2024

fyi i will most likely remove the whole ArrayCollection (and inline it to the Collection) since that abstraction makes very little sense and only makes things harder to write/test. but that's something for v7, now we need to deal with it

@rubiin
Copy link
Contributor Author
rubiin commented Sep 23, 2024

fyi i will most likely remove the whole ArrayCollection (and inline it to the Collection) since that abstraction makes very little sense and only makes things harder to write/test. but that's something for v7, now we need to deal with it

Yeah its kind of confusing but then again laravels elequoent orm as well as doctrine seems to follow this pattern so mixed thoughts

@boenrobot
Copy link
Collaborator

fyi i will most likely remove the whole ArrayCollection (and inline it to the Collection) since that abstraction makes very little sense and only makes things harder to write/test. but that's something for v7, now we need to deal with it

Yeah its kind of confusing but then again laravels elequoent orm as well as doctrine seems to follow this pattern so mixed thoughts

PHP's plain arrays get passed around by value ("copy on write" technically, but from userland's pov, that's like passing by value), and the wrappers are there to ensure they get passed by reference instead.

In JS, arrays are passed by reference, so these sorts of wrappers are useless.

rubiin and others added 2 commits September 23, 2024 19:10
@B4nan B4nan changed the title feat(collections): add first and last feat(core): add first and last methods to Collection class Sep 24, 2024
@B4nan
Copy link
Member
B4nan commented Oct 2, 2024

@rubiin do you plan to finish this?

@rubiin
Copy link
Contributor Author
rubiin commented Oct 2, 2024

@rubiin do you plan to finish this?

Currently occupied with some work and couldnt push changes . Will update that sometime soon

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.

3 participants
0