Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable.js class contains a prefixed to method, like Map to List and to Set, etc.
Converting these types sometimes results in a loss of data as we will see when converting from Map to List.
I have a test here that we're going to want to see Map converted to List. We're creating a very basic map with two keys. OK? To use this method, we're going to create a constant called "Converted List." We're going to use the two list method on Map.
Now, this, see if this is actually a List. Do expect Immutable.list.is list converted list to be true. It passes, that's great.
One thing we'll note though, is that our keys are going to be discarded. Since lists don't have keys, those keys are then converted just to the indexes on the list itself.
Let's see what this looks like. Expect converted list.first, and we should see this to be equal to first item. Sure enough, we have that. We should also see the converted list's last item to equal second item. Sure enough, it does.
Let's do this in reverse. Let's go ahead and convert this list here, this list of first item and second item. There are no keys, keep that in mind, to a converted map. Go List.to Map. List has a corresponding to Map method, just like Map had a two list method.
Let's go ahead and see how these keys are converted. Well, they're converted ascending, so they're going to ascend numerically. Let's see what that looks like.
Let's do keys, let's go ahead and grab these keys. We're do a converted Map.keys, which will return all the keys, and see what those look like.
Expect keys.next.value to equal zero. The key's value is equal to zero. Which is what we would expect, because the first item is going to then take on a key of zero, since those ascend numerically. We can also do that for the second just as easily.
Let's see what the next value looks like and it should be one. That key is equal to one. The value is not equal to one, the key itself is equal to one.
Let's also make sure, this is a Map now, so we'll say ImmutableMap.ismap, converted map, and we want to see this to equal true, to be true. It does, a passing test over here.
Now, we also want to see the first and last item in this map are equal to the first item and second item of the List. We can go expect converted Map.first to equal and we'll just copy and paste first item like so. Get a passing test. Then we'll do the same thing with the second one, second item. Call this last. We have a passing test.
That's how you covert between Map and List and it's a very similar process for converting between the other structures.
We're going to create an array called ARR, Map.to array. We had two Lists and now have two arrays. The map will convert itself to an array.
Let's see that this looks like. Remember the keys are discarded, because an array does not have any keys. Let's expect the array, the first item to equal first item, and it does. We're good.
Let's see how this nested bit here, this key three, how that converted over. Let's just go ahead and take that item, which is the third item, and we'll see, and we'll also grab the key on it, the key4. There's a property now called key4 on key3, and see what that's equal to. It should be equal to nested item. Sure enough, it is, passed the test.
It'll just nest them and create objects on those keys and pretty much do as you would expect, converting it from a map to an array.
We can also convert a map to JSON, which is also extremely valuable when sending stuff to a server or converting between function calls. Let's go ahead and create that, so JSON equals map.to JSON, that's the method that we need to call. Lots of tos here. Let's see what we get. Let's expect JSON.key1 to equal first item.
That's how it converts over. It creates properties on this object and now we have an actual JSON bit that we can send to anywhere we want.
Let's see how the nested key works, as well. We'll go expect JSON.key3.key4 since we have key3, which contains a key4, to equal nested item. That's how you get JSON out of a Map.