This repository was archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Support for inspecting Immutable.js objects #149
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * Copyright (c) 2015-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. a additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| jest.dontMock('../immutableUtils'); | ||
| jest.dontMock('immutable'); | ||
| var immutableUtils = require('../immutableUtils'), | ||
| Immutable = require('immutable'); | ||
|
|
||
| describe('immutableUtils', () => { | ||
|
|
||
| describe('getImmutableName()', function() { | ||
| it('generates the right name for an OrderedMap', () => { | ||
| var name = immutableUtils.getImmutableName(Immutable.OrderedMap()); | ||
| expect(name).toBe('Immutable.OrderedMap'); | ||
| }); | ||
|
|
||
| it('generates the right name for an OrderedSet', () => { | ||
| var name = immutableUtils.getImmutableName(Immutable.OrderedSet()); | ||
| expect(name).toBe('Immutable.OrderedSet'); | ||
| }); | ||
|
|
||
| it('generates the right name for a Map', () => { | ||
| var name = immutableUtils.getImmutableName(Immutable.Map()); | ||
| expect(name).toBe('Immutable.Map'); | ||
| }); | ||
|
|
||
| it('generates the right name for a Set', () => { | ||
| var name = immutableUtils.getImmutableName(Immutable.Set()); | ||
| expect(name).toBe('Immutable.Set'); | ||
| }); | ||
|
|
||
| it('generates the right name for a List', () => { | ||
| var name = immutableUtils.getImmutableName(Immutable.List()); | ||
| expect(name).toBe('Immutable.List'); | ||
| }); | ||
|
|
||
| it('generates the right name for a Stack', () => { | ||
| var name = immutableUtils.getImmutableName(Immutable.Stack()); | ||
| expect(name).toBe('Immutable.Stack'); | ||
| }); | ||
|
|
||
| it('generates the right name for a Seq', () => { | ||
| var name = immutableUtils.getImmutableName(Immutable.Seq()); | ||
| expect(name).toBe('Immutable.Seq'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shallowToJS()', function() { | ||
| it('converts a Map correctly', function() { | ||
| var map = Immutable.Map({ | ||
| a: 1, | ||
| b: 2, | ||
| c: Immutable.Map() | ||
| }); | ||
| var result = immutableUtils.shallowToJS(map); | ||
| expect(result.a).toBe(1); | ||
| expect(result.b).toBe(2); | ||
| expect(Immutable.Map.isMap(result.c)).toBe(true); | ||
| }); | ||
|
|
||
| it('converts a List correctly', function() { | ||
| var list = Immutable.List(['a', 'b', Immutable.Map()]); | ||
| var result = immutableUtils.shallowToJS(list); | ||
| expect(result[0]).toBe('a'); | ||
| expect(result[1]).toBe('b'); | ||
| expect(Immutable.Map.isMap(result[2])).toBe(true); | ||
| }); | ||
|
|
||
| it('converts a Set correctly', function() { | ||
| var set = Immutable.Set([1, 2, 3, Immutable.List()]); | ||
| var result = immutableUtils.shallowToJS(set); | ||
|
|
||
| expect(Array.isArray(result)).toBe(true); | ||
| expect(result.filter(Immutable.List.isList).length > 0).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * Copyright (c) 2015-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| * @flow | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| var Immutable = require('immutable'); | ||
|
|
||
| var types = ['OrderedMap', 'OrderedSet', | ||
| 'Map', 'Set', 'List', 'Stack', | ||
| 'Seq']; | ||
|
|
||
| module.exports = { | ||
| isImmutable: Immutable.Iterable.isIterable, | ||
| getImmutableName(data: any): string { | ||
| if (Immutable.Iterable.isIterable(data)) { | ||
| var name = 'Immutable.', | ||
| typelen = types.length; | ||
| for (var i = 0; i < typelen; i++) { | ||
| if (Immutable[types[i]]['is' + types[i]](data)) { | ||
| name += types[i]; | ||
| break; | ||
| } | ||
| } | ||
| return name; | ||
| } else { | ||
| return data.constructor.name; | ||
| } | ||
| }, | ||
| shallowToJS(dataStructure: any): any { | ||
| if (!Immutable.Iterable.isIterable(dataStructure)) { | ||
| return dataStructure; | ||
| } | ||
|
|
||
| if (Immutable.Iterable.isKeyed(dataStructure)) { | ||
| return dataStructure.toObject(); | ||
| } else if (Immutable.Iterable.isIndexed(dataStructure) || | ||
| Immutable.Set.isSet(dataStructure)) { | ||
| return dataStructure.toArray(); | ||
| } | ||
|
|
||
| return dataStructure; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I ask your rationale behind providing a
shallowToJShere but not adeepToJSthat callsIterable.toJS()? For example, if a particular prop is anImmutable.KeyedIterablewith a property value that is itself an Immutable object, then it seems thatshallowToJSwould not convert the property value to a mutable object.Edit - I see that your test for this module already asserts the above. Would it not be valuable to do a deep
.toJS()call for any immutable object so that the dev tool user can more easily navigate nested immutable properties and state values?Edit 2 - After digging a bit deeper, it looks like you're doing a shallow conversion at each level so that the user can easily see whether or not some nested property is immutable or not while also being able to easily inspect its child properties. If that's the case, 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was just about to write that. The reason in edit 2 is precisely why.