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

accept plain JS Map and Set in fromJS #1858

Closed
wants to merge 1 commit into from
Closed

Conversation

jdeniau
Copy link
Member

@jdeniau jdeniau commented Jul 19, 2021

Fixes #1723

@jdeniau jdeniau requested a review from a team July 19, 2021 10:19
@jdeniau
Copy link
Member Author

jdeniau commented Jul 19, 2021

ping @conartist6 as you commented on #1723

Copy link
Collaborator

@leebyron leebyron left a comment

Choose a reason for hiding this comment

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

This doesn't handle WeakSet and WeakMap, and other collections.

Another way you might handle determining which kind of Seq to use is to look at the iterator methods for Iterable collections (all modern JS collections). eg if value[ITERATOR_SYMBOL] === value.entries then it's a Keyed collection. If value[ITERATOR_SYMBOL] === value.keys it's a Set collection. Otherwise probably safe to assume anything iterable is an Indexed collection (though it's often the case that value[ITERATOR_SYMBOL] === value.values - also careful since that's also true for Set)

@@ -1,27 +1,41 @@
import { is, List, Map, Range, Record, Seq } from 'immutable';
import { is, List, Map as ImmutableMap, Range, Record, Seq } from 'immutable';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of having to convert this whole test file to use an alias, how do you feel about just making a new test file for Map to IMap?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not knew how to call global constructor, I think the best way is to call global.Map as you did in another PR

}

if (value instanceof Set) {
return ImmutableSet;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be determining a kind of Seq to use

Suggested change
return ImmutableSet;
return SetSeq;

return IndexedSeq;
}

if (value instanceof Set) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

To disambiguate, you might want to specifically reference the global Set class

Suggested change
if (value instanceof Set) {
if (value instanceof global.Set) {

Comment on lines +58 to 62
if (ImmutableSet.isSet(v)) {
return v;
}

return isKeyed(v) ? v.toMap() : v.toList();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (ImmutableSet.isSet(v)) {
return v;
}
return isKeyed(v) ? v.toMap() : v.toList();
return isKeyed(v) ? v.toMap() : isSet(v) ? v.toSet() : v.toList();

@leebyron
Copy link
Collaborator

Trying out an alternative approach in #1865 that has a bit of a broader scope, curious for your thoughts

@jdeniau
Copy link
Member Author

jdeniau commented Jul 23, 2021

Your implementation is way better, and more generic. Closing in favor of #1865

@jdeniau jdeniau closed this Jul 23, 2021
@jdeniau jdeniau deleted the fromJSWithMapAndSet branch July 23, 2021 15:03
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.

Map and Set are not converted to Immutable.Map and Immutable.Set via Immutable.fromJS()
2 participants