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

feat: update predicate for obj is Symbol.iterator #110

Merged
merged 1 commit into from
May 17, 2024

Conversation

LviatYi
Copy link
Contributor

@LviatYi LviatYi commented May 14, 2024

allow iterators instead of just generators

allow iterators instead of just generators
@LviatYi
Copy link
Contributor Author

LviatYi commented May 14, 2024

BackGround

In JavaScript, iterators are slightly different from generators.

While a generator is a specific type of iterator that comes with an additional [Symbol.iterator] function pointing to itself, this property makes generators both iterators and iterable. This dual characteristic means that a generator can be used in any context that requires an iterator or an iterable object.

Iterators and generators | MDN

Feature

This pull request resolves an issue in the from function where it was not correctly accepting an Iterator<T> as indicated by its TypeScript signature.

Previously, the function checked obj for [Symbol.iterator] which limited the inputs to iterable objects (generators). This behavior may not align with the function's ability to process general iterators as intended.

Key Changes

  • Old Check: typeof obj[Symbol.iterator] !== 'undefined'

    • This check was meant to determine if obj was iterable. However, this constrained the function to accept only iterable objects like Arrays, Strings, or Generators.
  • New Check: typeof obj.next !== 'undefined'

    • This updated check correctly targets the presence of the next method, which is the defining characteristic of an iterator. This aligns with the function's TypeScript signature to accept Iterator<T>.

Why

  • Correct type matching: The function now correctly accepts all iterators, not just generators, by checking next instead of [Symbol.iterator].
  • Wider acceptance: This change increases the flexibility of the from function by allowing it to handle all qualifying iterators.

My understanding of JavaScript nuances is still growing, and this commit may deviate from the intended functionality in ways I might not fully grasp. Therefore, I would greatly appreciate any guidance or corrections. Your feedback will be invaluable to me, and I thank you in advance for your time and consideration in reviewing this fix!

@mihaifm
Copy link
Owner

mihaifm commented May 15, 2024

Thanks, I’ll test it when I get some time. It looks good at first sight.

@mihaifm mihaifm merged commit 5d81dd4 into mihaifm:master May 17, 2024
@mihaifm
Copy link
Owner

mihaifm commented May 17, 2024

This change makes sense, thanks for the fix.

Following your fix we need to have a separate case for objects that only have [Symbol.iterator]. For example, this doesn't currently work:

var test = new Map()
test.set('1', 1)
console.log(Enumerable.from(test).first()) //error

I'll try to fix this in the next version.

Also, consider disabling auto formatting when submitting PRs, you're introducing changes that are not related to the issue you're trying to fix.

@LviatYi
Copy link
Contributor Author

LviatYi commented May 18, 2024

Thank you very much for merging the pull request. I'm also sorry about the fact that I included a change in the commit that should have been marked as style, and I will try to avoid it as much as possible in the future.

Regarding the function of from that accepts an iterable object, I have probably completed a similar feature, please review it in a subsequent PR.

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